Question 9
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
public class Main {
static String show(String s) { return "String"; }
static String show(Integer i) { return "Integer"; }
public static void main(String[] args) {
System.out.println(show(null));
}
}
- A null cannot be passed to methods
- B null fits both String and Integer and neither is more specific, so the call is ambiguous; cast it, e.g. show((String) null) Correct
- C show must not be static
- D Overloads need different names
Correct Answer: Option B
With show(Object) and show(String), show(null) would choose String, the more specific type.
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!