What is the output of the following code: cout << "Hello\nWorld"; in C++?
A. Hello World
B. Hello
C. World
D. Hello\nWorld
Answer: Option A
Solution (By Examveda Team)
Let's break down this C++ code snippet!The code is: cout << "Hello\nWorld";
cout is used to display output on the screen.
The double quotes " " enclose a string of characters that will be printed.
Inside the string, we have "Hello\nWorld".
The important part here is \n. This is a special character called a newline character.
\n tells the program to move the cursor to the beginning of the next line.
So, "Hello\nWorld" means print "Hello", then move to the next line, and then print "World".
Therefore, the correct output is "Hello" on one line and "World" on the next line.
Option B and C are incorrect because they only output one word.
Option D is incorrect because it prints the actual '\n' character, rather than interpreting it as a newline.
The correct answer is to imagine the output with Hello appearing above World.
So, the best way to visualize this output is :
Hello
World
The code cout