C++

THIS IS NOTES FOR C++

























1. What is C++?


A general-purpose programming language, e.g., supports OOP and low-level operations.



2. What is the difference between C and C++?


C++ adds OOP, templates, STL, e.g., `class` vs. `struct` in C.



3. What is a variable in C++?


A named memory location, e.g., `int x



4. What are the basic data types in C++?


`int`, `float`, `double`, `char`, `bool`, e.g., `float f



5. What is the `auto` keyword in C++11?


Type inference, e.g., `auto x



6. What is the `constexpr` keyword?


Evaluates at compile-time, e.g., `constexpr int square(int x) { return x * x; }`.



7. What is a `const` variable?


Cannot be modified, e.g., `const int MAX



8. What is the difference between `const` and `constexpr`?


`const` prevents modification; `constexpr` ensures compile-time evaluation.



9. What are arithmetic operators in C++?


`+`, `-`, `*`, `/`, `%`, e.g., `int z



10. What is the increment operator?


`++`, e.g., `x++` increases `x` by 1.



11. What is the difference between `++x` and `x++`?


`++x` increments before use; `x++` increments after use.



12. What are relational operators?


`



13. What are logical operators?


`&&`, `||`, `!`, e.g., `if (x > 0 && y < 10)`.



14. What is the ternary operator?


`?:`, e.g., `int max



15. What is a `switch` statement?


Multi-way branch, e.g., `switch (x) { case 1: break; }`.



16. What is a `break` statement?


Exits loop or switch, e.g., `break;`.



17. What is a `continue` statement?


Skips to next iteration, e.g., `continue;`.



18. What is a `while` loop?


Repeats while condition is true, e.g., `while (x < 5) { x++; }`.



19. What is a `do-while` loop?


Executes at least once, e.g., `do { x++; } while (x < 5);`.



20. What is a `for` loop?


Iterates with counter, e.g., `for (int i



21. What is a range-based `for` loop (C++11)?


Iterates over range, e.g., `for (int x : arr) { }`.



22. What is a function in C++?


A reusable code block, e.g., `int add(int a, int b) { return a + b; }`.



23. What is function overloading?


Multiple functions with same name, different parameters, e.g., `void print(int x); void print(float x);`.



24. What is a default argument?


Optional parameter, e.g., `void func(int x



25. What is an inline function?


Suggests inlining, e.g., `inline int square(int x) { return x * x; }`.







26. What is the `static` keyword for variables?


Retains value across calls, e.g., `static int count



27. What is the `extern` keyword?


Declares external variable, e.g., `extern int x;`.



28. What is the `volatile` keyword?


Prevents optimization, e.g., `volatile int flag;`.



29. What is a namespace in C++?


Groups identifiers, e.g., `namespace myns { int x; }`.



30. How do you use a namespace?


Use `::` or `using`, e.g., `myns::x` or `using namespace myns;`.



31. What is the `std` namespace?


Standard library namespace, e.g., `std::cout`.



32. What is the `using` directive?


Imports namespace, e.g., `using namespace std;`.



33. What is the danger of `using namespace std;`?


Name conflicts, e.g., clashes with custom identifiers.



34. What is the `cout` object?


Prints to console, e.g., `std::cout << \"Hello\";`.



35. What is the `cin` object?


Reads input, e.g., `std::cin >> x;`.



36. What is the `endl` manipulator?


Adds newline and flushes, e.g., `std::cout << std::endl;`.



37. What is a reference in C++?


Alias for variable, e.g., `int& ref



38. What is the difference between pass-by-value and pass-by-reference?


Value copies; reference modifies original, e.g., `void func(int& x)`.



39. What is a pointer in C++?


Stores memory address, e.g., `int* ptr



40. What is the `new` operator?


Allocates memory, e.g., `int* ptr



41. What is the `delete` operator?


Frees memory, e.g., `delete ptr;`.



42. What is a null pointer?


Points to nothing, e.g., `int* ptr



43. What is `nullptr` (C++11)?


Type-safe null pointer, e.g., `int* ptr



44. What is a constant pointer?


Pointer cannot change, e.g., `int* const ptr



45. What is a pointer to constant?


Data cannot change, e.g., `const int* ptr



46. What is a function pointer?


Points to function, e.g., `int (*func)(int)



47. What is the `sizeof` operator?


Returns type size, e.g., `sizeof(int)` returns 4 (typically).



48. What is type casting in C++?


Converts types, e.g., `float f



49. What is `static_cast`?


Safe type conversion, e.g., `int x



50. What is `dynamic_cast`?


Safe downcasting, e.g., `Derived* d







51. What is `const_cast`?


Modifies constness, e.g., `const_cast(ptr);`.



52. What is `reinterpret_cast`?


Low-level cast, e.g., `reinterpret_cast(ptr);`.



53. What is the `enum` keyword?


Defines enumeration, e.g., `enum Color { RED, GREEN };`.



54. What is a scoped enum (C++11)?


Uses `enum class`, e.g., `enum class Color { RED, GREEN };`.



55. What is a `struct` in C++?


Groups data, e.g., `struct Point { int x, y; };`.



56. What is the difference between `struct` and `class`?


`struct` defaults to public; `class` defaults to private.



57. What is a `typedef`?


Creates type alias, e.g., `typedef int MyInt;`.



58. What is `using` for type alias (C++11)?


Modern alias, e.g., `using MyInt



59. What is a macro in C++?


Preprocessor directive, e.g., `#define MAX 100`.



60. What is the `#include` directive?


Includes header, e.g., `#include `.



61. What is the `#ifndef` directive?


Prevents multiple inclusions, e.g., `#ifndef HEADER_H`.



62. What is a header guard?


Protects header, e.g., `#ifndef HEADER_H #define HEADER_H ... #endif`.



63. What is the `main` function?


Program entry point, e.g., `int main() { return 0; }`.



64. What are command-line arguments?


Passed to `main`, e.g., `int main(int argc, char* argv[])`.



65. What is a `friend` function?


Accesses private members, e.g., `friend void func(Class&);`.



66. What is a `friend` class?


Grants access to another class, e.g., `friend class Helper;`.



67. What is the `mutable` keyword?


Allows modification in `const` methods, e.g., `mutable int cache;`.



68. What is the `explicit` keyword?


Prevents implicit conversion, e.g., `explicit MyClass(int x);`.



69. What is the `decltype` keyword (C++11)?


Deduces type, e.g., `decltype(x) y;`.



70. What is the `alignas` specifier (C++11)?


Controls alignment, e.g., `alignas(16) int x;`.



71. What is the `alignof` operator (C++11)?


Returns alignment, e.g., `alignof(int)`.



72. // Pointers and Memory Management: What is a pointer?


Stores memory address, e.g., `int* ptr



73. What is a reference?


Alias for variable, e.g., `int& ref



74. What is the difference between pointer and reference?


Pointer can be reassigned; reference cannot.



75. What is a dangling pointer?


Points to freed memory, e.g., `delete ptr; *ptr;`.







76. What is a null pointer dereference?


Accessing `nullptr`, e.g., `int* ptr



77. What is pointer arithmetic?


Manipulates addresses, e.g., `ptr++` moves to next element.



78. What is a void pointer?


Points to any type, e.g., `void* ptr



79. How do you cast a void pointer?


Use `static_cast`, e.g., `int* iptr



80. What is dynamic memory allocation?


Runtime allocation, e.g., `int* ptr



81. What is a memory leak?


Unfreed memory, e.g., `new int` without `delete`.



82. How do you allocate an array dynamically?


Use `new[]`, e.g., `int* arr



83. How do you deallocate an array?


Use `delete[]`, e.g., `delete[] arr;`.



84. What is a smart pointer (C++11)?


Manages memory, e.g., `std::unique_ptr`, `std::shared_ptr`.



85. What is `std::unique_ptr`?


Single ownership, e.g., `std::unique_ptr ptr



86. What is `std::shared_ptr`?


Shared ownership, e.g., `std::shared_ptr ptr



87. What is `std::weak_ptr`?


Non-owning reference, e.g., `std::weak_ptr weak



88. What is `std::make_unique` (C++14)?


Creates `unique_ptr`, e.g., `std::make_unique(5);`.



89. What is `std::make_shared`?


Creates `shared_ptr`, e.g., `std::make_shared(5);`.



90. What is the difference between `unique_ptr` and `shared_ptr`?


`unique_ptr` has single owner; `shared_ptr` allows multiple.



91. How do you transfer ownership of a `unique_ptr`?


Use `std::move`, e.g., `unique_ptr p2



92. What is a raw pointer?


Manually managed, e.g., `int* ptr



93. Why avoid raw pointers?


Risk of leaks, e.g., forgetting `delete ptr;`.



94. What is RAII in C++?


Resource acquisition is initialization, e.g., smart pointers.



95. What is a double-free error?


Deleting pointer twice, e.g., `delete ptr; delete ptr;`.



96. What is a memory fence?


Ensures memory order, e.g., `std::atomic_thread_fence`.



97. What is stack memory?


Automatic allocation, e.g., `int x



98. What is heap memory?


Dynamic allocation, e.g., `new int;`.



99. What is the difference between stack and heap?


Stack is automatic, fast; heap is manual, slower.



100. What is a buffer overflow?


Writing beyond array, e.g., `arr[10]







101. How do you avoid buffer overflow?


Use `std::vector`, e.g., `std::vector vec(10);`.



102. What is a const pointer to const?


Neither pointer nor data changes, e.g., `const int* const ptr;`.



103. What is pointer to member?


Points to class member, e.g., `int Class::*ptr



104. What is a function pointer declaration?


Syntax, e.g., `int (*func)(int, int);`.



105. What is a callback function?


Passed as pointer, e.g., `void callback(int x) { }`.



106. How do you pass a function pointer?


As parameter, e.g., `void call(void (*cb)()) { cb(); }`.



107. What is `std::allocator`?


Manages raw memory, e.g., `std::allocator alloc;`.



108. What is placement new?


Constructs at specific address, e.g., `new (ptr) MyClass();`.



109. What is memory alignment?


Data placement for efficiency, e.g., 4-byte alignment for `int`.



110. What is a memory pool?


Pre-allocated memory, e.g., custom allocator.



111. What is the `std::addressof` function?


Gets address, e.g., `std::addressof(x);`.



112. What is a reference to pointer?


Alias to pointer, e.g., `int*& ref



113. What is a pointer to array?


Points to array, e.g., `int (*ptr)[5];`.



114. What is a pointer to function returning pointer?


Syntax, e.g., `int* (*func)();`.



115. What is the `std::move` function?


Enables move semantics, e.g., `std::move(ptr);`.



116. What is the `std::forward` function?


Preserves value category, e.g., `std::forward(arg);`.



117. What is a dangling reference?


Refers to destroyed object, e.g., `int& ref



118. How do you check for null pointer?


Compare with `nullptr`, e.g., `if (ptr



119. What is `std::bad_alloc`?


Thrown by `new` on failure, e.g., `try { new int[large]; } catch (std::bad_alloc&) { }`.



120. What is `std::nothrow`?


Prevents exception, e.g., `int* ptr



121. What is the `std::uninitialized_fill`?


Fills raw memory, e.g., `std::uninitialized_fill(ptr, ptr + n, value);`.



122. What is the `std::destroy` function (C++17)?


Destroys objects, e.g., `std::destroy(ptr, ptr + n);`.



123. What is a memory barrier?


Ensures memory operation order, e.g., in multithreading.



124. What is `std::aligned_alloc` (C++17)?


Allocates aligned memory, e.g., `std::aligned_alloc(16, size);`.



125. What is `std::launder` (C++17)?


Accesses object after placement new, e.g., `std::launder(ptr);`.







126. What is the `std::pmr` namespace (C++17)?


Polymorphic memory resources, e.g., `std::pmr::memory_resource`.



127. What is a monotonic allocator?


Non-deallocating allocator, e.g., `std::pmr::monotonic_buffer_resource`.



128. What is a segmented stack?


Dynamic stack allocation, e.g., used in coroutines.



129. What is the `std::hardware_destructive_interference_size` (C++17)?


Avoids cache line sharing, e.g., for alignment.



130. What is a memory order in C++?


Controls atomic operations, e.g., `std::memory_order_relaxed`.



131. What is `std::atomic`?


Thread-safe variable, e.g., `std::atomic x;`.



132. What is a lock-free programming?


Avoids locks, e.g., using `std::atomic`.



133. What is the `std::valarray` class?


Numeric array, e.g., `std::valarray arr(10);`.



134. What is `std::raw_storage_iterator`?


Iterates uninitialized memory, e.g., for copying.



135. What is a memory resource in C++17?


Custom memory management, e.g., `std::pmr::memory_resource`.



136. What is the `std::byte` type (C++17)?


Raw byte, e.g., `std::byte b{0xFF};`.



137. What is the `std::bit_cast` (C++20)?


Reinterprets bits, e.g., `std::bit_cast(x);`.



138. What is `std::assume_aligned` (C++20)?


Hints alignment, e.g., `std::assume_aligned<16>(ptr);`.



139. What is a custom allocator?


User-defined memory management, e.g., for `std::vector`.



140. What is the `std::construct_at` (C++20)?


Constructs object at address, e.g., `std::construct_at(ptr, value);`.



141. What is the `std::to_address` (C++20)?


Converts pointer-like to raw pointer, e.g., `std::to_address(ptr);`.



142. // Object-Oriented Programming: What is OOP in C++?


Programming using objects, e.g., encapsulation, inheritance.



143. What is a class in C++?


Blueprint for objects, e.g., `class MyClass { };`.



144. What is an object in C++?


Instance of class, e.g., `MyClass obj;`.



145. What is encapsulation?


Hiding data, e.g., `private int x; public int getX();`.



146. What is inheritance?


Class derives from another, e.g., `class Derived : public Base`.



147. What is polymorphism?


Multiple forms, e.g., virtual functions.



148. What is a virtual function?


Overridable function, e.g., `virtual void func() { }`.



149. What is a pure virtual function?


Must be overridden, e.g., `virtual void func()



150. What is an abstract class?


Has pure virtual function, e.g., `class Abstract { virtual void func()







151. What is the `override` keyword (C++11)?


Ensures override, e.g., `void func() override;`.



152. What is the `final` keyword (C++11)?


Prevents overriding, e.g., `void func() final;`.



153. What is a constructor?


Initializes object, e.g., `MyClass() { }`.



154. What is a destructor?


Cleans up object, e.g., `~MyClass() { }`.



155. What is a copy constructor?


Copies object, e.g., `MyClass(const MyClass& other);`.



156. What is a move constructor (C++11)?


Moves resources, e.g., `MyClass(MyClass&& other);`.



157. What is the rule of three?


Define destructor, copy constructor, copy assignment, e.g., for resource management.



158. What is the rule of five (C++11)?


Add move constructor, move assignment to rule of three.



159. What is the rule of zero?


Avoid custom resource management, e.g., use smart pointers.



160. What is a default constructor?


No parameters, e.g., `MyClass() { }`.



161. What is a parameterized constructor?


Has parameters, e.g., `MyClass(int x) { }`.



162. What is constructor delegation (C++11)?


Calls another constructor, e.g., `MyClass(int x) : MyClass() { }`.



163. What is a virtual destructor?


Ensures proper cleanup, e.g., `virtual ~Base() { }`.



164. Why use a virtual destructor?


Prevents undefined behavior, e.g., `delete base_ptr;`.



165. What is multiple inheritance?


Inherits multiple classes, e.g., `class C : public A, public B`.



166. What is the diamond problem?


Ambiguity in multiple inheritance, e.g., resolved by `virtual` inheritance.



167. What is virtual inheritance?


Single base instance, e.g., `class A : virtual public Base`.



168. What is a base class?


Parent class, e.g., `class Base { }`.



169. What is a derived class?


Child class, e.g., `class Derived : public Base`.



170. What is `public` inheritance?


Public members remain public, e.g., `class Derived : public Base`.



171. What is `private` inheritance?


Public members become private, e.g., `class Derived : private Base`.



172. What is `protected` inheritance?


Public members become protected, e.g., `class Derived : protected Base`.



173. What is a `this` pointer?


Points to current object, e.g., `this->x`.



174. What is a static member?


Belongs to class, e.g., `static int count;`.



175. What is a static member function?


Accesses static members, e.g., `static void func()`.







176. What is method overriding?


Redefines base method, e.g., `void func() override`.



177. What is method overloading?


Same name, different parameters, e.g., `void func(int); void func(float);`.



178. What is operator overloading?


Custom operators, e.g., `MyClass operator+(const MyClass&);`.



179. How do you overload the `<<` operator?


As friend, e.g., `friend std::ostream& operator<<(std::ostream&, const MyClass&);`.



180. What is a copy assignment operator?


Assigns object, e.g., `MyClass& operator



181. What is a move assignment operator (C++11)?


Moves resources, e.g., `MyClass& operator



182. What is the `explicit` constructor?


Prevents implicit conversion, e.g., `explicit MyClass(int x);`.



183. What is a member initializer list?


Initializes members, e.g., `MyClass(int x) : x_(x) { }`.



184. What is a constant member function?


Cannot modify object, e.g., `void func() const;`.



185. What is a mutable member?


Modifiable in `const` methods, e.g., `mutable int cache;`.



186. What is a friend function?


Accesses private members, e.g., `friend void print(const MyClass&);`.



187. What is a friend class?


Grants access, e.g., `friend class Helper;`.



188. What is a nested class?


Class within class, e.g., `class Outer { class Inner { }; };`.



189. What is a local class?


Class in function, e.g., `void func() { class Local { }; }`.



190. What is a virtual base class?


Shared in inheritance, e.g., `class A : virtual public Base`.



191. What is dynamic dispatch?


Calls virtual function, e.g., `base_ptr->func();`.



192. What is a vtable?


Virtual function table, e.g., stores function pointers.



193. What is a vptr?


Pointer to vtable, e.g., per object with virtual functions.



194. What is the `dynamic_cast` operator?


Safe downcasting, e.g., `Derived* d



195. What happens if `dynamic_cast` fails?


Returns `nullptr`, e.g., `if (dynamic_cast(base)



196. What is RTTI?


Run-time type information, e.g., `typeid`, `dynamic_cast`.



197. What is the `typeid` operator?


Returns type info, e.g., `typeid(obj).name();`.



198. What is a covariant return type?


Derived return type, e.g., `Derived* clone() override;`.



199. What is a pure virtual destructor?


Needs definition, e.g., `virtual ~Base()



200. What is an interface in C++?


Abstract class, e.g., all pure virtual functions.







201. What is a CRTP?


Curiously recurring template pattern, e.g., `class Derived : public Base`.



202. What is a constructor inheritance (C++11)?


Inherits constructors, e.g., `using Base::Base;`.



203. What is a delegating constructor?


Calls another constructor, e.g., `MyClass(int x) : MyClass() { }`.



204. What is a conversion operator?


Converts to type, e.g., `operator int() { return x; }`.



205. What is a user-defined literal (C++11)?


Custom literal, e.g., `long double operator\"\"_km(long double);`.



206. What is the `noexcept` specifier?


Promises no exceptions, e.g., `void func() noexcept;`.



207. What is a `constexpr` constructor (C++11)?


Compile-time construction, e.g., `constexpr MyClass(int x) : x_(x) { }`.



208. What is a `constexpr` member function?


Compile-time evaluation, e.g., `constexpr int get() const { return x_; }`.



209. What is a `protected` member?


Accessible in derived classes, e.g., `protected int x;`.



210. What is a `private` member?


Accessible only in class, e.g., `private int x;`.



211. What is a `public` member?


Accessible everywhere, e.g., `public int x;`.



212. What is a PIMPL idiom?


Pointer to implementation, e.g., hides implementation details.



213. What is a copy-and-swap idiom?


Safe assignment, e.g., `MyClass& operator



214. What is a non-virtual interface (NVI)?


Public non-virtual calls private virtual, e.g., for control.



215. What is a scope guard?


Ensures cleanup, e.g., RAII for resources.



216. // Standard Template Library (STL): What is the STL?


Library of containers, algorithms, iterators, e.g., `std::vector`.



217. What is a container in STL?


Stores data, e.g., `std::vector`, `std::map`.



218. What is an iterator?


Traverses container, e.g., `std::vector::iterator it;`.



219. What is `std::vector`?


Dynamic array, e.g., `std::vector vec;`.



220. How do you add an element to a `vector`?


Use `push_back()`, e.g., `vec.push_back(5);`.



221. How do you remove an element from a `vector`?


Use `erase()`, e.g., `vec.erase(vec.begin());`.



222. What is `std::array` (C++11)?


Fixed-size array, e.g., `std::array arr;`.



223. What is `std::list`?


Doubly-linked list, e.g., `std::list lst;`.



224. What is `std::deque`?


Double-ended queue, e.g., `std::deque dq;`.



225. What is `std::set`?


Sorted unique elements, e.g., `std::set s;`.







226. How do you insert into a `set`?


Use `insert()`, e.g., `s.insert(5);`.



227. What is `std::multiset`?


Sorted with duplicates, e.g., `std::multiset ms;`.



228. What is `std::map`?


Sorted key-value pairs, e.g., `std::map m;`.



229. How do you insert into a `map`?


Use `insert()` or `[]`, e.g., `m[\"key\"]



230. What is `std::multimap`?


Sorted with duplicate keys, e.g., `std::multimap mm;`.



231. What is `std::unordered_set` (C++11)?


Hashed unique elements, e.g., `std::unordered_set us;`.



232. What is `std::unordered_map` (C++11)?


Hashed key-value pairs, e.g., `std::unordered_map um;`.



233. What is `std::stack`?


LIFO container, e.g., `std::stack stk;`.



234. What is `std::queue`?


FIFO container, e.g., `std::queue q;`.



235. What is `std::priority_queue`?


Priority-based queue, e.g., `std::priority_queue pq;`.



236. How do you push to a `priority_queue`?


Use `push()`, e.g., `pq.push(5);`.



237. How do you pop from a `priority_queue`?


Use `pop()`, e.g., `pq.pop();`.



238. What is `std::begin` (C++11)?


Returns iterator to start, e.g., `std::begin(vec);`.



239. What is `std::end` (C++11)?


Returns iterator to end, e.g., `std::end(vec);`.



240. What is a reverse iterator?


Traverses backward, e.g., `std::vector::reverse_iterator rit;`.



241. What is `std::algorithm`?


STL algorithms, e.g., `std::sort`, `std::find`.



242. How do you sort a `vector`?


Use `std::sort`, e.g., `std::sort(vec.begin(), vec.end());`.



243. How do you find an element in a `vector`?


Use `std::find`, e.g., `auto it



244. What is `std::accumulate`?


Sums range, e.g., `std::accumulate(vec.begin(), vec.end(), 0);`.



245. What is `std::for_each`?


Applies function, e.g., `std::for_each(vec.begin(), vec.end(), func);`.



246. What is `std::transform`?


Applies operation, e.g., `std::transform(vec.begin(), vec.end(), vec.begin(), func);`.



247. What is `std::copy`?


Copies range, e.g., `std::copy(vec.begin(), vec.end(), out);`.



248. What is `std::move` in STL?


Moves range, e.g., `std::move(vec.begin(), vec.end(), out);`.



249. What is `std::fill`?


Fills range, e.g., `std::fill(vec.begin(), vec.end(), 0);`.



250. What is `std::replace`?


Replaces values, e.g., `std::replace(vec.begin(), vec.end(), 5, 0);`.







251. What is `std::unique`?


Removes duplicates, e.g., `std::unique(vec.begin(), vec.end());`.



252. What is `std::shuffle` (C++11)?


Randomizes range, e.g., `std::shuffle(vec.begin(), vec.end(), rng);`.



253. What is `std::next` (C++11)?


Advances iterator, e.g., `std::next(it, 2);`.



254. What is `std::distance`?


Counts elements, e.g., `std::distance(vec.begin(), vec.end());`.



255. What is `std::lower_bound`?


Finds first >



256. What is `std::upper_bound`?


Finds first > value, e.g., `std::upper_bound(vec.begin(), vec.end(), 5);`.



257. What is `std::binary_search`?


Checks if value exists, e.g., `std::binary_search(vec.begin(), vec.end(), 5);`.



258. What is `std::merge`?


Merges sorted ranges, e.g., `std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), out);`.



259. What is `std::partition`?


Groups by predicate, e.g., `std::partition(vec.begin(), vec.end(), pred);`.



260. What is `std::stable_partition`?


Preserves order, e.g., `std::stable_partition(vec.begin(), vec.end(), pred);`.



261. What is a functor?


Function object, e.g., `struct Func { bool operator()(int x) { return x > 0; } };`.



262. What is `std::function` (C++11)?


General function wrapper, e.g., `std::function f



263. What is `std::bind` (C++11)?


Binds arguments, e.g., `auto f



264. What is `std::mem_fn` (C++11)?


Wraps member function, e.g., `std::mem_fn(&MyClass::func);`.



265. What is `std::ref` (C++11)?


Passes reference, e.g., `std::ref(x);`.



266. What is `std::cref` (C++11)?


Passes const reference, e.g., `std::cref(x);`.



267. What is `std::tuple` (C++11)?


Holds multiple types, e.g., `std::tuple t(5, \"text\");`.



268. What is `std::get` for tuples?


Accesses element, e.g., `std::get<0>(t);`.



269. What is `std::tie` (C++11)?


Unpacks tuple, e.g., `std::tie(x, y)



270. What is `std::pair`?


Holds two elements, e.g., `std::pair p(5, \"text\");`.



271. What is `std::make_pair`?


Creates pair, e.g., `std::make_pair(5, \"text\");`.



272. What is `std::forward_list` (C++11)?


Singly-linked list, e.g., `std::forward_list fl;`.



273. What is `std::bitset`?


Fixed-size bit array, e.g., `std::bitset<8> bs;`.



274. What is `std::valarray`?


Numeric array, e.g., `std::valarray va(10);`.



275. What is `std::optional` (C++17)?


Optional value, e.g., `std::optional opt







276. What is `std::variant` (C++17)?


Type-safe union, e.g., `std::variant v;`.



277. What is `std::any` (C++17)?


Type-safe container, e.g., `std::any a



278. What is `std::string_view` (C++17)?


Non-owning string view, e.g., `std::string_view sv



279. What is `std::span` (C++20)?


Non-owning range view, e.g., `std::span s(arr, size);`.



280. What is `std::ranges` (C++20)?


Range-based algorithms, e.g., `std::ranges::sort(vec);`.



281. What is a range adaptor (C++20)?


Modifies range, e.g., `std::ranges::views::filter(pred);`.



282. What is `std::allocator`?


Manages memory, e.g., `std::allocator alloc;`.



283. What is `std::back_inserter`?


Inserts at end, e.g., `std::copy(src.begin(), src.end(), std::back_inserter(dest));`.



284. What is `std::inserter`?


Inserts into container, e.g., `std::inserter(set, set.begin());`.



285. What is `std::ostream_iterator`?


Outputs to stream, e.g., `std::ostream_iterator(std::cout, \" \");`.



286. What is `std::istream_iterator`?


Reads from stream, e.g., `std::istream_iterator it(std::cin);`.



287. // Templates and Generic Programming: What is a template in C++?


Generic code, e.g., `template T add(T a, T b) { return a + b; }`.



288. What is a function template?


Generic function, e.g., `template T max(T a, T b);`.



289. What is a class template?


Generic class, e.g., `template class Box { T value; };`.



290. What is template specialization?


Custom implementation, e.g., `template<> class Box { };`.



291. What is partial specialization?


Specializes subset, e.g., `template class Box { };`.



292. What is a template parameter?


Type or value, e.g., `template`.



293. What is a non-type template parameter?


Value parameter, e.g., `template class Array { int arr[N]; };`.



294. What is a default template argument?


Optional argument, e.g., `template



295. What is SFINAE?


Substitution failure is not an error, e.g., enables function overloads.



296. What is `std::enable_if` (C++11)?


Controls SFINAE, e.g., `template



297. What is `std::is_integral` (C++11)?


Checks integral type, e.g., `std::is_integral::value`.



298. What is `std::conditional` (C++11)?


Selects type, e.g., `std::conditional_t`.



299. What is a variadic template (C++11)?


Variable arguments, e.g., `template void func(Args... args);`.



300. How do you expand a variadic template?


Use `...`, e.g., `func(args...);`.







301. What is a parameter pack?


Set of template parameters, e.g., `typename... Args`.



302. What is `sizeof...` (C++11)?


Counts parameters, e.g., `sizeof...(Args)`.



303. What is a fold expression (C++17)?


Reduces pack, e.g., `(args + ...)` sums arguments.



304. What is a concept (C++20)?


Constraints type, e.g., `concept Integral



305. How do you use a concept?


With `requires`, e.g., `template requires Integral void func(T);`.



306. What is a `requires` clause (C++20)?


Specifies constraints, e.g., `requires std::is_integral_v`.



307. What is a `requires` expression?


Checks expressions, e.g., `requires { T{} + T{}; }`.



308. What is `std::integral_constant`?


Wraps constant, e.g., `std::integral_constant`.



309. What is `std::void_t` (C++17)?


Simplifies SFINAE, e.g., `template



310. What is template metaprogramming?


Compile-time computation, e.g., `template struct Factorial`.



311. What is a type trait?


Queries type properties, e.g., `std::is_same_v`.



312. What is `std::is_same` (C++11)?


Checks type equality, e.g., `std::is_same_v`.



313. What is `std::is_base_of` (C++11)?


Checks inheritance, e.g., `std::is_base_of_v`.



314. What is `std::is_convertible` (C++11)?


Checks conversion, e.g., `std::is_convertible_v`.



315. What is `std::decay` (C++11)?


Removes cv-qualifiers, e.g., `std::decay_t`.



316. What is `std::invoke` (C++17)?


Calls callable, e.g., `std::invoke(func, args...);`.



317. What is `std::apply` (C++17)?


Unpacks tuple, e.g., `std::apply(func, tuple);`.



318. What is `std::common_type` (C++11)?


Finds common type, e.g., `std::common_type_t`.



319. What is a template template parameter?


Template as parameter, e.g., `template class Container>`.



320. What is `std::tuple_size` (C++11)?


Counts tuple elements, e.g., `std::tuple_size_v>`.



321. What is `std::tuple_element` (C++11)?


Gets tuple type, e.g., `std::tuple_element_t<0, std::tuple>`.



322. What is a CRTP in templates?


Static polymorphism, e.g., `template class Base { }; class Derived : Base`.



323. What is a tag dispatch?


Selects overload, e.g., using `std::true_type`.



324. What is `std::bool_constant` (C++17)?


Wraps boolean, e.g., `std::bool_constant`.



325. What is `std::is_invocable` (C++17)?


Checks callable, e.g., `std::is_invocable_v`.







326. What is `std::invoke_result` (C++17)?


Return type of callable, e.g., `std::invoke_result_t`.



327. What is `std::disjunction` (C++17)?


Logical OR of traits, e.g., `std::disjunction_v`.



328. What is `std::conjunction` (C++17)?


Logical AND of traits, e.g., `std::conjunction_v`.



329. What is `std::negation` (C++17)?


Logical NOT of trait, e.g., `std::negation_v`.



330. What is a constrained template (C++20)?


Uses concepts, e.g., `template void func(T);`.



331. What is `std::is_constant_evaluated` (C++20)?


Checks compile-time, e.g., `if (std::is_constant_evaluated()) { }`.



332. What is a template lambda (C++20)?


Generic lambda, e.g., `[](T x) { }`.



333. What is a constrained auto (C++20)?


Uses concept, e.g., `std::integral auto x



334. What is `std::type_identity` (C++20)?


Preserves type, e.g., `std::type_identity_t`.



335. What is a compile-time loop in templates?


Recursive template, e.g., `template struct Loop { };`.



336. What is `std::index_sequence` (C++14)?


Compile-time indices, e.g., `std::index_sequence<0, 1, 2>`.



337. What is `std::make_index_sequence` (C++14)?


Generates indices, e.g., `std::make_index_sequence<3>`.



338. What is a dependent type in templates?


Type depends on parameter, e.g., `T::value_type`.



339. What is `typename` in templates?


Disambiguates dependent type, e.g., `typename T::value_type`.



340. What is a template alias (C++11)?


Simplifies template, e.g., `template using Vec



341. What is `std::declval` (C++11)?


Creates rvalue, e.g., `decltype(std::declval().func())`.



342. What is a template constraint?


Restricts template, e.g., `requires std::is_integral_v`.



343. What is `std::is_constructible` (C++11)?


Checks construction, e.g., `std::is_constructible_v`.



344. What is `std::is_trivially_constructible` (C++11)?


Checks trivial construction, e.g., `std::is_trivially_constructible_v`.



345. What is `std::is_nothrow_constructible` (C++11)?


Checks no-throw construction, e.g., `std::is_nothrow_constructible_v`.



346. What is a type alias template?


Generic alias, e.g., `template using Ptr



347. What is `std::underlying_type` (C++11)?


Gets enum type, e.g., `std::underlying_type_t`.



348. What is a template recursion?


Recursive template, e.g., `template struct Fact { static const int value



349. What is a template base class?


Template as base, e.g., `template class Base { }; class Derived : Base`.



350. // Advanced Features: What is exception handling in C++?


Managing errors, e.g., `try { } catch (std::exception& e) { }`.







351. What is a `try` block?


Contains risky code, e.g., `try { throw 5; }`.



352. What is a `catch` block?


Handles exception, e.g., `catch (int x) { }`.



353. What is the `throw` keyword?


Throws exception, e.g., `throw std::runtime_error(\"Error\");`.



354. What is `std::exception`?


Base exception class, e.g., `std::runtime_error`.



355. What is a custom exception?


Derived from `std::exception`, e.g., `class MyException : public std::exception`.



356. What is `std::bad_alloc`?


Thrown by `new` failure, e.g., `catch (std::bad_alloc&) { }`.



357. What is `std::out_of_range`?


Thrown for invalid access, e.g., `vec.at(10);`.



358. What is `noexcept` (C++11)?


Specifies no exceptions, e.g., `void func() noexcept;`.



359. What is `std::terminate`?


Called on unhandled exception, e.g., `throw` outside `try`.



360. What is `std::current_exception` (C++11)?


Accesses current exception, e.g., `std::exception_ptr p



361. What is `std::rethrow_exception` (C++11)?


Rethrows exception, e.g., `std::rethrow_exception(p);`.



362. What is a lambda expression (C++11)?


Anonymous function, e.g., `auto l



363. What is a capture in lambda?


Accesses variables, e.g., `[x]` or `[&x]`.



364. What is a capture by value?


Copies variable, e.g., `[x]`.



365. What is a capture by reference?


References variable, e.g., `[&x]`.



366. What is a default capture?


Captures all, e.g., `[



367. What is a generic lambda (C++14)?


Uses `auto`, e.g., `[](auto x) { return x; }`.



368. What is a lambda with `mutable`?


Modifies captured value, e.g., `[x]() mutable { x++; }`.



369. What is a lambda in STL?


Used in algorithms, e.g., `std::for_each(vec.begin(), vec.end(), [](int x) { });`.



370. What is move semantics (C++11)?


Transfers resources, e.g., `std::move(obj);`.



371. What is an rvalue reference?


Binds to temporary, e.g., `void func(int&& x);`.



372. What is perfect forwarding?


Preserves value category, e.g., `std::forward(arg);`.



373. What is `std::thread` (C++11)?


Manages thread, e.g., `std::thread t(func);`.



374. How do you join a thread?


Use `join()`, e.g., `t.join();`.



375. What is `std::mutex`?


Synchronizes access, e.g., `std::mutex mtx;`.







376. How do you lock a mutex?


Use `lock()`, e.g., `mtx.lock(); mtx.unlock();`.



377. What is `std::lock_guard` (C++11)?


RAII for mutex, e.g., `std::lock_guard guard(mtx);`.



378. What is `std::unique_lock` (C++11)?


Flexible mutex lock, e.g., `std::unique_lock lock(mtx);`.



379. What is `std::condition_variable`?


Synchronizes threads, e.g., `std::condition_variable cv; cv.notify_one();`.



380. What is `std::atomic` (C++11)?


Thread-safe operations, e.g., `std::atomic x;`.



381. What is `std::future` (C++11)?


Represents async result, e.g., `std::future f



382. What is `std::async` (C++11)?


Runs function asynchronously, e.g., `std::async(std::launch::async, func);`.



383. What is `std::promise` (C++11)?


Sets future value, e.g., `std::promise p; p.set_value(5);`.



384. What is a deadlock?


Threads block each other, e.g., mutual mutex locking.



385. How do you avoid deadlock?


Lock in consistent order, e.g., `std::lock(mtx1, mtx2);`.



386. What is a race condition?


Unpredictable access, e.g., unsynchronized shared data.



387. What is `std::call_once` (C++11)?


Executes once, e.g., `std::call_once(flag, func);`.



388. What is `std::shared_mutex` (C++17)?


Read-write lock, e.g., `std::shared_mutex smtx;`.



389. What is `std::jthread` (C++20)?


Auto-joining thread, e.g., `std::jthread t(func);`.



390. What is `std::latch` (C++20)?


Single-use barrier, e.g., `std::latch l(3); l.count_down();`.



391. What is `std::barrier` (C++20)?


Reusable barrier, e.g., `std::barrier b(3); b.arrive_and_wait();`.



392. What is `std::semaphore` (C++20)?


Controls access, e.g., `std::counting_semaphore<3> sem;`.



393. What is a coroutine (C++20)?


Suspendable function, e.g., `co_await`, `co_yield`.



394. What is `std::coroutine_handle` (C++20)?


Manages coroutine, e.g., `std::coroutine_handle<> h;`.



395. What is `std::filesystem` (C++17)?


File system operations, e.g., `std::filesystem::path p(\"file.txt\");`.



396. What is `std::chrono` (C++11)?


Time utilities, e.g., `std::chrono::milliseconds(100);`.



397. What is `std::random`?


Random number generation, e.g., `std::mt19937 rng;`.



398. What is `std::regex`?


Regular expressions, e.g., `std::regex r(\"\\\\d+\");`.



399. What is `std::bitset`?


Fixed-size bit array, e.g., `std::bitset<8> bs;`.



400. What is `std::complex`?


Complex numbers, e.g., `std::complex c(3, 4);`.







401. What is `std::numeric_limits`?


Type properties, e.g., `std::numeric_limits::max();`.



402. What is `std::source_location` (C++20)?


Code location, e.g., `std::source_location::current();`.



403. What is `std::format` (C++20)?


String formatting, e.g., `std::format(\"Value: {}\", x);`.



404. What is `std::bit` (C++20)?


Bit operations, e.g., `std::popcount(x);`.



405. What is `std::midpoint` (C++20)?


Safe midpoint, e.g., `std::midpoint(5, 10);`.



406. What is `std::lerp` (C++20)?


Linear interpolation, e.g., `std::lerp(a, b, t);`.



407. What is `std::execution` (C++17)?


Parallel execution, e.g., `std::execution::par`.



408. What is `std::invoke` (C++17)?


Calls callable, e.g., `std::invoke(func, args...);`.



409. What is `std::apply` (C++17)?


Unpacks tuple, e.g., `std::apply(func, tuple);`.



410. // Design Patterns and Best Practices: What is a design pattern?


Reusable solution, e.g., Singleton, Factory.



411. What is the Singleton pattern?


Single instance, e.g., `class Singleton { static Singleton& getInstance(); };`.



412. How do you implement a thread-safe Singleton?


Use `std::call_once`, e.g., `static std::once_flag flag; std::call_once(flag, init);`.



413. What is the Factory pattern?


Creates objects, e.g., `class Factory { static Product* create(); };`.



414. What is an Abstract Factory?


Creates families of objects, e.g., `class AbstractFactory { virtual Product* create()



415. What is the Observer pattern?


Notifies subscribers, e.g., `class Subject { void notify(); };`.



416. What is the Strategy pattern?


Swappable algorithms, e.g., `class Strategy { virtual void execute()



417. What is the Decorator pattern?


Extends functionality, e.g., `class Decorator : public Component { };`.



418. What is the Adapter pattern?


Converts interface, e.g., `class Adapter : public Target, private Adaptee { };`.



419. What is the Command pattern?


Encapsulates request, e.g., `class Command { virtual void execute()



420. What is the Template Method pattern?


Defines algorithm skeleton, e.g., `class Base { virtual void step()



421. What is the Visitor pattern?


Separates algorithm from structure, e.g., `class Visitor { virtual void visit(Element*)



422. What is the Builder pattern?


Constructs complex objects, e.g., `class Builder { Product* build(); };`.



423. What is the Prototype pattern?


Clones objects, e.g., `class Prototype { virtual Prototype* clone()



424. What is the Facade pattern?


Simplifies interface, e.g., `class Facade { void operation(); };`.



425. What is the Composite pattern?


Treats objects uniformly, e.g., `class Component { virtual void operation()







426. What is the Proxy pattern?


Controls access, e.g., `class Proxy : public Subject { };`.



427. What is RAII?


Resource management via scope, e.g., `std::unique_ptr`.



428. What is the Pimpl idiom?


Hides implementation, e.g., `struct MyClass::Impl; std::unique_ptr pimpl;`.



429. What is the Copy-and-Swap idiom?


Safe assignment, e.g., `MyClass& operator



430. What is the Non-Virtual Interface (NVI)?


Public non-virtual calls private virtual, e.g., `void run() { doRun(); }`.



431. What is the Scope Guard pattern?


Ensures cleanup, e.g., RAII for transactions.



432. What is the CRTP?


Static polymorphism, e.g., `class Derived : Base`.



433. What is type erasure?


Hides type details, e.g., `std::function` or custom wrapper.



434. What is the Bridge pattern?


Separates abstraction from implementation, e.g., `class Implementor { virtual void op()



435. What is the Memento pattern?


Saves state, e.g., `class Memento { State getState(); };`.



436. What is the State pattern?


Changes behavior by state, e.g., `class State { virtual void handle()



437. What is the Mediator pattern?


Centralizes communication, e.g., `class Mediator { void notify(Component*); };`.



438. What is the Chain of Responsibility pattern?


Passes request along chain, e.g., `class Handler { virtual void handle()



439. What is the Flyweight pattern?


Shares objects, e.g., `class Flyweight { shared_state; };`.



440. What is the Interpreter pattern?


Evaluates expressions, e.g., `class Expression { virtual int interpret()



441. What is a memory pool pattern?


Pre-allocates memory, e.g., `class Pool { void* allocate(); };`.



442. What is the Object Pool pattern?


Reuses objects, e.g., `class ObjectPool { Object* get(); };`.



443. What is the Null Object pattern?


Provides default behavior, e.g., `class NullObject : public Interface { void doNothing() { } };`.



444. What is the Service Locator pattern?


Centralizes services, e.g., `class ServiceLocator { Service* getService(); };`.



445. What is the Dependency Injection pattern?


Passes dependencies, e.g., `MyClass(Service* s) : service_(s) { }`.



446. What is the RAII pattern in practice?


Manages resources, e.g., `std::lock_guard` for mutex.



447. What is the Double-Checked Locking pattern?


Thread-safe initialization, e.g., `if (!ptr) { std::lock_guard lock(mtx); if (!ptr) ptr



448. What is the Policy-Based Design?


Customizes behavior via templates, e.g., `template class MyClass : Policy`.



449. What is the Mixin pattern?


Adds functionality, e.g., `class Mixin : public Base`.



450. What is the Event Loop pattern?


Handles events, e.g., `while (running) { processEvent(); }`.







451. What is the Blackboard pattern?


Central data store, e.g., `class Blackboard { void update(Key, Value); };`.



452. What is the Command Queue pattern?


Queues commands, e.g., `std::queue commands;`.



453. What is the Publish-Subscribe pattern?


Broadcasts messages, e.g., `class Publisher { void subscribe(Subscriber*); };`.



454. What is the Active Object pattern?


Decouples method execution, e.g., `class ActiveObject { void queue(Request); };`.



455. What is the Resource Acquisition Is Initialization (RAII)?


Manages lifecycle, e.g., `std::unique_ptr` for memory.



List Memory Pages
Share Via Whastapp/Facebook
Share to Your Friends

Share this protal to share friends and complete unlimited tests here. You can also make friends on our protal also start mutual competition tests with your firends easily.

Share to Facebook Share to WhatsApp Promote & Earn