Assignment (8) Chapter 11 Inheritance and Polymorphism. Dr. Essam Halim
Assignment (8) Chapter 11 Inheritance and Polymorphism. Dr. Essam Halim
1
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
B. A subclass is usually extended to contain more functions and more detailed information than its superclass.
Section 11.3 Using the super KeywordSection 11.3.1 Calling Superclass Constructors
3 Suppose you create a class Cylinder to be a subclass of Circle. Analyze the following code:
Cylinder(double radius) {
Circle(radius);
}
}
A. The program compiles fine, but you cannot create an instance of Cylinder because the constructor does not
specify the length of the cylinder.
B. The program has a compile error because you attempted to invoke the Circle class's constructor illegally.
C. The program compiles fine, but it has a runtime error because of invoking the Circle class's constructor illegally.
import java.util.StringTokenizer;
A. The program has a compilation error because A does not have a default constructor.
B. The program has a compilation error because the default constructor of A invokes the default constructor of
StringTokenizer, but StringTokenizer 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); }
2
t.print();
}
}
class A {
String s;
A(String s) {
this.s = 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.
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
3
Section 11.3.3 Calling Superclass Methods
7 Which of the statements regarding the super keyword is incorrect?
Sections 11.4-11.5
8 Analyze the following code:
class A {
int i;
class B extends A {
public void m(String s) {
}
}
A. The program has a compilation error, because m is overridden with a different signature in B.
B. The program has a compilation 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.
D. The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in
B.
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 compilation 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.
4
10 Which of the following statements are true?
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.
class A {
int 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.
Sections 11.7-11.8
12 Which of the following statements are true?
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 compilation time.
C. A method may be implemented in several subclasses. The Java Virtual Machine dynamically binds the
implementation of the method at runtime.
5
13 Given the following code, find the compile error?
class Person {
public String getInfo() {
return "Person";
}
6
public void printPerson() {
System.out.println(getInfo());
}
}
A. Person Person
B. Person Student
C. Stduent Student
D. Student Person
class Person {
private String getInfo() {
return "Person";
}
A. Person Person
B. Person Student
C. Stduent Student
D. Student Person
A. instanceOf
B. instanceof
C. cast
D. casting
7
17 Analyze the following code:
class C1 {};
class C2 extends C1 {};
class C3 extends C1 {};
C2 c2 = new C2();
C3 c3 = new C3();
c2 = (C2)((C1)c3);
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.
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
8
B. c2 instanceof C1
C. c3 instanceof C1
D. c4 instanceof C2
A. new char[100]
B. new int[100]
C. new double[100]
D. new String[100]
E. new java.util.Date[100]
A. false false
B. true true
C. false true
D. true false
9
public static void main(String[] args) {
String s1 = new String("Java");
String s2 = new String("Java");
System.out.print((s1 == s2) + " " + (s1.equals(s2)));
}
}
A. false false
B. true true
C. false true
D. true false
A. is always false
B. is always true
// 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;
// Program 2:
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;
10
}
}
// 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;
// Program 2:
public class Test {
public static void main(String[] args) {
A a1 = new A();
A a2 = new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
11
// Program 1
public class Test {
public static void main(String[] args) {
Object a1 = new A();
Object a2 = new A();
System.out.println(((A)a1).equals((A)a2));
}
}
class A {
int x;
// Program 2
public class Test {
public static void main(String[] args) {
A a1 = new A();
A a2 = new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
A. new ArrayList[]
B. new ArrayList[100]
C. new ArrayList()
D. ArrayList()
A. x.remove()
12
B. x.clean()
C. x.delete()
D. x.empty()
E. x.clear()
31 Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following methods will cause the list to
become [Beijing, Chicago, Singapore]?
A. x.add("Chicago")
B. x.add(0, "Chicago")
C. x.add(1, "Chicago")
D. x.add(2, "Chicago")
32 Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause the list to
become [Beijing]?
A. x.remove("Singapore")
B. x.remove(0)
C. x.remove(1)
D. x.remove(2)
33 Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause runtime
errors?
A. x.get(1)
C. x.get(2)
D. x.remove(2)
E. x.size()
A. x.first()
B. x.get(0)
C. x.get(1)
D. x.get()
A. x.getSize()
B. x.getLength(0)
C. x.length(1)
13
D. x.size()
A. The last line in the code causes a runtime error because there is no element at index 3 in the array list.
B. The last line in the code has a compile error because there is no element at index 3 in the array list.
C. If you replace the last line by list.add(3, "Hong Kong"), the code will compile and run fine.
D. If you replace the last line by list.add(4, "Hong Kong"), the code will compile and run fine.
A. true false
B. false true
C. true true
D. false false
A. true false
B. false true
C. true true
D. false false
14
A. public
B. private
C. protected
40 What modifier should you use on the members of a class so that they are not accessible to another class in a different
package, but are accessible to any subclasses in any package?
A. public
B. private
C. protected
A. class A { }
C. final class A { }
Section Comprehensive
15
45 Composition means ______________.
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
16
C. Both I and II
D. Neither
47 Suppose an ArrayList list contains {"red", "green", "red", "green"}. What is list after the following code?
list.remove("red");
C. {"green", "green"}
48 Suppose an ArrayList list contains {"red", "red", "green"}. What is list after the following code?
B. {"red", "green"}
C. {"green"}
D. {}
17