Java OCA OCP Practice Question 24

Question

Given the following code, which of the following will compile?

enum Food { RICE, NOODLE, CORIANDER, ROSEMARY; } 
  • A. Food sp = Food.RICE; Object ob = sp;
  • B. Food sp = Food.RICE; Object ob = (Object)sp;
  • C. Object ob = new Object(); Food sp = object;
  • D. Object ob = new Object(); Food sp = (Food)object;


A, B, D.

Note

Enum values may be converted to Object, just like other objects.

So A and B are legal, though the cast in B is not necessary.

Assigning an Object reference to an enum requires a cast, so C is illegal, but D is legal.




PreviousNext

Related