Java OCA OCP Practice Question 2600

Question

Consider the following program and predict the output:

class  Main {//from   w  w  w .j av a2 s . c  o m
        final Integer a;  // #1
        public void print(){
                System.out.println("a = " + a);
        }
        public static void main(String[] args) {
                Main obj = new Main();
                obj.print();
        }
}
  • a) The program will report a compiler error at statement #1.
  • b) The program will result in throwing a NullPointerException.
  • c) The program will print the following: a = 0.
  • d) The program will print the following: a = null.


a)

Note

Every final variable must be initialized.

If a final variable is not initialized at the time of variable declaration (known as blank final), then it must be initialized in all the constructors of the class.

Since the final variable is not initialized in this class, the code results in a compiler error.




PreviousNext

Related