Bird
Raised Fist0
C++programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1.

What is the main purpose of function overloading in C++?

easy
A. To create functions that return different data types
B. To allow multiple functions with the same name but different parameters
C. To define functions inside other functions
D. To overload operators like + and -

Solution

  1. Step 1: Understand function overloading concept

    Function overloading means having multiple functions with the same name but different parameter lists.
  2. Step 2: Compare options with definition

    Only To allow multiple functions with the same name but different parameters correctly describes this concept. Other options describe different features.
  3. Final Answer:

    To allow multiple functions with the same name but different parameters -> Option B
  4. Quick Check:

    Function overloading = same name, different parameters [OK]
Hint: Remember: same name, different parameters means overloading [OK]
Common Mistakes:
  • Confusing overloading with operator overloading
  • Thinking overloading changes return type only
  • Mixing up function overloading with function overriding
2.

Which of the following is a correct function overloading declaration in C++?

void print(int x);
void print(double x);
void print(int x, int y);
void print(int x);
easy
A. The last declaration causes a compilation error
B. The third declaration is invalid
C. The second declaration is invalid
D. All declarations are correct

Solution

  1. Step 1: Check for duplicate function signatures

    The first and last declarations have the same signature: void print(int x);.
  2. Step 2: Understand function overloading rules

    Multiple declarations with identical signatures are allowed only if they are declarations, but defining the same function multiple times causes compilation error.
  3. Final Answer:

    The last declaration causes a compilation error -> Option A
  4. Quick Check:

    Duplicate function definitions cause errors [OK]
Hint: Duplicate declarations allowed, duplicate definitions cause errors [OK]
Common Mistakes:
  • Thinking return type differentiates overloaded functions
  • Thinking duplicate declarations cause compilation errors
  • Assuming default parameters allow identical signatures
3.

What will be the output of the following code?

void display(int x) { std::cout << "Int: " << x << std::endl; }
void display(double x) { std::cout << "Double: " << x << std::endl; }

int main() {
display(5);
display(5.5);
return 0;
}
medium
A. Compilation error due to ambiguous call
B. Double: 5
Double: 5.5
C. Int: 5
Int: 5
D. Int: 5
Double: 5.5

Solution

  1. Step 1: Identify which function matches each call

    Call display(5) matches display(int). Call display(5.5) matches display(double).
  2. Step 2: Understand output from each function

    First call prints "Int: 5", second prints "Double: 5.5".
  3. Final Answer:

    Int: 5
    Double: 5.5
    -> Option D
  4. Quick Check:

    Matching parameter type calls correct function [OK]
Hint: Match argument type to function parameter type [OK]
Common Mistakes:
  • Assuming implicit conversion causes ambiguity
  • Confusing int and double overloads
  • Expecting compilation error for valid overloads
4.

Find the error in the following code snippet:

void sum(int a, int b) { std::cout << a + b << std::endl; }
void sum(int a, int b, int c) { std::cout << a + b + c << std::endl; }

int main() {
sum(5, 10);
sum(5, 10, 15);
sum(5, 10, 15, 20);
return 0;
}
medium
A. Third call to sum causes compilation error
B. No error, code runs fine
C. First sum function is missing return type
D. Second sum function has wrong parameter types

Solution

  1. Step 1: Check function declarations

    Two sum functions exist: one with 2 int parameters, one with 3 int parameters.
  2. Step 2: Analyze function calls

    Calls with 2 and 3 arguments match existing functions. Call with 4 arguments has no matching function, causing error.
  3. Final Answer:

    Third call to sum causes compilation error -> Option A
  4. Quick Check:

    Function call must match declared overloads [OK]
Hint: Check number of arguments matches function parameters [OK]
Common Mistakes:
  • Assuming extra arguments are ignored
  • Thinking default parameters exist without declaration
  • Ignoring compilation errors on unmatched calls
5.

Consider the following overloaded functions:

int multiply(int a, int b) { return a * b; }
double multiply(double a, double b) { return a * b; }
int multiply(int a, int b, int c = 1) { return a * b * c; }

What will be the output of std::cout << multiply(2, 3) << std::endl;?

hard
A. 6 (from the first function)
B. 6 (from the third function with default parameter)
C. Compilation error due to ambiguous call
D. 6.0 (from the second function)

Solution

  1. Step 1: Identify overloaded functions with matching signatures

    Two functions can match call multiply(2, 3): int multiply(int, int) and int multiply(int, int, int=1) because default parameter allows 2 arguments.
  2. Step 2: Understand ambiguity in overload resolution

    Compiler cannot decide which function to call because both are equally good matches, causing ambiguity error.
  3. Final Answer:

    Compilation error due to ambiguous call -> Option C
  4. Quick Check:

    Default parameters can cause ambiguous overloads [OK]
Hint: Default parameters can create ambiguous overloads [OK]
Common Mistakes:
  • Ignoring ambiguity caused by default arguments
  • Assuming first declared function is chosen
  • Confusing return type with overload resolution
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