Java OCA OCP Practice Question 1582

Question

Which of the lines will cause a compile time error in the following program?

public class MyClass{
   public static void main (String args []){
      char c;/*from w w w  . j  a v  a 2  s . c  om*/
      int i;
      c =  'a';//1
      i = c;  //2
      i++;    //3
      c = i;  //4
      c++;    //5
    }
}

Select 1 option

  • A. line 1
  • B. line 2
  • C. line 3
  • D. line 4
  • E. line 5


Correct Option is  : D

Note

A char value can always be assigned to an int variable, since the int type is wider than the char type. So line 2 is valid.

Line 4 will not compile because it is trying to assign an int to a char.

Although the value of i can be held by the char but since 'i' is not a constant but a variable, implicit narrowing will not occur.




PreviousNext

Related