Introduction to Java Programming, Seventh Edition, Y. Daniel Liang
Chapter 9 Thinking in Objects
Section 9.2 Immutable Objects and Classes
1
Which of the following statements are true about an immutable object?
A.
The contents of an immutable object cannot be modified.
B.
All properties of an immutable object must be private.
C.
All properties of an immutable object must be of primitive types.
D.
An object type property in an immutable object must also be immutable.
E.
An immutable object contains no mutator methods.
Section 9.3 Scope of Variables
2
What is the printout for the first statement in the main method?
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);
   }
}
A.
i + j is 5
B.
i + j is 6
C.
i + j is 22
D.
i + j is 23
3
What is the printout for the second statement in the main method?
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);
   }
}
A.
k is 0
B.
k is 1
C.
k is 2
D.
k is 3
4
What is the printout for the third statement in the main method?
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);
   }
}
A.
j is 0
B.
j is 1
C.
j is 2
D.
j is 3
5
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 (two nested blocks means one being inside the other)
D.
different methods in a class
Section 9.4 The this Keyword
6
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.
7
Analyze the following code:
class Test {
   private double i;
  
   public Test(double i) {
     this.t();
     this.i = i;
   }
   public Test() {
     System.out.println("Default constructor");
     this(1);
   }
   public void t() {
     System.out.println("Invoking t");
   }
}
A.
this.t() may be replaced by t().
B.
this.i may be replaced by i.
C.
this(1) must be called before System.out.println("Default constructor").
D.
this(1) must be replaced by this(1.0).