Challenge - 5 Problems
Multiple Input/Output Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Remember that only variables passed by reference can be changed outside the function.
✗ Incorrect
The variable 'a' is passed by value, so changes inside the function do not affect 'x'. Variables 'b' and 'c' are passed by reference, so their values are updated.
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Pointers allow the function to modify variables outside its scope.
✗ Incorrect
The function sets '*sum' to 4+5=9 and '*product' to 4*5=20, so the output is '9 20'.
🔧 Debug
advanced2: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; }
Attempts:
2 left
💡 Hint
Check how the function parameters are passed and if changes affect the original variables.
✗ Incorrect
The function parameters are passed by value, so swapping inside the function does not affect 'x' and 'y'. The output remains '10 20'.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
Structured bindings unpack the tuple into variables.
✗ Incorrect
The function returns the smaller and larger values correctly. Structured bindings assign them to minVal and maxVal.
🧠 Conceptual
expert2:00remaining
Understanding multiple output methods in C++ functions
Which of the following statements about returning multiple outputs from a C++ function is NOT true?
Attempts:
2 left
💡 Hint
Think about pointer usage and safety in C++.
✗ Incorrect
Pointers can be used safely if handled correctly. They do not always cause runtime errors.