Java Method Overloading with Double and Float Arguments

Answer Correct answer: D — The program outputs D A D because decimal literals are doubles by default, bypassing float overloads.

Given: What is the result? - image

  1. B A D
  2. D A D
  3. B A C
  4. D D D Correct Answer

Community Votes

B
100%

100% of anonymous learners picked answer B. Votes are pick records left by other test-takers — they are not the verified answer.

Community Insight

The core trap is recognizing that integer literals combined with decimal literals are promoted to double by default, preventing a match with the float parameter unless explicitly suffixed.

This Java 17 exam question tests the method overloading resolution rules for numeric primitives, specifically how the compiler chooses between float and varargs double when given literal arguments.

Many candidates select B (B A C) because they assume the decimals are treated as floats or overlook that int-to-float promotion is not allowed without an explicit cast, forcing a fall-through to varargs.

Community Discussion (5 comments)

Tojose 👍 5
the right answer is B. D A D
SrinivasJasti 👍 1 Selected: B
15.25 and 10.25 are considered double 15.25f and 10.25f will only be considered as float
manjulata 👍 1
correct answer is DAD
xplorerpj 👍 1 Selected: B
B is correct answer
james2033 👍 4 Selected: B
package q34; public class Test { public void sum(int a, int b) { System.out.print(" A"); } public void sum(int a, float b) { System.out.print(" B"); } public void sum(float a, float b) { System.out.print(" C"); } public void sum(double... a) { System.out.print(" D"); } public static void main(String[] args) { Test t = new Test(); t.sum(10, 15.25); t.sum(10, 24); t.sum(10.25, 10.25); } } // Result: // D A D

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

Expert Analysis

Why the Answer Is Correct

The correct output sequence is D A D. In the first call t.sum(10, 15.25), the argument 10 is an int and 15.25 is a double literal. Java does not allow implicit narrowing conversion from double to float, so the sum(int, float) method is not applicable. The only matching method is the varargs sum(double...), which prints "D". In the second call t.sum(10, 24), both arguments are ints. This perfectly matches the sum(int, int) signature (named sum in the code but taking two ints), printing "A". In the third call t.sum(10.25, 10.25), both are double literals. This matches the sum(double, double) varargs method, printing "D".

Why the Other Options Are Wrong

Option A (B A D) and C (B A C) incorrectly assume that the decimal values are treated as floats. Since no explicit f suffix is used, Java treats them as doubles. Option B (D D D) fails to recognize that two integer arguments can match the specific int, int overload rather than falling through to the varargs version.

Community Comment Notes

While the majority voted for B, several comments correctly identified the answer as DAD. User SrinivasJasti noted that '15.25 and 10.25 are considered double', which is the key insight. Users Tojose and manjulata also confirmed 'the right answer is B. D A D' and 'correct answer is DAD'.

Exam Strategy

Always check if your literal arguments require explicit casting to fit narrower types like float. If they don't fit exactly, look for varargs methods as the fallback target for overloading resolution.

Frequently Asked Questions

Why doesn't sum(int, float) match 10 and 15.25?

Java does not implicitly narrow a double literal (15.25) to a float. It must be explicitly cast or suffixed with 'f'.

When does the varargs method get called?

It is called when no other more specific method signature matches the provided arguments, such as when all args are doubles.

More 1Z0-829 FAQ →

Related Analysis

← Back to 1Z0-829 Study Guide