CPSC 124, Fall 1998

Quiz Number 4


This is the fourth quiz given in CPSC 124: Introductory Programming, Fall 1998. 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. In some cases, I give more detailed answers here than would be necessary to get full credit.


Question 1: Object-oriented programming uses classes and objects. What are classes and what are objects? What is the relationship between classes and objects?

Answer: A class is a template for creating objects. An object is a collection of data and behaviors that represent some entity (real or abstract). A class represents all entities of a given type, while an object is one particular "instance" of that type of entity. For example if Dog is a class, than Lassie would be an object of type Dog.


Question 2: Suppose that Kumquat is the name of a class and that fruit is a variable of type Kumquat. What is the meaning of the statement ``fruit = new Kumquat();''? That is, what does the computer do when it executes this statement? (Try to give a complete answer. The computer does several things.)

Answer: This statement creates a new object belonging to the class Kumquat, and it stores a reference to that object in the variable fruit. (As part of creating the object, it executes a constructor from the Kumquat class. This constructor can initialize the instance variables of the object. But it can also perform any other action that the programmer wants to have performed when Kumquat objects are created.)

Note that this statement does not declare the variable, fruit. It must already have been declared with a statement of the form

             Kumquat fruit;

This is what it means to say that fruit is a variables of type Kumquat. It would be possible to declare and initialize fruit in one statement. This would be done with the statement:

             Kumquat fruit = new Kumquat();

This is a variable declaration statement, while "fruit = new Kumquat();" is an assignment statement. It is important to distinguish between these two types of statements. A declaration statement can occur in a class outside any subroutine. But an assignment statement can only occur inside a subroutine.


Question 3: What is meant by the terms instance variable and instance method?

Answer: Instance variables and instance methods are non-static variables and methods in a class. This means that they do not belong to the class itself. Instead, they specify what variables and methods are in an object that belongs to that class. (Such an object is called an "instance" of the class.) Thus, instance variables and instance methods are the data and behaviors of objects.


Question 4: Write a subroutine with three parameters of type int. The subroutine should determine which of its parameters is smallest. The value of the smallest parameter should be returned as the value of the subroutine.

           static int smallest(int x, int y, int z) {
              .
              .
              .
           }

Answer: The value returned by the subroutine has to be either x or y or z. The answer will be x if x is less than or equal to both y and z. The correct syntax for checking this is "if (x <= y && x <= z)". Similarly for y. The only other remaining possibility is z, so there is no necessity for making any further test before returning z.

           static int smallest(int x, int y, int z) {
              if (x <= y && x <= z) {
                 return x;
              }
              else if (y <= x && y <= z) {
                 return y;
              }
              else
                 return z;
           }

Since a return statement causes the computer to terminate the execution of a subroutine, this could also be written:

           static int smallest(int x, int y, int z) {
              if (x <= y && x <= z) {
                 return x;
              }
              if (y <= x && y <= z) {
                 return y;
              }
              return z;
           }

David Eck, 15 October 1998