Search Tutorials


Java TreeMap MCQ Questions and Answers | JavaInUse

Java LinkedHashSet MCQ Questions and Answers

Q. What is the default sorting order of keys in a TreeMap?

A. Ascending order
B. Descending order
C. No specific order
D. Depends on the data type of keys

Q. Which interface does TreeMap implement?

A. Map
B. SortedMap
C. NavigableMap
D. All of the above

Q. What is the output of the following code snippet?

TreeMap<String, Integer> map = new TreeMap<>();
map.put("C", 3);
map.put("A", 1);
map.put("B", 2);
System.out.println(map.firstKey());
A. A
B. B
C. C
D. None of the above

Q. How can you iterate over the keys in a TreeMap in ascending order?

A. Using an iterator obtained from the keySet() method
B. Using a for-each loop directly on the TreeMap
C. Using the values() method and iterating over the values
D. None of the above

Q. What is the output of the following code snippet?

TreeMap<Integer, String> map = new TreeMap<>();
map.put(3, "C");
map.put(1, "A");
map.put(2, "B");
System.out.println(map.higherKey(2));
A. 2
B. 3
C. null
D. None of the above

Q. What is the output of the following code snippet?

TreeMap<Integer, String> map = new TreeMap<>();
map.put(1, "A");
map.put(2, "B");
map.put(3, "C");
map.remove(2);
System.out.println(map.get(2));
A. A
B. B
C. C
D. null

Q. What is the output of the following code snippet?

TreeMap<Integer, String> map = new TreeMap<>();
map.put(3, "C");
map.put(1, "A");
map.put(2, "B");
System.out.println(map.pollFirstEntry());
A. (1, A)
B. (2, B)
C. (3, C)
D. None of the above

Q. What is the output of the following code snippet?

TreeMap<Integer, String> map = new TreeMap<>();
map.put(1, "A");
map.put(2, "B");
map.put(3, "C");
map.descendingMap();
System.out.println(map.firstKey());
A. 1
B. 3
C. null
D. None of the above

Q. What is the output of the following code snippet?

TreeMap<Integer, String> map = new TreeMap<>();
map.put(1, "A");
map.put(2, "B");
map.put(3, "C");
map.put(4, "D");
System.out.println(map.subMap(2, 4).firstKey());
A. 1
B. 2
C. 3
D. 4

Q. What is the output of the following code snippet?

TreeMap<Integer, String> map = new TreeMap<>();
map.put(1, "A");
map.put(2, "B");
map.put(3, "C");
map.put(4, "D");
System.out.println(map.headMap(3).lastKey());
A. 1
B. 2
C. 3
D. None of the above





Q. What is the output of the following code snippet?

TreeMap<Integer, String> map = new TreeMap<>();
map.put(1, "A");
map.put(2, "B");
map.put(3, "C");
map.put(4, "D");
System.out.println(map.tailMap(2).firstKey());
A. 1
B. 2
C. 3
D. 4

Q. What is the output of the following code snippet?

TreeMap<Integer, String> map = new TreeMap<>();
map.put(1, "A");
map.put(2, "B");
map.put(3, "C");
map.put(4, "D");
System.out.println(map.containsKey(2));
A. true
B. false
C. null
D. An error occurs

Q. What is the output of the following code snippet?

TreeMap<Integer, String> map = new TreeMap<>();
map.put(1, "A");
map.put(2, "B");
map.put(3, "C");
map.put(4, "D");
System.out.println(map.containsValue("B"));
A. true
B. false
C. null
D. An error occurs

Q. What will be the output of the following code?

TreeMap<String, Integer> treeMap = new TreeMap<>();
treeMap.put("Apple", 5);
treeMap.put("Banana", 3);
treeMap.put("Cherry", 8);
treeMap.put("Apple", 10);

System.out.println(treeMap.get("Apple"));
A.
5
B.
10
C.
null
D.
Exception: Duplicate key "Apple"

Q. How can you iterate through the keys and values of a TreeMap in ascending order?

A.
for (Map.Entry<String, Integer> entry : treeMap.entrySet()) {
    String key = entry.getKey();
    int value = entry.getValue();
    // Process key and value
}
B.
for (String key : treeMap.keySet()) {
    int value = treeMap.get(key);
    // Process key and value
}
C.
for (int value : treeMap.values()) {
    String key = treeMap.getKey(value);
    // Process key and value
}
D.
Iterator<Map.Entry<String, Integer>> iterator = treeMap.entrySet().iterator();
while (iterator.hasNext()) {
    Map.Entry<String, Integer> entry = iterator.next();
    String key = entry.getKey();
    int value = entry.getValue();
    // Process key and value
}

Q. What will be the output of the following code?

TreeMap<String, Integer> treeMap = new TreeMap<>();
treeMap.put("Apple", 5);
treeMap.put("Banana", 3);
treeMap.put("Cherry", 8);

System.out.println(treeMap.firstKey());
System.out.println(treeMap.lastKey());
A.
Apple
Cherry
B.
Banana
Cherry
C.
Apple
Banana
D.
Banana
Apple

Q. What will be the output of the following code?

TreeMap<String, Integer> treeMap = new TreeMap<>();
treeMap.put("Apple", 5);
treeMap.put("Banana", 3);
treeMap.put("Cherry", 8);

treeMap.remove("Banana");

System.out.println(treeMap);
A.
{Apple=5, Cherry=8}
B.
{Apple=5}
C.
{Banana=3, Cherry=8}
D.
{}

Q. What will be the output of the following code?

TreeMap<String, Integer> treeMap = new TreeMap<>();
treeMap.put("Apple", 5);
treeMap.put("Banana", 3);
treeMap.put("Cherry", 8);

System.out.println(treeMap.lowerKey("Banana"));
A.
Apple
B.
Banana
C.
Cherry
D.
null

Q. What will be the output of the following code?

TreeMap<String, Integer> treeMap = new TreeMap<>();
treeMap.put("Apple", 5);
treeMap.put("Banana", 3);
treeMap.put("Cherry", 8);

System.out.println(treeMap.higherKey("Banana"));
A.
Apple
B.
Banana
C.
Cherry
D.
null

Q. What will be the output of the following code?

TreeMap<String, Integer> treeMap = new TreeMap<>();
treeMap.put("Apple", 5);
treeMap.put("Banana", 3);
treeMap.put("Cherry", 8);

System.out.println(treeMap.subMap("Apple", "Cherry"));
A.
{Apple=5, Banana=3}
B.
{Apple=5, Banana=3, Cherry=8}
C.
{Banana=3, Cherry=8}
D.
{Apple=5, Cherry=8}

Q. What will be the output of the following code?

TreeMap<String, Integer> treeMap = new TreeMap<>();
treeMap.put("Apple", 5);
treeMap.put("Banana", 3);
treeMap.put("Cherry", 8);

System.out.println(treeMap.headMap("Banana"));
A.
{Apple=5}
B.
{Apple=5, Banana=3}
C.
{Banana=3}
D.
{Apple=5, Banana=3, Cherry=8}

Q. What will be the output of the following code?

TreeMap<String, Integer> treeMap = new TreeMap<>();
treeMap.put("Apple", 5);
treeMap.put("Banana", 3);
treeMap.put("Cherry", 8);

System.out.println(treeMap.tailMap("Banana"));
A.
{Banana=3, Cherry=8}
B.
{Apple=5, Banana=3}
C.
{Apple=5, Banana=3, Cherry=8}
D.
{Banana=3}

Q. What will be the output of the following code?

TreeMap<String, Integer> treeMap = new TreeMap<>();
treeMap.put("Apple", 5);
treeMap.put("Banana", 3);
treeMap.put("Cherry", 8);

System.out.println(treeMap.containsKey("Mango"));
A.
true
B.
false
C.
Exception: Key not found
D.
null