Question 71
HardWhy can throwing an exception from a destructor be dangerous in C++?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
Why can throwing an exception from a destructor be dangerous in C++?
Destructors can't throw at all
If it happens during stack unwinding from another exception, std::terminate is called
It leaks memory always
It is ignored
Sign up free to see the answer and a step-by-step explanation.
What does 'explicit' on a constructor prevent?
Implicit conversions, such as passing an int where the class is expected
Destruction
Copying
Inheritance
Sign up free to see the answer and a step-by-step explanation.
In Java, what runs first when a subclass object is created?
The main method
The finalize() method
The child's constructor body
Static initialisers (once per class), then instance initialisation of the parent, then the child
Sign up free to see the answer and a step-by-step explanation.
What is a Java instance initialiser block?
A { … } block in a class body that runs before every constructor's body
A static method
A constructor with no name
A destructor
Sign up free to see the answer and a step-by-step explanation.
What is the 'most vexing parse' in C++?
A declaration like Widget w(); being read as a function declaration, not an object
Operator precedence errors
Parsing templates
Missing semicolons
Sign up free to see the answer and a step-by-step explanation.
In Python, when is del called?
When the object is about to be destroyed, which in CPython is usually when its reference count reaches zero, but not guaranteed
Every time del is used on any name
Never
Immediately after creation
Sign up free to see the answer and a step-by-step explanation.
What is a factory method's advantage over a constructor?
It must be private
It can't be overridden
It has a descriptive name, can return a cached or subclass instance, and need not create a new object
It is always faster
Sign up free to see the answer and a step-by-step explanation.
What is the 'Rule of Five' in C++?
Five levels of inheritance
If a class needs a custom destructor, copy constructor, copy assignment, move constructor or move assignment, it probably needs all five
Use five constructors
A class must have five members
Sign up free to see the answer and a step-by-step explanation.
Why does std::vector<T>::emplace_back avoid a temporary compared with push_back(T(args))?
It skips the constructor
It is the same
It constructs the element directly in the vector's storage from the arguments
It copies twice
Sign up free to see the answer and a step-by-step explanation.
What happens to members of a C++ object whose constructor throws an exception?
The exception is ignored
The destructor runs normally
Everything leaks
Fully constructed members and bases are destroyed; the object's own destructor is not run
Sign up free to see the answer and a step-by-step explanation.