1Z0-808 · Question #10
Given: public class SumTest { public static void doSum(Integer x, Integer y) { System.out.println("Integer sum is " + (x + y)); } public static void doSum(double x, double y) {…
The correct answer is B. int sum is 30 double sum is 30. Option B is correct because Java integer literals (10, 20) are int by default, so doSum(10, 20) resolves to doSum(int, int) - not doSum(Integer, Integer), since Java prefers an exact primitive match over autoboxing. Similarly, floating-point literals (10.0, 20.0) are double by…
Question
Options
- Aint sum is 30 float sum is 30.0
- Bint sum is 30 double sum is 30
- CInteger sum is 30 double sum is 30.0
- DInteger sum is 30 float sum is 30.0
How the community answered
(18 responses)- A11% (2)
- B78% (14)
- C6% (1)
- D6% (1)
Explanation
Option B is correct because Java integer literals (10, 20) are int by default, so doSum(10, 20) resolves to doSum(int, int) - not doSum(Integer, Integer), since Java prefers an exact primitive match over autoboxing. Similarly, floating-point literals (10.0, 20.0) are double by default unless suffixed with f/F, so doSum(10.0, 20.0) resolves to doSum(double, double).
Why the distractors fail:
- A is wrong because
floatwould requiref-suffixed literals (e.g.,10.0f); plain10.0isdouble, notfloat. - C and D are wrong because they show
Integer sum is 30, implying autoboxing was chosen - but Java's overload resolution always prefers an exact primitive match (int) over boxing to a wrapper type (Integer).
Memory tip: Think "primitives before wrappers, doubles before floats." Java picks the closest primitive type first - int beats Integer, and an unsuffixed decimal literal is always a double, not a float. If the f isn't there, it's a double.
Note: Option B appears to have a typo - the actual output of
doSum(double, double)would print"double sum is 30.0"(not30), since Java'sDouble.toString(30.0)includes the decimal. B is still the intended correct answer because it correctly identifies which overload is called; the omission of.0is an error in the question.
Topics
Community Discussion
No community discussion yet for this question.