50 technical C++ Interview Questions with Answers
50 technical C++ Interview Questions with Answers
● Answer:
new is an operator that initializes the object and calls the constructor, while
malloc is a function that only allocates memory and does not initialize the
object. new returns a pointer of the appropriate type, whereas malloc returns a
void*, which needs to be cast to the appropriate type.
la
● Answer:
ng
A constructor is a special member function that is automatically called when an
object is created to initialize it. A destructor is a member function that is
Si
automatically invoked when an object is destroyed, typically used for cleanup
purposes such as releasing resources.
tra
● Answer:
The only difference between struct and class in C++ is the default access
level. For struct, members are public by default, whereas for class, members
are private by default.
● Answer:
The virtual keyword is used to declare a function as virtual, enabling
polymorphism. It allows derived classes to override the function. When using a
base class pointer, the virtual function ensures that the correct function is called,
depending on the object's actual type.
5. Explain RAII (Resource Acquisition Is Initialization) in C++.
● Answer:
RAII is a C++ programming idiom in which resources like memory or file handles
are managed by objects. When an object is created, it acquires resources, and
when it goes out of scope or is destroyed, it releases them automatically,
preventing resource leaks.
● Answer:
A pure virtual function is a function in a base class that has no definition. It is
la
declared using = 0 at the end of the function declaration. A class containing a
pure virtual function is an abstract class and cannot be instantiated. Derived
ng
classes must implement this function.
Si
7. What is the difference between overloading and overriding in C++?
tra
● Answer:
Overloading occurs when multiple functions with the same name are defined but
hi
● Answer:
A smart pointer is an object in C++ that automatically manages the lifetime of
dynamically allocated memory. Smart pointers help avoid memory leaks and
dangling pointers by automatically deallocating memory when it is no longer
needed. Examples include std::unique_ptr, std::shared_ptr, and
std::weak_ptr.
9. What is the difference between a shallow copy and a deep copy?
● Answer:
A shallow copy copies the values of the members of an object, but it does not
clone dynamically allocated memory or pointed-to objects. A deep copy
duplicates everything, including dynamically allocated memory, ensuring that the
copy is independent of the original.
● Answer:
The explicit keyword is used to mark constructors or conversion operators to
prevent implicit conversions. It ensures that a constructor or operator is not used
for automatic conversions unless explicitly invoked by the programmer.
la
ng
11. What is the difference between public, private, and protected
Si
access modifiers?
● Answer:
tra
● Answer:
The this pointer is an implicit pointer passed to all non-static member functions
of a class. It points to the current instance of the class, allowing access to the
object’s members and methods.
● Answer:
A virtual destructor is necessary for ensuring that derived class destructors are
called when an object is deleted through a base class pointer. Without a virtual
destructor, only the base class destructor is called, leading to potential memory
la
leaks or undefined behavior.
ng
Si
15. Explain the concept of object slicing in C++.
● Answer:
tra
● Answer:
const means that the value of a variable cannot be changed after initialization,
but it may be computed at runtime. constexpr indicates that the value of a
variable is constant and must be computed at compile-time.
● Answer:
std::move is used to enable moving of resources from one object to another. It
casts an object to an rvalue reference, allowing resources to be transferred
without copying, improving performance, especially with large objects.
la
19. What is the difference between static_cast, dynamic_cast,
ng
const_cast, and reinterpret_cast?
Si
● Answer:
○ static_cast: Used for normal type conversions (e.g., converting
tra
● Answer:
An rvalue reference is a reference to a temporary object (rvalue) that can be
used to implement move semantics. It is declared using && (double ampersand),
allowing efficient transfer of resources from one object to another.
● Answer:
nullptr is a type-safe null pointer constant introduced in C++11. It is used to
indicate that a pointer does not point to any object or valid memory.
la
● Answer:
ng
Templates allow writing generic functions and classes that can operate with any
data type. Template classes and functions are defined using the template
Si
keyword, followed by the type parameter(s).
tra
● Answer:
C
● Answer:
std::vector is a dynamic array that can resize itself automatically as elements
are added or removed. It provides fast random access and is typically used when
the number of elements is not known in advance or changes frequently.
● Answer:
The mutable keyword allows modification of a member variable even in a
const object or const member function. It is often used when caching or lazy
initialization is needed.
la
28. What is the difference between std::list and std::vector?
● Answer:
ng
○ std::vector: Provides fast random access and is better for scenarios
Si
where elements are frequently accessed by index. However, insertions
and deletions in the middle are slow.
tra
● Answer:
C++ provides features like object-oriented programming, type safety with
stronger typing, templates for generic programming, and a standard library that
includes containers, algorithms, and iterators, making it more versatile and
powerful than C.
● Answer:
A vtable is a table of pointers to virtual functions in a class. It is used to support
runtime polymorphism. Each class with virtual functions has its own vtable, and
objects use it to determine the correct function to call for their type.
la
32. Explain the difference between override and final in C++11.
● Answer:
ng
Si
○ override: Indicates that a member function is meant to override a base
class function, ensuring at compile-time that the function signature
matches.
tra
33. What are move constructors and move assignment operators in C++?
● Answer:
Move constructors and move assignment operators are special functions that
allow resources to be transferred from one object to another, instead of copying.
This is useful for optimizing performance by avoiding deep copies. Declared
using rvalue references (&&).
● Answer:
std::string_view is a lightweight, non-owning reference to a string. It allows
efficient manipulation of strings without creating copies, making it faster for
read-only string operations.
● Answer:
○ throw(): Deprecated in C++11, it indicated that a function does not throw
exceptions.
○ noexcept: Introduced in C++11, it specifies that a function is guaranteed
not to throw exceptions.
○ noexcept(true): Explicitly indicates that the function does not throw
exceptions.
la
ng
36. What is the role of std::unordered_map in C++?
Si
● Answer:
std::unordered_map is an associative container that provides fast access to
tra
key-value pairs using a hash table. It has average constant-time complexity for
lookups, insertions, and deletions.
hi
C
● Lambda functions are often used for short, throwaway functions in algorithms.
● Answer:
○ delete: Used to deallocate memory for a single object allocated with
new.
○ delete[]: Used to deallocate memory for arrays allocated with new[].
la
40. What is a function pointer in C++?
ng
Si
Answer:
A function pointer is a pointer that stores the address of a function. It allows functions to
be passed as arguments to other functions or stored in containers.
tra
Example:
hi
funcPtr();
● Answer:
A dangling pointer is a pointer that points to memory that has been deallocated
or released. Accessing it leads to undefined behavior.
● Answer:
std::atomic provides a way to perform lock-free and thread-safe operations
on shared variables. It is commonly used in multi-threaded programs.
la
44. What are variadic templates in C++?
ng
Si
Answer:
Variadic templates allow a function or class template to accept an arbitrary number of
template arguments. Defined using ... (ellipsis).
tra
Example:
hi
template<typename... Args>
void func(Args... args) { }
C
45. What are the differences between inline and constexpr functions?
● Answer:
○ inline: Suggests to the compiler to replace function calls with the
function body to reduce overhead.
○ constexpr: Ensures that the function can be evaluated at compile-time if
the input is constant.
47. What is the difference between shallow and deep copying in C++?
● Answer:
○ Shallow Copy: Copies object structure as-is, including pointers, without
duplicating pointed-to data.
○ Deep Copy: Creates new copies of the pointed-to data, ensuring
la
independence from the original object.
ng
48. What is the rule of three in C++?
Si
● Answer:
If a class requires a user-defined destructor, copy constructor, or copy
tra
assignment operator, it likely needs all three. This is because the class manages
resources that need explicit handling.
hi
C
● Answer:
std::function is a wrapper for callable entities (functions, lambda functions,
function pointers). It provides a uniform way to store and invoke any callable
entity.
50. What are the major differences between C++11 and C++17?
● Answer:
○ C++11: Introduced features like auto, lambda expressions, nullptr,
smart pointers, and move semantics.
○ C++17: Added std::optional, std::variant, std::string_view,
structured bindings, and constexpr improvements.
la
ng
Si
tra
hi
C