Bird
Raised Fist0
C++programming~20 mins

Parameterized constructor in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Parameterized Constructor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of parameterized constructor initialization
What is the output of this C++ program using a parameterized constructor?
C++
#include <iostream>
using namespace std;

class Box {
  int length;
public:
  Box(int l) { length = l; }
  void show() { cout << length << endl; }
};

int main() {
  Box b(10);
  b.show();
  return 0;
}
A0
BCompilation error
CGarbage value
D10
Attempts:
2 left
πŸ’‘ Hint
The constructor sets the length to the passed value.
❓ Predict Output
intermediate
2:00remaining
Value of member after parameterized constructor call
What is the value of member 'width' after this code runs?
C++
#include <iostream>
using namespace std;

class Rectangle {
  int width;
public:
  Rectangle(int w) { width = w; }
  int getWidth() { return width; }
};

int main() {
  Rectangle r(15);
  cout << r.getWidth() << endl;
  return 0;
}
ACompilation error
B0
C15
DUndefined behavior
Attempts:
2 left
πŸ’‘ Hint
The constructor sets width to the passed value.
πŸ”§ Debug
advanced
2:00remaining
Identify the error in parameterized constructor usage
What error does this code produce when compiled?
C++
#include <iostream>
using namespace std;

class Circle {
  int radius;
public:
  Circle() { radius = 5; }
  Circle(int r) { radius = r; }
  void show() { cout << radius << endl; }
};

int main() {
  Circle c();
  c.show();
  return 0;
}
ACompilation error: 'c' is not an object
BOutput: 5
COutput: 0
DRuntime error: segmentation fault
Attempts:
2 left
πŸ’‘ Hint
Look at how 'c' is declared in main.
❓ Predict Output
advanced
2:00remaining
Output of constructor with member initializer list
What is the output of this program?
C++
#include <iostream>
using namespace std;

class Point {
  int x, y;
public:
  Point(int a, int b) : x(a), y(b) {}
  void display() { cout << x << "," << y << endl; }
};

int main() {
  Point p(3, 4);
  p.display();
  return 0;
}
A3,4
B0,0
CCompilation error
D4,3
Attempts:
2 left
πŸ’‘ Hint
The member initializer list sets x and y to the passed values.
🧠 Conceptual
expert
3:00remaining
Behavior of parameterized constructor with default arguments
Consider this class with a parameterized constructor having default arguments. What will be the output of the program?
C++
#include <iostream>
using namespace std;

class Sample {
  int a, b;
public:
  Sample(int x = 1, int y = 2) { a = x; b = y; }
  void print() { cout << a << "," << b << endl; }
};

int main() {
  Sample s1;
  Sample s2(5);
  Sample s3(7, 8);
  s1.print();
  s2.print();
  s3.print();
  return 0;
}
A
0,0
5,2
7,8
B
1,2
5,2
7,8
CCompilation error due to ambiguous constructor calls
D
1,2
5,0
7,8
Attempts:
2 left
πŸ’‘ Hint
Default arguments fill in missing parameters from right to left.

Practice

(1/5)
1.

What is the main purpose of a parameterized constructor in C++?

easy
A. To initialize an object with specific values when it is created
B. To delete an object from memory
C. To copy one object to another
D. To create a function outside the class

Solution

  1. Step 1: Understand constructor role

    A constructor sets up an object when it is created.
  2. Step 2: Identify parameterized constructor use

    Parameterized constructors allow passing values to set object properties immediately.
  3. Final Answer:

    To initialize an object with specific values when it is created -> Option A
  4. Quick Check:

    Parameterized constructor = initialize with values [OK]
Hint: Think: constructor sets initial values when making an object [OK]
Common Mistakes:
  • Confusing constructor with destructor
  • Thinking constructors copy objects
  • Believing constructors are separate functions
2.

Which of the following is the correct syntax for a parameterized constructor in C++?

class Box {
public:
    Box(int w, int h) { width = w; height = h; }
private:
    int width, height;
};
easy
A. Box(int w, int h) { width = w; height = h; }
B. void Box(int w, int h) { width = w; height = h; }
C. Box() { width = w; height = h; }
D. Box(int w, int h) -> void { width = w; height = h; }

Solution

  1. Step 1: Check constructor name and return type

    Constructor must have the same name as the class and no return type.
  2. Step 2: Verify parameter list and body

    Box(int w, int h) { width = w; height = h; } matches correct syntax: name matches class, parameters present, no return type.
  3. Final Answer:

    Box(int w, int h) { width = w; height = h; } -> Option A
  4. Quick Check:

    Constructor = class name + no return type [OK]
Hint: Constructor has no return type and matches class name [OK]
Common Mistakes:
  • Adding void return type to constructor
  • Using different name than class
  • Missing parameters in parameterized constructor
3.

What will be the output of this code?

#include <iostream>
class Point {
public:
    int x, y;
    Point(int a, int b) { x = a; y = b; }
};

int main() {
    Point p(3, 4);
    std::cout << p.x << ", " << p.y << std::endl;
    return 0;
}
medium
A. Compilation error
B. 3, 4
C. 0, 0
D. Random values

Solution

  1. Step 1: Understand constructor call

    The object p is created with values 3 and 4 passed to the constructor.
  2. Step 2: Check member variable assignment and output

    The constructor sets x=3 and y=4, so printing p.x and p.y outputs "3, 4".
  3. Final Answer:

    3, 4 -> Option B
  4. Quick Check:

    Constructor sets values correctly = 3, 4 [OK]
Hint: Constructor sets values; output matches passed parameters [OK]
Common Mistakes:
  • Assuming default values instead of passed ones
  • Expecting compilation error due to missing default constructor
  • Confusing output format
4.

Identify the error in this code snippet:

#include <iostream>
class Car {
public:
    int speed;
    Car(int s) { speed = s; }
};

int main() {
    Car c;
    std::cout << c.speed << std::endl;
    return 0;
}
medium
A. Variable speed is not declared
B. Incorrect syntax in constructor definition
C. Missing default constructor causes error when creating Car c;
D. Output will be garbage value

Solution

  1. Step 1: Check object creation

    The line Car c; tries to create an object without parameters.
  2. Step 2: Verify constructors available

    Only a parameterized constructor Car(int s) exists; no default constructor is defined.
  3. Step 3: Understand compilation error

    Because no default constructor exists, Car c; causes a compilation error.
  4. Final Answer:

    Missing default constructor causes error when creating Car c; -> Option C
  5. Quick Check:

    No default constructor + no args object = error [OK]
Hint: If no default constructor, you must pass arguments when creating object [OK]
Common Mistakes:
  • Assuming default constructor exists automatically
  • Thinking variable declaration is missing
  • Expecting garbage output instead of compile error
5.

Given this class, how can you create an object r with width 5 and height 10 using the parameterized constructor?

class Rectangle {
public:
    int width, height;
    Rectangle(int w, int h) { width = w; height = h; }
};
hard
A. Rectangle r = Rectangle{5};
B. Rectangle r();
C. Rectangle r = Rectangle();
D. Rectangle r = Rectangle(5, 10);

Solution

  1. Step 1: Identify constructor parameters

    The constructor requires two integers: width and height.
  2. Step 2: Match object creation syntax

    Rectangle r = Rectangle(5, 10); correctly calls the constructor with two arguments (5, 10) to create object r.
  3. Step 3: Check other options

    Rectangle r(); is a function declaration, not object creation. Rectangle r = Rectangle(); calls default constructor which doesn't exist. Rectangle r = Rectangle{5}; passes only one argument, which is invalid.
  4. Final Answer:

    Rectangle r = Rectangle(5, 10); -> Option D
  5. Quick Check:

    Pass all required parameters to constructor [OK]
Hint: Call constructor with all parameters to create object [OK]
Common Mistakes:
  • Using empty parentheses which declare a function
  • Calling constructor without required arguments
  • Passing wrong number of arguments
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