C++ Construction and initialization test

Table of Contents

1. What does std::set::lower_bound(key) return?

lower_bound() returns the first element that is greater than or equal to the specified key.

2. What does std::set::upper_bound(key) return?

upper_bound() returns the first element that is strictly greater than the specified key.

3. Which property defines a Binary Search Tree?

In a BST, all values in the left subtree are smaller and all values in the right subtree are greater.

4. Why are balanced trees preferred over naive BSTs?

Balanced trees maintain logarithmic height, making search, insert and erase efficient.

5. Which condition must always hold for an AVL tree?

AVL trees maintain a balance factor whose absolute value cannot exceed one.

6. Which condition is NOT a Red-Black Tree invariant?

Height difference ≤ 1 is an AVL property, not a Red-Black Tree property.

7. What operation is used to rebalance binary search trees?

Left and right rotations preserve BST ordering while changing tree shape.

8. Which of the following breaks aggregate initialization?

A class with private members is no longer an aggregate.

9. Which initialization syntax performs aggregate initialization?

Brace initialization is used for aggregates.

10. What is direct initialization?

Direct initialization uses parentheses.

11. What is copy initialization?

Copy initialization uses the assignment-like syntax.

12. What famous parsing problem does this trigger? MyClass m(List(), List());

The compiler interprets this as a function declaration.

13. Which syntax avoids Most Vexing Parse?

Brace initialization cannot be interpreted as a function declaration.

14. Why is an initializer list preferred over assignment in the constructor body?

Initialization happens before entering the constructor body.

15. Which member must be initialized in the initializer list?

References cannot be default-initialized and assigned later.

16. In what order are members initialized?

The declaration order always determines initialization order.

17. What is a delegating constructor?

C++11 introduced constructor delegation.

18. Why is a recursive tree destructor dangerous?

Deep recursion can exhaust the stack.

19. Is delete nullptr valid?

Deleting a null pointer is guaranteed to do nothing.

20. Why is setting members to nullptr inside a destructor usually useless?

The object no longer exists after the destructor completes.

21. What does int x{}; perform?

Value initialization zero-initializes built-in types.

22. What value does int x{} contain?

Value initialization of int results in zero.

23. What value does int x; contain?

Local built-in variables are not initialized automatically.

24. What is the purpose of a copy constructor?

Copy construction creates a new object.

25. Which signature is the canonical copy constructor?

The canonical copy constructor takes a const reference.

26. What is assignment?

Assignment works on objects that already exist.

27. What should operator= normally return?

Returning a reference enables assignment chaining.

28. Why do we check if (this != &other) in operator=?

Self-assignment can corrupt the object if resources are released first.

29. When should a custom copy constructor usually be written?

STL containers already implement correct copy semantics.

30. What does '= delete' on a copy constructor do?

The compiler rejects any attempt to copy the object.