🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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());
    }
}
  1. A.[agra, delhi, pune]
  2. B.[pune, agra, delhi]
  3. C.[agra, delhi, pune, pune]
  4. 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 →