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

Q. What does `getOrDefault` return when the key is missing?

A. null
B. The provided default value
C. Throws NullPointerException
D. The first entry in the map

Q. Which iteration order does HashMap guarantee?

A. Insertion order
B. Natural key order
C. No guaranteed order
D. Reverse insertion order

Q. How do you create a thread-safe HashMap alternative?

A. Collections.synchronizedMap(new HashMap<>())
B. new Hashtable<>()
C. new ConcurrentHashMap<>()
D. All of the above

Q. What happens when two keys have the same hashCode but are not equal?

A. One replaces the other
B. They go to the same bucket with equals used to distinguish
C. Map throws an exception
D. Map discards both entries

Q. Which method copies mappings from another Map?

A. put()
B. putAll()
C. merge()
D. clone()

Q. What is the default value returned by `remove(key)` when key is absent?

A. false
B. null
C. 0
D. Empty Optional

Q. Which Java version introduced tree bins to improve worst-case lookups?

A. Java 6
B. Java 7
C. Java 8
D. Java 11

Q. What does `clear()` do on a HashMap?

A. Removes all entries
B. Shrinks capacity to zero
C. Removes half the entries
D. Throws UnsupportedOperationException

Q. Which method checks for both key and expected value before removing?

A. remove(key)
B. remove(key, value)
C. compute()
D. replaceAll()

Q. What is the result of `putIfAbsent` when the key already exists?

A. Replaces the value
B. Leaves map unchanged and returns existing value
C. Removes the key
D. Throws IllegalStateException

Q. Which interface do HashMap keys and values generically implement?

A. Serializable
B. Cloneable
C. No interface is required; any Object works
D. Comparable

Q. What does `size()` return?

A. Current capacity
B. Number of entries
C. Load factor
D. Hash bucket count

Q. Which method replaces a value only if it is currently mapped?

A. replace(key, value)
B. put(key, value)
C. merge(key, value, fn)
D. computeIfAbsent(key, fn)

Q. How do you iterate over both keys and values at once?

A. for (String k : map.keySet())
B. for (Map.Entry e : map.entrySet())
C. map.iterator()
D. map.stream()

Q. Which call returns a Set of all keys?

A. map.values()
B. map.entrySet()
C. map.keySet()
D. map.keys()

Q. What does `containsValue` check?

A. Presence of a specific key
B. Presence of a specific value
C. Both key and value
D. Only null values

Q. What is returned by `clone()` on a HashMap?

A. Deep copy of keys/values
B. Shallow copy of the map structure
C. Empty map
D. Immutable copy

Q. Which statement about nulls is true for HashMap?

A. Allows one null key and multiple null values
B. Forbids null keys but allows null values
C. Forbids all nulls
D. Allows unlimited null keys

Q. Which operation can trigger a resize?

A. Adding entries beyond threshold
B. Calling size()
C. Calling keySet()
D. Calling containsKey()

Q. Which statement is true about fail-fast iterators of HashMap?

A. They never throw exceptions
B. They throw ConcurrentModificationException on structural change
C. They automatically synchronize modifications
D. They reorder entries to avoid conflicts

Popular Posts