AI-assisted, automatically checked• Updated August 2026

C++ MCQs

C++ OOP, pointers, STL, templates, memory management, and references.

47 MCQsProgrammingAll Levels
Key Concept Overview & Exam Context

Review core principles of C++ before answering questions. These questions simulate placement technical assessments (TCS, Infosys, Amazon), university exams (CS/IT), and technical certifications.

Target Readiness
Placements & Job Interviews
Question Format
Multiple-Choice with Explanations
Explained Solutions
Written explanations

C++ Fundamentals Quiz

10 questions · Interactive timed test · Instant score & review

C++ MCQ Questions

Page 1 of 1 (47 total)
Q1

What is the purpose of a constructor in C++?

easy
View Answer & Explanation
Correct Answer: To initialize an object's data members automatically when the object is created
Explanation:
A constructor shares the same name as its class and runs automatically whenever a new object is instantiated, ensuring the object starts in a valid, properly initialized state.
Q2

What is the difference between a pointer and a reference in C++?

medium
View Answer & Explanation
Correct Answer: A pointer can be reassigned to point to a different variable and can be null, while a reference must be initialized when declared and cannot be changed to refer to another variable
Explanation:
Because a reference is essentially an alias for an existing variable, it must be bound to something at the moment it is created, whereas a pointer is a separate variable that stores a memory address and can be reassigned or left null.
Q3

What is a virtual function in C++?

hard
View Answer & Explanation
Correct Answer: A member function that can be overridden in a derived class and is resolved at runtime based on the actual object type
Explanation:
Declaring a function as virtual in a base class enables runtime polymorphism, meaning that calling the function through a base class pointer will invoke the derived class's version if the object is actually of that derived type.
Q4

What is the purpose of a destructor in C++?

medium
View Answer & Explanation
Correct Answer: To automatically release resources and clean up when an object goes out of scope or is deleted
Explanation:
A destructor shares the class name preceded by a tilde and is called automatically, making it the natural place to release dynamically allocated memory or other resources the object acquired during its lifetime.
Q5

What is function overloading in C++?

easy
View Answer & Explanation
Correct Answer: Defining multiple functions with the same name but different parameter types or counts within the same scope
Explanation:
The compiler determines which overloaded version to call based on the number and types of arguments provided at the call site, resolving this at compile time rather than at runtime.
Q6

What is the purpose of the 'const' keyword when applied to a function parameter in C++?

medium
View Answer & Explanation
Correct Answer: It prevents the function from modifying the value of that parameter
Explanation:
Marking a parameter as const is a way of documenting and enforcing that a function will not change the value passed to it, which is especially useful when passing large objects by reference to avoid unnecessary copying while still guaranteeing they remain unmodified.
Q7

What is the diamond problem in C++?

hard
View Answer & Explanation
Correct Answer: An ambiguity that arises when a class inherits from two classes that both inherit from a common base class
Explanation:
When two parent classes both inherit from the same base class and a child class inherits from both parents, the compiler can end up with two separate copies of the base class, creating ambiguity that is typically resolved using virtual inheritance.
Q8

What is the purpose of the 'new' operator in C++?

medium
View Answer & Explanation
Correct Answer: To dynamically allocate memory on the heap for an object or array at runtime
Explanation:
Memory allocated with 'new' persists until it is explicitly released using 'delete', which is different from stack allocated variables that are automatically cleaned up when they go out of scope.
Q9

What is operator overloading in C++?

hard
View Answer & Explanation
Correct Answer: The ability to redefine how standard operators, such as +, behave when used with user-defined types
Explanation:
Operator overloading allows objects of a custom class to be used with familiar syntax, such as adding two custom Vector objects together using the + symbol, by defining what that operation should actually do for that class.
Q10

What is the purpose of the 'this' pointer in C++?

medium
View Answer & Explanation
Correct Answer: It points to the current object on which a member function is being called
Explanation:
Every non-static member function has access to an implicit 'this' pointer, which is useful for distinguishing member variables from parameters with the same name or for returning a reference to the current object.
Q11

What is a template in C++ used for?

hard
View Answer & Explanation
Correct Answer: To write generic, reusable functions or classes that work with any data type without rewriting code for each type
Explanation:
Templates let a single function or class definition work with multiple data types, with the actual type substituted in at compile time, which avoids duplicating nearly identical code for each type it needs to support.
Q12

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

medium
View Answer & Explanation
Correct Answer: Members of a struct are public by default, while members of a class are private by default
Explanation:
Aside from this default access level difference, struct and class behave almost identically in C++, and both can have constructors, member functions, and support inheritance.
Q13

What does the C++ Standard Template Library (STL) provide?

medium
View Answer & Explanation
Correct Answer: A collection of pre-built generic classes and functions, such as containers, iterators, and algorithms
Explanation:
The STL includes commonly needed data structures like vector, map, and list, along with algorithms for sorting or searching, which saves developers from having to reimplement these fundamental building blocks from scratch.
Q14

What is the purpose of the 'friend' keyword in C++?

hard
View Answer & Explanation
Correct Answer: It grants a function or class access to the private and protected members of another class
Explanation:
Declaring a function or class as a friend breaks normal encapsulation rules deliberately, allowing that specific function or class to reach into another class's private data when there is a legitimate need for tight coupling between them.
Q15

What happens if a class in C++ does not explicitly define a destructor?

medium
View Answer & Explanation
Correct Answer: The compiler automatically generates a default destructor that performs basic cleanup
Explanation:
This compiler generated default destructor calls the destructors of the class's member objects but does not automatically release dynamically allocated memory, which is why classes that use 'new' typically need to define their own destructor.
Q16

What is the difference between pass by value and pass by reference in C++ function calls?

medium
View Answer & Explanation
Correct Answer: Pass by value creates a copy of the argument, while pass by reference allows the function to modify the original variable directly
Explanation:
When an argument is passed by value, any changes made inside the function do not affect the original variable, while passing by reference gives the function direct access to the original variable's memory location.
Q17

What is the purpose of namespaces in C++?

medium
View Answer & Explanation
Correct Answer: To group related identifiers together and avoid naming conflicts between different parts of a program
Explanation:
Namespaces are especially useful in larger projects or when combining multiple libraries, since they let two functions or variables share the same name as long as they belong to different namespaces.
Q18

What is a smart pointer in modern C++?

hard
View Answer & Explanation
Correct Answer: An object that automatically manages the memory of a dynamically allocated resource, releasing it when no longer needed
Explanation:
Smart pointers, such as unique_ptr and shared_ptr, wrap a raw pointer and automatically handle deletion when the smart pointer itself goes out of scope, which greatly reduces the risk of memory leaks compared to manual memory management.
Q19

What is the difference between compile-time polymorphism and runtime polymorphism in C++?

hard
View Answer & Explanation
Correct Answer: Compile-time polymorphism, such as function overloading, is resolved during compilation, while runtime polymorphism, such as virtual functions, is resolved during program execution
Explanation:
Function overloading and templates are examples of compile-time polymorphism because the compiler decides which version to use before the program even runs, while virtual functions rely on the actual object type at runtime to decide which implementation to call.
Q20

What is the purpose of an inline function in C++?

hard
View Answer & Explanation
Correct Answer: To suggest to the compiler that the function's code be inserted directly at the call site to potentially reduce function call overhead
Explanation:
Marking a small, frequently called function as inline is a hint to the compiler, though the compiler is free to ignore it, particularly for larger functions where inlining would bloat the compiled code without much performance benefit.
Q21

What is the purpose of a copy constructor in C++?

medium
View Answer & Explanation
Correct Answer: It creates a new object as a copy of an existing object of the same class
Explanation:
A copy constructor is automatically generated by the compiler if not explicitly defined, but classes that manage resources like dynamic memory often need a custom one to avoid shallow copy issues.
Q22

What is the difference between 'malloc' and 'new' in C++?

medium
View Answer & Explanation
Correct Answer: 'new' calls the constructor of an object and returns a typed pointer, while 'malloc' only allocates raw memory without initialization
Explanation:
Because 'new' is aware of object types and constructors, it is generally preferred in C++ code, while 'malloc' is more of a holdover from C style memory management.
Q23

What is a pure virtual function in C++?

medium
View Answer & Explanation
Correct Answer: A virtual function with no implementation in the base class, forcing derived classes to provide one
Explanation:
A class containing at least one pure virtual function becomes an abstract class, meaning it cannot be instantiated directly and must be inherited from.
Q24

What is the purpose of the 'explicit' keyword in a C++ constructor?

hard
View Answer & Explanation
Correct Answer: It prevents the compiler from using that constructor for implicit type conversions
Explanation:
Without 'explicit', a single argument constructor could be silently invoked during an implicit conversion, sometimes leading to confusing bugs that the keyword helps avoid.
Q25

What is the difference between a shallow copy and a deep copy in C++?

hard
View Answer & Explanation
Correct Answer: A shallow copy duplicates pointer values, sharing the same underlying data, while a deep copy duplicates the actual data itself
Explanation:
If a class manages dynamically allocated memory, relying on the default shallow copy can lead to two objects pointing at and eventually double freeing the same memory.
Q26

What is the purpose of the C++ 'static_cast' operator?

hard
View Answer & Explanation
Correct Answer: It performs a compile time type conversion between related types, such as converting an int to a float
Explanation:
static_cast is generally considered safer and more explicit than old C-style casts because the compiler can catch certain kinds of invalid conversions at compile time.
Q27

What is a destructor's role when an object goes out of scope in C++?

medium
View Answer & Explanation
Correct Answer: It automatically runs to release resources the object was holding, such as dynamically allocated memory
Explanation:
This automatic cleanup is part of what makes C++'s RAII pattern possible, tying resource management directly to an object's lifetime.
Q28

What is the purpose of a C++ vector, part of the Standard Template Library?

easy
View Answer & Explanation
Correct Answer: It provides a dynamically resizable array that can grow or shrink as elements are added or removed
Explanation:
Unlike a plain C-style array, a vector automatically handles resizing behind the scenes, which makes it a common default choice when the number of elements is not known ahead of time.
Q29

What is the purpose of the C++ 'const' keyword when applied to a member function?

medium
View Answer & Explanation
Correct Answer: It promises that the function will not modify any of the object's member variables
Explanation:
A const member function can be called on both const and non-const objects, while a non-const member function can only be called on non-const objects.
Q30

What is the difference between function overloading and function overriding in C++?

medium
View Answer & Explanation
Correct Answer: Overloading involves multiple functions with the same name but different parameters, while overriding replaces a base class virtual function's implementation in a derived class
Explanation:
Overloading is resolved at compile time based on the arguments provided, while overriding relies on virtual functions and is resolved at runtime based on the actual object type.
Q31

What is the purpose of an initializer list in a C++ constructor?

hard
View Answer & Explanation
Correct Answer: It initializes member variables directly before the constructor's body executes, which is required for const members and references
Explanation:
Using an initializer list is generally more efficient than assigning values inside the constructor body, since it avoids creating a member with a default value first and then reassigning it.
Q32

What does the C++ 'nullptr' keyword represent?

medium
View Answer & Explanation
Correct Answer: A type-safe null pointer constant introduced to replace the older NULL macro
Explanation:
Unlike the older NULL, which was often just defined as zero, nullptr has its own distinct type, which helps avoid certain ambiguous overload resolution problems.
Q33

What is the purpose of the C++ 'delete' operator?

medium
View Answer & Explanation
Correct Answer: It deallocates memory that was previously allocated with 'new', calling the object's destructor first
Explanation:
Forgetting to call delete on memory allocated with new is a common source of memory leaks, since the memory remains reserved even after it is no longer needed.
Q34

What is the purpose of a C++ abstract class?

medium
View Answer & Explanation
Correct Answer: It serves as a base class that cannot be instantiated on its own and defines an interface that derived classes must implement
Explanation:
Abstract classes typically contain at least one pure virtual function, and any derived class must provide an implementation for that function before it can be instantiated.
Q35

What is the difference between 'std::cin' and 'std::cout' in C++?

easy
View Answer & Explanation
Correct Answer: 'std::cin' is used to read input from the standard input stream, while 'std::cout' is used to write output to the standard output stream
Explanation:
The insertion operator '<<' is used with cout to send data to the output, while the extraction operator '>>' is used with cin to read data from the input.
Q36

What is the purpose of the C++ 'this' pointer inside a non-static member function?

medium
View Answer & Explanation
Correct Answer: It points to the specific object instance on which the member function was called
Explanation:
Every non-static member function implicitly receives 'this' as a hidden parameter, which is how the function knows which object's data it should operate on.
Q37

What is a friend function in C++ used for?

hard
View Answer & Explanation
Correct Answer: It allows a function outside a class to access that class's private and protected members
Explanation:
Friend functions are useful in specific cases, such as overloading certain operators, where the function logically belongs outside the class but still needs internal access.
Q38

What is the purpose of the C++ Standard Template Library's 'map' container?

medium
View Answer & Explanation
Correct Answer: It stores key value pairs, automatically keeping keys sorted, and allows fast lookup by key
Explanation:
Internally, a std::map is usually implemented as a balanced binary search tree, which is why lookups, insertions, and deletions all run in logarithmic time.
Q39

What is the purpose of exception handling using try, catch, and throw in C++?

medium
View Answer & Explanation
Correct Answer: It allows a program to detect and respond to runtime errors gracefully instead of crashing abruptly
Explanation:
Code that might fail is placed inside a try block, and if an exception is thrown, control passes to a matching catch block designed to handle that specific type of error.
Q40

What is the purpose of the C++ 'sizeof' operator?

easy
View Answer & Explanation
Correct Answer: It returns the size, in bytes, that a data type or variable occupies in memory
Explanation:
sizeof is evaluated at compile time for most types, which is why it is often used to calculate the correct amount of memory to allocate for arrays or buffers.
Q41

What does RAII stand for in C++ programming, and what does it describe?

hard
View Answer & Explanation
Correct Answer: Resource Acquisition Is Initialization, a pattern where resource management is tied to object lifetime
Explanation:
With RAII, a resource such as a file handle or memory block is acquired in a constructor and automatically released in the destructor, reducing the chance of leaks.
Q42

What is the purpose of the C++ 'auto' keyword when declaring a variable?

medium
View Answer & Explanation
Correct Answer: It lets the compiler automatically deduce the variable's type based on its initializer
Explanation:
Using auto can make code shorter and easier to maintain, especially with complex types like iterators, though it still requires the variable to be initialized right away.
Q43

What is the purpose of operator overloading for the '<<' operator in C++, commonly seen with custom classes?

hard
View Answer & Explanation
Correct Answer: It allows objects of a custom class to be output directly using std::cout in a readable format
Explanation:
By overloading the insertion operator for a class, developers can write something like 'cout << myObject' and have it display meaningful information instead of a raw memory address.
Q44

What is the purpose of the C++ 'protected' access specifier?

medium
View Answer & Explanation
Correct Answer: It allows members to be accessed within the class itself and by derived classes, but not from outside code
Explanation:
Protected access is a middle ground between private and public, giving subclasses the access they often need to a parent class's internals while still hiding those details from outside code.
Q45

What happens if a base class destructor in C++ is not declared virtual, and an object is deleted through a base class pointer?

hard
View Answer & Explanation
Correct Answer: Only the base class destructor runs, potentially leaving derived class resources improperly released, leading to undefined behavior
Explanation:
This is a common source of subtle bugs in C++ inheritance hierarchies, which is why base classes intended for polymorphic use should almost always declare a virtual destructor.
Q46

Which operator cannot be overloaded in C++?

medium
View Answer & Explanation
Correct Answer: :: (Scope Resolution)
Explanation:
The scope resolution operator (::), member access (.), and ternary (?:) cannot be overloaded in C++.
Q47

Which operator cannot be overloaded in C++?

medium
View Answer & Explanation
Correct Answer: :: (Scope Resolution)
Explanation:
The scope resolution operator (::), member access (.), and ternary (?:) cannot be overloaded in C++.