Bird
Raised Fist0
C++programming~20 mins

Using cout for output in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Cout Output Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of chained cout statements
What is the output of this C++ code?
C++
#include <iostream>
int main() {
    std::cout << "Hello" << ", " << "World!" << std::endl;
    return 0;
}
AHello, World!\n
BHello World!\n
CHello,World!\n
DHello\n, \nWorld!\n
Attempts:
2 left
💡 Hint
Look at how the strings and commas are chained with << operators.
❓ Predict Output
intermediate
2:00remaining
Output with multiple cout lines
What will be printed when this code runs?
C++
#include <iostream>
int main() {
    std::cout << "Line 1" << std::endl;
    std::cout << "Line 2";
    std::cout << " Line 3" << std::endl;
    return 0;
}
ALine 1\nLine 2 Line 3\n
BLine 1\nLine 2 Line 3
CLine 1\nLine 2\nLine 3\n
DLine 1 Line 2 Line 3\n
Attempts:
2 left
💡 Hint
Check where std::endl is used to add new lines.
❓ Predict Output
advanced
2:00remaining
Output with mixed data types using cout
What is the output of this code snippet?
C++
#include <iostream>
int main() {
    int a = 5;
    double b = 3.2;
    std::cout << "a = " << a << ", b = " << b << std::endl;
    return 0;
}
Aa = 5 b = 3.2\n
Ba = 5, b = 3.2\n
Ca = 5, b = 3.200000\n
Da = 5, b = 3.20\n
Attempts:
2 left
💡 Hint
By default, double prints with minimal decimals unless formatted.
❓ Predict Output
advanced
2:00remaining
Output with cout and escape sequences
What will this program print?
C++
#include <iostream>
int main() {
    std::cout << "First Line\nSecond Line\tTabbed" << std::endl;
    return 0;
}
A
First Line\nSecond Line Tabbed
BFirst Line\nSecond Line\tTabbed\n
C
First Line Second Line Tabbed
D
First Line
Second Line	Tabbed
Attempts:
2 left
💡 Hint
Escape sequences like \n and \t create new lines and tabs in output.
❓ Predict Output
expert
2:00remaining
Output of cout with pointer and address
What is the output of this code?
C++
#include <iostream>
int main() {
    int x = 10;
    int* p = &x;
    std::cout << "Value: " << *p << ", Address: " << p << std::endl;
    return 0;
}
AValue: 10, Address: *p\n
BValue: 10 Address: 0x7ffee3bff6ac\n
CValue: 10, Address: 0x7ffee3bff6ac\n
DValue: 10, Address: 10\n
Attempts:
2 left
💡 Hint
The pointer prints the memory address, which looks like a hex number starting with 0x.

Practice

(1/5)
1. What does the following C++ code do?
std::cout << "Hello, world!" << std::endl;
easy
A. Creates a variable named 'Hello, world!'
B. Reads input from the user
C. Saves 'Hello, world!' to a file
D. Prints 'Hello, world!' followed by a new line on the screen

Solution

  1. Step 1: Understand the use of std::cout

    std::cout is used to print output to the screen.
  2. Step 2: Analyze the code line

    The code sends the string "Hello, world!" to std::cout and then adds a new line with std::endl.
  3. Final Answer:

    Prints 'Hello, world!' followed by a new line on the screen -> Option D
  4. Quick Check:

    cout prints text + endl adds new line = A [OK]
Hint: cout prints text; endl adds a new line [OK]
Common Mistakes:
  • Thinking cout reads input
  • Confusing cout with file operations
  • Missing std:: before cout or endl
2. Which of the following is the correct syntax to print the number 10 using cout?
easy
A. cout << 10 << end;
B. cout >> 10 >> endl;
C. std::cout << 10 << std::endl;
D. std::cout << "10" >> std::endl;

Solution

  1. Step 1: Identify correct insertion operator

    The operator to send data to cout is <<, not >>.
  2. Step 2: Check for correct use of std::endl

    The correct manipulator to end the line is std::endl, not end.
  3. Final Answer:

    std::cout << 10 << std::endl; -> Option C
  4. Quick Check:

    Use << and std::endl for output [OK]
Hint: Use << to send data to cout and std::endl to end line [OK]
Common Mistakes:
  • Using >> instead of <<
  • Forgetting std:: before cout or endl
  • Typing end instead of std::endl
3. What is the output of the following code?
#include <iostream>
int main() {
  std::cout << "Sum: " << 5 + 3 << std::endl;
  return 0;
}
medium
A. Sum: 53
B. Sum: 8
C. 8
D. Sum:

Solution

  1. Step 1: Understand operator precedence in cout

    The expression 5 + 3 is evaluated first because + has higher precedence than <<.
  2. Step 2: Combine the string and sum

    The string "Sum: " is printed, then the result 8, then a new line.
  3. Final Answer:

    Sum: 8 -> Option B
  4. Quick Check:

    5+3=8, printed after 'Sum: ' [OK]
Hint: Remember + evaluates before << in cout [OK]
Common Mistakes:
  • Thinking cout concatenates numbers as strings
  • Reading output as 'Sum: 53'
  • Ignoring operator precedence
4. Find the error in this code snippet:
#include <iostream>
int main() {
  std::cout << "Hello" << std::endl
  return 0;
}
medium
A. Missing semicolon after std::endl
B. Wrong operator used instead of <<
C. std::cout should be cout only
D. Missing #include <string>

Solution

  1. Step 1: Check line endings

    Every statement in C++ must end with a semicolon. The line with std::endl is missing it.
  2. Step 2: Verify other syntax

    The operators and includes are correct; only the semicolon is missing.
  3. Final Answer:

    Missing semicolon after std::endl -> Option A
  4. Quick Check:

    Statements end with ; in C++ [OK]
Hint: Check for missing semicolons after cout lines [OK]
Common Mistakes:
  • Forgetting semicolon at end of statement
  • Confusing << with >>
  • Removing std:: prefix incorrectly
5. You want to print multiple variables on one line separated by spaces. Which code correctly prints int a = 5; and int b = 10; as "5 10" using cout?
hard
A. std::cout << a << " " << b << std::endl;
B. std::cout << a + " " + b << std::endl;
C. std::cout << a, " ", b << std::endl;
D. std::cout << a << b << std::endl;

Solution

  1. Step 1: Understand how to print multiple items with spaces

    Use << to send each item separately, including a string with a space.
  2. Step 2: Check each option

    std::cout << a << " " << b << std::endl; correctly sends a, then a space string, then b, then ends the line.
  3. Final Answer:

    std::cout << a << " " << b << std::endl; -> Option A
  4. Quick Check:

    Use << between variables and strings for spaces [OK]
Hint: Use << between variables and " " for spaces [OK]
Common Mistakes:
  • Trying to add int and string with +
  • Using commas instead of <<
  • Printing variables without spaces
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard