🎙 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 1 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 HashMap<>();
        m.put("x", 1);
        m.put("x", 2);
        System.out.println(m.size() + " " + m.get("x"));
    }
}
  1. A.2 1
  2. B.1 2
  3. C.2 2
  4. D.1 1
Answer & explanation

Correct answer: 1 2

The second put replaces the value for the same 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 →