Java OCA OCP Practice Question 1744

Question

What is the label of the first line that will cause a compile time error in the following program?.

public class Main {
  public static void main(String[] args) {
    char c;//from w w  w.  j av  a2  s. c o m
    int i;
    c = 'a'; // (1)
    i = c;   // (2)
    i++;     // (3)
    c = i;   // (4)
    c++;     // (5)
  }
}

Select the one correct answer.

  • (a) (1)
  • (b) (2)
  • (c) (3)
  • (d) (4)
  • (e) (5)
  • (f) None of the above. The compiler will not report any errors.


(d)

Note

The types char and int are both integral.

A char value can be assigned to an int variable since the int type is wider than the char type and an implicit widening conversion will be done.

An int type cannot be assigned to a char variable because the char type is narrower than the int type.

The compiler will report an error about a possible loss of precision in (4).




PreviousNext

Related