What is the result?

Given:

import java.util.Map;

import java.util.Set;

import java.util.TreeMap;

public class MapClass {

public static void main(String[] args) {

Map <String, String> partList = new TreeMap<>();

partList.put(“P002”, “Large Widget”);

partList.put(“P001”, “Widget”);

partList.put(“P002”, “X-Large Widget”);

Set<String> keys = partList.keySet();

for (String key:keys) {

System.out.println(key + ” ” + partList.get(key));

}

}

}

What is the result?

Given:

import java.util.Map;

import java.util.Set;

import java.util.TreeMap;

public class MapClass {

public static void main(String[] args) {

Map <String, String> partList = new TreeMap<>();

partList.put(“P002”, “Large Widget”);

partList.put(“P001”, “Widget”);

partList.put(“P002”, “X-Large Widget”);

Set<String> keys = partList.keySet();

for (String key:keys) {

System.out.println(key + ” ” + partList.get(key));

}

}

}

What is the result?

A.
p001 Widget
p002 X-Large Widget

B.
p002 Large Widget
p001 Widget

C.
p002 X-large Widget
p001 Widget

D.
p001 Widget
p002 Large Widget

E.
compilation fails

Explanation:
Compiles fine. Output is:
P001 Widget
P002 X-Large Widget

Line:partList.put(“P002”, “X-Large Widget”);
overwrites line:partList.put(“P002”, “Large Widget”);



Leave a Reply 1

Your email address will not be published. Required fields are marked *