Question 41
MediumWhen does a C++ compiler stop generating a default constructor automatically?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
When does a C++ compiler stop generating a default constructor automatically?
When the class has a destructor
Never
As soon as you declare any constructor yourself
When the class has static members
Sign up free to see the answer and a step-by-step explanation.
Which constructor is called by 'Student s2 = s1;' in C++?
The copy constructor
The move constructor always
The default constructor
The assignment operator
Sign up free to see the answer and a step-by-step explanation.
Which is called by 's2 = s1;' when s2 already exists?
The copy constructor
The copy assignment operator
The destructor only
The default constructor
Sign up free to see the answer and a step-by-step explanation.
A class owns a pointer to heap memory. Which functions should it define (the Rule of Three)?
Destructor, copy constructor and copy assignment operator
Only the default constructor
Constructor, getter and setter
Only the destructor
Sign up free to see the answer and a step-by-step explanation.
What does a move constructor do in C++11?
Deletes the object
Copies an object twice
Moves an object to another file
Takes the resources of a temporary object instead of copying them
Sign up free to see the answer and a step-by-step explanation.
What does the 'explicit' keyword on a one-argument constructor prevent?
Inheritance
Creating objects on the heap
Copying the object
Implicit conversions such as P p = 7;
Sign up free to see the answer and a step-by-step explanation.
Why should a base class's destructor be virtual if objects are deleted through base pointers?
Otherwise only the base part is destroyed and the derived resources leak
To make the class abstract
Destructors can't be called otherwise
To allow overloading of the destructor
Sign up free to see the answer and a step-by-step explanation.
Can a destructor take arguments or be overloaded in C++?
No; each class has exactly one destructor with no parameters
Only in derived classes
Yes, any number
Only one extra argument
Sign up free to see the answer and a step-by-step explanation.
In Java, what is a constructor chain such as this(0, 0) used for?
Calling the parent's constructor
Creating two objects
Destroying an object
Calling another constructor of the same class to reuse its setup
Sign up free to see the answer and a step-by-step explanation.
Why is Java's finalize() not a good replacement for a C++ destructor?
It cannot access fields
It runs twice
The garbage collector may run it late or never, so cleanup timing is unpredictable
It runs before the constructor
Sign up free to see the answer and a step-by-step explanation.