Create arrays, List, Set, Map and Deque collections, and add, remove, update, retrieve and sort their elements.
1. The correct answer is D.
Explanation:
0 0 0
0 0 0
0 1 2
0 1 2
0 0 0
1 1 1
0 1 2
1 2 3
arr[0][0] = 0 + 0 = 0
, arr[0][1] = 0 + 1 = 1
, arr[0][2] = 0 + 2 = 2
, arr[1][0] = 1 + 0 = 1
, arr[1][1] = 1 + 1 = 2
, arr[1][2] = 1 + 2 = 3
.2. The correct answer is B.
Explanation:
public static T getFirstElement(T[] array) {
return array[0];
}
<T>
is missing before the return type T
.public static <T> T getFirstElement(T[] array) {
return array[0];
}
<T>
is correctly declared before the return type T
.public static <T> getFirstElement(T[] array) {
return array[0];
}
T
is missing.public static <T> T[] getFirstElement(T[] array) {
return array[0];
}
T[]
, which does not match the intended method return type.3. The correct answer is D.
Explanation:
A) The code compiles and prints:
1 2 3
1.1 2.2 3.3
one two three
B) The code compiles and prints:
1 2 3
1.1 2.2 3.3
printList(strings)
line were removed, the code as written does not compile.C) The code does not compile due to an error in the printList
method.
printList
method is correctly defined using an upper bound wildcard <? extends Number>
.D) The code does not compile due to an error in the main
method.
main
method. printList(strings)
causes a compilation error because String
is not a subclass of Number
.E) The code compiles but throws a runtime exception when executed.
4. The correct answer is A.
Explanation:
[A, B, E, C, D]
add
method with an index parameter inserts the specified element at the specified position in the list. All elements after the specified position are shifted to the right. Hence, "E"
is inserted at index 2, pushing "C"
and "D"
to the right.[A, E, B, C, D]
"E"
were added at index 1, not index 2.[A, B, C, E, D]
"E"
were added at index 3, not index 2.[A, B, C, D, E]
"E"
were added at the end of the list, not at index 2.[A, C, B, E, D]
add
method with index 2. It seems like a random shuffle and doesn’t correspond to how elements are shifted when a new element is added.5. The correct answers are C and D..
Explanation:
Set
allows duplicate elements.
Set
is that it does not allow duplicate elements. Each element must be unique.Set
are maintained in the order they were inserted.
Set
interface. For example, HashSet
does not maintain any order, while LinkedHashSet
maintains insertion order, and TreeSet
maintains a sorted order.Set
interface includes methods for adding, removing, and checking the presence of elements.
Set
interface provides methods such as add()
, remove()
, and contains()
to manage its elements.Set
interface is implemented by classes like HashSet
, LinkedHashSet
, and TreeSet
.
HashSet
, LinkedHashSet
, and TreeSet
are all concrete implementations of the Set
interface, each with different characteristics regarding order and performance.Set
guarantees constant-time performance for the basic operations (add, remove, contains).
HashSet
specifically, which provides average constant-time performance for these operations. However, it is not true for all Set
implementations. For example, TreeSet
provides logarithmic time performance for these operations because it is based on a Red-Black tree.6. The correct answer is C.
Explanation:
[A, B, C, D]
addFirst
and addLast
methods.[C, B, A, D]
"A"
is added after "B"
, howerver, addFirst("A")
puts "A"
at the second position.[C, A, B, D]
addFirst("C")
puts “C” at the front, addFirst("A")
puts "A"
at the second position, addLast("B")
adds "B"
after "A"
, and addLast("D")
adds "D"
at the end. Thus, the final order is [C, A, B, D]
.[D, B, A, C]
[A, C, B, D]
"A"
is added before "C"
despite addFirst("C")
being called after addFirst("A")
.7. The correct answer is D.
Explanation:
{1=A, 2=B, 3=C, 2=D}
Map
. A key can only have one value associated with it at a time.{1=A, 2=B, 3=C}
2
is updated from "B"
to "D"
.{1=A, 2=D, 3=C, 2=D}
{1=A, 2=D, 3=C}
put
method updates the value associated with a key if the key already exists in the map. Therefore, the value associated with key 2
is updated from "B"
to "D"
.{1=A, 3=C, 2=B}
2
from "B"
to "D"
.8. The correct answer is C.
Explanation:
Alice 30
Bob 25
Charlie 35
Charlie 35
Alice 30
Bob 25
compareTo
method sorts in ascending order of age.Bob 25
Alice 30
Charlie 35
compareTo
method sorts the Person
objects in ascending order based on their age. Hence, the sorted order is Bob (25)
, Alice (30)
, and Charlie (35)
.Bob 25
Charlie 35
Alice 30
Alice 30
Charlie 35
Bob 25
9 .The correct answer is A.
Explanation:
Bob 25
Alice 30
Charlie 35
AgeComparator
sorts the Person
objects in ascending order based on their age. Hence, the sorted order is Bob (25)
, Alice (30)
, and Charlie (35)
.Charlie 35
Alice 30
Bob 25
AgeComparator
sorts in ascending order of age.Alice 30
Bob 25
Charlie 35
Bob 25
Charlie 35
Alice 30
Alice 30
Charlie 35
Bob 25
Do you like what you read? Would you consider?
Do you have a problem or something to say?