Chapter 11 Inheritance and Polymorphism

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

11.58 The visibility of these modifiers increases in this order:

private, none (if no modifier is used), protected, and public.

11.60 Which of the following statements is false? A. A public class can be accessed by a class from a different package. B. A private method cannot be accessed by a class in a different package. C. A protected method can be accessed by a subclass in a different package. D. A method with no visibility modifier can be accessed by a class in a different package.

D. A method with no visibility modifier can be accessed by a class in a different package.

Section 11.14 The protected Data and Methods 11.56 What modifier should you use on a class so that a class in the same package can access it but a class (including a subclass) in a different package cannot access it? A. public B. private C. protected D. Use the default modifier.

D. Use the default modifier.

11.63 Which of the following classes cannot be extended? A. class A { } B. class A { private A() { }} C. final class A { } D. class A { protected A() { }}

C. final class A { }

Section 11.11 The ArrayList Class 11.36 You can create an ArrayList using _________. A. new ArrayList[] B. new ArrayList[100] C. new ArrayList<>() D. ArrayList()

C. new ArrayList<>()

11.57 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 D. Use the default modifier.

C. protected

11.31 Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be ___________. A. true B. false

A. true

11.17 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

B. Person Student

11.53 Analyze the following code: double[] array = {1, 2, 3}; ArrayList<Double> list = new ArrayList<>(Arrays.asList(array)); System.out.println(list); A. The code is correct and displays [1, 2, 3]. B. The code is correct and displays [1.0, 2.0, 3.0]. C. The code has a compile error because an integer such as 1 is automatically converted into an Integer object, but the array element type is Double. D. The code has a compile error because asList(array) requires that the array elements are objects.

D. The code has a compile error because asList(array) requires that the array elements are objects.

11.47 Suppose an ArrayList list contains {"red", "red", "green"}. What is the list after the following code? String element = "red"; for (int i = 0; i < list.size(); i++) if (list.get(i).equals(element)) list.remove(element); A. {"red", "red", "green"} B. {"red", "green"} C. {"green"} D. {}

B. {"red", "green"}

Section 11.12 Useful Methods for Lists 11.51 Show the output of the following code: String[] array = {"red", "green", "blue"}; ArrayList<String> list = new ArrayList<>(Arrays.asList(array)); list.add(0, "red"); System.out.println(list); A. ["red", "green", "blue", "red"] B. ["red", "green", "blue"] C. ["red", "red", "green", "blue"] D. ["red", "green", "red", "blue"]

C. ["red", "red", "green", "blue"]

11.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

B. II

11.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(); } }

C. "The default constructor of A is invoked" followed by "The default constructor of B is invoked"

11.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 { 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(A a) { return this.x == a.x; } } A. Program 1 displays true and Program 2 displays true B. Program 1 displays false and Program 2 displays true C. Program 1 displays true and Program 2 displays false D. Program 1 displays false and Program 2 displays false

C. Program 1 displays true and Program 2 displays false

11.54 Analyze the following code: double[] c = {1, 2, 3}; System.out.println(java.util.Collections.max(c)); A. The code is correct and displays 3. B. The code is correct and displays 3.0. C. The code has a compile error on Collections.max(c). c cannot be an array. D. The code has a compile error on double[] c = {1, 2, 3}.

C. The code has a compile error on Collections.max(c). c cannot be an array.

11.59 A class design requires that a particular member variable must be accessible by any subclasses of this class, but otherwise not by classes which are not members of the same package. What should be done to achieve this? A. The variable should be marked public. B. The variable should be marked private. C. The variable should be marked protected. D. The variable should have no special access modifier. E. The variable should be marked private and an accessor method provided.

C. The variable should be marked protected.

11.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 { 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

A. Person Person

11.35 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(((A)a1).equals((A)a2)); } } class A { int x; public boolean equals(A a) { return this.x == a.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; public boolean equals(A a) { return this.x == a.x; } } A. Program 1 displays true and Program 2 displays true B. Program 1 displays false and Program 2 displays true C. Program 1 displays true and Program 2 displays false D. Program 1 displays false and Program 2 displays false

A. Program 1 displays true and Program 2 displays true Explanation: In Program 1, ((A)a1).equals((A)a2) matches the equals(A a) method in the class A.

11.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.

A. The code has a compile error.

11.34 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(A a) { return this.x == a.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; public boolean equals(A a) { return this.x == a.x; } } A. Program 1 displays true and Program 2 displays true B. Program 1 displays false and Program 2 displays true C. Program 1 displays true and Program 2 displays false D. Program 1 displays false and Program 2 displays false

B. Program 1 displays false and Program 2 displays true

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); } }

B. The program has a compile error because you attempted to invoke the GeometricObject class's constructor illegally.

11.20 Which of the following are Java keywords? A. instanceOf B. instanceof C. cast D. casting

B. instanceof

Section 11.10 The Object?s equals() Method 11.27 The equals method is defined in the Object class. Which of the following is correct to override it in the String class? A. public boolean equals(String other) B. public boolean equals(Object other) C. public static boolean equals(String other) D. public static boolean equals(Object other)

B. public boolean equals(Object other)

11.41 Invoking _________ returns the first element in an ArrayList x. A. x.first() B. x.get(0) C. x.get(1) D. x.get()

B. x.get(0)

11.46 Suppose an ArrayList list contains {"red", "green", "red", "green"}. What is the list after the following code? list.remove("red"); A. {"red", "green", "red", "green"} B. {"green", "red", "green"} C. {"green", "green"} D. {"red", "green", "green"}

B. {"green", "red", "green"}

11.52 Analyze the following code: Double[] array = {1, 2, 3}; ArrayList<Double> list = new ArrayList<>(Arrays.asList(array)); System.out.println(list); A. The code is correct and displays [1, 2, 3]. B. The code is correct and displays [1.0, 2.0, 3.0]. C. The code has a compile error because an integer such as 1 is automatically converted into an Integer object, but the array element type is Double. D. The code has a compile error because asList(array) requires that the array elements are objects.

C. The code has a compile error because an integer such as 1 is automatically converted into an Integer object, but the array element type is Double.

11.55 Analyze the following code: Integer[] c = {3, 5}; java.util.Collections.shuffle(c); System.out.println(java.util.Arrays.toString(c)); A. The code is correct and displays [3, 5]. B. The code is correct and displays [5, 3]. C. The code has a compile error on Collections.shuffle(c). c cannot be an array. D. The code has a compile error on Integer[] c = {3, 5}.

C. The code has a compile error on Collections.shuffle(c). c cannot be an array.

11.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.

C. The code is fine.

11.30 What is the output of the following code? public class Test { 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

C. false true

11.44 What is the output of the following code? ArrayList<java.util.Date> list = new ArrayList<java.util.Date>(); java.util.Date d = new java.util.Date(); list.add(d); list.add(d); System.out.println((list.get(0) == list.get(1)) + " " + (list.get(0)).equals(list.get(1))); A. true false B. false true C. true true D. false false

C. true true Explanation: list.get(0) and list.get(1) point to the same object.

11.38 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")

C. x.add(1, "Chicago")

11.48 Suppose an ArrayList list contains {"red", "red", "green"}. What is the list after the following code? String element = "red"; for (int i = 0; i < list.size(); i++) if (list.get(i).equals(element)) { list.remove(element); i--; } A. {"red", "red", "green"} B. {"red", "green"} C. {"green"} D. {}

C. {"green"}

11.45 What is the output of the following code? ArrayList<String> list = new ArrayList<String>(); String s1 = new String("Java"); String s2 = new String("Java"); list.add(s1); list.add(s2); System.out.println((list.get(0) == list.get(1)) + " " + (list.get(0)).equals(list.get(1))); A. true false B. false true C. true true D. false false

B. false true Explanation: list.get(0) and list.get(1) point to two different objects with the same string contents.

11.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.

A. A constructor may be static.

11.61 Which statements are most accurate regarding the following classes? class A { private int i; protected int j; } class B extends A { private int k; protected int m; } A. An object of B contains data fields i, j, k, m. B. An object of B contains data fields j, k, m. C. An object of B contains data fields j, m. D. An object of B contains data fields k, m.

A. An object of B contains data fields i, j, k, m.

11.62 Which statements are most accurate regarding the following classes? class A { private int i; protected int j; } class B extends A { private int k; protected int m; // some methods omitted } A. In the class B, an instance method can only access i, j, k, m. B. In the class B, an instance method can only access j, k, m. C. In the class B, an instance method can only access j, m. D. In the class B, an instance method can only access k, m.

B. In the class B, an instance method can only access j, k, m.

Object-oriented programming allows you to derive new classes from existing classes. This is called ____________. A. encapsulation B. inheritance C. abstraction D. generalization

B. inheritance

11.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.

C. You can use super.super.p to invoke a method in superclass's parent class.

11.50 The output from the following code is __________. java.util.ArrayList<String> list = new java.util.ArrayList<String>(); list.add("New York"); java.util.ArrayList<String> list1 = list; list.add("Atlanta"); list1.add("Dallas"); System.out.println(list1); A. [New York] B. [New York, Atlanta] C. [New York, Atlanta, Dallas] D. [New York, Dallas]

C. [New York, Atlanta, Dallas]

11.32 Given two reference variables t1 and t2, if t1.equals(t2) is true, t1 == t2 ___________. A. is always true B. is always false C. may be true or false

C. may be true or false Explanation: Two different objects may be equal with the same contents.

11.42 Invoking _________ returns the number of the elements in an ArrayList x. A. x.getSize() B. x.getLength(0) C. x.length(1) D. x.size()

D. x.size()

11.29 What is the output of the following code? public class Test { public static void main(String[] args) { Object o1 = new Object(); Object o2 = new Object(); System.out.print((o1 == o2) + " " + (o1.equals(o2))); } } A. false false B. true true C. false true D. true false

A. false false

11.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. 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".

B. The constructor of class A is called and it displays "i from A is 7".

11.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. D. The statement is correct.

B. You will get a runtime error because you cannot cast objects from sibling classes.

11.19 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.

D. Dynamic binding can apply to static methods.

11.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".

D. The constructor of class A is called and it displays "i from A is 60".

11.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; } } 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. D. The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in 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.

11.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

D. c4 instanceof C2

11.37 Invoking _________ removes all elements in an ArrayList x. A. x.remove() B. x.clean() C. x.delete() D. x.empty() E. x.clear()

E. x.clear()

11.49 Suppose an ArrayList list contains {"red", "red", "green"}. What is the list after the following code? String element = "red"; for (int i = list.size() - 1; i >= 0; i--) if (list.get(i).equals(element)) list.remove(element); A. {"red", "red", "green"} B. {"red", "green"} C. {"green"} D. {}

C. {"green"}

11.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.

D. s, o, and d reference the same String object.


Ensembles d'études connexes

Chapter 19 the respiratory system

View Set

Criminal Procedure: Investigation - Fifth Amendment

View Set

american history, urban america guided reading activities 13-1 to 13-2

View Set

Тестовые вопросы по Истории медицины

View Set

Chapter 14 - Databases and Database Management Systems

View Set