nerdexam
Oracle

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…

Data Types and Operators

Question

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); } } Which statement, when inserted at line n1, enables the Course class to compile?

Options

  • Aint newFee;
  • Bdouble newFee;
  • Clong newFee;
  • Dfloat newFee;

How the community answered

(40 responses)
  • B
    93% (37)
  • C
    3% (1)
  • D
    5% (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 than double; Java won't silently shrink a double into a float, 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

#Type Promotion#Arithmetic Operations#Type Compatibility#Primitive Types

Community Discussion

No community discussion yet for this question.

Full 1Z0-811 Practice