CPSC 124, Spring 2013
Sample Answers to Quiz #5
Question 1. A "black box" has an implementation and an interface. Briefly explain what is meant by implementation and interface.
Answer. The implementation of the black box is what's inside the box. The implementation is what actually carries out the task that is performed by the black box. But to use the black box, it is not necessary to know anything about the implementation.
The interface is the means by which the black box and the outside world communicate. It is the connection between the inside of the black box and the outside. One uses a black box through its interface.
Question 2. What exactly does the computer do when it executes a subroutine call statement such as: processItem( "data", 3 );
This is a question about what the computer does, not about guessing the meaning of the subroutine. Your explanation should include what is done with the parameters.
Answer. When the computer is executing a list of program commands and comes across a subroutine call, it does the following:
- It saves the return address, so that it knows where it has to jump back to after executing the subroutine.
- It jumps to the beginning of the subroutine. (This is a memory location where the subroutine code is stored.)
- It assigns the actual parameter values from the subroutine call statement to the corresponding dummy parameters in the subroutine.
- It executes the subroutine code until it comes to the end of the subroutine or to a return statement.
- It jumps back to the previously saved return address, which means that it continues on with the program at the line immediately following the subroutine call statement.
Question 3. Write a complete subroutine that computes the maximum of two integers. The name of the subroutine is max. It has two parameter of type int and a return type of int. The subroutine is a function whose return value is the larger of the two parameter values.
Answer.
public static int max( int x, int y ) { if ( x > y ) { return x; } else { return y; } }
(The "public" and "static" are not required by the problem statement. The return type, "int", and the subroutine name, "max", are. The types of the formal parameters must be "int", but the names can be anything. The body of the subroutine can be written in other ways.)