0
0
C++programming~20 mins

Function parameters in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of function with default parameters
What is the output of this C++ code?
C++
#include <iostream>
#include <string>
using namespace std;

void greet(string name = "Guest", int times = 1) {
    for (int i = 0; i < times; i++) {
        cout << "Hello, " << name << "!\n";
    }
}

int main() {
    greet();
    greet("Alice", 2);
    return 0;
}
A
Hello, Guest!
Hello, Guest!
Hello, Alice!
Hello, Alice!
B
Hello, Guest!
Hello, Alice!
Hello, Alice!
C
Hello, Guest!
Hello, Alice!
D
Hello, Alice!
Hello, Alice!
Attempts:
2 left
💡 Hint
Remember how default parameters work when no arguments are passed.
Predict Output
intermediate
2:00remaining
Output of function with pass-by-reference parameter
What is the output of this C++ code?
C++
#include <iostream>
using namespace std;

void addOne(int &num) {
    num += 1;
}

int main() {
    int a = 5;
    addOne(a);
    cout << a << endl;
    return 0;
}
A
5
B
Compilation error
C
6
D
Undefined behavior
Attempts:
2 left
💡 Hint
Think about how passing by reference affects the original variable.
Predict Output
advanced
2:00remaining
Output of function with const reference parameter
What is the output of this C++ code?
C++
#include <iostream>
using namespace std;

void printValue(const int &val) {
    cout << val << endl;
}

int main() {
    int x = 10;
    printValue(x);
    return 0;
}
A
10
B
Compilation error
C
0
D
Undefined behavior
Attempts:
2 left
💡 Hint
Const references allow reading but not modifying the argument.
Predict Output
advanced
2:00remaining
Output of function with pointer parameter
What is the output of this C++ code?
C++
#include <iostream>
using namespace std;

void setZero(int *ptr) {
    if (ptr) {
        *ptr = 0;
    }
}

int main() {
    int a = 5;
    setZero(&a);
    cout << a << endl;
    return 0;
}
A
0
B
5
C
Compilation error
D
Segmentation fault
Attempts:
2 left
💡 Hint
Pointers can be used to modify the value they point to.
Predict Output
expert
2:00remaining
Output of overloaded functions with different parameter types
What is the output of this C++ code?
C++
#include <iostream>
using namespace std;

void print(int x) {
    cout << "int: " << x << endl;
}

void print(double x) {
    cout << "double: " << x << endl;
}

int main() {
    print(5);
    print(5.0);
    print('5');
    return 0;
}
A
int: 5
double: 5.0
int: 53
B
int: 5
double: 5
int: 5
C
int: 5
double: 5
Compilation error
D
int: 5
double: 5
int: 53
Attempts:
2 left
💡 Hint
Remember how char is promoted to int in function overloading.