1. What is the difference between stack and heap memory?
Show Answer
Ans. Stack memory is used for static memory allocation and is automatically managed. Heap memory is used for dynamic memory allocation and must be manually managed using new
and delete
in C++.
2. What is the use of the 'this' pointer in C++?
Show Answer
Ans. The this
pointer is an implicit pointer available in non-static member functions of a class. It points to the object for which the member function is called.
3. What are access specifiers in C++?
Show Answer
Ans. Access specifiers define the scope of class members. C++ has three: public
(accessible anywhere), private
(accessible only within the class), and protected
(accessible in the class and its derived classes).
4. What is operator overloading?
Show Answer
Ans. Operator overloading allows you to redefine the way operators work for user-defined types (classes). For example, you can overload the +
operator to add two objects.
5. What is function overloading?
Show Answer
Ans. Function overloading is when multiple functions have the same name but different parameters. The correct function is chosen based on the arguments passed.
6. What is a virtual function?
Show Answer
Ans. A virtual function is a member function in a base class that can be overridden in a derived class. It enables runtime polymorphism.
7. What is a pure virtual function?
Show Answer
Ans. A pure virtual function is a function declared in a base class with = 0
. It must be overridden by derived classes. A class with at least one pure virtual function is called an abstract class.
8. What is an abstract class?
Show Answer
Ans. An abstract class in C++ is a class that cannot be instantiated and contains at least one pure virtual function.
9. What is a namespace in C++?
Show Answer
Ans. A namespace is a declarative region that provides a scope to identifiers to avoid name conflicts. The standard C++ library uses the std
namespace.
10. What is exception handling in C++?
Show Answer
Ans. Exception handling allows you to handle runtime errors using try
, catch
, and throw
blocks, preventing crashes and enabling graceful error handling.
11. What are templates in C++?
Show Answer
Ans. Templates allow writing generic programs. You can write a function or class that works with any data type using template<typename T>
.
12. What is the difference between struct and class in C++?
Show Answer
Ans. In C++, both can have member functions, but the default access specifier in struct
is public
, while in class
it is private
.
13. What are static members in a class?
Show Answer
Ans. Static members belong to the class, not any object. They are shared among all instances and are accessed using the class name.
14. What is memory leak in C++?
Show Answer
Ans. A memory leak occurs when dynamically allocated memory is not properly released using delete
, leading to memory being wasted.
15. What is the difference between deep copy and shallow copy?
Show Answer
Ans. A shallow copy copies only the pointer value, not the data it points to. A deep copy creates a new copy of the data as well, preventing shared references.
16. What is the role of the 'friend' keyword?
Show Answer
Ans. A friend
function or class can access private and protected members of another class in which it is declared a friend.
17. What is function overriding?
Show Answer
Ans. Function overriding occurs when a derived class provides a specific implementation of a virtual function defined in a base class.
18. What is the use of the 'mutable' keyword?
Show Answer
Ans. The mutable
keyword allows a member of an object to be modified even if the object is declared as const
.
19. What are smart pointers in C++?
Show Answer
Ans. Smart pointers like std::unique_ptr
, std::shared_ptr
, and std::weak_ptr
automatically manage dynamic memory, reducing memory leaks.
20. What is the difference between 'new/delete' and 'malloc/free'?
Show Answer
Ans. new
and delete
are C++ operators that also call constructors and destructors. malloc
and free
are C-style functions that do not call constructors/destructors and should be avoided in C++.
21. What is the use of the 'inline' function in C++?
Show Answer
Ans. An inline
function suggests the compiler to insert the complete function code at the point of function call to reduce overhead of function call.
22. What is the difference between reference and pointer?
Show Answer
Ans. A pointer can be reassigned and can be NULL, while a reference must be initialized when declared and cannot be changed to refer to another object.
23. What is RAII in C++?
Show Answer
Ans. RAII (Resource Acquisition Is Initialization) is a programming idiom where resource allocation is tied to object lifetime, ensuring proper release in destructors.
24. What is the difference between compile-time and runtime polymorphism?
Show Answer
Ans. Compile-time polymorphism is achieved via function overloading and operator overloading. Runtime polymorphism is achieved using virtual functions.
25. What is the use of the 'explicit' keyword?
Show Answer
Ans. The explicit
keyword prevents the compiler from using a constructor for implicit conversions.
26. What is a dangling pointer?
Show Answer
Ans. A dangling pointer is a pointer that points to memory that has been deallocated or deleted.
27. What is the Standard Template Library (STL)?
Show Answer
Ans. STL is a library of generic classes and functions including containers (like vector, list), algorithms, and iterators.
28. What are iterators in C++?
Show Answer
Ans. Iterators are objects used to traverse through the elements of STL containers like vectors and lists.
29. What is the difference between vector and array?
Show Answer
Ans. An array has a fixed size and is not resizable, while a vector
is dynamic and can grow or shrink during runtime.
30. What is the 'const_cast' in C++?
Show Answer
Ans. const_cast
is used to add or remove the const
qualifier from a variable.
31. What is the 'dynamic_cast' in C++?
Show Answer
Ans. dynamic_cast
is used for safe downcasting in class hierarchies with polymorphism. It returns nullptr if the cast is invalid.
32. What is the 'static_cast' in C++?
Show Answer
Ans. static_cast
is used for conversions between related types like int to float or base to derived class pointers (if safe).
33. What is the 'reinterpret_cast' in C++?
Show Answer
Ans. reinterpret_cast
is used for low-level reinterpreting of bit patterns, like converting an int pointer to a float pointer.
34. What is the difference between 'throw' and 'noexcept'?
Show Answer
Ans. throw
is used to raise an exception, while noexcept
is a specifier that indicates a function does not throw exceptions.
35. What are function pointers in C++?
Show Answer
Ans. Function pointers are used to store the address of a function. They can be used to call functions dynamically.
36. What is a lambda expression in C++?
Show Answer
Ans. Lambda expressions allow you to define anonymous functions in place, useful especially with STL algorithms.
37. What is the use of 'auto' keyword?
Show Answer
Ans. The auto
keyword lets the compiler automatically deduce the variable's type from its initializer.
38. What is the scope resolution operator (::)?
Show Answer
Ans. The scope resolution operator ::
is used to define functions outside a class or to access global variables or class members.
39. What are bitwise operators in C++?
Show Answer
Ans. Bitwise operators operate on bits and include AND (&
), OR (|
), XOR (^
), NOT (~
), left shift (<<
), and right shift (>>
).
40. What is the difference between pre-increment and post-increment?
Show Answer
Ans. Pre-increment (++i
) increments the value before using it, while post-increment (i++
) uses the value first and then increments.
41. What is the difference between prefix and postfix increment operators?
Show Answer
Ans. Prefix (++i) increments the value before use, while postfix (i++) increments the value after use. Example: int a = 5; int b = ++a;
makes b = 6; int c = a++;
makes c = 6, a = 7.
42. What are the different types of inheritance in C++?
Show Answer
Ans. C++ supports single, multiple, multilevel, hierarchical, and hybrid inheritance.
43. What is a constructor in C++?
Show Answer
Ans. A constructor is a special member function automatically called when an object is created. It initializes the object.
44. What is a copy constructor?
Show Answer
Ans. A copy constructor is used to create a new object as a copy of an existing object. Syntax: ClassName(const ClassName &obj)
.
45. What is the Rule of Three in C++?
Show Answer
Ans. If a class defines a destructor, copy constructor, or copy assignment operator, it should define all three to manage resources properly.
46. What is the difference between override and overload?
Show Answer
Ans. Overloading means having multiple functions with the same name but different parameters. Overriding means redefining a base class’s virtual function in a derived class.
47. What is the 'explicit' keyword used for in constructors?
Show Answer
Ans. The explicit
keyword prevents automatic and implicit conversions. It is used with single-argument constructors to avoid unexpected conversions.
48. What is the significance of 'const' keyword in C++?
Show Answer
Ans. const
is used to make variables read-only. It can also be applied to function parameters and member functions to prevent modification.
49. What is the difference between reference and pointer?
Show Answer
Ans. A pointer can be reassigned and can be null. A reference must be initialized when declared and cannot be changed to refer to another object.
50. What is RAII in C++?
Show Answer
Ans. RAII (Resource Acquisition Is Initialization) is a C++ programming idiom where resource allocation is tied to object lifetime, ensuring resources are released when the object goes out of scope.
51. What are macros in C++?
Show Answer
Ans. Macros are defined using #define
and replaced by the preprocessor before compilation. They are not type-safe and should be used carefully.
52. What is the use of the inline keyword?
Show Answer
Ans. The inline
keyword suggests to the compiler to insert the function code directly into the caller’s code to reduce overhead of function calls.
53. What is dynamic dispatch?
Show Answer
Ans. Dynamic dispatch is the process of selecting which implementation of a polymorphic function to call at runtime, enabled by virtual functions in C++.
54. What is the difference between static_cast and dynamic_cast?
Show Answer
Ans. static_cast
is used for compile-time type conversions. dynamic_cast
is used for safe downcasting in polymorphic class hierarchies and requires RTTI.
55. What is the use of typeid in C++?
Show Answer
Ans. The typeid
operator is used to determine the type of an object at runtime. It returns a type_info object containing the type name.
56. What is STL in C++?
Show Answer
Ans. STL (Standard Template Library) provides generic classes and functions including containers (vector, list), algorithms (sort, find), and iterators.
57. What are iterators in C++?
Show Answer
Ans. Iterators are objects that point to elements in containers and allow traversal using operators like *
and ++
.
58. What is a lambda function in C++?
Show Answer
Ans. A lambda function is an anonymous function that can capture variables from the surrounding scope and can be used as a callback or inline logic.
59. What are move semantics in C++?
Show Answer
Ans. Move semantics allow the resources of one object to be moved to another instead of copied, improving performance. It uses rvalue references (&&
).
60. What is std::move in C++?
Show Answer
Ans. std::move
is a cast that turns an object into an rvalue, enabling move semantics. It does not actually move data; it only allows moving.
61. What is the difference between class and object in C++?
Show Answer
Ans. A class is a blueprint for creating objects. An object is an instance of a class, representing a real-world entity with state and behavior.
62. What is a virtual destructor?
Show Answer
Ans. A virtual destructor ensures the correct destructor is called for derived classes when an object is deleted through a base class pointer.
63. What is an initializer list in C++?
Show Answer
Ans. An initializer list is used in constructors to initialize class members directly before the constructor body executes.
64. What is function hiding in C++?
Show Answer
Ans. Function hiding occurs when a derived class defines a function with the same name as a base class function, hiding all overloaded versions in the base class.
65. What is a volatile keyword in C++?
Show Answer
Ans. The volatile
keyword tells the compiler not to optimize a variable, as it may change unexpectedly, often used in multithreading or hardware access.
66. What is an enum in C++?
Show Answer
Ans. An enum (enumeration) is a user-defined type consisting of a set of named integral constants, improving code readability and maintainability.
67. What is name mangling in C++?
Show Answer
Ans. Name mangling is the process by which C++ encodes function and variable names to support function overloading and namespaces.
68. What is an lvalue and rvalue in C++?
Show Answer
Ans. lvalues refer to memory locations and can appear on the left side of an assignment. rvalues are temporary and cannot be assigned to.
69. What is std::vector in C++?
Show Answer
Ans. std::vector
is a dynamic array provided by the STL that can grow or shrink in size and offers random access to elements.
70. What is the difference between emplace_back() and push_back() in vectors?
Show Answer
Ans. push_back()
adds a copy of an element, while emplace_back()
constructs the element in place, improving performance by avoiding unnecessary copies.
71. What is a functor in C++?
Show Answer
Ans. A functor is an object that acts like a function. It overloads the ()
operator and can maintain state across calls.
72. What is a segmentation fault?
Show Answer
Ans. A segmentation fault occurs when a program tries to access a restricted or unallocated area of memory, often due to pointer errors.
73. What is the difference between public, private, and protected inheritance?
Show Answer
Ans. In public inheritance, base class public/protected members retain their access level. In protected, public becomes protected. In private, both become private.
74. What is std::map in C++?
Show Answer
Ans. std::map
is an associative container that stores key-value pairs in sorted order. Each key is unique and mapped to a value.
75. What is type casting in C++?
Show Answer
Ans. Type casting is converting one data type to another. C++ supports static_cast
, dynamic_cast
, const_cast
, and reinterpret_cast
.
76. What is std::pair in C++?
Show Answer
Ans. std::pair
is a container that stores two heterogeneous objects as a single unit. It's useful for returning multiple values from functions.
77. What is the difference between delete and delete[]?
Show Answer
Ans. delete
is used for single object deallocation. delete[]
is used to deallocate arrays. Using the wrong one can cause undefined behavior.
78. What is the use of std::move_if_noexcept?
Show Answer
Ans. std::move_if_noexcept
moves an object only if the move constructor is noexcept; otherwise, it copies the object to avoid exceptions.
79. What is the diamond problem in C++?
Show Answer
Ans. The diamond problem occurs in multiple inheritance when two base classes inherit from the same base, and a class inherits from both. It’s solved using virtual inheritance.
80. What is std::unique_ptr in C++?
Show Answer
Ans. std::unique_ptr
is a smart pointer that owns and manages a dynamically allocated object. It ensures single ownership and deletes the object when out of scope.
81. What is the use of the `typeid` operator in C++?
Show Answer
Ans. The typeid
operator is used to get type information at runtime. It returns a reference to a type_info
object representing the type of the expression.
82. What is a lambda expression in C++?
Show Answer
Ans. A lambda expression is an anonymous function that can capture variables from the surrounding scope. It is useful for short, inline function definitions.
83. What is a function object (functor) in C++?
Show Answer
Ans. A functor is a class that overloads the operator()
, making its instances callable like regular functions.
84. What is the Rule of Three in C++?
Show Answer
Ans. If a class needs a user-defined destructor, copy constructor, or copy assignment operator, it likely needs all three. This is called the Rule of Three.
85. What is the Rule of Five in C++11?
Show Answer
Ans. The Rule of Five extends the Rule of Three to include the move constructor and move assignment operator introduced in C++11.
86. What is the use of `constexpr` in C++?
Show Answer
Ans. constexpr
declares that a variable or function can be evaluated at compile time, improving performance and enabling compile-time computation.
87. What is the difference between `const` and `constexpr`?
Show Answer
Ans. const
means a variable cannot be modified after initialization. constexpr
means the variable or function can be evaluated at compile time.
88. What is `std::move` used for?
Show Answer
Ans. std::move
is used to cast an object to an rvalue reference, enabling move semantics to avoid unnecessary copying.
89. What are rvalue references?
Show Answer
Ans. Rvalue references (denoted as T&&
) bind to temporary objects and are used in move semantics to optimize performance.
90. What is a delegate constructor in C++?
Show Answer
Ans. A delegate constructor calls another constructor of the same class to avoid code duplication.
91. What is `std::array` in C++?
Show Answer
Ans. std::array
is a fixed-size array container introduced in C++11 that encapsulates a C-style array and provides STL functionalities.
92. What is the difference between `std::vector` and `std::list`?
Show Answer
Ans. std::vector
provides fast random access and is contiguous in memory. std::list
is a doubly linked list providing efficient insertions/deletions but no random access.
93. What is RAII in C++?
Show Answer
Ans. RAII (Resource Acquisition Is Initialization) is a programming idiom where resource allocation is tied to object lifetime, ensuring proper cleanup via destructors.
94. What is the use of `noexcept` in C++?
Show Answer
Ans. noexcept
specifies that a function does not throw exceptions. It allows better optimization and exception guarantees.
95. What is the difference between `emplace_back` and `push_back`?
Show Answer
Ans. push_back
copies or moves an existing object into a container. emplace_back
constructs the object in-place using constructor arguments.
96. What is the diamond problem in C++?
Show Answer
Ans. The diamond problem occurs in multiple inheritance when two base classes inherit from the same base class, causing ambiguity. It is resolved using virtual inheritance.
97. What is a virtual destructor?
Show Answer
Ans. A virtual destructor ensures that the destructor of the derived class is called when deleting an object through a base class pointer.
98. What is the difference between prefix and postfix increment operators?
Show Answer
Ans. Prefix increments the value and returns the updated value. Postfix returns the original value and then increments it.
99. What is `std::map` and how is it different from `std::unordered_map`?
Show Answer
Ans. std::map
is an ordered associative container implemented as a balanced binary tree. std::unordered_map
is an unordered associative container implemented using hash tables.
100. What is the use of `std::tie` in C++?
Show Answer
Ans. std::tie
is used to unpack tuples or pair values into individual variables, typically used in structured bindings or comparisons.