Java OCA OCP Practice Question 2473

Question

Given:.

1. public class Main {  
2.   static MyClass w;  
3.   public static void main(String[] args) {  
4.     System.out.print(w.Summer.count + " " + w.Sunny.count + " ");  
5.   }    /*from   ww w. j  av  a 2  s .  c  om*/
6. }  
7. enum MyClass {  
8.   Summer, Sunny;  
9.   int count = 0;  
10.   MyClass() {   
11.     System.out.print("c ");  
12.     count++;   
13.   }  
14. }  

What is the result?.

  • A. c 1 c 1
  • B. c 1 c 2
  • C. c c 1 1
  • D. c c 1 2
  • E. c c 2 2
  • F. Compilation fails.
  • G. An exception is thrown at runtime.


C is correct.

Note

All of an enum's values are initialized at the same time, and an enum's variables are treated as if they were instance variables, not static variables.




PreviousNext

Related