Java OCA OCP Practice Question 305

Question

Given:

 3. import java.io.*;
 4. public class Main {
 5.  public static void main(String[] args) {
 6.    Console c = System.console();
 7.    String u = c.readLine("%s", "username: ");
 8.    System.out.println("hello " + u);
 9.    String pw;/*from  ww  w  .  j a v a 2s . com*/
10.    if(c != null && (pw = c.readPassword("%s", "password: ")) != null)
11.      // check for valid password
12.  }
13. }

If line 6 creates a valid Console object and if the user enters fred as a username and 1234 as a password, what is the result?

Choose all that apply.

A.   username:/*from ww  w.  j  a  v  a  2  s .  co m*/
     password:

B.   username: fred
     password:

C.   username: fred
     password: 1234

D.   Compilation fails

E.   An Exception is thrown at runtime


D is correct.

Note

The readPassword() method returns a char[].

If a char[] were used, answer B would be correct.




PreviousNext

Related