1Z0-811 · Question #35
Given: public class Course { public static void main (String [] args) { double courseFee = 1000.0; float percentage = 5.0f; // line n1 newFee = courseFee * percentage; System.out.println (newFee); }…
The correct answer is B. double newFee. Option B is correct because multiplying a double by a float in Java produces a double result - the float is automatically promoted to double during the operation, and only a double variable can hold that result without an explicit cast. Why the distractors fail: A (int) and C…
Question
Options
- Aint newFee;
- Bdouble newFee;
- Clong newFee;
- Dfloat newFee;
How the community answered
(40 responses)- B93% (37)
- C3% (1)
- D5% (2)
Explanation
Option B is correct because multiplying a double by a float in Java produces a double result - the float is automatically promoted to double during the operation, and only a double variable can hold that result without an explicit cast.
Why the distractors fail:
- A (
int) and C (long) are integer types; assigning a floating-point result to them requires an explicit cast, so the compiler rejects the implicit narrowing conversion. - D (
float) is a narrower floating-point type thandouble; Java won't silently shrink adoubleinto afloat, so this also causes a compile error.
Memory tip: Think of Java's numeric promotion as a "widening ladder" - int → long → float → double. Any arithmetic involving a double climbs to the top, and the result must be stored in a type at that level or higher. If the receiver type sits below the result type on the ladder, you need an explicit cast or the code won't compile.
Topics
Community Discussion
No community discussion yet for this question.