Developed by Y. Daniel Liang using Java Servlets (Current time is Sat Nov 21 19:48:05 EST 2009)

The questions are to accompany Introduction to Java Programming, 4E by Y. Daniel Liang. Please report errors to y.daniel.liang@gmail.com.

Introduction to Java Programming, 4E has been superseded by Introduction to Java Programming, 5E.

Chapter 6: Objects and Classes

Sections 6.2-6.4
1   Analyze the following code:

class Circle {
   private double radius;
  
   public Circle(double radius) {
     radius = radius;
   }
}

A. The program has a compilation error because it does not have a main method.
B. The program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0.
C. The program has a compilation error because you cannot assign radius to radius.
D. The program does not compile because Circle does not have a default constructor.

2   Analyze the following code:

public class Test {
   public static void main(String args[]) {
     NClass nc = new NClass();
     nc.t = nc.t++;
   }
}

class NClass {
   int t;
   private NClass() {
   }
}

A. The program has a compilation error because the NClass class has a private constructor.
B. The program does not compile because the parameter list of the main method is wrong.
C. The program compiles, but has a runtime error because t has no initial value.
D. The program compiles and runs fine.

In the following code, suppose that f is an instance of Foo. Answer Questions 3 to 4.
public class Foo {
   int i;
   static int s;

   public static void main(String[] args) {
     Foo f1 = new Foo();
     System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s);
     Foo f2 = new Foo();
     System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s);
     Foo f3 = new Foo();
     System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s);
   }

   public Foo() {
     i++;
     s++;
   }
}

3   What is the printout of the second println statement in the main method?

A. f2.i is 1 f2.s is 1
B. f2.i is 1 f2.s is 2
C. f2.i is 2 f2.s is 2
D. None of the above

4   What is the printout of the third println statement in the main method?

A. f3.i is 1 f3.s is 1
B. f3.i is 1 f3.s is 2
C. f3.i is 1 f3.s is 3
D. f3.i is 3 f3.s is 3
E. f3.i is 3 f3.s is 3

5   Analyze the following code.

public class Test {
   int x;

   public Test(String t) {
      System.out.println("Test");
   }

   public static void main(String[] args) {
     Test test = new Test();
     System.out.println(test.x);
   }
}

A. The program has a syntax error because System.out.println method cannot be invoked from the constructor.
B. The program has a syntax error because x has not been initialized.
C. The program has a syntax error because you cannot create an object from the class that defines the object.
D. The program has a syntax error because Test does not have a default constructor.
E. None of the above.

6   Suppose TestCircle and Circle in Example 6.1 are in two separate files named TestCircle.java and Circle.java, respectively. What is the outcome of compiling TestCircle.java and then Circle.java?

A. Only TestCircle.java compiles.
B. Only Circle.java compiles.
C. Both compile fine.
D. Neither compiles successfully.

7   An object is an instance of a __________.

A. program
B. class
C. method
D. data

8   The keyword __________ is required to declare a class.

A. public
B. private
C. class
D. All of the above.

9   Analyze the following code:

public class Test {
   public static void main(String[] args) {
     A a = new A();
     a.print();
   }
}

class A {
   String s;

   A(String s) {
     this.s = s;
   }

   void print() {
     System.out.println(s);
   }
}

A. The program has a compilation error because class A is not a public class.
B. The program has a compilation error because class A does not have a default constructor.
C. The program compiles and runs fine and prints nothing.
D. None of the above.

10   What is wrong in the following code?

class TempClass {
   int i;
   public void TempClass(int j) {
     int i = j;
   }
}

public class C {
   public static void main(String[] args) {
     TempClass temp = new TempClass(2);
   }
}

A. The program has a compilation error because TempClass does not have a default constructor.
B. The program has a compilation error because TempClass does not have a constructor with an int argument.
C. The program compiles fine, but it does not run because class C is not public.
D. a and b.

Note: Questions 11-12 are based on the following statements:

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(
       "myCount.count = " + myCount.count);
     System.out.println("times = "+ times);
   }

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

class Count {
   int count;

   Count(int c) {
     count = c;
   }

   Count() {
     count = 1;
   }
}

11   What is the value of myCount.count displayed?

A. 101
B. 100
C. 99
D. 98
E. None of the above.

12   What is the value of times displayed?

A. 101
B. 100
C. 99
D. 98
E. None of the above.

13   Which of the following statement is most accurate?

A. A reference variable is an object.
B. A reference variable refers to an object.

14   Which of the following statement is most accurate?

A. An object may contain other objects.
B. An object may contain the references of other objects.

15   The java.util.Date class is introduced in Section 6.4. Analyze the following code and choose the best answer:

Which of the following code in A or B, or both creates an object of the Date class:

A:
public class Test {
   public Test() {
     new java.util.Date();
   }
}

B:
public class Test {
   public Test() {
     java.util.Date date = new java.util.Date();
   }
}

A. A.
B. B.
C. Neither

Section 6.5 Visibility Modifiers, Accessors, and Mutators
16   To prevent a class from being instantiated, _____________________

A. don't use any modifiers on the constructor.
B. use the public modifier on the constructor.
C. use the private modifier on the constructor.
D. use the static modifier on the constructor.
E. None of the above.

17   Analyze the following code:

public class Test {
   private int t;

   public static void main(String[] args) {
     int x;
     System.out.println(t);
   }
}

A. The variable t is not initialized and therefore causes errors.
B. The variable t is private and therefore cannot be accessed in the main method.
C. t is non-static and it cannot be referenced in a static context in the main method.
D. The variable x is not initialized and therefore causes errors.
E. The program compiles and runs fine.

18   Analyze the following code:

public class Test {
   public static void main(String[] args) {
     double radius;
     final double PI= 3.15169;
     double area = radius * radius * PI;
     System.out.println("Area is " + area);
   }
}

A. The program has syntax errors because the variable radius is not initialized.
B. The program has a syntax error because a constant PI is defined inside a method.
C. The program has no syntax errors but will get a runtime error because radius is not initialized.
D. The program compiles and runs fine.

19   Analyze the following code and choose the best answer:

public class Foo {
   private int x;

   public static void main(String[] args) {
     Foo foo = new Foo();
     System.out.println(foo.x);
   }
}

A. Since x is private, it cannot be accessed from an object foo.
B. Since x is defined in the class Foo, it can be accessed by any method inside the class without using an object. You can write the code to access x without creating an object such as foo in this code.
C. Since x is an instance variable, it cannot be directly used inside a main method. However, it can be accessed through an object such as foo in this code.
D. You cannot create a self-referenced object; that is, foo is created inside the class Foo.

Section 6.7 Static Variables, Constants, and Methods
20   Variables that are shared by every instances of a class are __________.

A. public variables
B. private variables
C. instance variables
D. class variables

21   A method that is associated with an individual object is called __________.

A. a static method
B. a class method
C. an instance method
D. an object method

22   To declare a constant MAX_LENGTH as a member of the class, you write

A. final static MAX_LENGTH = 99.98;
B. final static float MAX_LENGTH = 99.98;
C. static double MAX_LENGTH = 99.98;
D. final double MAX_LENGTH = 99.98;
E. final static double MAX_LENGTH = 99.98;

Section 6.8 Scope of Variables
Note: Questions 23 to 25 are based on the following code:

public class Foo {
   static int i = 0;
   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);
   }
}

23   What is the printout for the first statement in the main method?

A. i + j is 5
B. i + j is 6
C. i + j is 22
D. i + j is 23

24   What is the printout for the second statement in the main method?

A. k is 0
B. k is 1
C. k is 2
D. k is 3

25   What is the printout for the third statement in the main method?

A. j is 0
B. j is 1
C. j is 2
D. j is 3

26   You can declare two variables with the same name in __________.
A. a method one as a formal parameter and the other as a local variable
B. a block
C. two nested blocks in a method
D. different methods in a class