0
0
C++programming~20 mins

Input and output using cin and cout in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
InputOutputMaster
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this C++ code?
Consider the following code snippet. What will be printed when the input is 5 10?
C++
#include <iostream>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    cout << a + b << endl;
    return 0;
}
A5 10
B510
C15
DCompilation error
Attempts:
2 left
💡 Hint
Remember that operator >> reads integers and skips spaces.
Predict Output
intermediate
2:00remaining
What does this code print given input "Hello World"?
Look at this code. What will it output if the user types Hello World?
C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    string s;
    cin >> s;
    cout << s << endl;
    return 0;
}
AHello World
BHello
CWorld
DCompilation error
Attempts:
2 left
💡 Hint
cin >> string reads only until the first space.
Predict Output
advanced
2:00remaining
What is the output of this code reading multiple inputs?
Given the input 3 4 5, what will this program print?
C++
#include <iostream>
using namespace std;

int main() {
    int x, y, z;
    cin >> x >> y >> z;
    cout << x << y << z << endl;
    return 0;
}
A345
B3 4 5
C12
DCompilation error
Attempts:
2 left
💡 Hint
cout with multiple << outputs values without spaces.
Predict Output
advanced
2:00remaining
What error does this code cause?
What error will this code produce when run?
C++
#include <iostream>
using namespace std;

int main() {
    int a;
    cout << "Enter a number: ";
    cin >> a;
    cout << "You entered: " << b << endl;
    return 0;
}
ACompilation error: 'b' was not declared
BRuntime error: segmentation fault
COutputs the entered number
DNo output
Attempts:
2 left
💡 Hint
Check variable names carefully.
🧠 Conceptual
expert
2:30remaining
What is the output of this code with mixed input types?
Given the input 42 hello 3.14, what will this program output?
C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    int i;
    string s;
    double d;
    cin >> i >> s >> d;
    cout << s << " " << i << " " << d << endl;
    return 0;
}
ACompilation error
B42 hello 3.14
C3.14 42 hello
Dhello 42 3.14
Attempts:
2 left
💡 Hint
cin reads inputs in order and cout prints in the order given.