Java OCA OCP Practice Question 1978

Question

Given the following class, which instance initializer block inserted at (1) will allow the class to be compiled?.

public class Main {
  static int v = 10;
  double length;
  final boolean active;

  // (1) INSERT CODE HERE
}

Select the one correct answer.

  • (a) instance { active = true; }
  • (b) Main { v += 5; }
  • (c) { v = 5; length = (active ? 100 : 200) + v; }
  • (d) { ; }
  • (e) { length = 4.2; }
  • (f) { active = (v > 5); length = 5.5 + v;}


(f)

Note

This class has a blank final boolean variable active.

This variable must be initialized when an instance is constructed, or else the code will not compile.

The keyword static is used to signify that a block is a static initializer block.

No keyword is used to signify that a block is an instance initializer block.

(a) and (b) are not instance initializers blocks, and (c), (d), and (e) fail to initialize the blank final variable active.




PreviousNext

Related