Java Method Overloading with Double and Float Arguments
Given: What is the result? - 
Community Votes
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)
Comments & Corrections
No comments yet — spotted an error or have a note? Share it below.
Expert Analysis
Why the Answer Is Correct
The correct output sequence is D A D. In the first callt.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 explicitf 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.