Java OCA OCP Practice Question 187

Question

Given:

3. public class Main { 
4.   public static void main(String[] args) { 
5.     Days d1 = Days.TH; /*from w w  w. ja va2s  .c o  m*/
6.     Days d2 = Days.M; 
7.     for(Days d: Days.values()) { 
8.       if(d.equals(Days.F)) break; 
9.       d2 = d; 
10.     } 
11.     System.out.println((d1 == d2)?"same old" : "newly new"); 
12.   } 
13.   enum Days {M, T, W, TH, F, SA, SU}; 
14. } 

What is the result?

  • A. same old
  • B. newly new
  • C. Compilation fails due to multiple errors
  • D. Compilation fails due only to an error on line 7
  • E. Compilation fails due only to an error on line 8
  • F. Compilation fails due only to an error on line 11
  • G. Compilation fails due only to an error on line 13


A is correct.

Note

All of this syntax is correct.

The for-each iterates through the enum using the values() method to return an array.

An enum can be compared using either equals() or ==.

An enum can be used in a ternary operator's boolean test.




PreviousNext

Related