Chapter 11 Inheritance and Polymorphism
Section 11.2 Superclasses and Subclasses
1. Object-oriented programming allows you to derive new classes from existing classes. This is called .
a. encapsulation
b. inheritance
c. abstraction
d. generalization
#
2. Which of the following statements are true?
a. A subclass is a subset of a superclass.
b. A subclass is usually extended to contain more functions and more detailed information than its superclass.
c. “class A extends B” means A is a subclass of B.
d. “class A extends B” means B is a subclass of A.
#
66. Inheritance means .
a. that data fields should be declared private
b. that a class can extend another class
c. that a variable of supertype can refer to a subtype object
d. that a class can contain another class
#
Section 11.3 Using the super Keyword
Section 11.3.1 Calling Superclass Constructors
3. Suppose you create a class Square to be a subclass of GeometricObject. Analyze the following code:
class Square extends GeometricObject {
double length;
Square(double length) {
GeometricObject(length);
}
}
a. The program compiles fine, but you cannot create an instance of Square because the constructor does not specify the
length of the Square.
b. The program has a compile error because you attempted to invoke the GeometricObject class’s constructor illegally.
c. The program compiles fine, but it has a runtime error because of invoking the Square class’s constructor illegally.
#
4. Analyze the following code:
public class A extends B {
}
class B {
public B(String s) {
}
}
a. The program has a compile error because A does not have a default constructor.
b. The program has a compile error because the default constructor of A invokes the default constructor of B, but B
does not have a default constructor.
c. The program would compile fine if you add the following constructor into A: A(String s) { }
d. The program would compile fine if you add the following constructor into A: A(String s) { super(s); }
#
5. Analyze the following code:
public class Test extends A {
public static void main(String[] args) {
Test t = new Test();
t.print();
}
}
class A {
String s;
A(String s) {
this.s = s;
}
public void print() {
System.out.println(s);
}
}
a. The program does not compile because Test does not have a default constructor Test().
b. The program has an implicit default constructor Test(), but it cannot be compiled, because its super class does not
have a default constructor. The program would compile if the constructor in the class A were removed.
c. The program would compile if a default constructor A(){ } is added to class A explicitly.
d. The program compiles, but it has a runtime error due to the conflict on the method name print.
#
Section 11.3.2 Constructor Chaining
6. 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() {
System.out.println(
“The default constructor of B is invoked”);
}
}
public class C {
public static void main(String[] args) {
B b = new B();
}
}
a. Nothing displayed
b. “The default constructor of B is invoked”
c. “The default constructor of A is invoked” followed by “The default constructor of B is invoked”
d. “The default constructor of B is invoked” followed by “The default constructor of A is invoked”
e. “The default constructor of A is invoked”
#
7. Which of the following is incorrect?
a. A constructor may be static.
b. A constructor may be private.
c. A constructor may invoke a static method.
d. A constructor may invoke an overloaded constructor.
e. A constructor invokes its superclass no-arg constructor by default if a constructor does not invoke an overloaded
constructor or its superclass’s constructor.
#
Section 11.3.3 Calling Superclass Methods
8. Which of the statements regarding the super keyword is incorrect?
a. You can use super to invoke a super class constructor.
b. You can use super to invoke a super class method.
c. You can use super.super.p to invoke a method in superclass’s parent class.
d. You cannot invoke a method in superclass’s parent class.
#
Section 11.4 Overriding Methods
9. Analyze the following code:
public class Test {
public static void main(String[] args) {
B b = new B();
b.m(5);
System.out.println(“i is + b.i);
}
}
class A {
int i;
public void m(int i) {
this.i = i;
}
file:///C|/Users/usuller/Downloads/chapter11.txt[12/4/2019 3:47:54 PM]
}
class B extends A {
public void m(String s) {
}
}
a. The program has a compile error, because m is overridden with a different signature in B.
b. The program has a compile error, because b.m(5) cannot be invoked since the method m(int) is hidden in B.
c. The program has a runtime error on b.i, because i is not accessible from b.
#
10. The getValue() method is overridden in two ways. Which one is correct?
I:
public class Test {
public static void main(String[] args) {
A a = new A();
System.out.println(a.getValue());
}
}
class B {
public String getValue() {
return “Any object”;
}
}
class A extends B {
public Object getValue() {
return “A string”;
}
}
II:
public class Test {
public static void main(String[] args) {
A a = new A();
System.out.println(a.getValue());
}
}
class B {
public Object getValue() {
return “Any object”;
}
}
class A extends B {
public String getValue() {
return “A string”;
}
}
a. I
b. II
c. Both I and II
d. Neither
#
Section 11.5 Overriding vs. Overloading
11. Which of the following statements are true?
a. To override a method, the method must be defined in the subclass using the same signature and compatible return
type as in its superclass.
b. Overloading a method is to provide more than one method with the same name but with different signatures to
distinguish them.
c. It is a compile error if two methods differ only in return type in the same class.
d. A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two
methods are completely unrelated.
e. A static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the
method defined in the superclass is hidden.
#
13. Analyze the following code:
public class Test {
public static void main(String[] args) {
new B();
}
}
class A {
int i = 7;
public A() {
System.out.println(“i from A is + i);
}
public void setI(int i) {
this.i = 2 * i;
}
}
class B extends A {
public B() {
setI(20);
// System.out.println(“i from B is + i);
}
@Override
public void setI(int i) {
this.i = 3 * i;
}
}
a. The constructor of class A is not called.
file:///C|/Users/usuller/Downloads/chapter11.txt[12/4/2019 3:47:54 PM]
b. The constructor of class A is called and it displays “i from A is 7″.
c. The constructor of class A is called and it displays “i from A is 40″.
d. The constructor of class A is called and it displays “i from A is 60″.
#
14. Analyze the following code:
public class Test {
public static void main(String[] args) {
new B();
}
}
class A {
int i = 7;
public A() {
setI(20);
System.out.println(“i from A is + i);
}
public void setI(int i) {
this.i = 2 * i;
}
}
class B extends A {
public B() {
// System.out.println(“i from B is + i);
}
@Override
public void setI(int i) {
this.i = 3 * i;
}
}
a. The constructor of class A is not called.
b. The constructor of class A is called and it displays “i from A is 7″.
c. The constructor of class A is called and it displays “i from A is 40″.
d. The constructor of class A is called and it displays “i from A is 60″.
#
12. Which of the following statements are true?
a. A method can be overloaded in the same class.
b. A method can be overridden in the same class.
c. If a method overloads another method, these two methods must have the same signature.
d. If a method overrides another method, these two methods must have the same signature.
e. A method in a subclass can overload a method in the superclass.
#
Section 11.6 The Object Class and Its toString() Method
15. Analyze the following code:
public class Test {
public static void main(String[] args) {
Object a1 = new A();
Object a2 = new Object();
System.out.println(a1);
System.out.println(a2);
}
}
class A {
int x;
@Override
public String toString() {
return “A’s x is + x;
}
}
a. The program cannot be compiled, because System.out.println(a1) is wrong and it should be replaced by
System.out.println(a1.toString());
b. When executing System.out.println(a1), the toString() method in the Object class is invoked.
c. When executing System.out.println(a2), the toString() method in the Object class is invoked.
d. When executing System.out.println(a1), the toString() method in the A class is invoked.
#
Section 11.7 Polymorphism
17. Given the following code, find the compile error.
public class Test {
public static void main(String[] args) {
m(new GraduateStudent());
m(new Student());
m(new Person());
m(new Object());
}
public static void m(Student x) {
System.out.println(x.toString());
}
}
class GraduateStudent extends Student {
}
class Student extends Person {
@Override
public String toString() {
return “Student”;
}
}
class Person extends Object {
@Override
public String toString() {
return “Person”;
}
}
a. m(new GraduateStudent()) causes an error
b. m(new Student()) causes an error
c. m(new Person()) causes an error
d. m(new Object()) causes an error
#
64. Polymorphism means .
a. that data fields should be declared private
b. that a class can extend another class
c. that a variable of supertype can refer to a subtype object
d. that a class can contain another class
#
Section 11.8 Dynamic Binding
18. What is the output of the following code?
public class Test {
public static void main(String[] args) {
new Person().printPerson();
new Student().printPerson();
}
}
class Student extends Person {
@Override
public String getInfo() {
return “Student”;
}
}
class Person {
public String getInfo() {
return “Person”;
}
public void printPerson() {
System.out.println(getInfo());
}
}
a. Person Person
b. Person Student
c. Student Student
d. Student Person
#
19. What is the output of the following code?
public class Test {
public static void main(String[] args) {
new Person().printPerson();
new Student().printPerson();
}
}
class Student extends Person {
private String getInfo() {
return “Student”;
}
}
class Person {
private String getInfo() {
return “Person”;
}
public void printPerson() {
System.out.println(getInfo());
}
}
a. Person Person
b. Person Student
c. Student Student
d. Student Person
#
16. Which of the following statements is false?
a. You can always pass an instance of a subclass to a parameter of its superclass type. This feature is known as
polymorphism.
b. The compiler finds a matching method according to parameter type, number of parameters, and order of the
parameters at compile time.
c. A method may be implemented in several subclasses. The Java Virtual Machine dynamically binds the
implementation of the method at runtime.
d. Dynamic binding can apply to static methods.
e. Dynamic binding can apply to instance methods.
#
Section 11.9 Casting Objects and the instanceof Operator
20. Which of the following are Java keywords?
a. instanceOf
b. instanceof
c. cast
d. casting
#
25. Analyze the following code:
public class Test {
public static void main(String[] args) {
String s = new String(“Welcome to Java”);
Object o = s;
String d = (String)o;
}
}
a. When assigning s to o in Object o = s, a new object is created.
b. When casting o to s in String d = (String)o, a new object is created.
c. When casting o to s in String d = (String)o, the contents of o is changed.
d. s, o, and d reference the same String object.
#
22. Assume Cylinder is a subtype of Circle. Analyze the following code:
Circle c = new Circle (5);
Cylinder c = cy;
a. The code has a compile error.
b. The code has a runtime error.
c. The code is fine.
#
23. Given the following classes and their objects:
class C1 {};
class C2 extends C1 {};
class C3 extends C1 {};
C2 c2 = new C2();
C3 c3 = new C3();
Analyze the following statement:
c2 = (C2)((C1)c3);
a. c3 is cast into c2 successfully.
b. You will get a runtime error because you cannot cast objects from sibling classes.
c. You will get a runtime error because the Java runtime system cannot perform multiple casting in nested form.
#
24. Given the following code, which of the following expressions evaluates to false?
class C1 {}
class C2 extends C1 { }
class C3 extends C2 { }
class C4 extends C1 {}
C1 c1 = new C1();
C2 c2 = new C2();
C3 c3 = new C3();
C4 c4 = new C4();
a. c1 instanceof C1
b. c2 instanceof C1
c. c3 instanceof C1
d. c4 instanceof C2
#
26. You can assign to a variable of Object[] type.
a. new char[100]
b. new int[100]
c. new double[100]
d. new String[100]
e. new java.util.Date[100]
#
21. Assume Cylinder is a subtype of Circle. Analyze the following code:
Cylinder cy = new Cylinder(1, 1);
Circle c = cy;
a. The code has a compile error.
b. The code has a runtime error.
c. The code is fine.
#
Section 11.10 The Object’s equals() Method
33. Analyze the following code.
// Program 1:
public class Test {
public static void main(String[] args) {
Object a1 = new A();
Object a2 = new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
public boolean equals(Object a) {
return this.x == ((A)a).x;
}
}
// Program 2:
public class Test {