Inheritance and Polymorphism

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

C. The variable should be marked protected.

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

Which of the following statements are true?

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

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); } public void setI(int i) { this.i = 3 * i; } }

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

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); } public void setI(int i) { this.i = 3 * i; } }

D. The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.

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

D. that a class contains a data field that references another object.

Composition means ______________.

A. that data fields should be declared private.

Encapsulation means ______________.

A. Override the toString method defined in the Object class whenever possible. B. Override the equals method defined in the Object class whenever possible. C. A public default no-arg constructor is assumed if no constructors are defined explicitly. D. You should follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods.

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. E. Dynamic binding can apply to instance methods.

Which of the following statements are true?

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.

Which of the following statements are true?

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

Which of the following statements is false?

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

Which of the statements regarding the super keyword is incorrect?

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

Which statements are most accurate regarding the following classes? class A { private int i; protected int j; public int getI() { return i; } public int getJ() { return j; } } class B extends A { private int k; protected int m; public int getK() { return k; } public int getM() { return m; } }

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

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 }

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

The visibility of these modifiers increases in this order:

C. final class A { }

Which of the following classes cannot be extended?

A. Program 1 displays true and Program 2 displays true

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

B. Program 1 displays false and Program 2 displays true Explanation: In Program 1, the equals method in the Object class is invoked. In Program 2, the equals method in the class A is invoked. There are now two overloaded methods available in the class A. i.e. public boolean equals(Object a) and public boolean equals(A a). Which of the two is used by a1.equals(a2) is determined at compilation time. a1.equals(a2) in Program 1 matches the equals method defined in Object and a1.equals(a2) in Program 2 matches the equals method defined in the class A.

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

C. Program 1 displays true and Program 2 displays false Explanation: In Program 1, the equals method in the Object class is overridden. a1.equals(a2) invokes this method. It returns true. In Program 2, the equals method in the Object class is not overridden. a1.equals(a2) invokes the equals method defined in the Object class, which returns false in this case.

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

B. The program has a compilation error because the default constructor of A invokes the default constructor of B, but B does not have a default constructor. D. The program would compile fine if you add the following constructor into A: A(String s) { super(s); }

Analyze the following code: public class A extends B { } class B { public B(String s) { } }

A. The last line in the code causes a runtime 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.

Analyze the following code: ArrayList<String> list = new ArrayList<String>(); list.add("Beijing"); list.add("Tokyo"); list.add("Shanghai"); list.set(3, "Hong Kong");

A. The code has a compile error.

Analyze the following code: Circle c = new Circle (5); Cylinder c = cy;

C. The code is fine.

Analyze the following code: Cylinder cy = new Cylinder(1, 1); Circle c = cy;

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.

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

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. Explanation: Since a1 is an instance of A, the toString() method in the A class is invoked at runtime.

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; public String toString() { return "A's x is " + x; } }

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

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

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

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. You will get a runtime error because you cannot cast objects from sibling classes.

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

C. m(new Person()) causes an error D. m(new Object()) causes an error

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 { public String toString() { return "Student"; } } class Person extends Object { public String toString() { return "Person"; } }

D. c4 instanceof C2

Given the following code: 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(); Which of the following expressions evaluates to false?

B. true

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

C. may be true or false

Given two reference variables t1 and t2, if t1.equals(t2) is true, t1 == t2 ___________.

B. that a class can extend another class.

Inheritance means ______________.

E. x.clear()

Invoking _________ removes all elements in an ArrayList x.

B. x.get(0)

Invoking _________ returns the first element in an ArrayList x.

D. x.size()

Invoking _________ returns the number of the elements in an ArrayList x.

B. inheritance

Object-oriented programming allows you to derive new classes from existing classes. This is called ____________.

B. ArrayList<Double> list = new ArrayList<>(); list.add(3.4); D. ArrayList<Integer> list = new ArrayList<>(); list.add(3);

Which of the following code is corrcet?

A. A constructor may be static.

Which of the following is incorrect?

A. A method can be overloaded in the same class. 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.

Which of the following statements are true?

C. that a variable of supertype can refer to a subtype object.

Polymorphism means ______________.

B. x.set(2, "New York"); C. x.get(2) D. x.remove(2)

Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause runtime errors?

A. x.remove("Singapore") C. x.remove(1)

Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause the list to become [Beijing]?

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

Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following methods will cause the list to become [Beijing, Chicago, Singapore]?

B. {"green", "red", "green"} Explanation: The remove(o) method removes teh first element o from the list.

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

B. {"red", "green"}

Suppose an ArrayList list contains {"red", "red", "green"}. What is 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);

C. {"green"}

Suppose an ArrayList list contains {"red", "red", "green"}. What is 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--; }

C. {"green"}

Suppose an ArrayList list contains {"red", "red", "green"}. What is 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);

B. public boolean equals(Object other)

The equals method is defined in the Object class. Which of the following is correct to override it in the String class?

B. II

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

C. [New York, Atlanta, Dallas]

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

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

What is 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)));

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

What is 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)));

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

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

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

C. false true

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

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

B. Person Student

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 { public String getInfo() { return "Student"; } } class Person { public String getInfo() { return "Person"; } public void printPerson() { System.out.println(getInfo()); } }

B. 1

What is the output of the following code? public class Test1 { public static void main(String[] args) { ChildClass c = new ChildClass(); c.print(); } } class ParentClass { int id = 1; void print() { System.out.println(id); } } class ChildClass extends ParentClass { int id = 2; }

D. Use the default modifier.

What modifier should you use on a class so that a class in the same package can access it but a class in a different package cannot access it?

C. protected

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?

B. instanceof

Which of the following are Java keywords?

D. new String[100] E. new java.util.Date[100]

You can assign _________ to a variable of Object[] type.

C. new ArrayList()

You can create an ArrayList using _________.


Kaugnay na mga set ng pag-aaral

Intro to Sociology -- Chapter 12

View Set

Practice certificate exam results

View Set

Public Health 10: Stress Management Lesson 14, Public Health 10: Stress Management Lesson 13, UCI Public Health 10: Final Study Guide, Public Health 10

View Set

Operating Environments - UNIX Ch3

View Set

US History "The Great Depression begins"

View Set

064 - Chapter 64 - The Changing Nature of Agriculture

View Set