



















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The CPP certification practice exam tests advanced concepts in C++ programming. It includes object-oriented principles, templates, STL (Standard Template Library), exception handling, and complex algorithm design.
Typology: Exams
1 / 91
This page cannot be seen from the preview
Don't miss anything!




















































































Question 1. Which member function of std::vector invalidates all iterators, including the end iterator, when called? A) push_back B) pop_back C) insert D) clear Answer: D Explanation: clear() removes all elements and deallocates the internal storage, causing every iterator (including end()) to become invalid. Question 2. What is the complexity of std::deque::push_front? A) Amortized O(1) B) O(log n) C) O(n) D) Constant O(1) Answer: A Explanation: push_front on a deque may need to allocate a new block, but overall it is amortized constant time. Question 3. Which iterator category does std::list provide? A) RandomAccessIterator B) BidirectionalIterator C) ForwardIterator D) InputIterator Answer: B Explanation: std::list supports bidirectional traversal but not random access.
Question 4. When using std::vector::reserve(n), what guarantee does the standard give? A) Size becomes n B) Capacity becomes exactly n C) No reallocation occurs for subsequent insertions up to n elements D) All existing elements are copied to a new buffer of size n Answer: C Explanation: reserve ensures that capacity is at least n, preventing reallocation until size exceeds that capacity. Question 5. **Which operation on std::list is guaranteed to be constant time? ** A) splice(pos, other) B) sort() C) remove(value) D) unique() Answer: A Explanation: splice moves nodes between lists without copying or allocation, thus O(1). Question 6. What does the expression vec.at(5) do if the vector has only 4 elements? A) Returns a reference to undefined memory B) Throws std::out_of_range exception C) Causes undefined behavior D) Returns the last element Answer: B
Answer: A Explanation: Providing std::greater causes the smallest element to have highest priority. Question 10. Which of the following containers guarantees O(1) average-case lookup by key? A) std::map B) std::set C) std::unordered_map D) std::multiset Answer: C Explanation: unordered_map uses hashing, giving average constant-time lookup. Question 11. What is the result of std::count(v.begin(), v.end(), 10)? A) Number of elements equal to 10 in the range B) Position of first 10 C) True if any element equals 10 D) Sum of all elements that are 10 Answer: A Explanation: count returns how many times the value appears in the range. Question 12. Which algorithm removes elements from a container without changing its size? A) std::remove B) std::erase C) std::unique
D) std::pop_back Answer: A Explanation: remove reorders elements and returns a new logical end; the container size stays unchanged until erase is called (erase-remove idiom). Question 13. **What does std::stable_sort guarantee that std::sort does not? ** A) Faster runtime B) No additional memory usage C) Relative order of equal elements is preserved D) Sorting in descending order only Answer: C Explanation: stable_sort maintains the original order of equivalent elements. Question 14. Which function can be used to obtain the iterator to the first element that is not less than a given key in a sorted std::vector? A) std::lower_bound B) std::upper_bound C) std::binary_search D) std::equal_range Answer: A Explanation: lower_bound returns the first position where key could be inserted without breaking order. Question 15. When calling std::make_heap on a range, which element becomes the first element of the range after the call? A) The smallest element B) The median element
A) Creates a tuple named x and y B) Declares two variables x and y initialized from the pair C) Calls the copy constructor of std::pair D) Generates a compile-time error Answer: B Explanation: Structured bindings unpack the pair into separate variables. Question 19. Which of the following is a strongly-typed enumeration? A) enum Color { red, green }; B) enum class Direction { North, South }; C) typedef enum { A, B } MyEnum; D) enum { X, Y } ; Answer: B Explanation: enum class introduces a scoped, strongly-typed enumeration. Question 20. What does the manipulator std::setw(10) affect? A) The width of the next numeric output only B) The width of all subsequent outputs until changed C) The width of the next formatted output (including strings) D) The precision of floating-point numbers Answer: C Explanation: setw sets the field width for the next insertion operation only. Question 21. Which stream state flag indicates that an operation attempted to read past the end of file? A) fail()
B) bad() C) eof() D) good() Answer: C Explanation: eof() becomes true after an attempt to read beyond the stream’s end. Question 22. What is the effect of declaring a member function with the override specifier? A) It makes the function virtual automatically B) It forces the compiler to check that the function overrides a base-class virtual function C) It prevents further overriding in derived classes D) It changes the function’s calling convention Answer: B Explanation: override tells the compiler to ensure a matching virtual function exists in a base class. Question 23. Which operator must be declared as a non-member function to allow implicit conversions on the left-hand operand? A) operator+ B) operator= C) operator[] D) operator() Answer: A Explanation: For binary operators where the left operand is not of the class type, a non-member function enables implicit conversion.
Question 27. **Which of the following statements about std::variant is true? ** A) It can hold more than one alternative simultaneously B) Accessing the wrong alternative throws a compile-time error C) std::get(v) throws std::bad_variant_access if T is not the active type D) It requires a default constructor for every alternative Answer: C Explanation: std::get checks the active alternative at runtime and throws if mismatched. Question 28. When using std::transform with two input ranges, which overload is appropriate? A) transform(first, last, result, unary_op) B) transform(first1, last1, first2, result, binary_op) C) transform(first, last, result, binary_op) D) transform(first1, last1, first2, binary_op) Answer: B Explanation: The overload taking two input ranges applies a binary operation to each pair of elements. Question 29. **What is the effect of calling std::ios::sync_with_stdio(false);? ** A) Enables thread-safe I/O B) Disables synchronization between C and C++ standard streams, potentially speeding up I/O C) Forces all output to be flushed after each insertion D) Changes the default numeric base to hexadecimal Answer: B
Explanation: Disabling sync removes the overhead of keeping stdio and iostream buffers consistent. Question 30. Which of the following containers does NOT provide a push_back member function? A) std::vector B) std::deque C) std::list D) std::forward_list Answer: D Explanation: forward_list is a singly-linked list offering push_front but not push_back. Question 31. If you need a container that provides fast insertion and removal at both ends and also random access, which STL container is most appropriate? A) std::vector B) std::deque C) std::list D) std::forward_list Answer: B Explanation: deque offers amortized O(1) insertion/removal at both ends and constant-time random access. Question 32. What does the member function std::map::emplace do? A) Constructs a key-value pair in place, avoiding unnecessary copies B) Inserts a copy of an existing pair C) Returns an iterator to the inserted element only if the key was absent
C) const char* Name D) std::string Str Answer: D Explanation: Non-type parameters must have literal type; std::string is not allowed. Question 36. In a class template, how can you declare a friend function that is itself a function template? A) friend void foo(); B) template friend void foo(const MyClass&); C) friend void foo(const MyClass&); D) friend void foo(MyClass&); Answer: B Explanation: The friend declaration must itself be a template, using its own template parameters. Question 37. **What does the expression std::move_if_noexcept(x) return? ** A) x if x's move constructor is noexcept, otherwise a copy B) std::move(x) unconditionally C) std::move(x) only if the move is noexcept; otherwise x unchanged D) The same as std::forward(x) Answer: A Explanation: move_if_noexcept chooses to move only when the move constructor is noexcept; otherwise it returns a const reference to allow copy. Question 38. **Which of the following statements about std::array is true? **
A) Its size can be changed at runtime B) It provides bounds-checked access via operator[] C) Its size is part of its type D) It supports dynamic allocation via new[] Answer: C Explanation: std::array encodes N in the type, making its size fixed at compile time. Question 39. When using std::for_each with a lambda that modifies captured variables by value, what must be added to the lambda definition? A) mutable keyword B) const keyword C) noexcept specifier D) explicit keyword Answer: A Explanation: mutable allows the body to modify variables captured by value. Question 40. What is the effect of std::ios_base::fixed manipulator on floating-point output? A) Forces scientific notation B) Disables any formatting C) Prints the number in fixed-point notation with the current precision D) Prints the number with trailing zeros removed Answer: C Explanation: fixed forces fixed-point representation; precision then controls digits after the decimal point.
Explanation: Deleting copy disables copying; a defaulted move enables move semantics. Question 44. Which of the following containers guarantees that iterators are never invalidated by insertions? A) std::vector B) std::deque C) std::list D) std::map Answer: C Explanation: list insertions only affect iterators to the inserted element; all others remain valid. Question 45. What is the purpose of std::enable_if in template metaprogramming? A) To enable runtime polymorphism B) To conditionally remove a function or overload from the candidate set based on a compile-time boolean C) To force a template to be instantiated D) To provide a default type argument Answer: B Explanation: enable_if uses SFINAE to include or exclude functions based on a condition. Question 46. Which of the following is a correct way to declare a template that takes a non-type integral parameter named N? A) template class Array; B) template class Array;
C) template class Array; (C++17) D) Both A and C are correct Answer: D Explanation: Both explicit integral non-type parameters and generic auto (C++17) are valid. Question 47. When using std::unique_ptr with a custom deleter that is a function pointer, what is the size of the unique_ptr object? A) Same as raw pointer (usually 8 bytes) B) Size of the pointer plus size of the function pointer C) Always larger than raw pointer by at least 16 bytes D) Implementation-defined, but typically same as raw pointer because deleter is stateless Answer: D Explanation: If the deleter is a stateless function pointer, the compiler can store it as an empty type, making the size equal to the raw pointer. Question 48. Which of the following statements about std::shared_ptr's control block is correct? A) It is allocated separately from the managed object by default B) It is stored inside the managed object C) It cannot be shared between different shared_ptr instances D) It holds only the reference count, not the weak count Answer: A Explanation: By default, the control block is a separate allocation containing both strong and weak counts. Question 49. What does the [[nodiscard]] attribute indicate?
Question 52. What is the effect of std::ios::showpoint manipulator? A) Forces the display of the decimal point and trailing zeros for floating-point numbers B) Shows the sign of positive numbers C) Displays numbers in scientific notation D) Enables hexadecimal output for integers Answer: A Explanation: showpoint ensures the decimal point is always shown, even if the fractional part is zero. Question 53. Which algorithm can be used to partition a range into two groups while preserving the relative order of elements? A) std::partition B) std::stable_partition C) std::remove_if D) std::sort Answer: B Explanation: stable_partition maintains the original order within each partition. Question 54. When a class defines a move constructor but no copy constructor, what happens when you try to copy an object of that class? A) The compiler generates a copy constructor automatically B) The copy operation is ill-formed; compilation fails C) The move constructor is used instead D) The object is copied using memcpy Answer: B
Explanation: If a move constructor is user-declared and no copy constructor is provided, the copy constructor is implicitly deleted. Question 55. Which of the following is a correct way to specialize a function template for the type int? A) template<> void foo(int); B) template<> void foo(int); C) template void foo(int); D) template<> void foo<>(int); Answer: B Explanation: For function templates, the empty template argument list after template<> is used; the parameter type is then specified. Question 56. What does std::forward(arg) do in a perfect-forwarding scenario? A) Casts arg to an rvalue reference unconditionally B) Preserves the value category (lvalue or rvalue) of arg based on T C) Performs a static_cast to T D) Copies arg into a new object of type T Answer: B Explanation: forward returns arg as an rvalue if T is an rvalue reference, otherwise as an lvalue, preserving the original category. Question 57. Which of the following containers is guaranteed to keep elements in the order of insertion? A) std::set B) std::unordered_map C) std::vector