CPSC 124, Fall 1996

Quiz Number 4


This is the fourth 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: What are "constructors," and how are they used? Give an example.

Answer: A constructor is a kind of subroutine that is called when the new operator is used to create a new object. Its purpose is to perform any necessary initialization of the object -- and to perform any other actions that the programmer whats to have happen automatically when an object is created. For example, in the statement

mosaic = new MosaicFrame("Symmetry",30,30,300,300);

the constructor is MosaicFrame. The parameters passed to the constructor are used to initialize the new MosaicFrame object.


Question 2: What does "null" represent in Java?

Answer: null is a special value that can be stored in a variable to indicate that the variable does not refer to any object at all. (When the type of a variable is given by a class, then that variable holds either a reference to some instance of that class, or it holds the value null to indicate that it does not at the moment refer to an instance.)


Question 3: Explain what is meant by the terms "subclass" and "superclass."

Answer: In object oriented programming, one class can inherit all the properties and behaviors from another class. It can then add to and modify what it inherits. The class that inherits is called a subclass, and the class that it inherits from is said to be its superclass. In Java, the fact that ClassA is a subclass of ClassB is indicated in the definition of ClassA as follows:

class ClassA extends ClassB {...}


Question 4: What is the meaning of the operator == when it is applied to objects? Explain why this operator should almost never be used to test whether two Strings are equal.

Answer: The operator == tests whether two objects are actually the same object, occupying the same location in memory. This is different from testing whether the contents of the memory locations occupied by the objects are the same. When testing two Strings for equality, it is most likely that you just want to check whether the strings have the same characters -- not whether the strings are in the same place in memory. For example, it is likely that two strings that are both given as "Cat" will be judged to be not equal by the == operator, because there are two different copies of "Cat" in the different memory locations. (But note that if two objects are judged to be equal by ==, then they will certainly be judged equal by any other reasonable test that you can think of.)


Question 5: Java uses "garbage collection" for memory management. Explain what is meant here by garbage collection. What is the alternative to garbage collection?

Answer: The purpose of garbage collection is to identify objects that can no longer be used, and to dispose of such objects and reclaim the memory space that they occupy. If garbage collection is not used, then programmer must be responsible for keeping track of which objects are still in use and disposing of objects when they are no longer needed. If the programmer makes a mistake, then there is a "memory leak," which might gradually fill up memory with useless objects until the program crashes for lack of memory.


David Eck