Java Intermediate

0% completed

Previous
Next
Quiz
Question 1
Examine the following code snippet:
import java.util.HashMap;

public class Test {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("Alice", 85);
        map.put("Bob", 90);
        map.put("Alice", 95);
        System.out.println(map.get("Alice"));
    }
}
What is the output?
A
85
B
90
C
95
D
null
Question 2
What is the main difference between a HashMap and a LinkedHashMap?
A
LinkedHashMap maintains insertion order; HashMap does not.
B
HashMap maintains insertion order; LinkedHashMap does not.
C
HashMap is sorted by keys; LinkedHashMap is not.
D
LinkedHashMap does not allow null values; HashMap does.
Question 3
Examine the following code snippet:
import java.util.TreeMap;

public class Test {
    public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(3, "C");
        map.put(1, "A");
        map.put(2, "B");
        System.out.println(map);
    }
}
What is the output?
A
{3=C, 1=A, 2=B}
B
{2=B, 1=A, 3=C}
C
{1=A, 2=B, 3=C}
D
The order is undefined.
Question 4
Examine the following code snippet that demonstrates bulk operations on a HashMap:
import java.util.HashMap;

public class Test {
    public static void main(String[] args) {
        HashMap<String, Integer> map1 = new HashMap<>();
        map1.put("A", 1);
        map1.put("B", 2);
        
        HashMap<String, Integer> map2 = new HashMap<>();
        map2.put("C", 3);
        map2.put("D", 4);
        
        map1.putAll(map2);
        System.out.println(map1);
        map1.clear();
        System.out.println("Map1 after clearing: " + map1);
    }
}
What is the expected behavior of this code?
A
Only map2 will be printed.
B
The maps will be merged, and then map1 will be empty after calling clear().
C
An exception will be thrown during the merge.
D
The maps remain unchanged.

.....

.....

.....

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