C++ Advanced Quiz

1. What is the purpose of a virtual function in C++?

a) To optimize code
b) To enable polymorphism
c) To make functions private
d) To reduce memory usage

2. What does `delete` do in `delete ptr;`?

a) Deletes a variable
b) Frees dynamically allocated memory
c) Resets ptr to 0
d) Deletes the pointer

3. True or False: A pure virtual function must be implemented in the base class.

a) True
b) False

4. What is the output of `template T add(T a, T b) { return a + b; } cout << add(3, 4);`?

a) 7
b) Error
c) 34
d) Undefined

5. What is a smart pointer?

a) A pointer to a class
b) A pointer that manages memory automatically
c) A constant pointer
d) A pointer to a function

6. What does `override` keyword ensure?

a) Function is virtual
b) Function overrides a base class virtual function
c) Function is private
d) Function is static

7. What is the output of `class A { public: A() { cout << "A"; } }; A a;`?

a) Nothing
b) A
c) Error
d) Undefined

8. True or False: RAII manages resource lifetime in C++.

a) True
b) False

9. What is `std::move` used for?

a) Copying objects
b) Enabling move semantics
c) Deleting objects
d) Locking resources

10. What happens if a destructor is not virtual in a base class?

a) Compilation error
b) Derived class destructor not called
c) No effect
d) Program crashes