Introduction To Java Programming Comprehensive Version 9th Edition Liang Test Bank
Introduction To Java Programming Comprehensive Version 9th Edition Liang Test Bank
Part I:
(A)
B’s constructor is invoked
A’s constructor is invoked
(B)
(a) The program has a syntax error because x does not have the compareTo
method.
(b) The program has a syntax error because the member access operator (.) is
executed before the casting operator.
(C)
(1) false
(2) true
(3) false (because they are created at different times)
(4) true
(E) The method throws a checked exception. You have to declare to throw
the exception in the method header.
Part II:
1
return result;
}
a. private method
b. protected method
c. public method
d. a and c
e. b and c
Key:e
2
#
2. Show the output of running the class Test in the following code:
interface A {
void print();
}
class C {}
a. Nothing.
b. b is an instance of A.
c. b is an instance of C.
d. b is an instance of A followed by b is an instance of C.
Key:d
#
3. When you implement a method that is defined in a superclass, you
__________ the original method.
a. overload
b. override
c. copy
d. call
Key:b
#
4. What is the output of running the class C.
public class C {
public static void main(String[] args) {
Object[] o = {new A(), new B()};
System.out.print(o[0]);
System.out.print(o[1]);
}
}
class A extends B {
public String toString() {
return "A";
}
}
3
class B {
public String toString() {
return "B";
}
}
a. AB
b. BA
c. AA
d. BB
e. None of above
Key:a
#
5. What is the output of running class C?
class A {
public A() {
System.out.println(
"The default constructor of A is invoked");
}
}
class B extends A {
public B(String s) {
System.out.println(s);
}
}
public class C {
public static void main(String[] args) {
B b = new B("The constructor of B is invoked");
}
}
a. none
b. "The constructor of B is invoked"
c. "The default constructor of A is invoked" "The constructor of B
is invoked"
d. "The default constructor of A is invoked"
Key:c
#
6. Analyze the following code:
a. The program has a syntax error because Test1 does not have a main
method.
4
b. The program has a syntax error because o1 is an Object instance
and it does not have the compareTo method.
c. The program has a syntax error because you cannot cast an Object
instance o1 into Comparable.
d. The program would compile if ((Comparable)o1.compareTo(o2) >= 0)
is replaced by (((Comparable)o1).compareTo(o2) >= 0).
e. b and d are both correct.
Key:e
#
7. The method _____ overrides the following method:
#
8. Which of the following possible modifications will fix the errors in
this code?
#
9. Analyze the following code.
class Test {
public static void main(String[] args) {
Object x = new Integer(2);
System.out.println(x.toString());
}
}
5
Key:c
#
10. What exception type does the following program throw?
public class Test {
public static void main(String[] args) {
Object o = new Object();
String d = (String)o;
}
}
a. ArithmeticException
b. No exception
c. StringIndexOutOfBoundsException
d. ArrayIndexOutOfBoundsException
e. ClassCastException
Key:e
#
11. What exception type does the following program throw?
public class Test {
public static void main(String[] args) {
Object o = null;
System.out.println(o.toString());
}
}
a. ArrayIndexOutOfBoundsException
b. ClassCastException
c. NullPointerException
d. ArithmeticException
e. StringIndexOutOfBoundsException
Key:c