Java OCA OCP Practice Question 1742

Question

Which of these assignments are valid?.

Select the four correct answers.

  • (a) short s = 12;
  • (b) long l = 012;
  • (c) int other = (int) true;
  • (d) float f = -123;
  • (e) double d = 0x12345678;


(a), (b), (d), and (e)

Note

In (a), the conditions for implicit narrowing conversion are fulfilled: the source is a constant expression of type int, the destination type is of type short, the value of the source (12) is in the range of the destination type.

The assignments in (b), (d), and (e) are valid, since the source type is narrower than the target type and an implicit widening conversion will be applied.

The expression (c) is not valid.

Values of type boolean cannot be converted to other types.




PreviousNext

Related