Challenge - 5 Problems
Function Overloading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Look at the parameter types of each overloaded function and which one matches the argument.
✗ Incorrect
The function print(int) is called with 5 (an int), so it prints "Int: 5". The function print(double) is called with 5.5 (a double), so it prints "Double: 5.5".
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Non-const references cannot bind to const variables.
✗ Incorrect
The first call show(a) uses the non-const reference version because a is non-const. The second call show(b) uses the const reference version because b is const.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
Default parameters do not participate in overload resolution if a better match exists.
✗ Incorrect
The call display(3) matches display(int) exactly, so that is called. The call display(3,7) matches display(int,int) with both parameters, so that is called.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
nullptr is a null pointer literal and prefers pointer overloads over bool.
✗ Incorrect
nullptr matches the pointer version exactly. The bool version would require conversion. So the pointer version is called.
🧠 Conceptual
expert3: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?Attempts:
2 left
💡 Hint
Both functions require one conversion from int to double, so compiler cannot decide.
✗ Incorrect
When calling func(5,5), the first overload needs to convert second argument from int to double, the second overload needs to convert first argument from int to double. Both conversions are equally good, so the call is ambiguous and causes a compilation error.