Question discussion

What is the output of the following Java program?import java.util.*; public class Main { public static void main(String[] args) { Map m = n…

1149Medium DSSSB TGT Computer Science 0 replies

What is the output of the following Java program?

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Map<String, Integer> m = new HashMap<>();
        m.put("a", 1); m.put("b", 2); m.put("a", 3);
        System.out.println(m.size() + " " + m.get("a"));
    }
}
  • A3 1
  • B3 3
  • C2 1
  • D2 3
Correct answer: D

Explanation

put("a", 3) replaces the earlier value for the same key, so the map has 2 entries and a → 3.

Discussion 0

Nobody has replied yet. Start the discussion!