Java OCA OCP Practice Question 3199

Question

What will be the result of compiling and running the following program?

public class Main {
  static int a;
  int b;/*from w  w  w .  j  a  v  a 2  s  . c  o  m*/

  public Main() {
    int c;
    c = a;
    a++;
    b += c;
  }

  public static void main(String[] args) {
    new Main();
  }
}

Select the one correct answer.

  • (a) The code will fail to compile, since the constructor is trying to access the static members.
  • (b) The code will fail to compile, since the constructor is trying to use the static field a before it has been initialized.
  • (c) The code will fail to compile, since the constructor is trying to use the field b before it has been initialized.
  • (d) The code will fail to compile, since the constructor is trying to use the local variable c before it has been initialized.
  • (e) The code will compile and run without any problems.


(e)

Note

The fact that a field is static does not mean that it is not accessible from non-static methods and constructors.

All fields are assigned a default value if no initializer is supplied.

Local variables must be explicitly initialized before use.




PreviousNext

Related