Chapter 11 Quiz Study Guide

Ace your homework & exams now with Quizwiz!

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

new ArrayList<>()

You can create an ArrayList using _________.

Yes, because you can always cast from subclass to superclass.

Can the following statements be compiled? Circle circle = new Circle(5); GeometricObject object = circle;

instanceof Explanation: A simple rule: the keywords are all in lowercase.

Which of the following are Java keywords? * instanceOf * instanceof * cast * casting

final class A { }

Which of the following classes cannot be extended? * class A { } * class A { private A() { }} * final class A { } * class A { protected A() { }}

To use Collections.max(array), array must be an ArrayList, not an array.

Correct errors in the following statements: int[] array = {3, 5, 95, 4, 15, 34, 3, 6, 5}; System.out.println(java.util.Collections.max(array));

Yes, because m() is protected. Even if A, B, and Main are different pakages, the Main class can be compiled, becasue the protected members can be accessed from a different package.

In the following code, the classes A, B, and Main are in the same package. Can the Main class be compiled? class A { protected void m() { } } class B extends A { } class Main { public void p() { B b = new B(); b.m(); } }

(a) True. You can always successfully cast an instance of a subclass to a superclass. for example, casting apple to fruit is fine. (b) False. You cannot always successfully cast an instance of a superclass to a subclass. For example, casting fruit to apple is not always succeful unless a fruit is an apple.

Indicate true or false for the following statements: (a) You can always successfully cast an instance of a subclass to a superclass. (b) You can always successfully cast an instance of a superclass to a subclass.

x.clear()

Invoking _________ removes all elements in an ArrayList x.

x.get(0)

Invoking _________ returns the first element in an ArrayList x.

x.size()

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

It is method overridden.

If a method in a subclass has the same signature as a method in its superclass with the same return type, is the method overridden or overloaded?

False. You can only override accessible instance methods.

True or false? You can override a static method defined in a superclass.

*A constructor may be static. Explanation: A constructor cannot be static, because you use a constructor to create a specific instance. A constructor may be private. In this case, the use cannot create an instance using this constructor. For example, the constructor in the Math class is private. A constructor may invoke a static method just like any method can invoke a static method. A constructor can invoke an overloaded constructor using the this keyword. So, the correct answer is A.

Which of the following is incorrect? * A constructor may be static. * A constructor may be private. * A constructor may invoke a static method. * A constructor may invoke an overloaded constructor. * A constructor invokes its superclass no-arg constructor by default if a constructor does not invoke an overloaded constructor or its superclass?s constructor.

The variable should be marked protected. Explanation: See the section on the protected modifier.

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?

False

A final class can be extended.

True

A final class can have instances.

False

A final method can be overridden.

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

true

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

inheritance

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

["red", "red", "green", "blue"]

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

* x.set(2, "New York"); * x.get(2) * x.remove(2) Explanation: There is no element at index 2.

Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause runtime errors? * x.get(1) * x.set(2, "New York"); * x.get(2) * x.remove(2) * x.size()

* x.remove("Singapore") x.remove(1)

Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause the list to become [Beijing]? * x.remove("Singapore") * x.remove(0) * x.remove(1) * x.remove(2)

"The default constructor of A is invoked" followed by "The default constructor of B is invoked" Explanation: Superclass's constructor is called before executing the statements in the subclass constructor.

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

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.

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

Method overloading defines methods of the same name in a class. Method overriding modifies the methods that are defined in the superclasses.

Explain the difference between method overloading and method overriding.

list consists of Double objects. list.add(1) automatically converts 1 into an Integer object. It will work if you change it to list.add(1.0).

Explain why the following code is wrong. ArrayList<Double> list = new ArrayList<>(); list.add(1);

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 }

False. (But yes in a subclass that extends the class where the protected datum is defined.)

A protected datum or method can be accessed by any class in different packages.

True

A protected datum or method can be accessed by any class in the same package.

True

A protected datum or method can be accessed by its subclasses in any package.

* The last line in the code causes a runtime error because there is no element at index 3 in the array list. * If you replace the last line by list.add(3, "Hong Kong"), the code will compile and run fine. Explanation: There is no element at index 3

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

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.

Analyze the following code: Double[] array = {1, 2, 3}; ArrayList<Double> list = new ArrayList<>(Arrays.asList(array)); System.out.println(list);

new int[50] cannot be assigned to into a variable of Object[] type, but new Integer[50], new String[50], or new Object[50] are fine.

Can you assign new int[50], new Integer[50], new String[50], or new Object[50], into a variable of Object[] type?

{"green"}

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

Use the final keyword.

How do you prevent a class from being extended? How do you prevent a method from being overridden?

False. You can only override accessible instance methods.

True or false? You can override a private method defined in a superclass.

It forces the compiler to check the signature of the overridden method to ensure that the method is defined correctly.

What is the benefit of using the @Override annotation?

(a) The output is A's no-arg constructor is invoked (b) The default constructor of B attempts to invoke the default of constructor of A, but class A's default constructor is not defined.

What is the output of running the class C in (a)? What problem arises in compiling the program in (b)? (a) class A { public A() { System.out.println("A's no-arg constructor is invoked"); } } class B extends A { } public class C { public static void main(String[] args) { B b = new B(); } } (b) class A { public A(int x) { } } class B extends A { public B() { } } public class C { public static void main(String[] args) { B b = new B(); } }

The extends keyword is used to define a subclass that extends a superclass.

What keyword do you use to define a subclass?

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

s, o, and d reference the same String object. Explanation: Casting object reference variable does not affect the contents of the 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; } }

The constructor of class A is called and it displays "i from A is 7". Explanation: When invoking new B(), B's superclass A's constructor is invoked first. So 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); } @Override public void setI(int i) { this.i = 3 * i; } }

The code has a compile error. Explanation: You cannot assign a variable of a supertype to a subtype without explicit casting.

Assume Cylinder is a subtype of Circle. Analyze the following code: Circle c = new Circle (5); Cylinder c = cy;

(circle instanceof GeometricObject1) => true (object instanceof GeometricObject1) => true (circle instanceof Circle1) => true (object instanceof Circle1) => false

Assume are circle and object1 created as follows: Circle circle = new Circle(1); GeometricObject object1 = new GeometricObject(); Are the following Boolean expressions true or false? (circle instanceof GeometricObject) (object instanceof GeometricObject) (circle instanceof Circle) (object instanceof Circle)

To use asList(array), array must be an array of objects.

Correct errors in the following statements: int[] array = {3, 5, 95, 4, 15, 34, 3, 6, 5}; ArrayList<Integer> list = new ArrayList<>(Arrays.asList(array));

Error 1: list.add(new java.util.Date()); is wrong, because it is an array list of strings. You cannot add Date objects to this list . Error 2: list.set(3, "Dallas"); is wrong because there is no element at index 3 in the list. Error 3: list.get(3) is wrong because there is no element at index 3 in the list.

Identify the errors in the following code. ArrayList<String> list = new ArrayList<>(); list.add("Denver"); list.add("Austin"); list.add(new java.util.Date()); String city = list.get(0); list.set(3, "Dallas"); System.out.println(list.get(3));

i from A is 40 i from A is 60 i from B is 60

Show the output of following program: public class Test { public static void main(String[] args) { new A(); 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; } }

Apple constructor with weight Apple no-arg constructor Apple: 1.0 --------------- Apple constructor with weight GoldenDelicious constructor with weight GoldenDelicious: 7.0 --------------- Apple constructor with weight GoldenDelicious constructor with weight GoldenDelicious: 8.0

Show the output of following program: public class Test { public static void main(String[] args) { Apple a = new Apple(); System.out.println(a); System.out.println("---------------"); GoldenDelicious g = new GoldenDelicious(7); System.out.println(g); System.out.println("---------------"); Apple c = new GoldenDelicious(8); System.out.println(c); } } class Apple { double weight; public Apple() { this(1); System.out.println("Apple no-arg constructor"); } public Apple(double weight) { this.weight = weight; System.out.println("Apple constructor with weight"); } @Override public String toString() { return "Apple: " + weight; } } class GoldenDelicious extends Apple { public GoldenDelicious() { this(5); System.out.println("GoldenDelicious non-arg constructor"); } public GoldenDelicious(double weight) { super(weight); this.weight = weight; System.out.println("GoldenDelicious constructor with weight"); } @Override public String toString() { return "GoldenDelicious: " + weight; } }

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]?

{"green", "red", "green"}

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

Single inheritance allows a subclass to extend only one superclass. Multiple inheritance allows a subclass to extend multiple classes. Java does not allow multiple inheritance.

What is single inheritance? What is multiple inheritance? Does Java support multiple inheritance?

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

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

Line 8 causes a compile error, because int[] cannot be passed to Object[].

What is wrong in the following code? 1 public class Test { 2 public static void main(String[] args) { 3 Integer[] list1 = {12, 24, 55, 1}; 4 Double[] list2 = {12.4, 24.0, 55.2, 1.0}; 5 int[] list3 = {1, 2, 3}; 6 printArray(list1); 7 printArray(list2); 8 printArray(list3); 9 } 10 11 public static void printArray(Object[] list) { 12 for (Object o: list) 13 System.out.print(o + " "); 14 System.out.println(); 15 } 16 }

Object apple = (Apple)fruit; Causes a runtime ClassCastingException.

What is wrong in the following code? 1 public class Test { 2 public static void main(String[] args) { 3 Object fruit = new Fruit(); 4 Object apple = (Apple)fruit; 5 } 6 } 7 8 class Apple extends Fruit { 9 } 10 11 class Fruit { 12 }

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 (including a subclass) in a different package cannot access it?

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?

* Dynamic binding can apply to static methods. Explanation: Dynamic binding is applied to instance methods, not static methods. Static methods are bound in the compile time.

Which of the following statements is false? * You can always pass an instance of a subclass to a parameter of its superclass type. This feature is known as polymorphism. * The compiler finds a matching method according to parameter type, number of parameters, and order of the parameters at compile time. * A method may be implemented in several subclasses. The Java Virtual Machine dynamically binds the implementation of the method at runtime. * Dynamic binding can apply to static methods. * Dynamic binding can apply to instance methods.

You can use super.super.p to invoke a method in superclass's parent class. Explanation: Using super.super is not allowed in Java.

Which of the statements regarding the super keyword is incorrect? * You can use super to invoke a super class constructor. * You can use super to invoke a super class method. * You can use super.super.p to invoke a method in superclass's parent class. * You cannot invoke a method in superclass's parent class.

An object of B contains data fields i, j, k, m. Explanation: The data fields in a superclass are contained in a subclass. Whether the data fields in a superclass can be accessed in a subclass is a visibility issue. A private data field in a superclass cannot be directly accessed in a subclass, but the data field may have the getter or setter methods, which can be used to get or set a data field value.

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

MyStack stack = new MyStack(); stack.push(11); // 11 is autoboxed into new Integer(11)

Write statements that create a MyStack and add number 11 to the stack.

* new String[100] * new java.util.Date[100] Explanation: Primitive data type array is not compatible with Object[].

You can assign _________ to a variable of Object[] type. * new char[100] * new int[100] * new double[100] * new String[100] * new java.util.Date[100]

False. A subclass is an extension of a superclass and normally contains more details information than its superclass.

True or false? A subclass is a subset of a superclass.

False. If a subclass's constructor explicitly invoke a superclass's constructor, the superclass's no-arg constructor is not invoked.

True or false? When invoking a constructor from a subclass, its superclass's no-arg constructor is always invoked.

Encapsulation, inheritance, and polymorphism. In simple terms, polymorphism means that a variable of a supertype can refer to a subtype object.

What are the three pillars of object-oriented programming? What is polymorphism?

A method may be implemented in several classes along the inheritance chain. The JVM decides which method is invoked at runtime. This is known as dynamic binding.

What is dynamic binding?

The code is fine. Explanation: You can assign a variable of a subtype to a supertype.

Assume Cylinder is a subtype of Circle. Analyze the following code: Cylinder cy = new Cylinder(1, 1); Circle c = cy;

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

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

Analyze the following code: Integer[] c = {3, 5}; java.util.Collections.shuffle(c); System.out.println(java.util.Arrays.toString(c));

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

Analyze the following code: double[] array = {1, 2, 3}; ArrayList<Double> list = new ArrayList<>(Arrays.asList(array)); System.out.println(list);

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

Analyze the following code: double[] c = {1, 2, 3}; System.out.println(java.util.Collections.max(c));

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

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

* When executing System.out.println(a2), the toString() method in the Object class is invoked. * 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; @Override public String toString() { return "A's x is " + x; } }

The constructor of class A is called and it displays "i from A is 60". Explanation: When invoking new B(), B's superclass A's constructor is invoked first. It invokes setI(20). The setI method in B is used because object created is new B(). The setI method in B assigns 3 * 20 to i. So 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); } @Override public void setI(int i) { this.i = 3 * i; } }

Causing a runtime exception (ClassCastExcpetion)

Can the following statements be compiled? GeometricObject object = new GeometricObject(); Circle circle = (Circle)object;

Matching a method signature and binding a method implementation are two separate issues. The declared type of the reference variable decides which method to match at compile time. The compiler finds a matching method according to parameter type, number of parameters, and order of the parameters at compile time. A method may be implemented in several subclasses. The JVM dynamically binds the implementation of the method at runtime, decided by the actual class of the object referenced by the variable.

Describe the difference between method matching and method binding.

Yes, because these two methods are defined in the Object class; therefore, they are available to all Java classes. The subclasses usually override these methods to provide specific information for these methods. The toString() method returns a string representation of the object; the equals() method compares the contents of two objects to determine whether they are the same.

Does every object have a toString method and an equals method? Where do they come from? How are they used? Is it appropriate to override these methods?

The ArrayList class has two overloaded remove method remove(Object) and remove(int index). The latter is invoked for list.remove(1) to remove the element in the list at index 1. To remove value 3 from the list, use list.remove(new Integer(3)).

Explain why the following code displays [1, 3] rather than [2, 3]. ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.remove(1); System.out.println(list); How do you remove integer value 3 from the list?

* m(new Person()) causes an error * m(new Object()) causes an error Explanation: You cannot pass a supertype variable to a subtype without explicit casting.

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

c4 instanceof C2

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

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

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

Use super() or super(args). This statement must be the first in the constructor in the subclass.

How do you explicitly invoke a superclass's constructor from a subclass?

Use super.method() or super.method(args).

How do you invoke an overridden superclass method from a subclass?

A subclass can explicitly invoke a suplerclass's constructor using the super keyword.

How does a subclass invoke its superclass's constructor?

The following lines: { radius = radius; // Must use this.radius = radius } class B extends Circle { Circle(radius); // Must use super(radius) length = length; // Must use this.length = length } public double getArea() { return getArea() * length; // super.getArea() }

Identify the problems in the following code: 1 public class Circle { 2 private double radius; 3 4 public Circle(double radius) { 5 radius = radius; 6 } 7 8 public double getRadius() { 9 return radius; 10 } 11 12 public double getArea() { 13 return radius * radius * Math.PI; 14 } 15 } 16 17 class B extends Circle { 18 private double length; 19 20 B(double radius, double length) { 21 Circle(radius); 22 length = length; 23 } 24 25 @Override 26 public double getArea() { 27 return getArea() * length; 28 } 29 }

It is method overloading.

If a method in a subclass has the same name as a method in its superclass with different parameter types, is the method overridden or overloaded?

It will be a problem if the return type is not a compatible return type.

If a method in a subclass has the same signature as a method in its superclass with a different return type, will this be a problem?

B's constructor is invoked A's constructor is invoked The default constructor of Object is invoked, when new A(3) is invoked. The Object's constructor is invoked before any statements in B's constructor are executed.

Show the output of following program: 1 public class Test { 2 public static void main(String[] args) { 3 A a = new A(3); 4 } 5 } 6 7 class A extends B { 8 public A(int t) { 9 System.out.println("A's constructor is invoked"); 10 } 11 } 12 13 class B { 14 public B() { 15 System.out.println("B's constructor is invoked"); 16 } 17 }

{"red", "green"}

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

{"green"}

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

After list.remove("Dallas"), the list becomes {"Dallas", "Houston", "Dallas"}. Here is the reason: Suppose the list contains two string elements "red" and "red". You want to remove "red" from the list. After the first "red" is removed, i becomes 1 and the list becomes {"red"}. i < list.size() is false. So the loop ends. The correct code should be for (int i = 0; i < list.size(); i++) { if (list.remove(element)) i--; }

Suppose the ArrayList list contains {"Dallas", "Dallas", "Houston", "Dallas"}. What is the list after invoking list.remove("Dallas") one time? Does the following code correctly remove all elements with value "Dallas" from the list? If not, correct the code. for (int i = 0; i < list.size(); i++) list.remove("Dallas");

The program has a compile error because you attempted to invoke the GeometricObject class's constructor illegally. Explanation: You have to use super() or super(withapproriatearguments) to invoke a super class constructor explicitly.

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

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? * public boolean equals(String other) * public boolean equals(Object other) * public static boolean equals(String other) * public static boolean equals(Object other)

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

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

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

The visibility of these modifiers increases in this order: * private, protected, none (if no modifier is used), and public. * private, none (if no modifier is used), protected, and public. * none (if no modifier is used), private, protected, and public. * none (if no modifier is used), protected, private, and public.

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

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

false false Explanation: o1 == o2 is false, since o1 and o2 are two different objects. o1.equals(o2) is false since the equals method returns o1 == o2 in the Object class.

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

false true Explanation: s1 == s2 is false, since s1 and s2 are two different objects. s1.equals(s2) is true since the equals method returns true if two strings have the same content.

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

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

Person Person Explanation: Note that the getInfo method is private in Person. It is not known to the outside of the class. This is the method invoked from the printPerson() method.

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 method can be overloaded in the same class. * If a method overrides another method, these two methods must have the same signature. * A method in a subclass can overload a method in the superclass.

Which of the following statements are true? * A method can be overloaded in the same class. * A method can be overridden in the same class. * If a method overloads another method, these two methods must have the same signature. * If a method overrides another method, these two methods must have the same signature. * A method in a subclass can overload a method in the superclass.

* A subclass is usually extended to contain more functions and more detailed information than its superclass. * "class A extends B" means A is a subclass of B.

Which of the following statements are true? * A subclass is a subset of a superclass. * A subclass is usually extended to contain more functions and more detailed information than its superclass. * "class A extends B" means A is a subclass of B. * "class A extends B" means B is a subclass of A.

* Override the equals(Object) method in the Object class whenever possible. * Override the toString() method in the Object class whenever possible. * A public default no-arg constructor is assumed if no constructors are defined explicitly. * 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? * Override the equals(Object) method in the Object class whenever possible. * Override the toString() method in the Object class whenever possible. * A public default no-arg constructor is assumed if no constructors are defined explicitly. * You should follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods.

* To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass. * Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them. * It is a compile error if two methods differ only in return type in the same class. * A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated. * 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? * To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass. * Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them. * It is a compile error if two methods differ only in return type in the same class. * A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated. * 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.

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

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


Related study sets

NUR 228 Exam 3 Podcast & Interactive Module Q's

View Set

Biology 11A Chapter 20 - Module Questions

View Set

Clin Assess Ch. 6 (Table 6-5, 6-6) Secondary Skin Lesions

View Set

Retirement Accounts and IRA Rules

View Set

Anatomy & Physiology Chapter 1: The Sciences of Anatomy & Physiology

View Set

Network and Security Midterm Review

View Set