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
#
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
#
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
#
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)
#
28. Which of the following statements are true?
a. Override the equals(Object) method in the Object class whenever possible.
b. Override the toString() method 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.
#
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
#
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
true if two strings have the same content.
#
31. Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be .
a. true
b. false
#
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
#
Section 11.11 The ArrayList Class
43. Analyze the following code:
ArrayList<String> list = new ArrayList<>();
list.add(“Beijing”);
list.add(“Tokyo”);
list.add(“Shanghai”);
list.set(3, “Hong Kong”);
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.
#
44. What is the output of the following code?
ArrayList<java.util.Date> list = new ArrayList<>();
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
#
45. What is the output of the following code?
ArrayList<String> list = new ArrayList<>();
String s1 = new String(“Java”);
String s2 = new String(“Java”);
list.add(s1);
file:///C|/Users/usuller/Downloads/chapter11.txt[12/4/2019 3:47:54 PM]
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
#
36. You can create an ArrayList using .
a. new ArrayList[]
b. new ArrayList[100]
c. new ArrayList<>()
d. ArrayList()
#
37. Invoking removes all elements in an ArrayList x.
a. x.remove()
b. x.clean()
c. x.delete()
d. x.empty()
e. x.clear()
#
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”)
#
39. 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)
#
40. Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause runtime
errors?
a. x.get(1)
b. x.set(2, “New York”);
c. x.get(2)
d. x.remove(2)
file:///C|/Users/usuller/Downloads/chapter11.txt[12/4/2019 3:47:54 PM]
e. x.size()
#
41. Invoking returns the first element in an ArrayList x.
a. x.first()
b. x.get(0)
c. x.get(1)
d. x.get()
#
42. Invoking returns the number of the elements in an ArrayList x.
a. x.getSize()
b. x.getLength(0)
c. x.length(1)
#
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”}
#
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. {}
#
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;
}
file:///C|/Users/usuller/Downloads/chapter11.txt[12/4/2019 3:47:54 PM]
a. {“red”, “red”, “green”}
b. {“red”, “green”}
c. {“green”}
d. {}
#
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”}
#
50. The output from the following code is .
java.util.ArrayList<String> list
= new java.util.ArrayList<>();
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]
#
Section 11.12 Useful Methods for Lists
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”]
#
52. Analyze the following code:
file:///C|/Users/usuller/Downloads/chapter11.txt[12/4/2019 3:47:54 PM]
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.
#
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.
#
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}.
#
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}.
#
Section 11.14 The protected Data and Methods
59. A class design requires that a particular member variable must be accessible by any subclasses of this class, but
file:///C|/Users/usuller/Downloads/chapter11.txt[12/4/2019 3:47:54 PM]
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.
#
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.
#
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.
#
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.
#
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.
#
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.
#
58. The visibility of these modifiers increases in this order:
a. private, protected, none (if no modifier is used), and public.
b. private, none (if no modifier is used), protected, and public.
c. none (if no modifier is used), private, protected, and public.
d. none (if no modifier is used), protected, private, and public.
#
Section 11.15 Preventing Extending and Overriding
63. Which of the following classes cannot be extended?
a. class A { }
b. class A {&nbsp;&nbsp; private A() {&nbsp;&nbsp;}}
c. final class A { }
d. class A {&nbsp;&nbsp; protected A() {&nbsp;&nbsp;}}