nerdexam
Oracle

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…

Working with Methods and Encapsulation

Question

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) { System.out.println("double sum is " + (x + y)); } public static void doSum(float x, float y) { System.out.println("float sum is " + (x + y)); } public static void doSum(int x, int y) { System.out.println("int sum is " + (x + y)); } public static void main(String[] args) { doSum(10, 20); doSum(10.0, 20.0); } } What is the result?

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)
  • A
    11% (2)
  • B
    78% (14)
  • C
    6% (1)
  • D
    6% (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 float would require f-suffixed literals (e.g., 10.0f); plain 10.0 is double, not float.
  • 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" (not 30), since Java's Double.toString(30.0) includes the decimal. B is still the intended correct answer because it correctly identifies which overload is called; the omission of .0 is an error in the question.

Topics

#method overloading#type matching#primitive types#string concatenation

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice