Saturday, August 10, 2024

HashMap MCQs

 HashMap MCQs

Which of the following is a characteristic of a HashMap in Java?


A. It allows duplicate keys.

B. It allows null keys and values.

C. It maintains insertion order.

D. It is synchronized.

What method is used to add a key-value pair to a HashMap?


A. put(key, value)

B. add(key, value)

C. insert(key, value)

D. append(key, value)

What does the get() method return if the specified key is not found in the HashMap?


A. Throws a NullPointerException

B. Returns null

C. Returns a default value

D. Throws a NoSuchElementException

How can you check if a HashMap contains a specific key?


A. containsKey(key)

B. hasKey(key)

C. keyExists(key)

D. findKey(key)

What will be the output of the following code?


java

Copy code

HashMap<String, Integer> map = new HashMap<>();

map.put("A", 1);

map.put("B", 2);

map.put("A", 3);

System.out.println(map.get("A"));

A. 1

B. 2

C. 3

D. null

What method is used to remove a key-value pair from a HashMap?


A. delete(key)

B. remove(key)

C. erase(key)

D. discard(key)

Which method returns the set of all keys contained in a HashMap?


A. keys()

B. keySet()

C. getKeys()

D. allKeys()

What will be the output of the following code?


java

Copy code

HashMap<String, Integer> map = new HashMap<>();

map.put("X", 10);

map.put("Y", 20);

map.put("Z", 30);

System.out.println(map.size());

A. 0

B. 1

C. 2

D. 3

What is the time complexity for accessing an element by key in a HashMap?


A. O(1)

B. O(n)

C. O(log n)

D. O(n^2)

How do you retrieve a set of all key-value pairs in a HashMap?


A. entrySet()

B. pairSet()

C. getPairs()

D. valuesSet()

What will be the result of map.containsValue(10) if the value 10 is present in the HashMap?


A. true

B. false

C. null

D. 0

What method is used to replace a value associated with a specific key in a HashMap?


A. replace(key, newValue)

B. change(key, newValue)

C. update(key, newValue)

D. modify(key, newValue)

Which of the following statements about HashMap is true?


A. HashMap maintains the order of insertion.

B. HashMap allows multiple null keys.

C. HashMap is not synchronized.

D. HashMap does not allow null values.

What will map.remove("Key") return if the key "Key" is not present in the HashMap?


A. null

B. false

C. 0

D. Throws an exception

How can you check if a HashMap is empty?


A. isEmpty()

B. empty()

C. isNull()

D. hasNoElements()

Which method returns a collection view of the values contained in a HashMap?


A. values()

B. getValues()

C. allValues()

D. valueSet()

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


A. 0.5

B. 0.75

C. 0.85

D. 1.0

What is the purpose of the computeIfAbsent method in HashMap?


A. To compute a value and add it to the map if the key is absent.

B. To compute a value if the key is present.

C. To remove a key-value pair if the key is absent.

D. To compute a new key for an absent value.

Which method can be used to create a shallow copy of a HashMap?


A. clone()

B. copy()

C. duplicate()

D. newInstance()

What will be the output of the following code?


java

Copy code

HashMap<String, String> map = new HashMap<>();

map.put("K1", "V1");

map.put("K2", "V2");

map.put("K1", "V3");

System.out.println(map.get("K1"));

A. V1

B. V2

C. V3

D. null

Answers:

B. It allows null keys and values.

A. put(key, value)

B. Returns null

A. containsKey(key)

C. 3

B. remove(key)

B. keySet()

D. 3

A. O(1)

A. entrySet()

A. true

A. replace(key, newValue)

C. HashMap is not synchronized.

A. null

A. isEmpty()

A. values()

B. 0.75

A. To compute a value and add it to the map if the key is absent.

A. clone()

C. V3

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...