0
0
C++programming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function Overloading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of overloaded functions with different parameter types
What is the output of this C++ program?
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.5);
    return 0;
}
A
Double: 5
Double: 5.5
B
Int: 5
Double: 5.5
C
Int: 5
Int: 5
DCompilation error due to ambiguous call
Attempts:
2 left
💡 Hint
Look at the parameter types of each overloaded function and which one matches the argument.
Predict Output
intermediate
2:00remaining
Which overloaded function is called with const reference?
What is the output of this C++ program?
C++
#include <iostream>
using namespace std;

void show(int &x) {
    cout << "Non-const ref: " << x << endl;
}

void show(const int &x) {
    cout << "Const ref: " << x << endl;
}

int main() {
    int a = 10;
    const int b = 20;
    show(a);
    show(b);
    return 0;
}
A
Non-const ref: 10
Const ref: 20
B
Non-const ref: 10
Non-const ref: 20
C
Const ref: 10
Const ref: 20
DCompilation error due to ambiguous call
Attempts:
2 left
💡 Hint
Non-const references cannot bind to const variables.
Predict Output
advanced
2:00remaining
Output of overloaded functions with default parameters
What is the output of this C++ program?
C++
#include <iostream>
using namespace std;

void display(int x, int y = 5) {
    cout << "display(int, int): " << x << ", " << y << endl;
}

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

int main() {
    display(3);
    display(3, 7);
    return 0;
}
A
display(int, int): 3, 5
display(int, int): 3, 7
B
display(int): 3
display(int): 3
C
display(int): 3
display(int, int): 3, 7
DCompilation error due to ambiguous call
Attempts:
2 left
💡 Hint
Default parameters do not participate in overload resolution if a better match exists.
Predict Output
advanced
2:00remaining
Which overloaded function is called with pointer and nullptr?
What is the output of this C++ program?
C++
#include <iostream>
using namespace std;

void process(int *p) {
    cout << "Pointer version" << endl;
}

void process(bool b) {
    cout << "Bool version" << endl;
}

int main() {
    process(nullptr);
    return 0;
}
A
Pointer version
B
Bool version
C
Compilation error due to ambiguous call
D
Runtime error
Attempts:
2 left
💡 Hint
nullptr is a null pointer literal and prefers pointer overloads over bool.
🧠 Conceptual
expert
3:00remaining
Why does this overloaded function call cause ambiguity?
Consider these overloaded functions:
void func(int x, double y);
void func(double x, int y);
What happens when you call func(5, 5); and why?
ACalls func(int, int) which does not exist, so runtime error.
BCalls func(int, double) because int matches first parameter exactly.
CCalls func(double, int) because int converts to double better than double to int.
DCompilation error due to ambiguous call because both overloads are equally good matches.
Attempts:
2 left
💡 Hint
Both functions require one conversion from int to double, so compiler cannot decide.