Java OCA OCP Practice Question 1890

Question

Which statements about executing the following program are true?

package mypkg; 
import java.io.*; 
public class Main { 
   public static void main(String... robots) { 
      Console c = System.console(); 
      final String response = c.readLine("Are you human?"); 
      System.err.print(response); 
   } 
} 
  • I. The program may ask the user a question and print the response to the error stream.
  • II. The program may throw a NullPointerException at runtime.
  • III. The program may wait indefinitely.
  • A. I
  • B. I and III
  • C. II and III
  • D. I, II, and III


D.

Note

All three statements about the program are correct.

If System.console() is available, then the program will ask the user a question and then print the response if one is entered.

If System.console() is not available, then the program will exit with a NullPointerException.

Finally, the user may choose not to respond to the program's request for input, resulting in the program hanging indefinitely and making the last statement true.




PreviousNext

Related