Java Intermediate

0% completed

Previous
Next
Quiz
Question 1
Consider the following code snippet:
import java.util.HashSet;

public class Test {
    public static void main(String[] args) {
        HashSet<Integer> set = new HashSet<>();
        set.add(10);
        set.add(20);
        set.add(10);
        System.out.println(set.size());
    }
}
What is the output?
A
1
B
2
C
3
D
0
Question 2
Which method removes all elements from a Set?
A
clear()
B
deleteAll()
C
removeAll()
D
purge()
Question 3
Consider the following code snippet that uses a LinkedHashSet:
import java.util.LinkedHashSet;

public class Test {
    public static void main(String[] args) {
        LinkedHashSet<String> set = new LinkedHashSet<>();
        set.add("One");
        set.add("Two");
        set.add("Three");
        System.out.println(set);
    }
}
What is guaranteed about the output order?
A
The output is sorted in natural order.
B
The output order is random.
C
The output order is the same as the insertion order.
D
The output order is reversed compared to insertion order.
Examine the following code snippet for a TreeSet with natural ordering:
import java.util.TreeSet;

public class Test {
    public static void main(String[] args) {
        TreeSet<String> fruits = new TreeSet<>();
        fruits.add("Banana");
        fruits.add("Apple");
        fruits.add("Cherry");
        System.out.println(fruits);
    }
}
What is the output?
A
[Banana, Apple, Cherry]
B
[Apple, Banana, Cherry]
C
[Cherry, Banana, Apple]
D
The output order is undefined.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next