Bird
Raised Fist0
C++programming~20 mins

Multiple input and output in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Multiple Input/Output Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of function with multiple inputs and outputs using references
What is the output of the following C++ program?
C++
#include <iostream>
using namespace std;

void updateValues(int a, int& b, int& c) {
    a = a + 10;
    b = b + 20;
    c = c + 30;
}

int main() {
    int x = 1, y = 2, z = 3;
    updateValues(x, y, z);
    cout << x << " " << y << " " << z << endl;
    return 0;
}
A1 22 33
B11 22 33
C11 2 3
D1 2 3
Attempts:
2 left
💡 Hint
Remember that only variables passed by reference can be changed outside the function.
❓ Predict Output
intermediate
2:00remaining
Output of function returning multiple values via pointers
What will be printed by this C++ program?
C++
#include <iostream>
using namespace std;

void compute(int a, int b, int* sum, int* product) {
    *sum = a + b;
    *product = a * b;
}

int main() {
    int x = 4, y = 5, s = 0, p = 0;
    compute(x, y, &s, &p);
    cout << s << " " << p << endl;
    return 0;
}
A45 9
B9 9
C9 20
D20 9
Attempts:
2 left
💡 Hint
Pointers allow the function to modify variables outside its scope.
🔧 Debug
advanced
2:00remaining
Identify the error in multiple output function using references
This code is intended to swap two integers using references. What is the output of this C++ program?
C++
#include <iostream>
using namespace std;

void swap(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
}

int main() {
    int x = 10, y = 20;
    swap(x, y);
    cout << x << " " << y << endl;
    return 0;
}
ACompilation error: missing & in function parameters
BOutput: 10 20
COutput: 20 10
DRuntime error: segmentation fault
Attempts:
2 left
💡 Hint
Check how the function parameters are passed and if changes affect the original variables.
❓ Predict Output
advanced
2:00remaining
Output of function returning multiple values using std::tuple
What is the output of this C++ program that returns multiple values using std::tuple?
C++
#include <iostream>
#include <tuple>
using namespace std;

tuple<int, int> minMax(int a, int b) {
    if (a < b) return {a, b};
    else return {b, a};
}

int main() {
    int x = 7, y = 3;
    auto [minVal, maxVal] = minMax(x, y);
    cout << minVal << " " << maxVal << endl;
    return 0;
}
A3 7
B7 3
C0 0
DCompilation error: structured bindings not supported
Attempts:
2 left
💡 Hint
Structured bindings unpack the tuple into variables.
🧠 Conceptual
expert
2:00remaining
Understanding multiple output methods in C++ functions
Which of the following statements about returning multiple outputs from a C++ function is NOT true?
AFunctions can only return one value directly, so multiple outputs need workarounds.
BReturning a std::tuple can return multiple values but requires unpacking by the caller.
CUsing references as function parameters allows modifying variables outside the function.
DUsing pointers as parameters is unsafe and always causes runtime errors.
Attempts:
2 left
💡 Hint
Think about pointer usage and safety in C++.

Practice

(1/5)
1.

Which of the following statements correctly reads two integers a and b from the user in C++?

easy
A. std::cout >> a >> b;
B. std::cin << a << b;
C. std::cout << a << b;
D. std::cin >> a >> b;

Solution

  1. Step 1: Understand input operator usage

    In C++, std::cin is used for input and the extraction operator >> reads values.
  2. Step 2: Identify correct syntax for multiple inputs

    Using std::cin >> a >> b; reads two integers from the user correctly.
  3. Final Answer:

    std::cin >> a >> b; -> Option D
  4. Quick Check:

    Input uses std::cin with >> operator [OK]
Hint: Use std::cin with >> to read multiple inputs [OK]
Common Mistakes:
  • Using << instead of >> for input
  • Using std::cout for input
  • Mixing input and output operators
2.

Which of the following lines correctly prints three variables x, y, and z separated by spaces in C++?

easy
A. std::cout << x << ' ' << y << ' ' << z;
B. std::cout << x, y, z;
C. std::cout >> x >> y >> z;
D. std::cin << x << y << z;

Solution

  1. Step 1: Understand output operator usage

    In C++, std::cout is used for output and the insertion operator << prints values.
  2. Step 2: Print multiple variables with spaces

    Using std::cout << x << ' ' << y << ' ' << z; prints variables separated by spaces correctly.
  3. Final Answer:

    std::cout << x << ' ' << y << ' ' << z; -> Option A
  4. Quick Check:

    Output uses std::cout with << and spaces [OK]
Hint: Use std::cout with << and spaces to print multiple values [OK]
Common Mistakes:
  • Using commas instead of << for output
  • Using >> operator for output
  • Using std::cin for output
3.

What is the output of the following C++ code if the user inputs 5 10?

int a, b;
std::cin >> a >> b;
std::cout << a + b << ' ' << a * b << std::endl;
medium
A. 15 50
B. 510
C. 5 10
D. Syntax Error

Solution

  1. Step 1: Read input values

    The code reads two integers: a = 5 and b = 10 from user input.
  2. Step 2: Calculate and print sum and product

    The output prints a + b = 15 and a * b = 50 separated by a space.
  3. Final Answer:

    15 50 -> Option A
  4. Quick Check:

    Sum and product printed with space [OK]
Hint: Add and multiply inputs, print with space [OK]
Common Mistakes:
  • Concatenating numbers as strings
  • Forgetting space between outputs
  • Confusing input and output operators
4.

Identify the error in the following C++ code snippet:

int x, y;
std::cin << x << y;
std::cout << x << y;
medium
A. Missing semicolon after std::cin
B. >> used instead of << for output
C. << used instead of >> for input
D. Variables x and y not declared

Solution

  1. Step 1: Check input operator usage

    Input uses std::cin with extraction operator >>, but code uses << which is wrong.
  2. Step 2: Check output operator usage

    The code correctly uses std::cout with insertion operator <<.
  3. Final Answer:

    << used instead of >> for input -> Option C
  4. Quick Check:

    Input needs >> operator, not << [OK]
Hint: Input uses >>, output uses << operators [OK]
Common Mistakes:
  • Swapping << and >> operators
  • Confusing std::cin and std::cout
  • Ignoring operator direction
5.

Given the following code, what will be the output if the user inputs 3 4 5?

int a, b, c;
std::cin >> a >> b >> c;
std::cout << a + b << ' ' << b + c << ' ' << a + c << std::endl;
medium
A. Runtime Error
B. 7 9 8
C. 3 4 5
D. 12 9 7

Solution

  1. Step 1: Read three integers from input

    The variables are assigned as a=3, b=4, c=5 from user input.
  2. Step 2: Calculate sums and print

    Prints a + b = 7, b + c = 9, and a + c = 8 separated by spaces.
  3. Final Answer:

    7 9 8 -> Option B
  4. Quick Check:

    Sum pairs printed correctly [OK]
Hint: Add pairs of inputs and print with spaces [OK]
Common Mistakes:
  • Adding all three instead of pairs
  • Mixing input order
  • Forgetting spaces between outputs
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