Challenge - 5 Problems
Console Output Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of mixed Write and WriteLine calls
What is the output of the following C# code?
C Sharp (C#)
Console.Write("Hello"); Console.WriteLine(" World"); Console.WriteLine("!");
Attempts:
2 left
💡 Hint
Remember that Write does not add a new line, but WriteLine does.
✗ Incorrect
Console.Write writes text without moving to a new line. Console.WriteLine writes text and then moves to a new line. So the first Write prints "Hello" staying on the same line, then WriteLine prints " World" and moves to a new line, then the last WriteLine prints "!" and moves to a new line.
❓ Predict Output
intermediate2:00remaining
Output with multiple Write calls
What will be printed by this code snippet?
C Sharp (C#)
Console.Write("A"); Console.Write("B"); Console.WriteLine("C"); Console.WriteLine("D");
Attempts:
2 left
💡 Hint
Write does not add a new line, WriteLine does.
✗ Incorrect
The first two Write calls print 'A' and 'B' on the same line. Then WriteLine prints 'C' and moves to a new line. Finally, WriteLine prints 'D' and moves to a new line.
❓ Predict Output
advanced2:00remaining
Output of WriteLine with format string
What is the output of this code?
C Sharp (C#)
int x = 5; int y = 10; Console.WriteLine("Sum of {0} and {1} is {2}", x, y, x + y);
Attempts:
2 left
💡 Hint
Check how placeholders {0}, {1}, {2} are replaced by arguments.
✗ Incorrect
The placeholders {0}, {1}, and {2} are replaced by the values of x, y, and x + y respectively. So the output is 'Sum of 5 and 10 is 15' followed by a new line.
❓ Predict Output
advanced2:00remaining
Effect of WriteLine with no arguments
What does this code print?
C Sharp (C#)
Console.Write("Line1"); Console.WriteLine(); Console.WriteLine("Line3");
Attempts:
2 left
💡 Hint
WriteLine with no arguments just moves to the next line.
✗ Incorrect
Console.Write prints 'Line1' without a new line. Console.WriteLine() prints just a new line. Console.WriteLine("Line3") prints 'Line3' and moves to a new line. So the output has a blank line between Line1 and Line3.
❓ Predict Output
expert2:00remaining
Output of nested Write and WriteLine with escape sequences
What is the output of this code?
C Sharp (C#)
Console.Write("Start\n"); Console.WriteLine("Middle\tEnd"); Console.Write("Finish");
Attempts:
2 left
💡 Hint
Remember \n is newline and \t is tab. WriteLine adds a newline after printing.
✗ Incorrect
Console.Write("Start\n") prints 'Start' and a newline but does not add an extra newline. Console.WriteLine("Middle\tEnd") prints 'Middle', a tab, 'End', then a newline. Console.Write("Finish") prints 'Finish' without newline. So the output lines are:
Start
Middle End
Finish