MCQ Practice13 min read

C++ MCQs with Answers: OOP, STL and Pointers

By the QUFF Team

C++ MCQs sit at the intersection of two question banks: the C questions about pointers and memory, and the object-oriented questions about virtual dispatch, construction order and the STL. The ones that decide a written test are almost always about what the compiler generates for you and what happens at run time - why a virtual destructor matters, why a constructor cannot be virtual, why unordered_map beats map for lookups but not for iteration. Here are those questions, with the reasoning that makes them permanent.

A laptop code editor with curly braces and angle brackets beside a terminal window, representing C++ programming

Classes, constructors and access

Which of these is NOT a C++ access specifier? (a) private (b) protected (c) public (d) internal

(d) internal - that is C#. C++ has exactly three: private (accessible within the class and its friends), protected (also accessible in derived classes) and public.

What is the difference between a struct and a class in C++?

Only the default access level: struct members are public by default, class members private. Both support constructors, inheritance, member functions and access specifiers - the sharp C-style distinction does not apply in C++.

What is a constructor's return type? (a) void (b) The class type (c) int (d) It has none

(d) It has none, and you cannot declare one. Constructors are invoked as part of object creation rather than called for a value, which is also why they cannot be virtual.

What is the size of an empty class in C++? (a) 0 (b) 1 (c) 4 (d) Undefined

(b) 1 byte. The standard requires distinct objects to have distinct addresses, so an empty class still occupies at least one byte. Empty base optimisation can eliminate the cost when it is used as a base class.

In what order are constructors and destructors called in an inheritance chain?

Constructors run base first, then derived; destructors run in exactly the reverse order, derived first then base. A derived object cannot be initialised before the base part it is built on exists.

What is the signature of a copy constructor?

ClassName(const ClassName& other). The reference is essential - taking the parameter by value would require copying the argument, which would call the copy constructor, recursing forever.

Practice this now

Virtual functions and polymorphism

What does declaring a member function virtual achieve?

Run-time dispatch. A call through a base pointer or reference selects the override belonging to the object's actual type, using a virtual table. Without virtual, the call is resolved statically from the pointer's declared type - which is the wrong answer in almost every polymorphism question.

Why does a base class need a virtual destructor?

Because deleting a derived object through a base pointer with a non-virtual destructor runs only the base destructor. The derived part is never destroyed, so its resources leak. Any class intended as a polymorphic base needs one.

Can a constructor be virtual? (a) Yes (b) No (c) Only in an abstract class (d) Only if the destructor is virtual

(b) No. Virtual dispatch requires a constructed object with its virtual table set up, and during construction that object does not yet exist. Destructors, by contrast, can and often should be virtual.

What makes a class abstract in C++?

At least one pure virtual function, written virtual void f() = 0;. An abstract class cannot be instantiated, though it can hold data members and non-virtual functions - which is how C++ expresses what Java splits between interfaces and abstract classes.

Which type of polymorphism does function overloading represent? (a) Run-time (b) Compile-time (c) Both (d) Neither

(b) Compile-time, resolved from the argument types by the compiler. Overriding a virtual function is the run-time form. Operator overloading is also compile-time polymorphism.

How does C++ resolve the diamond problem in multiple inheritance?

With virtual inheritance - declaring the shared base virtual so that only one instance of it exists in the final object. Without it, a class inheriting the same base along two paths gets two copies and ambiguous member access.

Practice this now

References, pointers and memory

What is a reference in C++, and how does it differ from a pointer?

A reference is an alias for an existing object. It must be initialised when declared, cannot be reseated to refer to something else, and cannot be null. A pointer is an independent object that may be null and may be reassigned freely.

What is the difference between new and malloc?

new computes the size itself, returns a correctly typed pointer, runs the constructor, and throws std::bad_alloc on failure. malloc takes a byte count, returns void*, runs no constructor, and returns NULL on failure. Memory from new must be released with delete, never free.

When must you use delete[] rather than delete?

When the memory was allocated with new[]. delete[] destroys every element in the array; using plain delete on an array is undefined behaviour and typically destroys only the first element. Mismatching the pair is a classic MCQ.

What is the rule of three?

If a class needs a user-defined destructor, copy constructor or copy assignment operator, it almost certainly needs all three - because the need signals that the class manages a resource, and the compiler-generated versions would copy the handle rather than the resource. With move semantics this extends to the rule of five.

What does a const member function guarantee?

That it will not modify the object's non-mutable data members, which also lets it be called on a const object. Marking every read-only member function const is standard practice, and forgetting it is why some code fails to compile on const objects.

The STL

Container questions are almost always complexity or ordering questions in disguise. This table answers most of them directly.

Common STL containers
ContainerUnderlying structureLookupOrdered?
vectorDynamic arrayO(1) by indexInsertion order
listDoubly linked listO(n)Insertion order
dequeSegmented arrayO(1) by indexInsertion order
map / setRed-black treeO(log n)Sorted by key
unordered_map / unordered_setHash tableO(1) average, O(n) worstNo order
priority_queueBinary heapO(1) topBy priority

What is the complexity of push_back on a vector? (a) O(1) always (b) Amortised O(1) (c) O(n) always (d) O(log n)

(b) Amortised O(1). Most calls simply write into spare capacity, but when capacity is exhausted the vector reallocates and copies every element, which is O(n). Averaged over many insertions the cost per insertion is constant.

Which container would you choose for fast lookup by key when order does not matter?

unordered_map - a hash table with O(1) average lookup. Use map when you need keys iterated in sorted order or need worst-case guarantees, since it is a balanced tree with O(log n) operations.

What happens to iterators when a vector reallocates?

They are invalidated, along with pointers and references to its elements, because the data has moved to new storage. This is why holding an iterator across a push_back is a bug - and a frequent MCQ.

What is a template in C++?

A compile-time mechanism for writing code parameterised by type. The compiler instantiates a separate concrete version for each type used, which is why templates are type-safe with no run-time cost - and why template errors appear at compile time and can be verbose.

Output and operator questions

What is printed by: cout << 5 / 2 << " " << 5.0 / 2; ?

2 2.5. The first division has two int operands, so it truncates. The second has a double operand, so the int is converted and real division is performed. Mixed-type arithmetic promotes to the wider type.

What is a friend function?

A non-member function granted access to a class's private and protected members. It is not called on an object and does not appear in the class's interface - commonly used for operator overloads such as operator<< that must take the stream as the left operand.

What does a static data member of a class represent?

A single variable shared by every object of the class, not one per object. It must be defined outside the class (or be inline or constexpr) and can be accessed through the class name without any instance.

What does the inline keyword actually do?

It is a request to the compiler to expand the function at the call site rather than issuing a call, and it permits the definition to appear in multiple translation units. Compilers treat the expansion part as a hint and decide for themselves - the linkage effect is the part you can rely on.

Which of these operators cannot be overloaded in C++? (a) + (b) [] (c) :: (d) ->

(c) the scope resolution operator ::. The non-overloadable set also includes . (member access), .* , ?: and sizeof.

Practice this now

How to prepare C++ MCQs efficiently

  • For every OOP question, ask what the compiler generates and when it runs - constructor order, destructor order, and which of the special member functions was silently supplied.
  • For every polymorphism question, check for virtual first. Its presence or absence is the answer far more often than anything else in the code.
  • Memorise the STL container table. It converts a large family of questions into a lookup.
  • Keep your C pointer knowledge sharp - C++ MCQs still test pointer arithmetic, memory allocation and undefined behaviour.
  • Trace output questions on paper, watching for integer division and implicit type promotion.

The bottom line

Now go test yourself

C++ MCQs concentrate on a short list of mechanisms: virtual dispatch and why a base destructor must be virtual, construction and destruction order, the special member functions the compiler writes for you, the difference between references and pointers, and the STL's structure-to-complexity mapping. Every one of those is a rule with a reason, which means none of them needs to be memorised blindly.

Learn the reasons, then make the recall fast. Drill timed C++ and data-structures quizzes on QUFF, and treat every miss as a question about what the compiler was doing rather than about which option was correct.

FAQs

Frequently asked questions

What topics come in C++ MCQs?

Classes and access specifiers, constructors and destructors, inheritance and virtual functions, abstract classes, operator overloading, references and pointers, dynamic memory with new and delete, templates, and the STL containers with their complexities.

Why do we need a virtual destructor in C++?

Because deleting a derived object through a base-class pointer with a non-virtual destructor runs only the base destructor, leaving the derived part destroyed - and its resources leaked. Any class used as a polymorphic base needs a virtual destructor.

Can a constructor be virtual in C++?

No. Virtual dispatch needs a fully constructed object with its virtual table in place, which does not exist while the constructor runs. Destructors can be virtual, and for polymorphic bases they should be.

What is the difference between map and unordered_map?

map is a red-black tree with O(log n) operations and keys kept in sorted order. unordered_map is a hash table with O(1) average lookup, O(n) worst case, and no ordering. Choose by whether you need order or speed.

What is the difference between a reference and a pointer?

A reference is an alias that must be initialised, cannot be reseated and cannot be null. A pointer is an object holding an address; it can be null, reassigned, and has its own storage. References are preferred for parameters that must always refer to something.

What is the rule of three in C++?

If your class needs a custom destructor, copy constructor or copy assignment operator, it almost certainly needs all three - because that need signals resource ownership, which the compiler-generated shallow copies would mishandle. Move semantics extend this to the rule of five.

Is push_back on a vector O(1)?

Amortised O(1). Individual calls are constant time until capacity runs out, at which point the vector reallocates and copies all elements in O(n). Averaged across many insertions, the cost per insertion is constant.

Where can I practise C++ MCQs for free?

QUFF has a free adaptive C++ quiz with no sign-up. It escalates in difficulty and explains every answer, which helps most on the virtual-dispatch and memory-management questions where guessing rarely works.

Related quizzes

Put it into practice

Keep reading

Related articles

Browse all articles →

Test yourself in two minutes

Six adaptive questions, every answer explained by an AI tutor. Free.

▶ Start an AI quiz