0
0
C++programming~20 mins

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

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