Question 1
EasyWhat is a constructor?
- A.
A loop inside a class
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
What is a constructor?
A loop inside a class
A special method that runs automatically when an object is created
A static variable
A method that deletes an object
Answer: B
Constructors initialise the new object's fields.
What is a destructor in C++?
A function that creates objects
A virtual constructor
A special member function called when an object is destroyed, used to release resources
A function called before main
Answer: C
Its name is ~ClassName(), and it takes no arguments.
What is the return type of a constructor?
It has no return type
The class itself
void
int
Answer: A
Constructors don't declare a return type, not even void.
What is a default constructor?
A constructor that is private
The first constructor in a class
A constructor that takes no arguments
A constructor with default return type
Answer: C
If you write no constructor, the compiler supplies a default one.
What is a parameterised constructor?
A constructor that takes arguments to initialise the object
A constructor that returns a value
A destructor
A constructor with no body
Answer: A
For example, Point(int x, int y) sets the coordinates when the object is created.
In Python, which method acts as the constructor that initialises a new object?
str
del
init
new only
Answer: C
init initialises the object after new has created it.
What is the output of this code?
#include <iostream>
using namespace std;
class Demo {
public:
Demo() { cout << "Created "; }
~Demo() { cout << "Destroyed"; }
};
int main() {
Demo obj;
return 0;
}
Destroyed
Created
Answer: C
The constructor runs when obj is created, and the destructor runs automatically when obj goes out of scope at the end of main.
When is a constructor called?
Automatically, when an object is created
Only when the programmer calls it by name
When an object is deleted
When the program ends
Answer: A
The constructor initialises the new object.
In C++, when is the destructor of a local object called?
Only when delete is used on it
When the program starts
Never
When the object goes out of scope
Answer: D
Leaving the block where it was declared destroys a local object automatically.
What is constructor overloading?
A constructor in a child class
A constructor that returns a value
Calling a constructor many times
Having several constructors with different parameter lists
Answer: D
Objects can then be created in different ways, such as Box() and Box(5).
Created Destroyed
Destroyed Created