Java OCA OCP Practice Question 1984

Question

Which of the labeled lines in the following code can be uncommented by removing the // characters and still allow the code to compile correctly?

public class Main {
//int width = 14;              /* Line A */
  {/*from   w  w  w.j  av a 2 s .c o m*/
//  area = width * height;    /* Line B */
  }
  int width = 37;
  {
//  height = 11;               /* Line C */
  }
  int height, area;
//area = width * height;       /* Line D */
  {
//  int width = 15;            /* Line E */
    area = 100;
  }
};

Select the two correct answers.

  • (a) Line A
  • (b) Line B
  • (c) Line C
  • (d) Line D
  • (e) Line E


(c) and (e)

Note

Line A will cause illegal redefinition of the field width.

Line B uses an illegal forward reference to the fields width and height.

The assignment in line C is legal.

Line D is not a legal initializer, since it is neither a declaration nor a block.

Line E declares a local variable inside an initializer block, which is legal.




PreviousNext

Related