ProgrammingModerateAmazon/Microsoft SDE MCQ · Java placement

Which line fails to compile?

Which line fails to compile?
byte b = 10;
b += 5;          // line 1
b++;             // line 2
b = b + 1;       // line 3
b = 10 + 20;     // line 4
  1. A. Line 1
  2. B. Line 3
  3. C. Line 4
  4. D. Lines 1 and 3

Correct answer: B. Line 3

b + 1 is an int expression and needs an explicit cast to go back into a byte. Compound assignment and ++ include an implicit cast, and 10 + 20 is a constant 30 that fits in a byte, so it narrows automatically.

More OOP with C++ and Java questions

Exams that ask this topic