Java LinkedHashSet MCQ Questions and Answers
Q. What is the default sorting order of keys in a TreeMap?
A. Ascending orderB. Descending order
C. No specific order
D. Depends on the data type of keys
Q. Which interface does TreeMap implement?
A. MapB. SortedMap
C. NavigableMap
D. All of the above
Q. What is the output of the following code snippet?
TreeMap<String, Integer> map = new TreeMap<>();A. A
map.put("C", 3);
map.put("A", 1);
map.put("B", 2);
System.out.println(map.firstKey());
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() methodB. 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<>();A. 2
map.put(3, "C");
map.put(1, "A");
map.put(2, "B");
System.out.println(map.higherKey(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<>();A. A
map.put(1, "A");
map.put(2, "B");
map.put(3, "C");
map.remove(2);
System.out.println(map.get(2));
B. B
C. C
D. null
Q. What is the output of the following code snippet?
TreeMap<Integer, String> map = new TreeMap<>();A. (1, A)
map.put(3, "C");
map.put(1, "A");
map.put(2, "B");
System.out.println(map.pollFirstEntry());
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<>();A. 1
map.put(1, "A");
map.put(2, "B");
map.put(3, "C");
map.descendingMap();
System.out.println(map.firstKey());
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<>();A. 1
map.put(1, "A");
map.put(2, "B");
map.put(3, "C");
map.put(4, "D");
System.out.println(map.subMap(2, 4).firstKey());
B. 2
C. 3
D. 4
Q. What is the output of the following code snippet?
TreeMap<Integer, String> map = new TreeMap<>();A. 1
map.put(1, "A");
map.put(2, "B");
map.put(3, "C");
map.put(4, "D");
System.out.println(map.headMap(3).lastKey());
B. 2
C. 3
D. None of the above