Java Method Overloading with Double and Float Arguments
Short answer
D The program outputs D A D because decimal literals are doubles by default, bypassing float overloads.
Question

Key Takeaways
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.
Common mistake: 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.
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?
When does the varargs method get called?
Community Votes
100% of anonymous learners picked answer B. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 5 contributions
Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.
From public community threads 5
-
james2033 👍 4 Answered Bpackage 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
-
SrinivasJasti 👍 1 Answered B15.25 and 10.25 are considered double 15.25f and 10.25f will only be considered as float
-
Tojose 👍 5the right answer is B. D A D
-
manjulata 👍 1correct answer is DAD
-
xplorerpj 👍 1 Answered BB is correct answer
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.