0
0
C++programming~20 mins

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

Choose your learning style9 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.