Java OCA OCP Practice Question 552

Question

Which of the following expressions are legal? (Choose all that apply.)

  • A. String x = "1"; int y = 1; x += y;
  • B. String x = "2"; int y = 2; if (x == y) {}
  • C. String x = "3"; int y = 3; x = x + y;
  • D. String x = "4"; int y = 4; y = y + x;


A, C.

Note

Options A and C are equivalent.

They both add a String to an int.

The resulting String is assigned to x, whose type is String, so the code is legal.

Option B doesn't compile because an int and a String may not be compared.

C is illegal because the last statement tries to assign a String to an int.




PreviousNext

Related