Text preview & study summary
C++ Programming Fundamentals
A free sample of 5 questions from this quiz, shown in full with answer choices and explanations. No interactivity — everything is visible on this page for study and review.
Want to test your knowledge? Launch the Interactive Exam Simulator
Question 1
In C++, what is a **template**?
Explanation
C++ templates allow writing generic code that works with any data type. Function templates (e.g., `template<typename T> T max(T a, T b)`) and class templates (e.g., `template<typename T> class Stack`) are type-parameterized. The compiler generates type-specific versions at compile time (this is called template instantiation). The C++ Standard Template Library (STL) is built entirely on templates.
Question 2
**Statement:** In C++, `std::vector` is a dynamic array that automatically manages its size, and accessing an element with `at()` is safer than using `[]` because `at()` performs bounds checking and throws `std::out_of_range` if the index is invalid.
Explanation
`std::vector` is the C++ STL's dynamic array. The `[]` operator provides fast access without bounds checking (undefined behavior if out of range). The `at()` method checks bounds and throws `std::out_of_range` exception if the index is invalid. For safety-critical code, `at()` is preferred despite being slightly slower. `vector` is part of `<vector>` header.
Question 3
What is the **Rule of Three** in C++?
Explanation
The Rule of Three states that if a class needs to manage resources (like raw pointers) and defines any one of: destructor, copy constructor, or copy assignment operator—it almost certainly needs all three. Without them, the compiler-generated defaults will perform shallow copies, leading to double-free errors and dangling pointers. The Rule of Five (C++11) extends this to include the move constructor and move assignment operator.
Question 4
In object-oriented C++, the three core principles are [[blank1]] (hiding implementation details), [[blank2]] (using a base class interface to work with derived class objects), and [[blank3]] (deriving new classes from existing ones).
Explanation
Encapsulation uses access specifiers (private/protected/public) and getter/setter methods to hide internal data. Inheritance allows derived classes to reuse base class code (`class Dog : public Animal`). Polymorphism (via virtual functions) lets base class pointers/references call derived class methods—enabling runtime behavior variation.
Question 5
**Statement:** In C++, a **destructor** is called automatically when an object goes out of scope or is explicitly deleted, and it can be overloaded with multiple parameter versions.
Explanation
Destructors are called automatically when objects are destroyed (go out of scope or `delete` is called). However, destructors CANNOT be overloaded—a class can have only ONE destructor, and it takes no parameters and returns nothing. The destructor's name is `~ClassName()`. Its primary use is releasing resources (freeing memory, closing files) to prevent leaks.
