Chapter 7 Multiple Choice 7.1 Superclass is to subclass as a. child is to parent b. this is to super c. object is to primitive data d. base class is to derived class e. instance variable is to local variable 7.2 Look back at Figure 7.3 in the chapter. Which class in the diagram is both a subclass and a superclass? a. Animal b. Bird c. Parrot d. Lizard e. Horse 7.3 Suppose a class M inherits from a class P. In the constructor of M, how would you call the default (no arguments) constructor of P? a. P() b. this() c. super() d. parent() e. sub() 7.4 Which of the following from a parent class is usable in a child class? a. public constants b. private variables c. local variables d. private methods e. static methods 7.5 Look at Figure 7.3. Suppose animal is declared to be a reference to an Animal. Which of the following is a valid assignment? a. animal = new Reptile(); b. animal = new Parrot(); c. animal = new Horse(); d. animal = new Bat(); e. all of the above 7.6 Given the classes A and B, class A { void foo() { System.out.println("A’s foo"); } } class B extends A { void foo() { System.out.println("B’s foo"); } } what will be output by the following code? A aRef = new B(); aRef.foo(); a. A’s foo b. B’s foo c. There will be a compile error because aRef may only refer to A objects. d. There will be a compile error because the compiler can’t tell which foo method is being called. e. There will be a runtime error because the call to foo is ambiguous. 7.7 Given the following variable declaration Object obj = new String("hello"); which of the following expressions will not cause a compile error? a. obj.toUpperCase() b. obj.substring(0, 5) c. obj.equals("hi") d. obj.compareTo("hi") e. All of them will cause compile errors. 7.8 Given the method header void doSomething(Object param) which of the following is not a valid call to the method? a. doSomething("Java") b. doSomething(new String("Java")) c. doSomething(14) d. doSomething(new Integer(14)) e. All are valid calls 7.9 In this class header, which keyword should go in the blank to create an inheritance relationship? public class Child __________ Parent a. super b. extends c. implements d. inherits e. interface 7.10 If A inherits from B, B inherits from C, and C inherits from D, which of the following is true? a. A is the grandparent of B, C, and D. b. D is derived from B. c. C may use public variables that are declared in A. d. B may use public variables that are declared in A. e. A may use public variables that are declared in D. True/False 7.1 In an inheritance relationship, the parent class should be a more specific version of the child class. 7.2 A child class can use all public data and methods from its parent class. 7.3 The super reference means the same thing as the this reference, but it is only used in subclasses. 7.4 In Java, a subclass may inherit from many superclasses. 7.5 If a child class defines a method with the same signature as a method in its parent class, an error will occur. 7.6 A class in Java can be both a superclass and a subclass. 7.7 All classes are derived from the Object class. 7.8 An abstract class cannot be instantiated. 7.9 A polymorphic reference variable can refer to different types of objects at different times. 7.10 Polymorphism may occur with inheritance but not with interfaces. AP*-Style Multiple Choice Questions 7.1–7.5 refer to the following classes. public class Abc { private int a; public static final int b = 10; public void c() { System.out.print("ABC"); } } public class Xyz extends Abc { public void c() { super.c(); System.out.print("XYZ"); } public void z() { c(); } } 7.1 Which statement best characterizes the relationship between the two classes? (A) An Abc is an Xyz. (B) An Xyz is an Abc. (C) An Abc has an Xyz. (D) An Xyz has an Abc. (E) Abc and Xyz are siblings. 7.2 Which members of the class Abc can be used directly by the class Xyz? I. a II. b III. c (A) I only (B) II only (C) I and III only (D) II and III only (E) I, II, and III 7.3 What is printed by the following statements? Xyz q = new Xyz(); q.z(); (A) ABC (B) XYZ (C) ABCXYZ (D) XYZABC (E) Nothing is printed because there is a compile error. 7.4 Consider the following statement. ArrayList<Abc> items = new ArrayList<Abc>(); Which of the following statements will produce a compile error? I. items.add(new Abc()); II. items.add(new Xyz()); III. items.add(new Object()); (A) None will produce a compile error. (B) II only (C) III only (D) I and II only (E) II and III only 7.5 Consider the following statements. Abc p = new Xyz(); System.out.println( missing ); Which of the following, if used to replace missing above, will cause an error? (A) p.b (B) p.c (C) p.z (D) p (E) p.toString() 7.5 Consider the following statements. Abc p = new Xyz(); System.out.println( missing ); Which of the following, if used to replace missing above, will cause an error? (A) p.b (B) p.c (C) p.z (D) p (E) p.toString() 7.6 Consider the following code. public interface Speaker { public void speak(); } public interface Writer { public void write(); } public class Philosopher implements Speaker, Writer { // implementation not shown } Which of the following will not cause an error? (A) Speaker s = new Philosopher(); (B) Speaker s = new Writer(); (C) Philosopher p = new Speaker(); (D) Philosopher p = new Writer(); (E) Object o = new Writer();