1. What is an array in C++?
Correct Answer: b) A collection of elements of the same type placed in contiguous memory locations
Explanation:
In C++, a standard array is a fixed-size sequence of elements of the same data type stored in contiguous memory blocks.
2. Which of the following correctly declares an integer array of size 5 in C++?
Correct Answer: b) int arr[5];
Explanation:
The syntax to declare a built-in array in C++ is type name[size]; therefore, int arr[5]; is the correct declaration.
3. What is the index of the first element in a C++ array?
Correct Answer: b) 0
Explanation:
Arrays in C++ are zero-indexed, meaning the first element is accessed using the index 0.
4. What happens if you access an array out of its bounds in C++?
Correct Answer: c) It results in undefined behavior
Explanation:
C++ does not perform bounds checking on built-in arrays. Accessing memory outside the array bounds leads to undefined behavior, which may cause crashes or data corruption.
5. Which of the following initializes all elements of an integer array to zero?
Correct Answer: a) int arr[5] = {0};
Explanation:
Initializing an array with {0} sets the first element to 0 and value-initializes the rest to 0 as well.
6. How can you determine the number of elements in a standard C++ array named 'arr'?
Correct Answer: b) sizeof(arr) / sizeof(arr[0])
Explanation:
For a statically allocated array, sizeof(arr) gives the total bytes, and dividing by sizeof(arr[0]) gives the total number of elements.
7. When passing a built-in array to a function, what is actually passed?
Correct Answer: b) A pointer to the first element of the array
Explanation:
By default, an array decays into a pointer pointing to its first element when passed as an argument to a function.
8. Which of the following statements about C++ array sizes is true?
Correct Answer: a) The size must be a constant expression evaluated at compile time
Explanation:
For statically allocated arrays in C++, the array size must be a compile-time constant expression.
9. What is the output of sizeof() when applied to a dynamic array pointer (e.g., int* ptr = new int[10];)?
Correct Answer: b) The size of the pointer itself (e.g., 4 or 8 bytes)
Explanation:
When sizeof() is applied to a pointer, it returns the size of the pointer, not the memory dynamically allocated to it.
10. What does the declaration 'int arr[] = {1, 2, 3};' do?
Correct Answer: b) Declares an array of size 3 initialized with 1, 2, and 3
Explanation:
If the size is omitted, the compiler deduces the size of the array based on the number of elements in the initializer list.
11. Which of the following accesses the third element of the array 'arr'?
Correct Answer: b) arr[2]
Explanation:
Because arrays are zero-indexed, the first element is at index 0, the second at 1, and the third at index 2.
12. What is array decay in C++?
Correct Answer: a) The process of an array losing its size information when passed to a function
Explanation:
Array decay occurs when an array type implicitly converts to a pointer to its first element, losing its length information.
13. How do you dynamically allocate an array of 10 integers in C++?
Correct Answer: b) int* arr = new int[10];
Explanation:
The 'new' operator followed by the type and size in square brackets is used to allocate a dynamic array.
14. How must you deallocate memory allocated with 'new int[10]'?
Correct Answer: b) delete[] arr;
Explanation:
Dynamic arrays allocated with new[] must be deallocated using delete[] to ensure proper destruction and memory release.
15. In a 2D array declared as 'int matrix[3][4];', what does the '4' represent?
Correct Answer: b) The number of columns
Explanation:
In a 2D array declaration type name[rows][columns], the second dimension specifies the number of columns.
16. How are 2D arrays stored in memory in C++?
Correct Answer: b) Row-major order
Explanation:
C++ stores multidimensional arrays in row-major order, meaning elements of the same row are stored contiguously in memory.
17. Which header file is required to use std::array?
Correct Answer: c)
Explanation:
The std::array container is defined in the header file.
18. What is a primary advantage of std::array over built-in C-style arrays?
Correct Answer: b) It supports assignment semantics and knows its own size
Explanation:
std::array provides a member function size() and does not decay to a pointer, making it safer. It also supports direct assignment.
19. How do you declare a std::array of 5 integers?
Correct Answer: b) std::array<int, 5> arr;
Explanation:
The syntax for std::array specifies the type followed by the constant size inside the template brackets: std::array.
20. Which std::array member function accesses an element with bounds checking?
Correct Answer: b) at()
Explanation:
The at() function performs bounds checking and throws a std::out_of_range exception if the index is invalid. operator[] does not check bounds.
21. What does the front() method return for an array?
Correct Answer: b) A reference to the first element
Explanation:
The front() method returns a reference to the first element in the container (array, list, vector, etc.).
22. What is the equivalent of a doubly linked list in the C++ Standard Template Library (STL)?
Correct Answer: c) std::list
Explanation:
std::list is implemented as a doubly linked list, allowing fast insertions and deletions from anywhere in the container.
23. Which header is required to use std::list?
Correct Answer: b)
Explanation:
To use the std::list container in C++, the header must be included.
24. Which of the following operations has O(1) time complexity in std::list but O(N) in std::vector?
Correct Answer: b) Inserting an element in the middle (if an iterator is provided)
Explanation:
Inserting an element into a list with a known iterator takes O(1) time since it only involves changing pointers, whereas a vector requires shifting elements, taking O(N) time.
25. Does std::list support random access via the [] operator?
Correct Answer: c) No, it does not support random access
Explanation:
std::list does not support random access (operator[]) because finding an element requires traversing the list from the beginning or end.
26. Which container is a singly linked list in C++?
Correct Answer: d) std::forward_list
Explanation:
std::forward_list (introduced in C++11) is implemented as a singly linked list, which is more memory efficient than std::list.
27. What is the primary advantage of std::forward_list over std::list?
Correct Answer: c) It has lower memory overhead per node
Explanation:
std::forward_list uses only one pointer (next) per node instead of two (prev and next) used by std::list, thus saving memory.
28. Which of the following functions is available in std::list but NOT in std::forward_list?
Correct Answer: b) push_back()
Explanation:
std::forward_list does not have push_back() or back() because traversing to the end of a singly linked list to append takes O(N) time without a tail pointer.
29. How do you insert a node after a specific iterator in a std::forward_list?
Correct Answer: b) insert_after()
Explanation:
Because a singly linked list only has forward pointers, inserting at a position requires knowing the previous node, so insert_after() is used instead of insert().
30. Which of the following methods removes all elements from a std::list?
Correct Answer: c) clear()
Explanation:
The clear() method is standard across STL containers for removing all elements.
31. What does the empty() function of std::list do?
Correct Answer: b) Checks if the list has no elements
Explanation:
The empty() function returns a boolean true if the container is empty, and false otherwise.
32. How do you combine two sorted std::list containers into one sorted list?
Correct Answer: c) Using the merge() method
Explanation:
The merge() method merges another sorted list into the calling list, resulting in a single sorted list. The elements from the argument list are moved, not copied.
33. What does the std::list::splice() function do?
Correct Answer: b) It transfers elements from one list to another without copying or allocating new memory
Explanation:
splice() moves elements between lists by just relinking pointers, making it an O(1) operation (for moving specific elements) without any memory allocation.
34. Which iterators does std::list provide?
Correct Answer: b) Bidirectional iterators
Explanation:
Because it is a doubly linked list, std::list provides bidirectional iterators allowing iteration forward and backward, but not random access.
35. Which iterators does std::forward_list provide?
Correct Answer: c) Forward iterators
Explanation:
std::forward_list only supports iterating sequentially from the beginning to the end, thus providing forward iterators.
36. What will std::list::remove(val) do?
Correct Answer: b) Removes all occurrences of val
Explanation:
The remove() function scans the entire list and removes all elements that match the given value.
37. If you need to sort a std::list, how should you do it?
Correct Answer: b) Using list.sort()
Explanation:
std::sort requires random access iterators, which std::list does not have. Therefore, std::list provides its own member function sort().
38. What is the time complexity of finding the size of a std::list in C++11 and later?
Correct Answer: c) O(1)
Explanation:
Since C++11, the standard mandates that std::list::size() must have a constant time complexity, O(1), by maintaining an internal size counter.
39. Does std::forward_list have a size() method?
Correct Answer: c) No, it does not provide a size() method
Explanation:
std::forward_list deliberately omits the size() method to avoid the overhead of storing a size counter, ensuring it remains as lightweight as a raw C singly linked list.
40. How do you count elements in a std::forward_list if size() is not available?
Correct Answer: a) std::distance(flist.begin(), flist.end())
Explanation:
You can use std::distance() from the header to count the elements, though it will take O(N) time.
41. What does the reverse() method do in std::list?
Correct Answer: b) Reverses the order of the elements in the list
Explanation:
The reverse() member function flips the sequence of elements in the list.
42. Which list operation is most efficient at removing duplicate adjacent elements?
Correct Answer: c) unique()
Explanation:
The unique() function in std::list removes consecutive duplicate elements, leaving only the first of each.
43. If you want unique() to remove all duplicates in a std::list, what must you do first?
Correct Answer: b) Call sort()
Explanation:
unique() only removes *adjacent* duplicates. To remove all duplicates, the list must be sorted first so all identical elements are adjacent.
44. What happens to iterators when an element is inserted into a std::list?
Correct Answer: d) No iterators are invalidated
Explanation:
Unlike vectors, inserting into a list only involves updating pointers. Existing nodes stay at their memory addresses, so iterators remain valid.
45. What happens to iterators when an element is erased from a std::list?
Correct Answer: a) Only the iterator pointing to the erased element is invalidated
Explanation:
Erasing a node frees that node's memory. Iterators pointing to other nodes are entirely unaffected.
46. Which of the following creates a 2D array dynamically?
Correct Answer: b) int arr2D = new int*[rows]; for(int i=0; i<rows; ++i) arr2D[i] = new int[cols];
Explanation:
A dynamic 2D array is typically created as an array of pointers, where each pointer is initialized with a dynamically allocated row array.
47. When passing a multidimensional array (e.g., int arr[3][4]) to a function, what must be specified in the parameter list?
Correct Answer: c) All dimensions except the first
Explanation:
The compiler needs to know the sizes of all secondary dimensions to calculate pointer offsets properly (e.g., void func(int arr[][4])). The first dimension can be omitted.
48. Which function allows you to get a pointer to the underlying array used by std::array?
Correct Answer: a) data()
Explanation:
The data() method returns a direct pointer to the memory array used internally by std::array.
49. How are elements initialized in std::array<int, 5> arr; if no initializer is provided?
Correct Answer: c) They contain indeterminate (garbage) values
Explanation:
Like C-style arrays, an uninitialized std::array of built-in types contains garbage values if declared in a local scope without an initializer.
50. How do you zero-initialize a std::array?
Correct Answer: a) std::array<int, 5> arr = {0};
Explanation:
Using aggregate initialization like {0} or {} will zero-initialize all elements in the std::array.
51. What is the equivalent function to populate every element of a std::array or std::list with a specific value?
Correct Answer: b) fill()
Explanation:
The fill() member function assigns the given value to all elements in a std::array. std::list uses assign() for a similar purpose.
52. Which array property makes it unsuitable for situations requiring frequent insertions and deletions at the beginning?
Correct Answer: b) Contiguous memory layout
Explanation:
Because array elements are in contiguous memory, inserting at the beginning requires shifting all existing elements, taking O(N) time.
53. In a std::list, what is the time complexity of pushing an element to the front?
Correct Answer: c) O(1)
Explanation:
Lists manipulate pointers to add elements, making push_front() an O(1) constant time operation.
54. Which operation provides the highest iteration speed from beginning to end?
Correct Answer: c) std::array
Explanation:
Because std::array stores data contiguously, iterating over it is highly cache-friendly and faster than chasing pointers in a list.
55. What does a pointer to an array look like in C++?
Correct Answer: b) int (*arr)[10];
Explanation:
int (*arr)[10] declares a pointer to an array of 10 integers. Parentheses are needed because [] has higher precedence than *.
56. If int arr[5] = {10, 20, 30, 40, 50}; what is *(arr + 2)?
Correct Answer: c) 30
Explanation:
arr decays to a pointer to the first element. arr + 2 points to the third element (index 2). Dereferencing it yields 30.
57. Which statement correctly copies the contents of C-style array A to array B of the same size?
Correct Answer: c) std::copy(A, A + size, B);
Explanation:
C-style arrays do not support direct assignment. You must loop or use a function like std::copy to transfer elements.
58. Which statement correctly copies the contents of std::array A to std::array B of the same type and size?
Correct Answer: a) B = A;
Explanation:
Unlike C-arrays, std::array objects can be directly assigned to one another using the assignment operator if they have the same size and type.
59. What happens if you use the pop_back() method on an empty std::list?
Correct Answer: c) It causes undefined behavior
Explanation:
Calling pop_back() or pop_front() on an empty list results in undefined behavior. You should always check if it is empty() first.
60. What is the return type of the std::list::size() method?
Correct Answer: b) size_t (or size_type)
Explanation:
Container size methods in the STL return size_type, which is typically an unsigned integer type like size_t.
61. How do you declare an iterator for a std::list of strings?
Correct Answer: a) std::list::iterator it;
Explanation:
The iterator is a nested type within the container template, so the correct syntax is Container::iterator.
62. Which loop is the most modern and safest way to iterate through all elements of a std::list in C++11 and later?
Correct Answer: c) for(auto& val : list) { ... }
Explanation:
The range-based for loop (introduced in C++11) is the safest and most readable way to traverse containers.
63. What does a reverse iterator (rbegin) point to in an array or list?
Correct Answer: c) The last element
Explanation:
rbegin() returns a reverse iterator pointing to the very last element of the container, while rend() points to one before the first.
64. What is the result of applying the post-increment operator (it++) on a std::list iterator?
Correct Answer: b) It moves the iterator to the next node in the list
Explanation:
Incrementing a list iterator traverses the linked list by following the 'next' pointer to the subsequent node.
65. Can you use std::binary_search directly on a std::list?
Correct Answer: b) Yes, but it will be highly inefficient (O(N) time)
Explanation:
std::binary_search accepts forward iterators, so it compiles. However, because lists lack random access, traversing to the middle elements takes linear time, destroying the performance benefit of binary search.
66. To perform a fast lookup, which data structure is better than a list?
Correct Answer: b) std::array or std::vector
Explanation:
std::array and std::vector provide O(1) random access, whereas lists require O(N) traversal.
67. Which list function can remove elements based on a condition (a predicate)?
Correct Answer: b) remove_if()
Explanation:
remove_if() takes a predicate (a function returning true/false) and removes every element for which the predicate returns true.
68. In the declaration `int arr[2][3] = {{1,2,3}, {4,5,6}};`, what is the value of `arr[1][0]`?
Correct Answer: c) 4
Explanation:
arr[1][0] accesses the second row (index 1) and the first column (index 0), which is 4.
69. Which operator is heavily used when working with array decay and pointer arithmetic?
Correct Answer: c) The dereference (*) operator
Explanation:
Because arrays decay to pointers, the dereference operator (*) is used to access the values at the memory addresses the pointers point to.
70. Why is inserting an element into the middle of a std::list theoretically faster than inserting into the middle of a std::array?
Correct Answer: c) std::list merely adjusts a few pointers, while std::array requires shifting all subsequent elements
Explanation:
List insertion is O(1) after the position is found because it only involves rewiring node pointers. (Finding the position takes O(N), though).
71. What is the return type of std::list::emplace_back()?
Correct Answer: c) A reference to the newly inserted element (in C++17)
Explanation:
Since C++17, emplace_back() returns a reference to the newly constructed object. Prior to C++17, it returned void.
72. How is emplace_back() different from push_back() in a std::list?
Correct Answer: b) emplace_back() constructs the element in-place, avoiding a temporary copy/move
Explanation:
emplace_back takes constructor arguments and builds the object directly in the node's memory space, which can be more efficient than creating a temporary object and copying/moving it with push_back.
73. What does a std::list node typically contain?
Correct Answer: a) Data, a pointer to the next node, and a pointer to the previous node
Explanation:
As a doubly linked list, each node in a std::list holds the element's value along with pointers to both the successor and predecessor nodes.
74. Can you instantiate a std::array with a size of 0?
Correct Answer: a) Yes, std::array<int, 0> is valid in C++
Explanation:
std::array is explicitly supported by the standard. Calling front() or back() on it yields undefined behavior, but size() returns 0.
75. In memory hierarchy performance, why might std::array perform better than std::list?
Correct Answer: b) Because contiguous memory takes advantage of CPU cache prefetching
Explanation:
Arrays are cache-friendly. Modern CPUs fetch chunks of memory into cache; with contiguous memory, the next elements are already in the fast cache.
76. Which feature of C-style arrays causes the biggest security vulnerabilities?
Correct Answer: b) Lack of bounds checking leading to buffer overflows
Explanation:
Because C-arrays do not check if an index is out of bounds, hackers can overwrite adjacent memory (buffer overflow), leading to severe security exploits.
77. What is the memory overhead of a std::list of integers compared to a std::array of integers?
Correct Answer: b) std::list has significant overhead due to storing two pointers per element
Explanation:
Every element in a std::list requires allocation for the data itself plus a forward and backward pointer, adding at least 8 to 16 bytes of overhead per integer.
78. What is the type of the elements in argv (char* argv[]) passed to the main function?
Correct Answer: c) Pointers to characters (C-style strings)
Explanation:
argv is an array of pointers to characters. Each pointer points to a null-terminated C-style string representing a command-line argument.
79. Can an array be the return type of a C++ function?
Correct Answer: b) No, functions cannot return built-in arrays directly; they must return a pointer or a wrapper like std::array
Explanation:
C++ functions cannot return built-in array types. They must return a pointer, a struct containing an array, a std::array, or a std::vector.
80. Which of the following creates a reference to an array of 5 integers?
Correct Answer: b) int (&arr)[5];
Explanation:
Parentheses are necessary: int (&arr)[5]. Without them, int& arr[5] implies an array of references, which is illegal in C++.
81. What is illegal in C++ regarding arrays of references?
Correct Answer: c) C++ does not allow arrays of references
Explanation:
References are not objects in C++ (they have no storage identity), so you cannot create arrays of them. You can, however, use std::reference_wrapper.
82. How do you define an iterator for std::forward_list?
Correct Answer: a) std::forward_list::iterator
Explanation:
Like other containers, the iterator is accessed via the scope of the container template: std::forward_list::iterator.
83. Does std::forward_list support the insert() method?
Correct Answer: c) No, it supports insert_after() instead
Explanation:
Because it only has forward pointers, it can only efficiently insert a node *after* a known node, using insert_after().
84. To insert an element at the very beginning of a std::forward_list using insert_after, what iterator do you pass?
Correct Answer: b) before_begin()
Explanation:
std::forward_list provides a before_begin() iterator, which acts as a dummy node before the first element, allowing insertion at the absolute front via insert_after().
85. Which of these correctly swaps the contents of two lists `list1` and `list2` in O(1) time?
Correct Answer: b) std::swap(list1, list2);
Explanation:
std::swap (or list1.swap(list2)) swaps internal pointers to the head and tail, taking O(1) time. Assignment (list1 = list2) takes O(N) time because it copies elements.
86. If you dynamically allocate memory using `int* p = new int[5];`, how is the memory zero-initialized?
Correct Answer: b) int* p = new int[5]{0};
Explanation:
In modern C++ (C++11 and later), you can use aggregate initialization with new[]: new int[5]{0} initializes all 5 elements to 0.
87. Which std::list method allows removing elements if a specified boolean condition is met?
Correct Answer: c) remove_if()
Explanation:
std::list::remove_if() evaluates a unary predicate for each element and removes those for which the predicate evaluates to true.
88. What is the maximum theoretical size of a std::list?
Correct Answer: a) list.max_size()
Explanation:
All standard containers provide a max_size() method that returns the maximum number of elements the container can theoretically hold based on system architecture and allocator constraints.
89. Why might you choose a std::list over a std::vector?
Correct Answer: b) When you have frequent insertions/deletions in the middle of the container
Explanation:
Lists allow O(1) insertions and deletions anywhere (once the position iterator is known), whereas vectors require shifting elements.
90. What header contains algorithms like std::find that work on both arrays and lists?
Correct Answer: c)
Explanation:
The header provides a vast majority of STL functions (like find, count, copy) that operate on iterator ranges.
91. If `int arr[] = {1, 2, 3};`, what is the value of `2[arr]` in C++?
Correct Answer: b) 3
Explanation:
Due to pointer arithmetic properties inherited from C, arr[2] is equivalent to *(arr + 2), which is also equivalent to *(2 + arr), so 2[arr] is valid syntax and equals 3.
92. Which of the following is true regarding passing arrays to functions?
Correct Answer: c) Arrays decay to pointers, so modifications in the function affect the original array
Explanation:
Because a pointer to the original memory is passed, changes made using the array index inside the function modify the original elements.
93. How do you find the first element in a std::list that is greater than 10?
Correct Answer: a) Using std::find_if()
Explanation:
std::find_if accepts the begin and end iterators along with a lambda or predicate (e.g., [](int x){ return x > 10; }) to find the element.
94. Can an array contain references?
Correct Answer: b) No
Explanation:
Arrays of references are illegal in C++. Standard array elements must be assignable and have a size; references do not.
95. What does a std::list's sort() method use internally in most implementations?
Correct Answer: b) Mergesort
Explanation:
Mergesort is ideal for linked lists because it can merge sublists simply by reassigning pointers without needing random access.
96. If you want a fixed-size array that performs bounds checking via the at() method, you should use:
Correct Answer: b) std::array<int, 10>
Explanation:
std::array provides a compile-time fixed size along with the safety of member functions like at() which perform bounds checking.
97. Which of these containers requires memory reallocation when growing past its capacity?
Correct Answer: c) std::vector
Explanation:
std::vector allocates contiguous memory and must reallocate and copy/move when it outgrows capacity. Lists allocate single nodes as needed, and arrays cannot grow.
98. When iterating with `auto it = mylist.begin();`, what is the type of `it`?
Correct Answer: c) A std::list::iterator
Explanation:
The 'auto' keyword deduces the type returned by mylist.begin(), which is std::list::iterator.
99. What is the difference between `list.erase(it)` and `list.remove(val)`?
Correct Answer: a) erase removes by iterator position, remove removes by value match
Explanation:
erase() deletes elements at specific iterator locations, whereas remove() searches the list for a specific value and deletes all matching elements.
100. What is std::distance(a, b) used for?
Correct Answer: b) Finding the number of steps (elements) between two iterators
Explanation:
std::distance calculates the number of increments needed to go from iterator 'a' to iterator 'b'.
101. How does std::distance(a, b) perform on a std::list?
Correct Answer: b) O(N) time
Explanation:
Because std::list iterators are bidirectional (not random access), std::distance must step through the list one by one, taking linear O(N) time.
102. Is it possible to have an array of lists?
Correct Answer: b) Yes, e.g., std::list arr[10];
Explanation:
You can declare an array of lists perfectly fine. It is often used to implement a hash table using chaining.
103. Which of these expressions is used to access the last element of a std::array `arr`?
Correct Answer: b) arr.back()
Explanation:
The back() member function returns a reference to the last element. arr.end() returns an iterator past the end.
104. For a statically allocated array `int arr[5] = {1, 2};`, what is the value of `arr[4]`?
Correct Answer: b) 0
Explanation:
When an array is partially initialized, the remaining uninitialized elements are automatically initialized to zero.
105. What is the key danger of returning a local array from a function using a pointer?
Correct Answer: b) The local array's memory is deallocated when the function returns, causing a dangling pointer
Explanation:
Local arrays are stored on the stack and destroyed when the function ends. Returning a pointer to them yields undefined behavior upon access.
106. Which C++ cast is safest for converting an array of bytes to a struct pointer? (Advanced usage)
Correct Answer: c) reinterpret_cast
Explanation:
reinterpret_cast is used for low-level reinterpretation of bit patterns, such as mapping a byte array over a struct.
107. Why does std::array require two template parameters? (e.g., std::array<int, 5>)
Correct Answer: b) One for type, one for the size
Explanation:
std::array is fixed-size at compile time. Its type signature includes both the element data type and the exact number of elements.
108. If you need a container that grows in size but you NEVER insert in the middle, only append at the end, what is best?
Correct Answer: c) std::vector
Explanation:
std::vector is optimized for O(1) amortized insertion at the end and offers fast contiguous iteration.
109. How do you access the value an iterator points to in a list?
Correct Answer: c) *it
Explanation:
Iterators act like pointers. You dereference them using the asterisk (*) operator to access the underlying element.
110. If a std::list iterator `it` points to an object, how do you call a member function `func()` of that object?
Correct Answer: b) it->func()
Explanation:
The arrow operator (->) is overloaded on iterators to access members of the object being pointed to. (*it).func() is also correct but not an option.
111. What is the standard C++11 method to convert a C-array to a std::array?
Correct Answer: a) std::to_array (C++20)
Explanation:
Introduced in C++20, std::to_array converts a built-in array to a std::array. (Note: standard C++11 lacks a direct one-liner standard library function for this without manual copying or std::copy).
112. Is it valid to subtract two std::list iterators (e.g., it2 - it1)?
Correct Answer: b) No, because list iterators do not support subtraction
Explanation:
List iterators are bidirectional, not random-access. The subtraction operator requires random-access iterators. You must use std::distance() instead.
113. What happens when you call `cbegin()` on a container?
Correct Answer: b) It returns a const_iterator pointing to the first element
Explanation:
cbegin() strictly returns a constant iterator, ensuring the elements of the container cannot be modified through that iterator.
114. Can you use std::list to implement a Queue?
Correct Answer: b) Yes, by pushing at the back and popping from the front
Explanation:
A queue is FIFO (First-In-First-Out). std::list can push_back and pop_front in O(1) time, making it an acceptable underlying container for a queue.
115. What is the memory complexity of std::list compared to the amount of data 'N'?
Correct Answer: b) O(N)
Explanation:
The memory grows linearly with the number of elements N. Each element takes constant O(1) space, so N elements take O(N) space.
116. In a 3D array `int arr[2][3][4];`, what is the total number of integer elements?
Correct Answer: b) 24
Explanation:
The total capacity is the product of all dimensions: 2 * 3 * 4 = 24 elements.
117. When defining an array of strings, which is safest and easiest to manage in C++?
Correct Answer: c) std::array<std::string, 10>
Explanation:
std::array combined with std::string provides automatic memory management, deep copying, bounds safety, and rich member functions.
118. If you dynamically allocate an array of objects: `MyClass* arr = new MyClass[10];`, what happens?
Correct Answer: b) The default constructor is called 10 times
Explanation:
When allocating an array of objects with new[], C++ automatically invokes the default constructor for every element in the array.
119. If MyClass has NO default constructor, can you easily allocate `new MyClass[10]`?
Correct Answer: c) No, it will cause a compile-time error
Explanation:
Arrays require default constructors to initialize the elements. If a default constructor doesn't exist (because only parameterized ones were defined), `new MyClass[10]` will fail to compile.
120. Which header provides std::distance and std::advance?
Correct Answer: a)
Explanation:
Utility functions for iterators like advance, distance, next, and prev are found in the header.
121. What does `std::advance(it, 3)` do on a list iterator?
Correct Answer: b) Moves the iterator forward by 3 positions
Explanation:
std::advance modifies the iterator directly, moving it forward by the specified number of steps (or backward if negative).
122. Which of these correctly returns an iterator to the 4th element of a std::list?
Correct Answer: c) auto it = list.begin(); std::advance(it, 3);
Explanation:
Because list iterators cannot be added directly (like begin() + 3), you must use std::advance or std::next to step forward.










