OCA Java SE 8 Building Blocks - OCA Mock Question Building Block 14








Question

What does the following code output?

     1:public class Main { 
     2:   int count; 
     3:   public void Main() { 
     4:      count = 4; 
     5:   } 
     6:   public static void main(String[] args) { 
     7:     Main s = new Main(); 
     8:     System.out.println(s.count); 
     9:   } 
    10:} 
  1. 0
  2. 4
  3. Compilation fails on line 3.
  4. Compilation fails on line 4.
  5. Compilation fails on line 7.
  6. Compilation fails on line 8.




Answer



A.

Note

line 3 does compile, it is not a constructor because it has a return type.

It is a method that happens to have the same name as the class. IDE would give you an alert message saying you are using a constructor name.

When the code runs, the default constructor is called and count has the default value 0 for an int. 0 value is set to count by default for instance variable.