Java OCA OCP Practice Question 560

Question

What does the following code output?

1: public class Main { 
2:  int count; //from   w  w w  .j ava  2 s .com
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:} 
  • A. 0
  • B. 4
  • C. Compilation fails on line 3.
  • D. Compilation fails on line 4.
  • E. Compilation fails on line 7.
  • F. Compilation fails on line 8.


A.

Note

While the code on 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.

When the code runs, the default constructor is called and count has the default value (0) for an int.




PreviousNext

Related