C++ MCQs

1. Which of the following is a valid C++ comment?

A) // This is a comment
B) /* This is a comment */
C) # This is a comment
D) Both A and B
Answer: D) Both A and B


2. Which of the following is the correct way to declare an integer variable in C++?

A) int x;
B) integer x;
C) var x;
D) num x;
Answer: A) int x;


3. Which of the following is used for input in C++?

A) cin
B) cout
C) scanf
D) input
Answer: A) cin


4. Which of the following is used for output in C++?

A) cout
B) cin
C) printf
D) output
Answer: A) cout


5. What is the default access specifier in a C++ class?

A) private
B) public
C) protected
D) internal
Answer: A) private


6. What is the default access specifier in a C++ struct?

A) public
B) private
C) protected
D) internal
Answer: A) public


7. Which operator is used for scope resolution in C++?

A) ::
B) .
C) ->
D) :
Answer: A) ::


8. Which operator is used to access members of a class through an object pointer?

A) ->
B) .
C) *
D) ::
Answer: A) ->


9. Which of the following is correct to declare a constant variable?

A) const int x = 10;
B) int const x = 10;
C) Both A and B
D) constant int x = 10;
Answer: C) Both A and B


10. Which of the following is used for dynamic memory allocation in C++?

A) new
B) malloc
C) alloc
D) calloc
Answer: A) new


11. Which of the following is used for dynamic memory deallocation in C++?

A) delete
B) free
C) del
D) remove
Answer: A) delete


12. Which of the following is true about a constructor in C++?

A) It has the same name as the class
B) It has no return type
C) It is called automatically when an object is created
D) All of the above
Answer: D) All of the above


13. Which of the following is true about a destructor in C++?

A) It has the same name as the class with a ~ prefix
B) It has no return type
C) It is called automatically when an object is destroyed
D) All of the above
Answer: D) All of the above


14. Which keyword is used for inheritance in C++?

A) : public
B) inherit
C) extends
D) base
Answer: A) : public


15. Which of the following is a correct way to declare a pointer?

A) int *ptr;
B) int ptr*;
C) pointer int ptr;
D) int &ptr;
Answer: A) int *ptr;


16. Which operator is used to get the address of a variable?

A) &
B) *
C) #
D) @
Answer: A) &


17. Which operator is used to get the value stored at a pointer?

A) *
B) &
C) ->
D) %
Answer: A) *


18. Which of the following is a correct way to declare a reference variable?

A) int &ref = x;
B) int ref& = x;
C) reference int ref = x;
D) int ref = &x;
Answer: A) int &ref = x;


19. Which of the following is true about inline functions?

A) They suggest the compiler to replace the function call with function code
B) They improve execution speed for small functions
C) They may increase code size
D) All of the above
Answer: D) All of the above


20. Which of the following is used to handle exceptions in C++?

A) try, catch, throw
B) try, except, throw
C) catch, finally, throw
D) try, catch, finally
Answer: A) try, catch, throw


21. Which of the following is true about virtual functions?

A) They allow runtime polymorphism
B) Declared using virtual keyword
C) They can be overridden in derived classes
D) All of the above
Answer: D) All of the above


22. Which of the following is a type of polymorphism in C++?

A) Compile-time (function overloading, operator overloading)
B) Run-time (virtual functions)
C) Both A and B
D) None of the above
Answer: C) Both A and B


23. Which of the following is used to prevent inheritance of a class?

A) final keyword (C++11)
B) private access specifier
C) sealed keyword
D) protected keyword
Answer: A) final keyword (C++11)


24. Which of the following is true about friend functions?

A) They can access private and protected members of a class
B) They are not member functions
C) Declared using friend keyword
D) All of the above
Answer: D) All of the above


25. Which of the following is true about this pointer?

A) Points to the calling object
B) Cannot be modified
C) Useful in returning object references
D) All of the above
Answer: D) All of the above


26. Which of the following is a correct way to create a template function?

A) template <typename T> T add(T a, T b) { return a + b; }
B) template add<T>(T a, T b)
C) function<T> add(T a, T b)
D) template<T> add(a, b)
Answer: A) template <typename T> T add(T a, T b) { return a + b; }


27. Which of the following is a correct way to declare a class template?

A) template <class T> class MyClass { T x; };
B) class template <T> MyClass { T x; };
C) template MyClass<T> { T x; };
D) class MyClass<T> { T x; };
Answer: A) template <class T> class MyClass { T x; };


28. Which of the following is true about the Standard Template Library (STL)?

A) Contains vector, list, map, set
B) Provides algorithms like sort, find, reverse
C) Includes iterators to traverse containers
D) All of the above
Answer: D) All of the above


29. Which container is a dynamic array in STL?

A) vector
B) list
C) deque
D) set
Answer: A) vector


30. Which STL container implements a doubly linked list?

A) list
B) vector
C) deque
D) array
Answer: A) list


31. Which STL container allows fast retrieval using keys?

A) map
B) vector
C) list
D) stack
Answer: A) map


32. Which of the following is LIFO in STL?

A) stack
B) queue
C) deque
D) list
Answer: A) stack


33. Which of the following is FIFO in STL?

A) queue
B) stack
C) list
D) priority_queue
Answer: A) queue


34. Which STL container allows insertion/removal from both ends?

A) deque
B) vector
C) list
D) stack
Answer: A) deque


35. Which of the following is a C++11 feature?

A) auto keyword
B) nullptr keyword
C) Range-based for loop
D) All of the above
Answer: D) All of the above


36. Which keyword is used to prevent a function from being overridden?

A) final
B) override
C) const
D) static
Answer: A) final


37. Which keyword is used to indicate a function overrides a virtual function?

A) override
B) final
C) virtual
D) const
Answer: A) override


38. Which of the following is a smart pointer in C++11?

A) unique_ptr
B) shared_ptr
C) weak_ptr
D) All of the above
Answer: D) All of the above


39. Which of the following smart pointers ensures unique ownership?

A) unique_ptr
B) shared_ptr
C) weak_ptr
D) auto_ptr
Answer: A) unique_ptr


40. Which of the following smart pointers allows multiple ownership?

A) shared_ptr
B) unique_ptr
C) weak_ptr
D) auto_ptr
Answer: A) shared_ptr


41. Which of the following smart pointers prevents circular references?

A) weak_ptr
B) shared_ptr
C) unique_ptr
D) auto_ptr
Answer: A) weak_ptr


42. Which operator is used to overload in C++?

A) operator+()
B) operator-()
C) operator*()
D) All of the above
Answer: D) All of the above


43. Which of the following is a valid friend class declaration?

A) friend class MyClass;
B) friend MyClass;
C) friend MyClass();
D) friend class();
Answer: A) friend class MyClass;


44. Which of the following is used to define a pure virtual function?

A) virtual void func() = 0;
B) virtual void func();
C) void func() = 0;
D) pure virtual void func();
Answer: A) virtual void func() = 0;


45. Which of the following defines an abstract class in C++?

A) A class containing at least one pure virtual function
B) A class with no constructor
C) A class that cannot be instantiated
D) Both A and C
Answer: D) Both A and C


46. Which of the following is true about multiple inheritance in C++?

A) A class can inherit from more than one base class
B) It can cause ambiguity if the same member exists in multiple base classes
C) Can be resolved using scope resolution operator
D) All of the above
Answer: D) All of the above


47. Which of the following is used to prevent data from being copied?

A) Delete copy constructor or assignment operator
B) Make member variables private
C) Use static keyword
D) Use const keyword
Answer: A) Delete copy constructor or assignment operator


48. Which of the following represents RAII in C++?

A) Resource Acquisition Is Initialization
B) Memory is allocated at run-time
C) Destructor releases resources automatically
D) Both A and C
Answer: D) Both A and C


49. Which of the following is true about const member function?

A) Cannot modify member variables
B) Can be called on const objects
C) Declared using const at the end of the function
D) All of the above
Answer: D) All of the above


50. Which of the following is a new feature in C++17?

A) std::optional
B) std::variant
C) std::any
D) All of the above
Answer: D) All of the above


51. Which of the following is the correct way to declare a two-dimensional array in C++?

A) int arr[3][4];
B) int arr[3,4];
C) int arr[3][4][5];
D) int arr[3][4];[]
Answer: A) int arr[3][4];


52. Which of the following is true about function overloading?

A) Functions must have the same name but different parameter lists
B) Functions can differ by return type only
C) Functions must be in different namespaces
D) None of the above
Answer: A) Functions must have the same name but different parameter lists


53. Which of the following is true about default arguments in C++?

A) Must be declared from rightmost parameters
B) Can be applied to any parameter
C) Can be changed at runtime
D) Must be static
Answer: A) Must be declared from rightmost parameters


54. Which of the following is true about inline functions?

A) Suggested to reduce function call overhead
B) Cannot be recursive
C) Must be defined in header files
D) All of the above
Answer: D) All of the above


55. Which of the following is a valid way to declare a reference to a variable?

A) int &ref = x;
B) int ref& = x;
C) reference int ref = x;
D) int *ref = &x;
Answer: A) int &ref = x;


56. Which of the following is true about pointers to functions?

A) Can store address of a function
B) Syntax: int (*ptr)(int, int);
C) Can be called like ptr(a,b);
D) All of the above
Answer: D) All of the above


57. Which of the following is true about arrays and pointers?

A) Array name acts as a pointer to its first element
B) Pointer arithmetic can access array elements
C) Arrays and pointers are not completely the same
D) All of the above
Answer: D) All of the above


58. Which of the following is true about the const pointer?

A) const int *ptr; pointer points to a constant integer
B) int *const ptr; pointer itself is constant
C) Both A and B
D) None of the above
Answer: C) Both A and B


59. Which of the following is true about dynamic arrays in C++?

A) Allocated using new keyword
B) Can be resized manually
C) Must be deallocated using delete[]
D) A and C only
Answer: D) A and C only


60. Which of the following is true about vector in STL?

A) Dynamic size array
B) Random access is supported
C) Insertion/removal at the end is efficient
D) All of the above
Answer: D) All of the above


61. Which of the following is true about list in STL?

A) Implements doubly linked list
B) Random access is not supported
C) Efficient insertion/deletion anywhere
D) All of the above
Answer: D) All of the above


62. Which of the following is true about map in STL?

A) Stores key-value pairs
B) Keys are unique
C) Implemented as a balanced binary search tree
D) All of the above
Answer: D) All of the above


63. Which of the following is true about set in STL?

A) Stores unique elements in sorted order
B) Allows duplicate elements
C) Random access is supported
D) None of the above
Answer: A) Stores unique elements in sorted order


64. Which of the following is true about iterators in STL?

A) They provide a way to traverse containers
B) Similar to pointers
C) Can be used with algorithms like sort, find
D) All of the above
Answer: D) All of the above


65. Which of the following is true about smart pointers?

A) unique_ptr provides exclusive ownership
B) shared_ptr provides shared ownership
C) weak_ptr breaks circular references
D) All of the above
Answer: D) All of the above


66. Which of the following is true about move semantics in C++11?

A) Allows transfer of resources instead of copying
B) Uses rvalue references &&
C) Improves performance by avoiding unnecessary copies
D) All of the above
Answer: D) All of the above


67. Which of the following is true about lambda expressions?

A) Anonymous functions
B) Can capture variables by value or reference
C) Introduced in C++11
D) All of the above
Answer: D) All of the above


68. Which of the following is true about constexpr?

A) Evaluated at compile time
B) Introduced in C++11
C) Can be used for variables and functions
D) All of the above
Answer: D) All of the above


69. Which of the following is true about nullptr?

A) Represents a null pointer
B) Replaces NULL macro
C) Type-safe
D) All of the above
Answer: D) All of the above


70. Which of the following is true about auto keyword?

A) Allows compiler to deduce type automatically
B) Introduced in C++11
C) Reduces repetitive code
D) All of the above
Answer: D) All of the above


71. Which of the following is true about decltype keyword?

A) Deduces type of an expression
B) Introduced in C++11
C) Useful in templates
D) All of the above
Answer: D) All of the above


72. Which of the following is true about enum class in C++11?

A) Scoped enumerations
B) Type-safe
C) Avoids naming conflicts
D) All of the above
Answer: D) All of the above


73. Which of the following is true about static_assert in C++11?

A) Compile-time assertion
B) Checks conditions during compilation
C) Produces compilation error if false
D) All of the above
Answer: D) All of the above


74. Which of the following is true about thread in C++11?

A) Supports multithreading
B) Defined in <thread> header
C) Can be joined or detached
D) All of the above
Answer: D) All of the above


75. Which of the following is true about mutex in C++11?

A) Used for thread synchronization
B) Defined in <mutex> header
C) Prevents data races
D) All of the above
Answer: D) All of the above


76. Which of the following is true about unique_lock in C++11?

A) Flexible locking mechanism
B) Can lock and unlock multiple times
C) Works with condition variables
D) All of the above
Answer: D) All of the above


77. Which of the following is true about condition_variable in C++11?

A) Used for thread signaling
B) Defined in <condition_variable>
C) Works with mutex
D) All of the above
Answer: D) All of the above


78. Which of the following is true about forward_list in C++11?

A) Singly linked list
B) Low memory overhead
C) Cannot perform reverse traversal efficiently
D) All of the above
Answer: D) All of the above


79. Which of the following is true about array in C++11 STL?

A) Fixed-size container
B) Supports random access
C) Defined in <array> header
D) All of the above
Answer: D) All of the above


80. Which of the following is true about unordered_map?

A) Stores key-value pairs
B) Keys are unique
C) Implemented using hash tables
D) All of the above
Answer: D) All of the above

81. Which of the following is true about multimap in STL?

A) Allows multiple values for a single key
B) Keys are sorted
C) Implemented as a balanced binary search tree
D) All of the above
Answer: D) All of the above


82. Which of the following is true about multiset in STL?

A) Stores multiple elements with same value
B) Elements are sorted
C) Random access is not supported
D) All of the above
Answer: D) All of the above


83. Which of the following is true about priority_queue in STL?

A) Implements a max-heap by default
B) Supports insertion and deletion of elements
C) Top element is always the largest
D) All of the above
Answer: D) All of the above


84. Which of the following is true about std::move in C++11?

A) Casts an object to an rvalue
B) Enables move semantics
C) Improves performance by avoiding deep copy
D) All of the above
Answer: D) All of the above


85. Which of the following is true about std::forward?

A) Preserves value category (lvalue/rvalue)
B) Used in template forwarding functions
C) Introduced in C++11
D) All of the above
Answer: D) All of the above


86. Which of the following is true about decltype(auto) in C++14?

A) Deduces type based on initializer
B) Preserves references
C) Introduced in C++14
D) All of the above
Answer: D) All of the above


87. Which of the following is true about std::array vs C-style array?

A) std::array has fixed size at compile time
B) Provides member functions like size(), begin(), end()
C) Can be used in STL algorithms
D) All of the above
Answer: D) All of the above


88. Which of the following is true about std::vector vs std::list?

A) vector supports random access
B) list supports efficient insertion/deletion anywhere
C) vector may need reallocation when resized
D) All of the above
Answer: D) All of the above


89. Which of the following is true about virtual inheritance?

A) Solves diamond problem in multiple inheritance
B) Declared using virtual keyword
C) Ensures only one base class subobject exists
D) All of the above
Answer: D) All of the above


90. Which of the following is true about override keyword in C++11?

A) Ensures the function overrides a base class virtual function
B) Produces compile-time error if function does not override
C) Helps in avoiding accidental hiding of functions
D) All of the above
Answer: D) All of the above


91. Which of the following is true about final keyword in C++11?

A) Prevents a class from being inherited
B) Prevents a virtual function from being overridden
C) Both A and B
D) None of the above
Answer: C) Both A and B


92. Which of the following is true about std::tie?

A) Creates a tuple of references
B) Can be used to unpack tuple values
C) Useful with std::tuple
D) All of the above
Answer: D) All of the above


93. Which of the following is true about std::tuple?

A) Can store multiple heterogeneous elements
B) Fixed size
C) Elements accessed using std::get<N>()
D) All of the above
Answer: D) All of the above


94. Which of the following is true about std::any in C++17?

A) Can store a value of any type
B) Type-safe retrieval using any_cast
C) Useful for type-erasure
D) All of the above
Answer: D) All of the above


95. Which of the following is true about std::variant in C++17?

A) Can store one of several types
B) Type-safe access using std::get
C) Useful for alternatives
D) All of the above
Answer: D) All of the above


96. Which of the following is true about std::optional in C++17?

A) Represents a value that may or may not exist
B) Useful for functions that may not return a value
C) Type-safe alternative to nullptr or sentinel values
D) All of the above
Answer: D) All of the above


97. Which of the following is true about structured bindings in C++17?

A) Allows unpacking tuples, pairs, and arrays
B) Syntax: auto [a,b] = pair;
C) Improves readability
D) All of the above
Answer: D) All of the above


98. Which of the following is true about if constexpr in C++17?

A) Compile-time conditional
B) Code in false branch is discarded at compile-time
C) Useful in template metaprogramming
D) All of the above
Answer: D) All of the above


99. Which of the following is true about fold expressions in C++17?

A) Simplifies variadic template operations
B) Syntax: (args + ...)
C) Can perform binary operations on all arguments
D) All of the above
Answer: D) All of the above


100. Which of the following is true about inline variables in C++17?

A) Declared using inline keyword
B) Allows definition in header files
C) Avoids multiple definition linker errors
D) All of the above
Answer: D) All of the above


101. Which of the following is true about std::filesystem in C++17?

A) Provides classes and functions to work with files/directories
B) Supports path manipulation
C) Operations like create, remove, copy files
D) All of the above
Answer: D) All of the above


102. Which of the following is true about std::string_view in C++17?

A) Non-owning reference to a string
B) Efficient for passing strings to functions
C) Cannot modify the original string
D) All of the above
Answer: D) All of the above


103. Which of the following is true about range-based for loop in C++11?

A) Syntax: for(auto x: container)
B) Can iterate over arrays, vectors, lists
C) Improves readability
D) All of the above
Answer: D) All of the above


104. Which of the following is true about std::thread::detach()?

A) Allows thread to run independently
B) No need to join the thread
C) Thread resources released automatically on completion
D) All of the above
Answer: D) All of the above


105. Which of the following is true about std::thread::join()?

A) Waits for thread completion
B) Ensures resources are released properly
C) Can throw exception if already joined or detached
D) All of the above
Answer: D) All of the above


106. Which of the following is true about std::async in C++11?

A) Launches a task asynchronously
B) Returns a std::future object
C) Can use std::launch::async or std::launch::deferred
D) All of the above
Answer: D) All of the above


107. Which of the following is true about std::future in C++11?

A) Holds a result of asynchronous computation
B) Retrieved using .get()
C) Can check status using .valid()
D) All of the above
Answer: D) All of the above


108. Which of the following is true about std::promise in C++11?

A) Used to set value for std::future
B) Enables communication between threads
C) Works with std::thread
D) All of the above
Answer: D) All of the above


109. Which of the following is true about std::lock_guard?

A) RAII-based mutex wrapper
B) Locks mutex on creation
C) Unlocks mutex on destruction
D) All of the above
Answer: D) All of the above


110. Which of the following is true about std::scoped_lock in C++17?

A) Can lock multiple mutexes simultaneously
B) Prevents deadlock using lock hierarchy
C) RAII-based
D) All of the above
Answer: D) All of the above

111. Which of the following STL algorithms sorts a container in ascending order?

A) std::sort(begin, end)
B) std::reverse(begin, end)
C) std::min_element(begin, end)
D) std::max_element(begin, end)
Answer: A) std::sort(begin, end)


112. Which STL algorithm reverses the elements in a container?

A) std::reverse(begin, end)
B) std::sort(begin, end)
C) std::rotate(begin, mid, end)
D) std::shuffle(begin, end)
Answer: A) std::reverse(begin, end)


113. Which STL algorithm removes consecutive duplicate elements?

A) std::unique(begin, end)
B) std::remove(begin, end, value)
C) std::erase(begin, end)
D) std::distinct(begin, end)
Answer: A) std::unique(begin, end)


114. Which STL algorithm merges two sorted ranges?

A) std::merge(begin1, end1, begin2, end2, output)
B) std::accumulate(begin, end)
C) std::copy(begin, end, output)
D) std::combine(begin1, end1, begin2, end2)
Answer: A) std::merge(begin1, end1, begin2, end2, output)


115. Which STL algorithm applies a function to each element of a container?

A) std::for_each(begin, end, func)
B) std::transform(begin, end, output, func)
C) std::accumulate(begin, end, init)
D) std::apply(func, container)
Answer: A) std::for_each(begin, end, func)


116. Which STL algorithm transforms elements and stores results in another container?

A) std::transform(begin, end, output, func)
B) std::for_each(begin, end, func)
C) std::copy(begin, end, output)
D) std::accumulate(begin, end, init)
Answer: A) std::transform(begin, end, output, func)


117. Which STL algorithm finds the minimum element in a range?

A) std::min_element(begin, end)
B) std::max_element(begin, end)
C) std::find(begin, end, value)
D) std::lower_bound(begin, end, value)
Answer: A) std::min_element(begin, end)


118. Which STL algorithm finds the maximum element in a range?

A) std::max_element(begin, end)
B) std::min_element(begin, end)
C) std::find(begin, end, value)
D) std::upper_bound(begin, end, value)
Answer: A) std::max_element(begin, end)


119. Which STL algorithm counts occurrences of a value in a range?

A) std::count(begin, end, value)
B) std::find(begin, end, value)
C) std::accumulate(begin, end, init)
D) std::count_if(begin, end, predicate)
Answer: A) std::count(begin, end, value)


120. Which STL algorithm counts elements satisfying a condition?

A) std::count_if(begin, end, predicate)
B) std::count(begin, end, value)
C) std::find_if(begin, end, predicate)
D) std::filter(begin, end, predicate)
Answer: A) std::count_if(begin, end, predicate)


121. Which STL algorithm removes elements equal to a value?

A) std::remove(begin, end, value)
B) std::erase(begin, end, value)
C) std::delete(begin, end, value)
D) std::remove_if(begin, end, predicate)
Answer: A) std::remove(begin, end, value)


122. Which STL algorithm removes elements satisfying a condition?

A) std::remove_if(begin, end, predicate)
B) std::remove(begin, end, value)
C) std::erase(begin, end, value)
D) std::delete_if(begin, end, predicate)
Answer: A) std::remove_if(begin, end, predicate)


123. Which of the following is true about std::optional in C++17?

A) Represents an optional value
B) Can be checked using .has_value()
C) Accessed using .value()
D) All of the above
Answer: D) All of the above


124. Which of the following is true about C++20 concepts?

A) Constrain template parameters
B) Improves template readability and error messages
C) Declared using concept keyword
D) All of the above
Answer: D) All of the above


125. Which of the following is true about C++20 ranges?

A) Allow functional-style processing of containers
B) Includes views and adaptors
C) Introduced in <ranges> header
D) All of the above
Answer: D) All of the above


126. Which of the following is true about std::span in C++20?

A) Non-owning view of an array or container
B) Can access elements like array
C) Allows safe slicing of arrays
D) All of the above
Answer: D) All of the above


127. Which of the following is true about C++20 coroutines?

A) Functions can suspend and resume execution
B) Declared using co_await, co_yield, co_return
C) Improves asynchronous programming
D) All of the above
Answer: D) All of the above


128. Which of the following is true about std::jthread in C++20?

A) Automatically joins thread on destruction
B) Simplifies thread management
C) Introduced in C++20
D) All of the above
Answer: D) All of the above


129. Which of the following is true about std::stop_token in C++20?

A) Used to request thread cancellation
B) Works with std::jthread
C) Allows cooperative thread stopping
D) All of the above
Answer: D) All of the above


130. Which of the following is true about modules in C++20?

A) Replace traditional header files
B) Improves compilation speed
C) Declared using module and import keywords
D) All of the above
Answer: D) All of the above


131. Which of the following is true about concept usage in templates?

A) Can constrain type parameters
B) Improves compile-time error diagnostics
C) Syntax: template <Integral T>
D) All of the above
Answer: D) All of the above


132. Which STL algorithm checks if all elements satisfy a condition?

A) std::all_of(begin, end, predicate)
B) std::any_of(begin, end, predicate)
C) std::none_of(begin, end, predicate)
D) std::count_if(begin, end, predicate)
Answer: A) std::all_of(begin, end, predicate)


133. Which STL algorithm checks if any element satisfies a condition?

A) std::any_of(begin, end, predicate)
B) std::all_of(begin, end, predicate)
C) std::none_of(begin, end, predicate)
D) std::count_if(begin, end, predicate)
Answer: A) std::any_of(begin, end, predicate)


134. Which STL algorithm checks if no element satisfies a condition?

A) std::none_of(begin, end, predicate)
B) std::all_of(begin, end, predicate)
C) std::any_of(begin, end, predicate)
D) std::count_if(begin, end, predicate)
Answer: A) std::none_of(begin, end, predicate)


135. Which STL algorithm swaps two elements?

A) std::swap(a, b)
B) std::exchange(a, b)
C) std::replace(a, b)
D) std::move(a, b)
Answer: A) std::swap(a, b)


136. Which STL algorithm copies elements from one container to another?

A) std::copy(begin, end, output)
B) std::transform(begin, end, output, func)
C) std::move(begin, end, output)
D) Both A and C
Answer: D) Both A and C


137. Which STL algorithm rotates elements in a range?

A) std::rotate(begin, mid, end)
B) std::reverse(begin, end)
C) std::shuffle(begin, end)
D) std::shift(begin, end)
Answer: A) std::rotate(begin, mid, end)


138. Which STL algorithm randomizes the order of elements?

A) std::shuffle(begin, end, rng)
B) std::random_shuffle(begin, end)
C) Both A and B
D) None of the above
Answer: C) Both A and B


139. Which STL algorithm finds an element equal to a value?

A) std::find(begin, end, value)
B) std::find_if(begin, end, predicate)
C) std::binary_search(begin, end, value)
D) All of the above
Answer: A) std::find(begin, end, value)


140. Which STL algorithm finds an element satisfying a condition?

A) std::find_if(begin, end, predicate)
B) std::find(begin, end, value)
C) std::binary_search(begin, end, value)
D) All of the above
Answer: A) std::find_if(begin, end, predicate)


141. Which STL algorithm finds the first element not satisfying a condition?

A) std::find_if_not(begin, end, predicate)
B) std::find(begin, end, value)
C) std::binary_search(begin, end, value)
D) All of the above
Answer: A) std::find_if_not(begin, end, predicate)


142. Which STL algorithm performs a binary search in a sorted range?

A) std::binary_search(begin, end, value)
B) std::find(begin, end, value)
C) std::lower_bound(begin, end, value)
D) std::upper_bound(begin, end, value)
Answer: A) std::binary_search(begin, end, value)


143. Which STL algorithm returns an iterator to the first element not less than a value?

A) std::lower_bound(begin, end, value)
B) std::upper_bound(begin, end, value)
C) std::binary_search(begin, end, value)
D) std::find(begin, end, value)
Answer: A) std::lower_bound(begin, end, value)


144. Which STL algorithm returns an iterator to the first element greater than a value?

A) std::upper_bound(begin, end, value)
B) std::lower_bound(begin, end, value)
C) std::binary_search(begin, end, value)
D) std::find(begin, end, value)
Answer: A) std::upper_bound(begin, end, value)


145. Which STL algorithm merges two sorted ranges in-place?

A) std::inplace_merge(begin, mid, end)
B) std::merge(begin1, end1, begin2, end2, output)
C) std::rotate(begin, mid, end)
D) std::shuffle(begin, end)
Answer: A) std::inplace_merge(begin, mid, end)


146. Which of the following is true about coroutines return type?

A) Can return std::generator or std::future
B) Can use co_await, co_yield
C) Execution can be suspended and resumed
D) All of the above
Answer: D) All of the above


147. Which C++20 feature allows checking types and expressions at compile-time?

A) Concepts
B) static_assert
C) if constexpr
D) All of the above
Answer: D) All of the above


148. Which of the following is true about std::bitset?

A) Represents a fixed-size sequence of bits
B) Supports bitwise operations
C) Can convert to/from integers and strings
D) All of the above
Answer: D) All of the above


149. Which STL container supports constant time insertion at the front and back?

A) deque
B) vector
C) list
D) array
Answer: A) deque


150. Which of the following is true about std::stack adapter?

A) LIFO data structure
B) Implemented on top of deque or vector
C) Provides push(), pop(), top()
D) All of the above
Answer: D) All of the above

151. Which of the following is true about operator overloading in C++?

A) Allows redefining operator behavior for user-defined types
B) Can overload all operators including .
C) Can overload only binary operators
D) Only arithmetic operators can be overloaded
Answer: A) Allows redefining operator behavior for user-defined types


152. Which operator cannot be overloaded in C++?

A) :: (scope resolution)
B) + (addition)
C) [] (subscript)
D) * (multiplication)
Answer: A) :: (scope resolution)


153. Which operator can be overloaded as a member function only?

A) Assignment =
B) Addition +
C) Subscript []
D) Function call ()
Answer: A) Assignment =


154. Which operator can be overloaded as either member or non-member function?

A) +
B) =
C) ->
D) .
Answer: A) +


155. Which of the following is true about virtual functions?

A) Enable runtime polymorphism
B) Declared using virtual keyword
C) Can be overridden in derived class
D) All of the above
Answer: D) All of the above


156. Which of the following is true about pure virtual functions?

A) Declared using = 0
B) Must be overridden in derived classes to instantiate objects
C) Makes the class abstract
D) All of the above
Answer: D) All of the above


157. Which of the following statements is true about abstract classes?

A) Cannot be instantiated
B) Can have constructors
C) Can contain pure virtual functions
D) All of the above
Answer: D) All of the above


158. Which of the following is true about multiple inheritance?

A) A derived class inherits from more than one base class
B) May lead to ambiguity if base classes have the same member
C) Ambiguity can be resolved using scope resolution operator
D) All of the above
Answer: D) All of the above


159. Which of the following solves the diamond problem in multiple inheritance?

A) Virtual inheritance
B) Pure virtual functions
C) Operator overloading
D) Abstract classes
Answer: A) Virtual inheritance


160. Which keyword is used to declare virtual inheritance?

A) virtual
B) override
C) final
D) abstract
Answer: A) virtual


161. Which of the following is true about the final keyword in C++11?

A) Prevents a class from being inherited
B) Prevents a virtual function from being overridden
C) Both A and B
D) None of the above
Answer: C) Both A and B


162. Which of the following is true about the override keyword in C++11?

A) Ensures that a function overrides a virtual function
B) Produces compile-time error if function does not override
C) Improves code safety
D) All of the above
Answer: D) All of the above


163. Which of the following is true about this pointer in C++?

A) Points to the calling object
B) Cannot be modified
C) Can be used to return object reference from member function
D) All of the above
Answer: D) All of the above


164. Which of the following is true about friend functions?

A) Can access private and protected members of a class
B) Are not member functions
C) Declared using friend keyword
D) All of the above
Answer: D) All of the above


165. Which of the following is true about friend classes?

A) Can access private and protected members of another class
B) Declared using friend class
C) Are independent classes
D) All of the above
Answer: D) All of the above


166. Which of the following is true about copy constructor?

A) Initializes a new object from another object of the same class
B) Has one parameter of type reference to the same class
C) Can be compiler-provided or user-defined
D) All of the above
Answer: D) All of the above


167. Which of the following is true about assignment operator overloading?

A) Syntax: ClassName& operator=(const ClassName &rhs)
B) Used for deep copying objects
C) Must return reference to the object
D) All of the above
Answer: D) All of the above


168. Which of the following is true about destructors in C++?

A) Have the same name as class with ~ prefix
B) No return type
C) Automatically called when an object goes out of scope
D) All of the above
Answer: D) All of the above


169. Which of the following is true about virtual destructors?

A) Ensures correct destructor call in case of polymorphism
B) Declared using virtual keyword
C) Prevents resource leak
D) All of the above
Answer: D) All of the above


170. Which of the following is true about type casting in C++?

A) static_cast performs compile-time cast
B) dynamic_cast performs runtime safe cast
C) reinterpret_cast casts one type to unrelated type
D) All of the above
Answer: D) All of the above


171. Which of the following is true about dynamic casting?

A) Used with polymorphic types
B) Returns nullptr if cast fails (for pointers)
C) Used to safely downcast in inheritance hierarchy
D) All of the above
Answer: D) All of the above


172. Which of the following is true about const_cast in C++?

A) Used to add or remove const qualifier
B) Does not change actual object type
C) Can cast pointers or references
D) All of the above
Answer: D) All of the above


173. Which of the following is true about multiple inheritance ambiguity?

A) Occurs when two base classes have members with the same name
B) Resolved using scope resolution operator
C) Can be prevented using virtual inheritance
D) All of the above
Answer: D) All of the above


174. Which of the following is true about diamond problem?

A) Occurs in multiple inheritance when two base classes inherit from the same class
B) Can lead to multiple copies of base class
C) Resolved using virtual inheritance
D) All of the above
Answer: D) All of the above


175. Which of the following is true about abstract base class?

A) Cannot be instantiated
B) Can contain pure virtual functions
C) Can provide some concrete member functions
D) All of the above
Answer: D) All of the above


176. Which of the following is true about polymorphism in C++?

A) Compile-time polymorphism includes function overloading and operator overloading
B) Run-time polymorphism uses virtual functions
C) Both A and B
D) None of the above
Answer: C) Both A and B


177. Which of the following is true about constructor overloading?

A) A class can have multiple constructors with different parameter lists
B) Enables object initialization in multiple ways
C) Each constructor must differ in number or type of parameters
D) All of the above
Answer: D) All of the above


178. Which of the following is true about delegating constructors in C++11?

A) Allows one constructor to call another constructor
B) Reduces code duplication
C) Introduced in C++11
D) All of the above
Answer: D) All of the above


179. Which of the following is true about explicit constructors?

A) Prevents implicit conversions
B) Declared using explicit keyword
C) Useful for single-parameter constructors
D) All of the above
Answer: D) All of the above


180. Which of the following is true about virtual inheritance?

A) Only one copy of base class subobject exists
B) Avoids ambiguity in multiple inheritance
C) Declared using virtual keyword before base class
D) All of the above
Answer: D) All of the above


181. Which of the following is true about dynamic binding?

A) Function call resolved at runtime
B) Works with virtual functions
C) Enables run-time polymorphism
D) All of the above
Answer: D) All of the above


182. Which of the following is true about object slicing?

A) Occurs when derived class object assigned to base class object
B) Base part is copied, derived part is sliced
C) Avoided by using references or pointers
D) All of the above
Answer: D) All of the above


183. Which of the following is true about virtual base class constructor calls?

A) Constructor of virtual base class called by most derived class
B) Base class constructor called only once
C) Prevents multiple copies of base class
D) All of the above
Answer: D) All of the above


184. Which of the following is true about RTTI (Run-Time Type Information)?

A) Enables type identification at runtime
B) Includes typeid and dynamic_cast
C) Works only with polymorphic classes
D) All of the above
Answer: D) All of the above


185. Which of the following is true about typeid operator?

A) Returns type information of object or pointer
B) Requires <typeinfo> header
C) Works with polymorphic and non-polymorphic types
D) All of the above
Answer: D) All of the above


186. Which of the following is true about abstract classes and polymorphism?

A) Can contain pure virtual functions
B) Cannot instantiate objects
C) Enables polymorphic behavior via pointers/references
D) All of the above
Answer: D) All of the above


187. Which of the following is true about interface classes in C++?

A) Class with all pure virtual functions
B) No data members
C) Provides abstract behavior specification
D) All of the above
Answer: D) All of the above


188. Which of the following is true about virtual table (vtable)?

A) Used to implement runtime polymorphism
B) Contains addresses of virtual functions
C) One per class with virtual functions
D) All of the above
Answer: D) All of the above


189. Which of the following is true about virtual pointer (vptr)?

A) Pointer to vtable
B) Each polymorphic object has a vptr
C) Helps in dynamic dispatch of virtual functions
D) All of the above
Answer: D) All of the above


190. Which of the following is true about object layout in memory?

A) Non-static data members stored sequentially
B) Virtual function table pointer added for polymorphic classes
C) Memory alignment may add padding
D) All of the above
Answer: D) All of the above


191. Which of the following is true about final specifier on a class?

A) Class cannot be inherited
B) All virtual functions in the class can still be overridden
C) Both A and B
D) None of the above
Answer: A) Class cannot be inherited


192. Which of the following is true about override specifier on a function?

A) Ensures overriding a base class virtual function
B) Produces compile-time error if not overriding
C) Enhances code safety
D) All of the above
Answer: D) All of the above


193. Which of the following is true about covariant return types?

A) Derived class function can return pointer/reference to derived type
B) Base class function return type is pointer/reference to base type
C) Helps in overriding functions
D) All of the above
Answer: D) All of the above


194. Which of the following is true about abstract function with default implementation?

A) Pure virtual function can have definition
B) Allows derived class to call base implementation
C) Useful for template method pattern
D) All of the above
Answer: D) All of the above


195. Which of the following is true about aggregation in C++?

A) Has-a relationship
B) Container class contains object of another class
C) Container and contained object have independent lifetimes
D) All of the above
Answer: D) All of the above


196. Which of the following is true about composition in C++?

A) Has-a relationship
B) Container class owns the contained object
C) Lifetime of contained object depends on container
D) All of the above
Answer: D) All of the above


197. Which of the following is true about static member functions?

A) Belong to the class, not object
B) Cannot access non-static members directly
C) Can be called using class name
D) All of the above
Answer: D) All of the above


198. Which of the following is true about static data members?

A) Shared across all objects of the class
B) Must be defined outside the class
C) Can be accessed using class name
D) All of the above
Answer: D) All of the above


199. Which of the following is true about inline member functions?

A) Suggests compiler to replace function call with code
B) Improves performance for small functions
C) Defined inside class declaration
D) All of the above
Answer: D) All of the above


200. Which of the following is true about mutable keyword in C++?

A) Allows modification of member in const object
B) Used for caching or lazy evaluation
C) Only applicable to non-static members
D) All of the above
Answer: D) All of the above

201. Which of the following is true about function templates?

A) Allows writing generic functions
B) Declared using template<typename T>
C) Can work with multiple types
D) All of the above
Answer: D) All of the above


202. Which of the following is true about class templates?

A) Allows creating generic classes
B) Declared using template<typename T>
C) Can define member functions inside or outside the class
D) All of the above
Answer: D) All of the above


203. Which of the following is true about template specialization?

A) Provides specific implementation for particular type
B) Declared using template<>
C) Useful for optimizing certain types
D) All of the above
Answer: D) All of the above


204. Which of the following is true about variadic templates?

A) Can accept variable number of template parameters
B) Declared using template<typename... Args>
C) Introduced in C++11
D) All of the above
Answer: D) All of the above


205. Which of the following is true about template non-type parameters?

A) Template parameters can be constants, pointers, references
B) Useful for fixed-size containers
C) Declared as template<int N>
D) All of the above
Answer: D) All of the above


206. Which of the following is true about STL iterators?

A) Provide a way to traverse containers
B) Similar to pointers
C) Different types: input, output, forward, bidirectional, random-access
D) All of the above
Answer: D) All of the above


207. Which of the following is true about std::advance()?

A) Moves an iterator forward by n positions
B) Works with all iterator types
C) Changes the iterator in-place
D) All of the above
Answer: D) All of the above


208. Which of the following is true about std::distance()?

A) Returns the number of elements between two iterators
B) Works with all iterator types
C) Can be negative for bidirectional iterators
D) All of the above
Answer: D) All of the above


209. Which of the following is true about std::next() and std::prev()?

A) Returns iterator advanced or retreated by n positions
B) Does not modify original iterator
C) Introduced in C++11
D) All of the above
Answer: D) All of the above


210. Which of the following is true about std::vector iterators?

A) Random-access iterators
B) Support pointer arithmetic (+, -)
C) Can be invalidated on resizing
D) All of the above
Answer: D) All of the above


211. Which of the following is true about std::list iterators?

A) Bidirectional iterators
B) Support increment (++) and decrement (--)
C) Never invalidated on insertion or deletion at other positions
D) All of the above
Answer: D) All of the above


212. Which of the following is true about std::deque iterators?

A) Random-access iterators
B) Can be invalidated by insertion/deletion at front or back
C) Supports arithmetic and relational operators
D) All of the above
Answer: D) All of the above


213. Which STL algorithm copies elements conditionally?

A) std::copy_if(begin, end, output, predicate)
B) std::copy(begin, end, output)
C) std::transform(begin, end, output, func)
D) std::remove_if(begin, end, predicate)
Answer: A) std::copy_if(begin, end, output, predicate)


214. Which STL algorithm replaces elements satisfying a condition?

A) std::replace_if(begin, end, predicate, new_value)
B) std::replace(begin, end, old_value, new_value)
C) std::transform(begin, end, output, func)
D) Both A and B
Answer: D) Both A and B


215. Which STL algorithm applies a binary operation to two ranges?

A) std::transform(begin1, end1, begin2, output, binary_op)
B) std::for_each(begin, end, func)
C) std::accumulate(begin, end, init)
D) std::copy(begin, end, output)
Answer: A) std::transform(begin1, end1, begin2, output, binary_op)


216. Which STL algorithm rotates elements?

A) std::rotate(begin, middle, end)
B) std::reverse(begin, end)
C) std::shuffle(begin, end, rng)
D) std::rotate_copy(begin, middle, end, output)
Answer: A) std::rotate(begin, middle, end)


217. Which STL algorithm copies a rotated range to another container?

A) std::rotate_copy(begin, middle, end, output)
B) std::rotate(begin, middle, end)
C) std::copy(begin, end, output)
D) std::transform(begin, end, output, func)
Answer: A) std::rotate_copy(begin, middle, end, output)


218. Which STL algorithm reverses elements?

A) std::reverse(begin, end)
B) std::rotate(begin, middle, end)
C) std::shuffle(begin, end, rng)
D) std::reverse_copy(begin, end, output)
Answer: A) std::reverse(begin, end)


219. Which STL algorithm copies reversed elements to another container?

A) std::reverse_copy(begin, end, output)
B) std::reverse(begin, end)
C) std::copy(begin, end, output)
D) std::transform(begin, end, output, func)
Answer: A) std::reverse_copy(begin, end, output)


220. Which STL algorithm shuffles elements randomly?

A) std::shuffle(begin, end, rng)
B) std::random_shuffle(begin, end)
C) Both A and B
D) None of the above
Answer: C) Both A and B


221. Which STL algorithm performs a heap sort on a range?

A) std::sort_heap(begin, end)
B) std::make_heap(begin, end)
C) std::push_heap(begin, end)
D) std::pop_heap(begin, end)
Answer: A) std::sort_heap(begin, end)


222. Which STL algorithm transforms a range into a heap?

A) std::make_heap(begin, end)
B) std::sort_heap(begin, end)
C) std::push_heap(begin, end)
D) std::pop_heap(begin, end)
Answer: A) std::make_heap(begin, end)


223. Which STL algorithm adds an element to a heap range?

A) std::push_heap(begin, end)
B) std::pop_heap(begin, end)
C) std::make_heap(begin, end)
D) std::sort_heap(begin, end)
Answer: A) std::push_heap(begin, end)


224. Which STL algorithm removes the largest element from a heap?

A) std::pop_heap(begin, end)
B) std::push_heap(begin, end)
C) std::sort_heap(begin, end)
D) std::make_heap(begin, end)
Answer: A) std::pop_heap(begin, end)


225. Which STL algorithm finds the nth element in sorted order?

A) std::nth_element(begin, nth, end)
B) std::partial_sort(begin, nth, end)
C) std::sort(begin, end)
D) std::partition(begin, end, pred)
Answer: A) std::nth_element(begin, nth, end)


226. Which STL algorithm partially sorts a range?

A) std::partial_sort(begin, middle, end)
B) std::sort(begin, end)
C) std::nth_element(begin, nth, end)
D) std::partition(begin, end, pred)
Answer: A) std::partial_sort(begin, middle, end)


227. Which STL algorithm partitions a range based on a predicate?

A) std::partition(begin, end, pred)
B) std::sort(begin, end)
C) std::stable_partition(begin, end, pred)
D) Both A and C
Answer: D) Both A and C


228. Which STL algorithm keeps relative order of elements when partitioning?

A) std::stable_partition(begin, end, pred)
B) std::partition(begin, end, pred)
C) std::sort(begin, end)
D) std::nth_element(begin, nth, end)
Answer: A) std::stable_partition(begin, end, pred)


229. Which STL algorithm merges two sorted ranges into one sorted output?

A) std::merge(begin1, end1, begin2, end2, output)
B) std::inplace_merge(begin, mid, end)
C) std::copy(begin, end, output)
D) std::transform(begin1, end1, begin2, output, op)
Answer: A) std::merge(begin1, end1, begin2, end2, output)


230. Which STL algorithm merges two sorted ranges in-place?

A) std::inplace_merge(begin, mid, end)
B) std::merge(begin1, end1, begin2, end2, output)
C) std::sort(begin, end)
D) std::partial_sort(begin, middle, end)
Answer: A) std::inplace_merge(begin, mid, end)

You cannot copy content of this page

Scroll to Top