Bird
Raised Fist0
C++programming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Constructor Overloading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of overloaded constructors with default and parameterized calls

What is the output of this C++ program that uses constructor overloading?

C++
#include <iostream>
using namespace std;

class Box {
public:
    int length, width;
    Box() { length = 0; width = 0; cout << "Default constructor called\n"; }
    Box(int l) { length = l; width = 0; cout << "One-parameter constructor called\n"; }
    Box(int l, int w) { length = l; width = w; cout << "Two-parameter constructor called\n"; }
};

int main() {
    Box b1;
    Box b2(5);
    Box b3(3, 4);
    return 0;
}
A
Two-parameter constructor called
One-parameter constructor called
Default constructor called
B
Default constructor called
One-parameter constructor called
Two-parameter constructor called
C
One-parameter constructor called
Default constructor called
Two-parameter constructor called
D
Default constructor called
Two-parameter constructor called
One-parameter constructor called
Attempts:
2 left
πŸ’‘ Hint

Look at which constructor matches each object creation.

❓ Predict Output
intermediate
2:00remaining
Output when calling overloaded constructors with explicit casts

What will this C++ program print when creating objects with explicit casts?

C++
#include <iostream>
using namespace std;

class Point {
public:
    int x, y;
    Point() { x = 0; y = 0; cout << "Default Point\n"; }
    Point(int a) { x = a; y = 0; cout << "One-arg Point\n"; }
    Point(int a, int b) { x = a; y = b; cout << "Two-arg Point\n"; }
};

int main() {
    Point p1 = Point();
    Point p2 = Point(10);
    Point p3 = Point(7, 8);
    return 0;
}
A
Default Point
One-arg Point
Two-arg Point
B
One-arg Point
Default Point
Two-arg Point
C
Two-arg Point
One-arg Point
Default Point
D
Default Point
Two-arg Point
One-arg Point
Attempts:
2 left
πŸ’‘ Hint

Explicit cast calls the matching constructor.

πŸ”§ Debug
advanced
2:00remaining
Identify the error in overloaded constructors with default arguments

What error does this C++ code produce?

C++
#include <iostream>
using namespace std;

class Sample {
public:
    int a, b;
    Sample(int x = 0, int y = 0) { a = x; b = y; cout << "Constructor 1\n"; }
    Sample(int x) { a = x; b = 10; cout << "Constructor 2\n"; }
};

int main() {
    Sample s1;
    Sample s2(5);
    return 0;
}
ACompilation error: redefinition of constructor
BRuntime error: segmentation fault
C
No error, prints:
Constructor 1
Constructor 2
DCompilation error: ambiguous call to constructor
Attempts:
2 left
πŸ’‘ Hint

Check how default arguments affect constructor overloading.

πŸ“ Syntax
advanced
2:00remaining
Which constructor declaration is invalid in C++?

Which of the following constructor declarations will cause a syntax error in C++?

AMyClass(int x = 5, int y);
BMyClass(int x, int y = 10);
CMyClass();
DMyClass(const MyClass &obj);
Attempts:
2 left
πŸ’‘ Hint

Default arguments must be trailing parameters.

πŸš€ Application
expert
2:00remaining
Number of objects created and constructors called

Consider this C++ program. How many objects are created and how many constructor calls are made in total?

C++
#include <iostream>
using namespace std;

class Test {
public:
    Test() { cout << "Default\n"; }
    Test(int x) { cout << "Param\n"; }
};

int main() {
    Test t1;
    Test t2(10);
    Test t3 = t2;
    Test t4 = Test(5);
    return 0;
}
A4 objects created, 4 constructor calls
B3 objects created, 4 constructor calls
C4 objects created, 5 constructor calls
D3 objects created, 3 constructor calls
Attempts:
2 left
πŸ’‘ Hint

Remember copy initialization calls copy constructor if defined, else default copy constructor is used.

Practice

(1/5)
1. What is constructor overloading in C++?
easy
A. Creating constructors only with no parameters
B. Having multiple constructors with different parameters in the same class
C. Using the same constructor multiple times in a program
D. Defining constructors outside the class only

Solution

  1. Step 1: Understand constructor overloading concept

    Constructor overloading means having more than one constructor in a class, each with different parameters.
  2. Step 2: Match definition with options

    Having multiple constructors with different parameters in the same class correctly describes this concept, while others describe unrelated ideas.
  3. Final Answer:

    Having multiple constructors with different parameters in the same class -> Option B
  4. Quick Check:

    Constructor overloading = multiple constructors with different parameters [OK]
Hint: Look for multiple constructors with different parameter lists [OK]
Common Mistakes:
  • Confusing overloading with overriding
  • Thinking constructors must have no parameters
  • Believing constructors can only be defined outside the class
2. Which of the following is the correct syntax for constructor overloading in C++?
easy
A. class MyClass { MyClass(); MyClass(int x); };
B. class MyClass { void MyClass(); void MyClass(int x); };
C. class MyClass { int MyClass(); int MyClass(int x); };
D. class MyClass { MyClass() int x; MyClass(int x) int x; };

Solution

  1. Step 1: Check constructor syntax

    Constructors have no return type and share the class name.
  2. Step 2: Identify correct syntax

    class MyClass { MyClass(); MyClass(int x); }; shows constructors with no return type and different parameters, which is correct. Others wrongly use return types or invalid syntax.
  3. Final Answer:

    class MyClass { MyClass(); MyClass(int x); }; -> Option A
  4. Quick Check:

    Constructors have no return type [OK]
Hint: Remember constructors have no return type, just the class name [OK]
Common Mistakes:
  • Adding return types to constructors
  • Using void or int before constructor name
  • Incorrect punctuation or missing semicolons
3. What will be the output of the following code?
class Box {
public:
  int length;
  Box() { length = 5; }
  Box(int l) { length = l; }
};

int main() {
  Box b1;
  Box b2(10);
  std::cout << b1.length << " " << b2.length;
  return 0;
}
medium
A. 5 5
B. 0 10
C. 5 10
D. 10 5

Solution

  1. Step 1: Identify which constructor is called for each object

    b1 uses the default constructor setting length to 5; b2 uses the parameterized constructor setting length to 10.
  2. Step 2: Determine output from std::cout

    Output prints b1.length (5) and b2.length (10) separated by space.
  3. Final Answer:

    5 10 -> Option C
  4. Quick Check:

    Default constructor sets 5, parameterized sets 10 [OK]
Hint: Match constructor parameters with object creation [OK]
Common Mistakes:
  • Assuming uninitialized variables default to 0
  • Mixing up which constructor is called
  • Forgetting to print both values
4. Find the error in this constructor overloading example:
class Sample {
public:
  Sample() { }
  Sample() { }
};
medium
A. Two constructors have the same parameter list causing a redefinition error
B. Constructors must have return type
C. Constructors cannot be public
D. Class must have at least one data member

Solution

  1. Step 1: Check constructor signatures

    Both constructors have no parameters, so they have identical signatures.
  2. Step 2: Understand C++ rules on overloading

    Constructors must differ in parameter lists to overload; identical signatures cause redefinition error.
  3. Final Answer:

    Two constructors have the same parameter list causing a redefinition error -> Option A
  4. Quick Check:

    Constructor signatures must differ [OK]
Hint: Overloaded constructors must differ in parameters [OK]
Common Mistakes:
  • Trying to overload with same parameter list
  • Adding return types to constructors
  • Thinking constructors can't be public
5. Given the class below, which constructor will be called when creating MyClass obj(3.14);?
class MyClass {
public:
  MyClass(int x) { std::cout << "Int constructor"; }
  MyClass(double x) { std::cout << "Double constructor"; }
  MyClass(float x) { std::cout << "Float constructor"; }
};
hard
A. Compilation error due to ambiguity
B. Int constructor
C. Float constructor
D. Double constructor

Solution

  1. Step 1: Identify argument type in object creation

    3.14 is a double literal by default in C++.
  2. Step 2: Match argument type to constructor

    The constructor with double parameter matches exactly, so it is called.
  3. Final Answer:

    Double constructor -> Option D
  4. Quick Check:

    Exact type match constructor is chosen [OK]
Hint: Literal 3.14 is double by default, so double constructor called [OK]
Common Mistakes:
  • Assuming float constructor is called for 3.14
  • Thinking int constructor is called due to truncation
  • Believing ambiguity causes error here
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