Java OCA OCP Practice Question 2805

Question

Given the proper import statement(s) and:

4.     Console c = System.console();  
5.     char[] pw;  
6.     if(c == null) return;   
7.     pw = c.readPassword("%s", "pw: ");  
8.     System.out.println(c.readLine("%s", "input: ")); 

Which statements are true? (Choose all that apply.)

  • A. An exception will be thrown on line 8.
  • B. The variable pw must be a String, not a char[].
  • C. No import statements are necessary for the code to compile.
  • D. If the code compiles and is invoked, line 7 might never run.
  • E. Without a third argument, the readPassword() method will echo the password the user types.
  • F. If line 4 was replaced with the following code, the program would still compile: "Console c = new Console();".


D is correct.

Note

The code is all legal, but it's possible to invoke a Java program in an environment that doesn't have a Console object-therefore, the if test on line 6 can sometimes be true.

The readPassword() method Always disables echoing.

The Console object is Always constructed using System.console(), but the Console class is in java.io.




PreviousNext

Related