CPSC 124, Fall 1996

Quiz Number 3


This is the third quiz given in CPSC 124: Introductory Programming, Fall 1996. See the information page for that course for more information.

The answers given here are sample answers that would receive full credit. However, they are not necessarily the only correct answers.


Question 1: Define the term "subroutine."

Answer: A subroutine is a set of instructions that have been "chunked" together into a black box and given a name. Once a subroutine has been defined, the task can be performed simply by "calling" the subroutine by name.


Question 2: What are "parameters," and how are they used? (Give an example.)

Answer: Parameters are used for communication between a subroutine and the rest of the world. When a subroutine is called, values -- called "actual parameters" -- can be provided as input to the subroutine. For example, in the statement console.putln(x*y), x*y is an actual parameter. Inside the subroutine, the parameter values are represented by names called "formal parameters."


Question 3: Explain what the term "static" means when used as a modifier on a variable or method declaration in a class.

Answer: If a variable or method is declared to be static, than that variable or method really belongs to the class as a whole, rather than to any of the objects that might be created from that class. This means, for example, that the variable or method is shared by all such objects and that there is only one copy of the variable or method that exists the whole time a program is running.


Question 4: Write a Java subroutine named min that takes two ints as input and that returns the smaller of the two input numbers. For example, min(7,3) would be 3.

Answer: The subroutine has two parameters. Each parameter is of type int. It also has a return value, which must also be of type int, since it is equal to one of the input values. An if statement can be used to determine which of the two input numbers is smaller.

                 int min (int x, int y) {
                         // returns the smaller of the two inputs, x and y
                    if (x < y)
                        return x;
                    else
                        return y;
                  }

Question 5: An "animation" consists of a sequence of images shown rapidly one after the other on the computer screen. Each image in the animation is called a "frame." Suppose that you have a subroutine

static void showFrame(int frameNum)

that displays one frame in an animation. That is, showFrame(1) displays the first frame, showFrame(2) displays the second frame, and so on. Suppose that there are a total of thirty different frames.

a) Write a for statement that will display all 30 frames in order.

b) Now suppose that you want to replay the animation 10 times. That is you want to show the sequence of 30 frames over and over, 10 times. Write the Java code that would do this.

Answer:

           a)     for (int i = 1; i <= 30; i++)
                      showFrame(i);
                      
           b)     for (int play = 0; play < 10; play++) {
                      for (int i = 1; i < 30; i++)
                         showFrame(i);
                   }

David Eck