Search Tutorials


Java HashMap MCQ Questions and Answers | JavaInUse

Java HashMap MCQ Questions and Answers

Q. What is the purpose of the 'equals' method in the HashMap key-value pairs?

A. To compare the values of two keys
B. To determine if two keys are equal
C. To compare the hash codes of two keys
D. To establish the order of keys in the HashMap

Q. What is the default initial capacity of a HashMap in Java?

A. 0
B. 10
C. 16
D. It depends on the JVM

Q. What is the default load factor of a HashMap in Java?

A. 0.25
B. 0.5
C. 0.75
D. 1.0

Q. What is the time complexity of inserting a key-value pair into a HashMap?

A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)

Q. What is the time complexity of retrieving a value from a HashMap using its key?

A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)

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

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

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

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

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

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

Q. What is the purpose of the 'hashCode' method in the HashMap key-value pairs?

A. To compare the values of two keys
B. To determine if two keys are equal
C. To generate a unique identifier for each key
D. To establish the order of keys in the HashMap

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

HashMap<String, Integer> map = new HashMap<>(); 
     map.put("one", 1); 
     map.put("two", 2); 
     map.put("three", 3); 
     System.out.println(map.keySet());
A. [one, two, three]
B. [one, two]
C. [one, three]
D. [two, three]





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

HashMap<String, Integer> map = new HashMap<>();
     map.put("one", 1); 
     map.put("two", 2);
      map.put("three", 3);
       System.out.println(map.values());
A. [1, 2, 3]
B. [1, 2]
C. [1, 3]
D. [2, 3]

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

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

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

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

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

HashMap<String, Integer> map = new HashMap<>();
     map.put("one", 1); 
     map.put("two", 2); 
     map.put("three", 3); 
     System.out.println(map.size());
A. 1
B. 2
C. 3
D. 0

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

HashMap<String, Integer> map = new HashMap<>(); 
    map.put("one", 1); 
    map.put("two", 2); 
    map.put("three", 3); 
    map.clear(); 
    System.out.println(map.size());
A. 0
B. 1
C. 2
D. 3

Q. What is the output of the following code?

HashMap<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);

System.out.println(map.get("two"));
A.
2
B.
null
C.
Exception: Key not found
D.
1

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

HashMap<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("one", 11);

System.out.println(map.get("one"));
A.
1
B.
11
C.
null
D.
Exception: Duplicate key

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

HashMap<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);

map.remove("one");

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

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

HashMap<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);

map.keySet().remove("one");

System.out.println(map.get("one"));
A.
1
B.
null
C.
Exception: Key not found
D.
Exception: Invalid operation

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

HashMap<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);

map.clear();

System.out.println(map.isEmpty());
A.
true
B.
false
C.
null
D.
Exception: Map is not empty

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

HashMap<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);

map.putIfAbsent("one", 11);

System.out.println(map.get("one"));
A.
1
B.
11
C.
null
D.
Exception: Duplicate key

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

HashMap<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);

map.computeIfAbsent("three", k -> 3);

System.out.println(map.get("three"));
A.
3
B.
null
C.
Exception: Key not found
D.
Exception: Invalid key

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

HashMap<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);

map.computeIfPresent("one", (k, v) -> v * 2);

System.out.println(map.get("one"));
A.
1
B.
2
C.
null
D.
Exception: Key not found

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

HashMap<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);

map.merge("one", 11, Integer::sum);

System.out.println(map.get("one"));
A.
12
B.
11
C.
1
D.
Exception: Duplicate key

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

HashMap<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);

map.replaceAll((k, v) -> v * 2);

System.out.println(map.get("one"));
System.out.println(map.get("two"));
A.
2
4
B.
1
2
C.
null
null
D.
Exception: Invalid operation