


















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



















































































Question 1. Which keyword is used to declare a function template? A) template B) typename C) class D) generic Answer: A Explanation: The template keyword introduces a template declaration; it is followed by a template‑parameter‑list. Question 2. In a function template, how does the compiler deduce the type of a non‑type template parameter? A) From the return type B) From the function arguments C) From the function name D) It cannot deduce non‑type parameters Answer: B Explanation: Template argument deduction uses the types of the function arguments to infer template parameters, including non‑type ones if they appear in the parameter list. Question 3. Which of the following is a correct way to explicitly instantiate a class template MyVec<T> for int? A) template class MyVec<int>; B) MyVec<int> myvec; C) explicit MyVec<int>; D) instantiate MyVec<int>; Answer: A
Explanation: template class MyVec<int>; forces the compiler to generate code for the int specialization. Question 4. What is the effect of a full (explicit) specialization of a class template? A) It provides a separate definition for a specific type combination. B) It overloads the template for additional types. C) It makes the template abstract. D) It disables the template for that type. Answer: A Explanation: Full specialization defines a completely different implementation for the given template arguments. Question 5. Which syntax correctly declares a member function of a class template outside the class definition? A) template<class T> void MyClass<T>::func(); B) void MyClass<T>::func(); C) template<typename T> MyClass::func(); D) template<class T> MyClass::func(); Answer: A Explanation: The template header must precede the qualified name when defining members outside the class. Question 6. In a nested template scenario Outer<T>::Inner<U>, how do you refer to Inner inside Outer? A) template<class U> class Inner; B) template<class T, class U> class Inner; C) template<class U> class Outer<T>::Inner;
B) std::deque C) std::vector D) std::forward_list Answer: C Explanation: std::vector stores elements contiguously, enabling O(1) index access via operator[] or at. Question 10. When does std::vector reallocate its internal storage? A) When size() exceeds capacity(). B) When push_back is called. C) When reserve is called with a larger value. D) Both A and C. Answer: D Explanation: Reallocation occurs when the size would exceed current capacity or when reserve requests a larger capacity. Question 11. Which member function of std::deque provides access to the first element? A) front() B) back() C) at(0) D) begin() Answer: A Explanation: front() returns a reference to the first element of any sequence container, including deque. Question 12. Which container adapter is implemented on top of std::deque by default?
A) std::stack B) std::queue C) std::priority_queue D) All of the above Answer: D Explanation: All three adapters can use deque as the underlying container unless another container is specified. Question 13. What is the complexity of std::list::splice for moving a whole list into another? A) O(1) B) O(n) C) O(log n) D) O(n log n) Answer: A Explanation: splice merely relinks node pointers; no element copying occurs, making it constant time. Question 14. Which iterator category supports the operator+ arithmetic? A) Input iterator B) Forward iterator C) Bidirectional iterator D) Random‑access iterator Answer: D Explanation: Only random‑access iterators provide pointer‑like arithmetic such as + and -.
Question 18. How does std::map::operator[] behave when the key does not exist? A) Throws std::out_of_range. B) Inserts a default‑constructed value and returns a reference. C) Returns a null pointer. D) Does nothing. Answer: B Explanation: operator[] inserts a new element with a default‑constructed mapped value if the key is absent. Question 19. Which algorithm can be used to obtain the range of elements with a given key in a std::multimap? A) find(key) B) lower_bound(key) and upper_bound(key) C) equal_range(key) D) Both B and C Answer: D Explanation: equal_range returns a pair of iterators equivalent to lower_bound and upper_bound. Question 20. What is the purpose of a custom comparator in an associative container? A) To change the hashing algorithm. B) To define a different ordering criterion than operator<. C) To enable duplicate keys. D) To improve I/O performance. Answer: B
Explanation: A custom comparator replaces the default std::less and determines how keys are ordered. Question 21. Which algorithm returns true if the range [first, last) contains at least one element equal to value? A) std::any_of B) std::find C) std::count D) std::binary_search Answer: B Explanation: std::find searches for the first occurrence of value and returns last if not found. Question 22. Which algorithm copies elements from one range to another while preserving the original order? A) std::copy_backward B) std::copy C) std::move D) std::transform Answer: B Explanation: std::copy copies from the source to the destination forward, keeping order intact. Question 23. Which algorithm removes elements that satisfy a predicate without changing the container size? A) std::remove_if B) std::erase C) std::filter
Question 26. What is the time complexity of std::stable_sort on a random‑access range of n elements? A) O(n log n) average, O(n²) worst B) O(n log² n) C) O(n log n) guaranteed D) O(n) Answer: C Explanation: std::stable_sort typically uses merge sort, guaranteeing O(n log n) time and stability. Question 27. Which algorithm can be used to find the first occurrence of a subsequence s2 within s1? A) std::search B) std::find C) std::adjacent_find D) std::mismatch Answer: A Explanation: std::search looks for a sequence of elements matching a given subsequence. Question 28. Which function object represents the binary addition operation? A) std::plus<> B) std::minus<> C) std::multiplies<> D) std::divides<> Answer: A Explanation: std::plus is a standard functor that returns the sum of its two arguments.
Question 29. In the context of std::bind, what does std::placeholders::_1 represent? A) The first argument to the bound function. B) A compile‑time constant. C) The return type placeholder. D) It has no meaning. Answer: A Explanation: _1 is a placeholder that will be replaced by the first argument supplied when the bound callable is invoked. Question 30. Which manipulator forces floating‑point output to show exactly six digits after the decimal point? A) std::fixed B) std::setprecision(6) alone C) std::showpoint D) std::scientific Answer: A Explanation: std::fixed together with setprecision(6) prints exactly six digits after the decimal point. Question 31. What does std::boolalpha do when applied to an output stream? A) Prints true/false instead of 1/0. B) Forces numeric output to be in hexadecimal. C) Aligns text to the left. D) Enables scientific notation. Answer: A
Explanation: When deducing, auto discards top‑level const qualifiers and reference qualifiers, yielding the underlying type. Question 35. In a lambda expression [=](int x){ return x + a; }, what does the capture list [=] mean? A) Capture nothing. B) Capture all automatic variables by value. C) Capture all automatic variables by reference. D) Capture only a by value. Answer: B Explanation: [=] captures every automatic variable that appears in the lambda body by value. Question 36. Which of the following is a valid use of a range‑based for loop? A) for (auto &elem : myMap) {} B) for (auto elem : myArray) {} C) for (auto &&elem : std::move(vec)) {} D) All of the above. Answer: D Explanation: Range‑based for works with any type that provides begin() and end(), and the loop variable can be any appropriate reference type. Question 37. What does the keyword constexpr guarantee about a function? A) It can be evaluated at compile time if all arguments are constant expressions. B) It must be inline. C) It can only be used with integral return types. D) It disables recursion.
Answer: A Explanation: A constexpr function may be evaluated at compile time when called with constant expressions; otherwise it behaves like a normal function. Question 38. Which container provides the strongest exception safety guarantee for element insertion? A) std::vector B) std::deque C) std::list D) std::forward_list Answer: C Explanation: std::list inserts by linking new nodes; if allocation fails, the container remains unchanged, giving the strong guarantee. Question 39. What is the effect of calling std::make_heap on a std::vector<int>? A) Sorts the vector in ascending order. B) Rearranges elements to satisfy the heap property (max‑heap by default). C) Removes the largest element. D) Converts the vector into a priority queue object. Answer: B Explanation: make_heap transforms the range into a heap where the first element is the largest (max‑heap). Question 40. Which algorithm can be used to merge two sorted ranges into a third sorted range without extra memory allocation? A) std::merge B) std::inplace_merge
A) Random‑access iterator B) Bidirectional iterator C) Forward iterator only D) Input iterator only Answer: B Explanation: std::set (and other ordered associative containers) provide bidirectional iterators. Question 44. What does the member function std::vector::shrink_to_fit request? A) It forces the capacity to become equal to size. B) It always reduces capacity to zero. C) It is a non‑binding request to reduce capacity to fit size. D) It swaps the vector with an empty one. Answer: C Explanation: shrink_to_fit is a non‑guaranteed request; the implementation may reduce capacity to match size. Question 45. Which algorithm returns the number of elements satisfying a predicate in a range? A) std::count_if B) std::find_if C) std::any_of D) std::all_of Answer: A Explanation: count_if iterates over the range and increments a counter for each element where the predicate returns true.
Question 46. Which of the following is a valid way to create a std::unique_ptr to an array of 10 ints? A) std::unique_ptr<int[]> p(new int[10]); B) std::unique_ptr<int> p(new int[10]); C) std::unique_ptr<int> p = std::make_unique<int>(10); D) std::unique_ptr<int[]> p = std::make_unique<int>(10); Answer: A Explanation: For arrays, unique_ptr must use the T[] specialization; make_unique for arrays is available in C++14 (std::make_unique<int[]>(10)). Question 47. What does std::generate_n do? A) Copies n elements from one range to another. B) Fills n elements of a range using a generator function. C) Generates n random numbers and stores them. D) Returns an iterator to the n‑th element. Answer: B Explanation: generate_n calls the generator function n times, assigning each result to successive elements in the output range. Question 48. Which of the following containers guarantees that iterators remain valid after insertion at any position? A) std::vector B) std::deque C) std::list D) std::map Answer: C
D) [&this](){ return member; } Answer: C Explanation: [this] captures the current object pointer by value (i.e., copies the pointer), allowing member access. Question 52. What is the result of applying std::transform with std::negate<int>() to a vector of integers? A) All elements become their additive inverse. B) Elements are multiplied by - 1 and stored in a new container. C) The original vector is unchanged; a new vector is returned. D) The vector is sorted in descending order. Answer: A Explanation: std::transform applies the unary operation to each element, replacing each with its negated value. Question 53. Which manipulator resets the formatting flags of a stream to their default state? A) std::resetiosflags B) std::setiosflags C) std::noshowpos D) std::clear Answer: A Explanation: resetiosflags clears the specified format flags, restoring default behavior for those flags. Question 54. Which of the following is true about std::move_iterator? A) It converts a regular iterator into an iterator that yields rvalue references.
B) It moves elements out of a container permanently. C) It can only be used with std::vector. D) It swaps the underlying container. Answer: A Explanation: std::move_iterator adapts an iterator so that dereferencing yields an rvalue reference, enabling move semantics in algorithms. Question 55. In the context of std::function, what does std::function<void(int)> f = [](int){} represent? A) A pointer to a regular function. B) A type‑erased callable wrapper that can store any compatible callable. C) A compile‑time constant. D) An overloaded function set. Answer: B Explanation: std::function is a type‑erased container for any callable with a matching signature. Question 56. Which container adapter can be used to implement a LIFO (last‑in, first‑out) data structure? A) std::queue B) std::stack C) std::priority_queue D) std::deque Answer: B Explanation: std::stack provides push/pop operations that follow LIFO semantics.