Java OCA OCP Practice Question 1520

Question

Which of the following results is not a possible output of this program?

package sea; 
enum Direction { NORTH, SOUTH, EAST, WEST; }; 
public class Main { 
   public static void main(String[] compass) { 
      System.out.print(Direction.valueOf(compass[0])); 
   } 
} 
  • A. WEST is printed.
  • B. south is printed.
  • C. An ArrayIndexOutOfBoundsException is thrown at runtime.
  • D. An IllegalArgumentException is thrown at runtime.


B.

Note

If the program is called with a single input WEST, then WEST would be printed at runtime.

If the program is called with no input, then the compass array would be of size zero, and an ArrayIndexOutOfBoundsException would be thrown at runtime.

If the program is called with a string that does not match one of the values in Direction, then an IllegalArgumentException would be thrown at runtime.

The only result not possible is south, since the enum value is in uppercase, making Option B the correct answer.




PreviousNext

Related