What is the initial capacity of the HashMap when no size is defined, and how is it related to the number of entries it can ideally accommodate without resizing?
- 16, and it can accommodate 16 entries.
- 10, and it can accommodate 10 entries.
- 32, and it can accommodate 32 entries.
- The initial capacity is not defined, and it dynamically adjusts as entries are added.
In Java, when you create a HashMap without specifying an initial capacity, it defaults to an initial capacity of 16. This means that initially, the HashMap can accommodate 16 key-value pairs. However, as the number of entries increases and reaches a certain threshold (usually 75% of the capacity), the HashMap automatically resizes itself, doubling its capacity. So, option (a) is correct, with the explanation that it can ideally accommodate 16 entries initially but will resize when necessary.
Loading...
Related Quiz
- In a large-scale application that reads data from external files, how would you design an exception-handling mechanism to not only log the issues for the developers but also to provide friendly feedback to the end-users, ensuring that the system does not crash upon encountering an exception?
- What is the worst-case time complexity of Linear Search?
- Imagine a scenario where a project utilizes several classes extending a single superclass. If a method in the superclass is modified, how might this impact the subclasses, and what precautions should be taken?
- When an array element, such as arr[2][3], is accessed, Java uses ________ to locate it in memory.
- If we have a 2D array int[][] arr, the expression arr[i] refers to the __________.