Java Chapters 11, 12, and 13- Questions and
Answers| Latest Update
1 Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a no-arg
constructor. Which of the following is correct?
A. A a = new A();
B. A a = new B();
C. B b = new A();
D. B b = new B(); ✔️✔️B. A a = new B();
D. B b = new B();
2. What is the output of running class Test?
public class Test {
public static void main(String[] args) {
new Circle9();
}
}
public abstract class GeometricObject {
protected GeometricObject() {
System.out.print("A");
}
protected GeometricObject(String color, boolean filled) {
System.out.print("B");
}
,}
public class Circle9 extends GeometricObject {
/** No-arg constructor */
public Circle9() {
this(1.0);
System.out.print("C");
}
/** Construct circle with a specified radius */
public Circle9(double radius) {
this(radius, "white", false);
System.out.print("D");
}
/** Construct a circle with specified radius, filled, and color */
public Circle9(double radius, String color, boolean filled) {
super(color, filled);
System.out.print("E");
}
}
A. ABCD
B. BACD
C. CBAE
D. AEDC
E. BEDC ✔️✔️E. BEDC
,2. The java.lang.Number and its subclasses are introduced in Chapter 11. Analyze the following
code.
Number numberRef = new Integer(0);
Double doubleRef = (Double)numberRef;
A. There is no such class named Integer. You should use the class Int.
B. The compiler detects that numberRef is not an instance of Double.
C. A runtime class casting exception occurs, since numberRef is not an instance of Double.
D. The program runs fine, since Integer is a subclass of Double.
E. You can convert an int to double, so you can cast an Integer instance to a Double instance.
✔️✔️C. A runtime class casting exception occurs, since numberRef is not an instance of Double.
4. Analyze the following code.
Number[] numberArray = new Integer[2];
numberArray[0] = new Double(1.5);
A. You cannot use Number as a data type since it is an abstract class.
B. Since each element of numberArray is of the Number type, you cannot assign an Integer
object to it.
C. Since each element of numberArray is of the Number type, you cannot assign a Double
object to it.
D. At runtime, new Integer[2] is assigned to numberArray. This makes each element of
numberArray an Integer object. So you cannot assign a Double object to it. ✔️✔️D. At runtime,
new Integer[2] is assigned to numberArray. This makes each element of numberArray an
Integer object. So you cannot assign a Double object to it.
5. Analyze the following code. Which of the following statements is correct?
public class Test {
public static void main(String[] args) {
, Number x = new Integer(3);
System.out.println(x.intValue());
System.out.println(x.compareTo(new Integer(4)));
}
}
A. The program has a compile error because an Integer instance cannot be assigned to a
Number variable.
B. The program has a compile error because intValue is an abstract method in Number.
C. The program has a compile error because x does not have the compareTo method.
D. The program compiles and runs fine. ✔️✔️C. The program has a compile error because x does
not have the compareTo method.
6. Analyze the following code. Which of the following statements is correct?
public class Test {
public static void main(String[] args) {
Number x = new Integer(3);
System.out.println(x.intValue());
System.out.println((Integer)x.compareTo(new Integer(4)));
}
}
A. The program has a compile error because an Integer instance cannot be assigned to a
Number variable.
B. The program has a compile error because intValue is an abstract method in Number.
C. The program has a compile error because x cannot be cast into Integer.
D. The program has a compile error because the member access operator (.) is executed before
the casting operator.
E. The program compiles and runs fine. ✔️✔️D. The program has a compile error because the
member access operator (.) is executed before the casting operator.
Answers| Latest Update
1 Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a no-arg
constructor. Which of the following is correct?
A. A a = new A();
B. A a = new B();
C. B b = new A();
D. B b = new B(); ✔️✔️B. A a = new B();
D. B b = new B();
2. What is the output of running class Test?
public class Test {
public static void main(String[] args) {
new Circle9();
}
}
public abstract class GeometricObject {
protected GeometricObject() {
System.out.print("A");
}
protected GeometricObject(String color, boolean filled) {
System.out.print("B");
}
,}
public class Circle9 extends GeometricObject {
/** No-arg constructor */
public Circle9() {
this(1.0);
System.out.print("C");
}
/** Construct circle with a specified radius */
public Circle9(double radius) {
this(radius, "white", false);
System.out.print("D");
}
/** Construct a circle with specified radius, filled, and color */
public Circle9(double radius, String color, boolean filled) {
super(color, filled);
System.out.print("E");
}
}
A. ABCD
B. BACD
C. CBAE
D. AEDC
E. BEDC ✔️✔️E. BEDC
,2. The java.lang.Number and its subclasses are introduced in Chapter 11. Analyze the following
code.
Number numberRef = new Integer(0);
Double doubleRef = (Double)numberRef;
A. There is no such class named Integer. You should use the class Int.
B. The compiler detects that numberRef is not an instance of Double.
C. A runtime class casting exception occurs, since numberRef is not an instance of Double.
D. The program runs fine, since Integer is a subclass of Double.
E. You can convert an int to double, so you can cast an Integer instance to a Double instance.
✔️✔️C. A runtime class casting exception occurs, since numberRef is not an instance of Double.
4. Analyze the following code.
Number[] numberArray = new Integer[2];
numberArray[0] = new Double(1.5);
A. You cannot use Number as a data type since it is an abstract class.
B. Since each element of numberArray is of the Number type, you cannot assign an Integer
object to it.
C. Since each element of numberArray is of the Number type, you cannot assign a Double
object to it.
D. At runtime, new Integer[2] is assigned to numberArray. This makes each element of
numberArray an Integer object. So you cannot assign a Double object to it. ✔️✔️D. At runtime,
new Integer[2] is assigned to numberArray. This makes each element of numberArray an
Integer object. So you cannot assign a Double object to it.
5. Analyze the following code. Which of the following statements is correct?
public class Test {
public static void main(String[] args) {
, Number x = new Integer(3);
System.out.println(x.intValue());
System.out.println(x.compareTo(new Integer(4)));
}
}
A. The program has a compile error because an Integer instance cannot be assigned to a
Number variable.
B. The program has a compile error because intValue is an abstract method in Number.
C. The program has a compile error because x does not have the compareTo method.
D. The program compiles and runs fine. ✔️✔️C. The program has a compile error because x does
not have the compareTo method.
6. Analyze the following code. Which of the following statements is correct?
public class Test {
public static void main(String[] args) {
Number x = new Integer(3);
System.out.println(x.intValue());
System.out.println((Integer)x.compareTo(new Integer(4)));
}
}
A. The program has a compile error because an Integer instance cannot be assigned to a
Number variable.
B. The program has a compile error because intValue is an abstract method in Number.
C. The program has a compile error because x cannot be cast into Integer.
D. The program has a compile error because the member access operator (.) is executed before
the casting operator.
E. The program compiles and runs fine. ✔️✔️D. The program has a compile error because the
member access operator (.) is executed before the casting operator.