Java OCA OCP Practice Question 665

Question

Consider the following program :

class Main{ 
  public static void main (String [] args){ 
    short s = 10;   // 1 
    char c = s;     // 2 
    s = c;          // 3 
   } 
} 

Identify the correct statements.

Select 2 options

  • A. Line 3 is not valid.
  • B. Line 2 is not valid.
  • C. It will compile because both short and char can hold 10.
  • D. None of the lines 1, 2 and 3 is valid.


Correct Options are  : A B

Note

Not all short values are valid char values, and neither are all char values valid short values, therefore compiler complains for both the lines 2 and 3.

They will require an explicit cast.




PreviousNext

Related