nerdexam
Oracle

1Z0-808 · Question #14

Given the code fragment: 3. public static void main(String[] args) { 4. int iVar = 100; 5. float fVar = 100.100f; 6. double dVar = 123; 7. iVar = fVar; 8. fVar = iVar; 9. dVar = fVar; 10. fVar =…

The correct answer is A. Line 7 D. Line 10 F. Line 12. Lines 7, 10, and 12 all attempt narrowing conversions without an explicit cast - assigning a larger/wider type into a smaller/narrower one, which Java refuses to compile automatically. Line 7 (iVar = fVar) assigns a float into an int; line 10 (fVar = dVar) assigns a double into…

Working With Java Data Types

Question

Given the code fragment: 3. public static void main(String[] args) { 4. int iVar = 100; 5. float fVar = 100.100f; 6. double dVar = 123; 7. iVar = fVar; 8. fVar = iVar; 9. dVar = fVar; 10. fVar = dVar; 11. iVar = iVar; 12. iVar = dVar; 13. } Which three lines fail to compile?

Options

  • ALine 7
  • BLine 8
  • CLine 9
  • DLine 10
  • ELine 11
  • FLine 12

How the community answered

(41 responses)
  • A
    88% (36)
  • B
    2% (1)
  • C
    7% (3)
  • E
    2% (1)

Explanation

Lines 7, 10, and 12 all attempt narrowing conversions without an explicit cast - assigning a larger/wider type into a smaller/narrower one, which Java refuses to compile automatically. Line 7 (iVar = fVar) assigns a float into an int; line 10 (fVar = dVar) assigns a double into a float; and line 12 (iVar = dVar) assigns a double into an int - all three lose precision or magnitude, so the compiler demands an explicit cast like (int).

The distractors (B, C, E) are all widening conversions, which Java performs implicitly with no cast required: line 8 (fVar = iVar) widens intfloat, line 9 (dVar = fVar) widens floatdouble, and line 11 (iVar = iVar) is a same-type assignment - all perfectly legal.

Memory tip: Think of the type hierarchy as a one-way escalator: int → long → float → double. Going up (widening) is free; going down (narrowing) requires a cast token - if you forget the cast, the compiler stops you cold.

Topics

#Type Conversions#Narrowing Conversions#Primitive Types#Implicit vs Explicit Casting

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice