What Is the Output of Console readLine and readPassword?

Java I/O API
Answer Correct answer: A — The console reads Duke and Java; String.valueOf(password) then prints the char[] password as Java in the final output line.

Given: and the command: java ConsoleTest The user will input Duke and Java when the input is prompted. What is the output? - image

  1. Input Name: Duke - Correct Answer
  2. Input Name: Duke -
  3. Input Name: Duke -
  4. Input Name: Duke -

Community Votes

A
100%

100% of anonymous learners picked answer A. Votes are pick records left by other test-takers — they are not the verified answer.

Community Insight

It tests Console prompt and echo behavior plus the char[] conversion trap: String.valueOf(password) prints the typed password as Java, whereas direct char[] concatenation would produce a hash code.

This 1Z0-819 question tests Console.readLine and Console.readPassword plus how String.valueOf(char[]) renders the password. It establishes that the correct output is Duke for the name and Java for the password, not a char[] hash code.

A common wrong choice assumes readPassword returns a String or that printing its char[] always shows [C@..., but here String.valueOf(password) converts the returned char[] into readable text.

Community Discussion (5 comments)

Omnisumem 👍 1 Selected: A
to check for yourself: import java.util.; import java.lang.Object.; import java.io.Console; public class q189{ public static void main (String args[]){ Console console = System.console(); var name = console.readLine("Input Name: "); var password = console.readPassword("Input Password: "); System.out.println ("Name is: " + name + ", Password is: " + String.valueOf(password)); }}
ASPushkin 👍 1 Selected: A
String public static String valueOf(char[] data)) java.io.Console public char[] readPassword() Returns: A character array containing the password
d7bb0b2 👍 1 Selected: A
A is correct eadpassword retrive a char[], but in print method uses toString method for this pass visible
d7bb0b2 👍 1
that is correct because, readpassword retrive a char[], but in print method uses to string for this pass is visible
Felipe47 👍 2
A. Tested.

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

Expert Analysis

Why the Answer Is Correct

The program calls console.readLine("Input Name: ") to echo and read Duke, then console.readPassword("Input Password: ") to read Java without echoing it. The final println combines name with String.valueOf(password); String.valueOf(char[]) returns a new String containing the password characters. Therefore the visible final line is "Name is: Duke, Password is: Java", so option A is correct. The prompt text and the silent password entry explain why only the prompts and final result appear, not "Input Password: Java".

Why the Other Options Are Wrong

Options that show [C@... for the password would be right only if the code concatenated the char[] directly or called toString on it. Options that show the password echoed at the readPassword prompt are wrong because readPassword disables echo. Options that omit the final println or show null ignore that readPassword returns a populated char[] from the Java input. Thus none of those match the String.valueOf(char[]) conversion in the given code.

Community Comment Notes

Omnisumem's test harness confirms the same flow by using String.valueOf(password) and printing the name and password after the two Console calls. ASPushkin points out that readPassword returns a char[] containing the password, which is exactly what String.valueOf(char[]) converts. d7bb0b2 notes that the char[] alone would not display the password the same way, and Felipe47 simply records "A. Tested." These comments align with the output that shows Java on the final line.

Official Reference

Exam Strategy

Trace the Console calls in order and separate prompt text from echoed input: readLine echoes Duke, readPassword suppresses echo for Java. Then identify the exact conversion used in println; String.valueOf(char[]) prints the password characters, while direct char[] concatenation would print a hash code.

Frequently Asked Questions

Why is Java not shown after the Input Password prompt?

Console.readPassword suppresses echo while Java is typed, so the password appears only later when String.valueOf(password) is printed.

What if the code printed the password char[] without String.valueOf?

Direct concatenation or println's Object handling would produce a hash code like [C@... instead of Java; String.valueOf(char[]) prevents that.

More 1Z0-819 FAQ →

Related Analysis

← Back to 1Z0-819 Study Guide