CPSC 124, Fall 2011
Quiz #6

Question 1. What is null?

Answer. null is a special value that can be stored in a variable to indicate that that variable is not pointing to any object. (The variable must be of object type, that is, one that can point to objects.) null is a pointer that doesn't point to anything!

Question 2. What is the main difference between static and non-static member variables in a class?

Answer. There is only one static variable. There is one non-static variable in each object that is created from the class that defines the non-static variable.

(A static variable is part of the class in which it is defined, and it exists as long as the class exists. A non-static variable is not part of the class -- you don't get a variable until you create an object from the class. The variable is part of the object, and each object has its own version of the variable.)

(Note: "instance variable" is another term for "non-static variable".)

Question 3. Explain what the following statements do. What does the computer do when it executes them, and what is the result? It might help to draw a picture. (Assume that Thing is the name of a class that contains an appropriate constructor.)

Thing  a;
a  =  new Thing("One");

Answer. The first line creates a variable named a of type Thing. (This variable is capable of referring to an object of type Thing, but it currently has no value.) The second line creates an object of type Thing, calls a constructor in that class, and stores a pointer to the new object in the variable a. (Class Thing must contain a constructor that takes one parameter of type String.)

The picture would show the variable a with an arrow from a to the object.

Question 4. The class on the left below creates objects that represent names consisting of a first name and a last name. Write a short main program to test this class by creating an object of type FullName and then printing out the first and last names from that object.

   public class FullName {
        private String first, last;
        public Name(String a, String b) {
            first = a;
            last = b;
        public String getFirst() {
            return first;
        public String getLast() {
            return last;


public class TestFullName {

   public static void main(String[] args) {
      FullName name;  // Create a variable of type FullName
      name = new FullName("Fred","Astaire");  
             // Create an object of type FullName
      System.out.println( name.getFirst() + " "
                                 + name.getLast() );