C++, Qt Framework & Data Structures Practice
Assessment
1. Which C++ feature allows a derived class to provide a specific implementation of a
method that is already defined in its base class?
A) Method Overloading
B) Method Overriding
C) Encapsulation
D) Data Abstraction
Rationale: Method overriding occurs when a subclass provides its own definition of a virtual
function inherited from a base class with the same signature.
2. What happens if a class contains at least one pure virtual function (virtual void
func() = 0;)?
A) The class becomes a final class.
B) The class becomes an abstract class and cannot be instantiated.
C) The program will throw a runtime error upon compilation.
D) The function must be executed in the main routine automatically.
,Rationale: Declaring a function as pure virtual makes the class abstract, meaning objects of
this class type cannot be created directly.
3. Which pointer in C++ points to the object that invoked the current member
function?
A) super
B) base
C) this
D) self
Rationale: The this pointer is an implicit parameter passed to all non-static member
functions, pointing to the invoking object.
4. When implementing a deep copy in a C++ class containing raw pointer data
members, what must be explicitly defined?
A) A default constructor only
B) A copy constructor and copy assignment operator (operator=)
C) A static member function
D) A template parameter list
Rationale: A shallow copy copies pointer addresses (causing double-free errors), while a
deep copy requires custom copy constructors and copy assignment operators to duplicate
the actual heap memory.
,5. What is the main advantage of using C++ templates for data structures like stacks
or linked lists?
A) Faster runtime execution compared to concrete types
B) Automatic memory management and garbage collection
C) Code reusability for different data types without code duplication
D) Prevention of memory leaks automatically
Rationale: C++ templates enable generic programming, allowing a single data structure or
algorithm implementation to work with arbitrary data types.
6. In object-oriented software design, what does the principle of Encapsulation
encourage?
A) Making all class attributes public for easy accessibility
B) Combining data and the methods that operate on that data into a single unit while
restricting direct access to internal components
C) Inheriting attributes from multiple base classes simultaneously
D) Overloading every operator in the class
Rationale: Encapsulation bundles data and functions while hiding implementation details
behind private/protected access modifiers to safeguard data integrity.
7. Which operator must be overloaded to permit custom object printing directly via
std::cout << obj;?
, A) operator+
B) operator[]
C) operator<<
D) operator->
Rationale: Overloading the stream insertion operator (<<) as a non-member friend function
allows custom class instances to be streamed directly to std::ostream objects like std::cout.
8. What is the time complexity of searching for an element in a balanced Binary
Search Tree (BST) containing $N$ nodes in the worst case?
A) $\mathcal{O}(1)$
B) $\mathcal{O}(\log N)$
C) $\mathcal{O}(N)$
D) $\mathcal{O}(N^2)$
Rationale: In a balanced BST, the height of the tree is logarithmic relative to the number of
nodes, making both search and insertion $\mathcal{O}(\log N)$.
9. Why is a virtual destructor necessary when deleting a derived class object through
a base class pointer?
A) It prevents syntax errors during compilation.
B) It ensures the derived class destructor is called, preventing resource leaks.
C) It automatically resets all pointers to nullptr.