CPSC 124, Spring 2014
Sample Answers to Quiz #6

Question 1. What is the effect of declaring a variable to be final, as in

     public static final double INTEREST_RATE = 0.034;

(Note: I am not asking why you might want to do this. This is an easy question!)

Answer. Since the variable INTEREST_RATE is final, its value can never be changed. The value will have to remain 0.034 for the entire run of the program.

Question 2. What is the meaning of null in Java?

Answer. null is a pointer value that doesn't point anywhere. When it is stored in a variable, it means that that variable is not pointing to any object.

Question 3. In the source code for a Java class, methods and variables can be either static or non-static. Explain what is done with the non-static parts of a class definition.

Answer. The non-static parts of a class definition are copied into each object that is created from that class, so that every object will have its own copies of the non-static variables and methods. (An object created from a class is called an instance of the class, and the non-static parts of the class become instance variables and instance methods in the object.)

Question 4. Consider the simple class shown on the left below. Suppose that the statements shown on the right are executed. What happens? Explain what the computer does when it executes these statements. Drawing a picture would be useful!

      public class Point{       |         Point p;
          int x;                |         p = new Point();
          int y;                |         p.x = 17;
      }                         |         p.y = 42;

Answer. The first line, Point p, makes a variable of type point. This variable is capable of holding a pointer to an object of type Point, but it doesn't yet hold anything. The second line creates an object of type Point by calling new Point(), and it stores a pointer to the new object in p. The object contains instance variables named x and y, of type int. The last two lines place values into the two instance variables that are inside the object. After the four lines are executed, the situation looks like this picture: