NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#
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
Key:c 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.

#
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
Key:b 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.

#
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
Key:a In Program 1, ((A)a1).equals((A)a2) matches the equals(A a) method in the class A.

#
Section 11.11 The ArrayList Class
36. You can create an ArrayList using _________.
a. new ArrayList[]
b. new ArrayList[100]
c. new ArrayList<>()
d. ArrayList()
Key:c

#
37. Invoking _________ removes all elements in an ArrayList x.
a. x.remove()
b. x.clean()
c. x.delete()
d. x.empty()
e. x.clear()
Key:e

#
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")
Key:c

#
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)
Key:ac

#
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)
e. x.size()
Key:bcd There is no element at index 2.

#
41. Invoking _________ returns the first element in an ArrayList x.
a. x.first()
b. x.get(0)
c. x.get(1)
d. x.get()
Key:b

#
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()
Key:d

#
43. 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 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.
Key:ac There is no element at index 3.

#
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
Key:c list.get(0) and list.get(1) point to the same object.

#
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
Key:b list.get(0) and list.get(1) point to two different objects with the same string contents.

#
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"}
Key:b

#
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. {}
Key:b

#
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. {}
Key:c

#
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. {}
Key:c

#
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]
Key:c

#
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"]
Key:c

#
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.
Key:c

#
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.
Key:d

#
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 Integer[] c = {1, 2, 3}.
Key:c

#
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}.
Key:c

#
Section 11.14 The protected Data and Methods
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.
Key:d

#
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.
Key:c

#
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.
Key:b

#
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.
Key:c See the section on the protected modifier.

#
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.
Key:d

#
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.
Key:a 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.

#
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.
Key:b

#
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;}}
Key:c

#
Section Comprehensive
64. Polymorphism means ______________.
a. that data fields should be declared private
b. that a class can extend another class
c. that a variable of supertype can refer to a subtype object
d. that a class can contain another class
Key:c

#
65. Encapsulation means ______________.
a. that data fields should be declared private
b. that a class can extend another class
c. that a variable of supertype can refer to a subtype object
d. that a class can contain another class
Key:a

#
66. Inheritance means ______________.
a. that data fields should be declared private
b. that a class can extend another class
c. that a variable of supertype can refer to a subtype object
d. that a class can contain another class
Key:b

#
67. Composition means ______________.
a. that data fields should be declared private
a. that data fields should be declared private
b. that a class extends another class
c. that a variable of supertype refers to a subtype object
d. that a class contains a data field that references another object
Key:d
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.