Question 5
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> age = new HashMap<>();
age.put("Asha", 21);
String a = age.get("Asha");
System.out.println(a);
}
}
- A get needs a default
- B get returns an Integer (the value type), which cannot be assigned to a String; use Integer a or String.valueOf(...) Correct
- C Asha must be a key object
- D age must be a TreeMap
Correct Answer: Option B
javac: "incompatible types: Integer cannot be converted to String".
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!