Due to the print book page limit, we cannot inlcude all good CheckPoint questions in the physical book. The CheckPoint on this Website may contain extra questions not printed in the book. The questions in some sections may have been reordered as a result. Nevertheless, it is easy to find the CheckPoint questions in the book on this Website. Please send suggestions and errata to Dr. Liang at y.daniel.liang@gmail.com. Indicate the book, edition, and question number in your email. Thanks!

Chapter 9 Check Point Questions

Section 9.3
9.3.1
Describe the relationship between an object and its defining class.
9.3.2
How do you define a class?
9.3.3
How do you declare an object's reference variable?
9.3.4
How do you create an object?
Section 9.4
9.4.1
What are the differences between constructors and methods?
9.4.2
When will a class have a default constructor?
Section 9.5
9.5.1
Which operator is used to access a data field or invoke a method from an object?
9.5.2
What is an anonymous object?
9.5.3
What is NullPointerException?
9.5.4
Is an array an object or a primitive type value? Can an array contain elements of an object type? Describe the default value for the elements of an array.
9.5.5
What is wrong with each of the following programs?
(a)
 1  public class ShowErrors {
 2    public static void main(String[] args) {
 3      ShowErrors t = new ShowErrors(5);
 4    }
 5  }
 
(b)
 1  public class ShowErrors {
 2    public static void main(String[] args) {
 3      ShowErrors t = new ShowErrors();
 4      t.x();
 5    }
 6  }
 
(c)
 1  public class ShowErrors {
 2    public void method1() {
 3      Circle c;
 4      System.out.println("What is radius "
 5        + c.getRadius());
 6      c = new Circle();
 7    }
 8  }
            
(d)
 1  public class ShowErrors {
 2    public static void main(String[] args) {
 3      C c = new C(5.0);
 4      System.out.println(c.value);
 5    }
 6  }
 7
 8  class C {
 9    int value = 2;
10  }
9.5.6
What is wrong in the following code?
 1  class Test {
 2    public static void main(String[] args) {
 3      A a = new A();
 4      a.print();
 5    }
 6  }
 7
 8  class A {
 9    String s;
10
11    A(String newS) {
12      s = newS;
13    }
14
15    public void print() {
16      System.out.print(s);
17    }
18  }
9.5.7
What is the output of the following code?
public class A {
  boolean x;
 
  public static void main(String[] args) {
    A a = new A();
    System.out.println(a.x);
  }
}
Section 9.6
9.6.1
How do you create a Date for the current time? How do you display the current time?
9.6.2
How do you create a Point2D? Suppose p1 and p2 are two instances of Point2D, how do you obtain the distance between the two points? How do you obtain the midpoint between the two points?
9.6.3
Which packages contain the classes Date, Random, Point2D, System, and Math?
Section 9.7
9.7.1
Suppose that the class F is defined in (a). Let f be an instance of F. Which of the statements in (b) are correct?
(a)
public class F {
  int i;
  static String s;

  void imethod() {
  }

  static void smethod() {
  }
}

(b)
System.out.println(f.i);
System.out.println(f.s);
f.imethod();
f.smethod();
System.out.println(F.i);
System.out.println(F.s);
F.imethod();
F.smethod();
9.7.2
Add the static keyword in the place of ? if appropriate.
public class Test { 
  int count; 
            
  public ? void main(String[] args) { 
    ...
  }
            
  public ? int getCount() { 
    return count;
  } 
  
  public ? int factorial(int n) { 
    int result = 1;
    for (int i = 1; i <= n; i++)
      result *= i; 
    return result;
  }
}
9.7.3
1. Can you invoke an instance method or reference an instance variable from a static method?
2. Can you invoke a static method or reference a static variable from an instance method?
3. What is wrong in the following code?
 1  public class C {
 2    Circle c = new Circle();
 3
 4    public static void main(String[] args) {
 5      method1();
 6    }
 7
 8    public void method1() {
 9      method2();
10    }
11
12    public static void method2() {
13      System.out.println("What is radius " + c.getRadius());
14    }
15  }
Section 9.9
9.9.1
What is an accessor method? What is a mutator method? What are the naming conventions for accessor methods and mutator methods?
9.9.2
What are the benefits of data field encapsulation?
9.9.3
In the following code, radius is private in the Circle class, and myCircle is an object of the Circle class. Does the highlighted code cause any problems? If so, explain why.
public class Circle {
  private double radius = 1;

  /** Find the area of this circle */
  public double getArea() {
    return radius * radius * Math.PI;
  }

  public static void main(String[] args) {
    Circle myCircle = new Circle();
    System.out.println("Radius is " + myCircle.radius);
  }
}
Section 9.10
9.10.1
Describe the difference between passing a parameter of a primitive type and passing a parameter of a reference type. Show the output of the following programs:
public class Test {
  public static void main(String[] args) {
    Count myCount = new Count();
    int times = 0;

    for (int i = 0; i < 100; i++)
      increment(myCount, times);

    System.out.println("count is " + myCount.count);
    System.out.println("times is " + times);
  }

  public static void increment(Count c, int times) {
    c.count++;
    times++;
  }
}

class Count {
  public int count;

  public Count(int c) {
    count = c;
  }

  public Count() {
    count = 1;
  }
}
9.10.2
Show the output of the following program:
public class Test {
  public static void main(String[] args) {
    Circle circle1 = new Circle(1);
    Circle circle2 = new Circle(2);

    swap1(circle1, circle2);
    System.out.println("After swap1: circle1 = " + 
    circle1.radius + " circle2 = " + circle2.radius);

    swap2(circle1, circle2);
    System.out.println("After swap2: circle1 = " + 
      circle1.radius + " circle2 = " + circle2.radius);
  }

  public static void swap1(Circle x, Circle y) {
    Circle temp = x;
    x = y;
    y = temp;
  }

  public static void swap2(Circle x, Circle y) {
    double temp = x.radius;
    x.radius = y.radius;
    y.radius = temp;
  }
}

class Circle {
  double radius;

  Circle(double newRadius) {
    radius = newRadius;
  }
}
9.10.3
Show the output of the following code:
(a)
public class Test {
  public static void main(String[] args) {
    int[] a = {1, 2};
    swap(a[0], a[1]);
    System.out.println("a[0] = " + a[0] 
      + " a[1] = " + a[1]);    
  }

  public static void swap(int n1, int n2) {
    int temp = n1;
    n1 = n2;
    n2 = temp;
  }
} 

(b)
public class Test {
  public static void main(String[] args) {
    int[] a = {1, 2};
    swap(a);
    System.out.println("a[0] = " + a[0] 
      + " a[1] = " + a[1]);    
  }

  public static void swap(int[] a) {
    int temp = a[0];
    a[0] = a[1];
    a[1] = temp;
  }
}

(c)
public class Test {
  public static void main(String[] args) {
    T t = new T();
    swap(t);
    System.out.println("e1 = " + t.e1 
       + " e2 = " + t.e2);    
  }

  public static void swap(T t) {
    int temp = t.e1;
    t.e1 = t.e2;
    t.e2 = temp;
  }
}

class T {
  int e1 = 1;
  int e2 = 2; 
}

(d)
public class Test {
  public static void main(String[] args) {
    T t1 = new T(); 
    T t2 = new T(); 
    System.out.println("t1's i = " + 
      t1.i + " and j = " + t1.j);
    System.out.println("t2's i = " + 
      t2.i + " and j = " + t2.j);
  }  
}

class T {
  static int i = 0;
  int j = 0;

  T() {
    i++;
    j = 1;
  }
}            
9.10.4
What is the output of the following programs?
(a)
import java.util.Date;

public class Test {
  public static void main(String[] args) {
    Date date = null;
    m1(date);
    System.out.println(date);
  }

  public static void m1(Date date) {
    date = new Date();
  }
}

(b)
import java.util.Date;

public class Test {
  public static void main(String[] args) {
    Date date = new Date(1234567);
    m1(date);
    System.out.println(date.getTime());
  }

  public static void m1(Date date) {
    date = new Date(7654321);
  }
} 

(c)
import java.util.Date;

public class Test {
  public static void main(String[] args) {
    Date date = new Date(1234567);
    m1(date);
    System.out.println(date.getTime());
  }

  public static void m1(Date date) {
    date.setTime(7654321);
  }
} 

(d)
import java.util.Date;

public class Test {
  public static void main(String[] args) {
    Date date = new Date(1234567);
    m1(date);
    System.out.println(date.getTime());
  }

  public static void m1(Date date) {
    date = null;
  }
}             
Section 9.11
9.11.1
What is wrong in the following code?
 1  public class Test {
 2    public static void main(String[] args) {
 3      java.util.Date[] dates = new java.util.Date[10];
 4      System.out.println(dates[0]);
 5      System.out.println(dates[0].toString());
 6    }
 7  }
Section 9.12
9.12.1
If a class contains only private data fields and no setter methods, is the class immutable?
9.12.2
If all the data fields in a class are private and of primitive types, and the class doesn't contain any setter methods, is the class immutable?
9.12.3
Is the following class immutable?
public class A {
  private int[] values;

  public int[] getValues() {
    return values;
  }
}
Section 9.13
9.13.1
What is the output of the following program?
public class Test {
  private static int i = 0;
  private static int j = 0;

  public static void main(String[] args) {
    int i = 2;
    int k = 3;

    {
      int j = 3;
      System.out.println("i + j is " + i + j);
    }

    k = i + j;
    System.out.println("k is " + k);
    System.out.println("j is " + j);
  }
}
Section 9.14
9.14.1
Describe the role of the this keyword.
9.14.2
What is wrong in the following code?
 1  public class C {
 2    private int p;
 3
 4    public C() {
 5      System.out.println("C's no-arg constructor invoked");
 6      this(0);
 7    }
 8
 9    public C(int p) {
10      p = p;
11    }
12
13    public void setP(int p) {
14      p = p;
15    }
16  }
9.14.3
What is wrong in the following code?
public class Test {
  private int id;

  public void m1() {
    this.id = 45;
  }

  public void m2() {
    Test.id = 45;
  }
}