KnowledgeBoat Logo
|

Computer Applications

Which of the following statements correctly prints the following output?

Hello
World

Java Intro

1 Like

Answer

System.out.println("Hello\nWorld");

Reason — Let's analyze each of the options:

1. System.out.println("Hello/nWorld");Incorrect

  • \n is a special escape sequence, but /n (forward slash) is not. Output would be:
    Hello/nWorld

2. System.out.println("Hello\\nWorld");Incorrect

  • \\n prints as \n literally (because \\ is an escape character for \). Output would be:
    Hello\nWorld

3. System.out.println("Hello World");Incorrect

  • Prints everything on a single line. Output would be:
    Hello World

4. System.out.println("Hello World");Correct

  • The \n (newline escape sequence) moves the cursor to the next line. Output would be:
Hello
World

Answered By

3 Likes


Related Questions