← Quiz: HashMap and Maps: all 10 questions
Quiz: HashMap and Maps · Question 6 of 10
What does this print? (code)
What does this print?
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<String, Integer> m = new LinkedHashMap<>();
m.put("pune", 3);
m.put("agra", 1);
m.put("delhi", 2);
m.put("pune", 9);
System.out.println(m.keySet());
}
}
- A.[agra, delhi, pune]
- B.[pune, agra, delhi]
- C.[agra, delhi, pune, pune]
- D.[delhi, agra, pune]
Answer & explanation
Correct answer: [pune, agra, delhi]
LinkedHashMap keeps the order in which keys were first inserted. Putting "pune" again only replaces its value; it does not move the key.
Can you get the rest right without peeking?
Take the timed Quiz: HashMap and Maps quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →