C++ Interview Questions and Answers (OOP, STL, Pointers)
C++ questions on OOP, the STL and modern C++ that product companies and competitive-programming-minded interviewers love, each with a short explanation.
Q1. Which STL container keeps unique elements in sorted order?
- A. stack
- B. set
- C. vector
- D. unordered_map
Show answerHide answer
Answer: B. set
std::set stores unique keys sorted; unordered_set is unsorted but faster on average.Q2. What is the main difference between a class and a struct in C++?
- A. Structs can't have methods
- B. Classes can't have constructors
- C. Default access: private for class, public for struct
- D. There is no difference at all
Show answerHide answer
Answer: C. Default access: private for class, public for struct
Otherwise they are the same; both can have methods, constructors and inheritance.Q3. What is a reference in C++?
- A. A copy of a variable
- B. A type of loop
- C. Another name (alias) for an existing variable
- D. A pointer that can be null
Show answerHide answer
Answer: C. Another name (alias) for an existing variable
int& r = x; makes r refer to x; it must be initialised and can't be reseated.Q4. Which keyword enables runtime polymorphism in C++?
- A. inline
- B. virtual
- C. friend
- D. static
Show answerHide answer
Answer: B. virtual
virtual functions are resolved through the vtable at run time.Q5. What does a pure virtual function (= 0) make a class?
- A. Final
- B. Template
- C. Abstract
- D. Static
Show answerHide answer
Answer: C. Abstract
An abstract class can't be instantiated; derived classes must override the function.Q6. Which operator is used to free memory allocated with new[]?
- A. delete[]
- B. delete
- C. free
- D. remove
Show answerHide answer
Answer: A. delete[]
Pair new with delete and new[] with delete[].Q7. What does RAII stand for?
- A. Random Access Is Immediate
- B. Runtime Allocation In Instances
- C. Reference And Inheritance Interface
- D. Resource Acquisition Is Initialization
Show answerHide answer
Answer: D. Resource Acquisition Is Initialization
Resources are tied to object lifetime and released in the destructor.Q8. Which smart pointer allows only one owner of an object?
- A. shared_ptr
- B. auto_ptr
- C. unique_ptr
- D. weak_ptr
Show answerHide answer
Answer: C. unique_ptr
unique_ptr can be moved but not copied; shared_ptr counts owners.Q9. What is function overloading?
- A. Redefining a base class function
- B. Several functions with the same name but different parameters
- C. A function inside a class
- D. A function calling itself
Show answerHide answer
Answer: B. Several functions with the same name but different parameters
The compiler picks the right version from the argument types.Q10. What is the time complexity of push_back on a vector (amortised)?
- A. O(1)
- B. O(n)
- C. O(n²)
- D. O(log n)
Show answerHide answer
Answer: A. O(1)
Occasionally the vector grows and copies, but on average push_back is constant time.Q11. When is a destructor called?
- A. When an object is created
- B. When an object goes out of scope or is deleted
- C. When a method returns a value
- D. Only when the program ends
Show answerHide answer
Answer: B. When an object goes out of scope or is deleted
Stack objects are destroyed at the end of their block; heap objects on delete.Q12. Why should a base class with virtual functions have a virtual destructor?
- A. To make the class abstract
- B. To allow copying
- C. It is required for templates
- D. So deleting through a base pointer also destroys the derived part
Show answerHide answer
Answer: D. So deleting through a base pointer also destroys the derived part
Without it, delete basePtr only runs the base destructor (undefined behaviour).Q13. What does the const after a member function, like int get() const, mean?
- A. The function can't be overridden
- B. The function returns a constant
- C. The function doesn't modify the object
- D. The function is static
Show answerHide answer
Answer: C. The function doesn't modify the object
Const member functions can be called on const objects.Q14. Which header provides sort(), reverse() and max_element()?
- A. <numeric>
- B. <algorithm>
- C. <vector>
- D. <utility>
Show answerHide answer
Answer: B. <algorithm>
accumulate lives in <numeric>.Q15. What is a template in C++?
- A. A comment block
- B. A pre-written class from the STL only
- C. A way to write code that works with any type
- D. A design pattern
Show answerHide answer
Answer: C. A way to write code that works with any type
template <typename T> T max(T a, T b) works for ints, doubles, strings and more.Q16. Which container gives the largest element first?
- A. queue
- B. priority_queue
- C. deque
- D. stack
Show answerHide answer
Answer: B. priority_queue
std::priority_queue is a max-heap by default.Q17. What does the 'auto' keyword do?
- A. Makes a variable global
- B. Lets the compiler deduce the variable's type
- C. Creates a constant
- D. Allocates on the heap
Show answerHide answer
Answer: B. Lets the compiler deduce the variable's type
auto x = 3.5; makes x a double.Q18. Which access specifier lets derived classes use a member but hides it from other code?
- A. protected
- B. private
- C. friend
- D. public
Show answerHide answer
Answer: A. protected
protected members are visible in the class and its subclasses.Q19. What happens when you read m["key"] from a std::map and the key is missing?
- A. It returns null
- B. Compile error
- C. An exception is thrown
- D. The key is inserted with a default value
Show answerHide answer
Answer: D. The key is inserted with a default value
Use find() or count() to check without inserting.Q20. What is the difference between a pointer and a reference?
- A. A pointer can be null and re-pointed; a reference can't
- B. References use more memory
- C. Pointers can't point to objects
- D. There is no difference
Show answerHide answer
Answer: A. A pointer can be null and re-pointed; a reference can't
References must be bound when created and always refer to the same object.Q21. What does this code print?
#include <iostream> using namespace std; void set(int& v) { v = 20; } int main() { int x = 10; set(x); cout << x << endl; return 0; }- A. 10
- B. 0
- C. Error
- D. 20
Show answerHide answer
Answer: D. 20
The parameter is a reference, so the function changes x itself.Q22. What does this code print?
#include <iostream> using namespace std; struct Animal { virtual void speak() { cout << "Animal" << endl; } virtual ~Animal() = default; }; struct Dog : Animal { void speak() override { cout << "Dog" << endl; } }; int main() { Animal* a = new Dog(); a->speak(); delete a; return 0; }- A. Dog
- B. AnimalDog
- C. Error
- D. Animal
Show answerHide answer
Answer: A. Dog
speak() is virtual, so the Dog version runs through the base pointer.Q23. What does this code print?
#include <iostream> using namespace std; struct Animal { void speak() { cout << "Animal" << endl; } }; struct Dog : Animal { void speak() { cout << "Dog" << endl; } }; int main() { Dog d; Animal* a = &d; a->speak(); return 0; }- A. Error
- B. Animal
- C. AnimalDog
- D. Dog
Show answerHide answer
Answer: B. Animal
Without virtual, the call is decided by the pointer's type (Animal) at compile time.Q24. What does this code print?
set<int> s = {1, 2, 2, 3, 3}; cout << s.size() << endl;- A. 3
- B. 4
- C. 5
- D. 2
Show answerHide answer
Answer: A. 3
A set ignores duplicates: {1, 2, 3}.Q25. What does this code print?
vector<int> v = {5, 2, 9, 1}; sort(v.begin(), v.end()); for (size_t i = 0; i < v.size(); i++) cout << v[i] << (i + 1 < v.size() ? " " : "\n");- A. 1 2 5 9
- B. 1 5 2 9
- C. 9 5 2 1
- D. 5 2 9 1
Show answerHide answer
Answer: A. 1 2 5 9
sort arranges the vector in ascending order.Q26. What does this code print?
#include <iostream> using namespace std; struct T { T() { cout << "Construct "; } ~T() { cout << "Destruct" << endl; } }; int main() { { T t; } return 0; }- A. Nothing
- B. Destruct Construct
- C. Construct Destruct
- D. Construct
Show answerHide answer
Answer: C. Construct Destruct
The object is created inside the block and destroyed when the block ends.Q27. What does this code print?
stack<int> st; st.push(1); st.push(2); st.push(3); while (!st.empty()) { cout << st.top() << (st.size() > 1 ? " " : "\n"); st.pop(); }- A. 1 2 3
- B. 3 1 2
- C. 1 3 2
- D. 3 2 1
Show answerHide answer
Answer: D. 3 2 1
A stack is last-in, first-out.Q28. What does this code print?
priority_queue<int> pq; for (int x : {4, 9, 1}) pq.push(x); cout << pq.top() << endl;- A. Error
- B. 1
- C. 4
- D. 9
Show answerHide answer
Answer: D. 9
priority_queue keeps the largest element on top.Q29. What does this code print?
map<string, int> m; m["a"] = 1; int x = m["b"]; cout << m.size() << endl;
- A. 3
- B. 1
- C. 0
- D. 2
Show answerHide answer
Answer: D. 2
m["b"] doesn't exist yet, so reading it inserts "b" with 0, making 2 keys.Q30. What does this code print?
#include <iostream> using namespace std; int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } int main() { cout << add(3, 4) << " " << add(1.5, 2.0) << endl; return 0; }- A. 3.5 7
- B. 7 3
- C. 7 3.5
- D. Error
Show answerHide answer
Answer: C. 7 3.5
Overloading picks add(int, int) for ints and add(double, double) for doubles.Q31. What does this code print?
string s = "Skill Park"; cout << s.substr(0, 5) << endl;
- A. Skill Park
- B. ark
- C. Skill
- D. Park
Show answerHide answer
Answer: C. Skill
substr(0, 5) takes 5 characters starting at index 0.Q32. What does this code print?
vector<int> a = {10}; vector<int> b = a; b[0] = 15; cout << a[0] << " " << b[0] << endl;- A. 15 10
- B. 15 15
- C. 10 15
- D. 10 10
Show answerHide answer
Answer: C. 10 15
b is a copy of a, so changing b doesn't change a.Q33. What does this code print?
vector<int> a = {10}; vector<int>& r = a; r[0] = 15; cout << a[0] << " " << r[0] << endl;- A. 15 10
- B. 10 10
- C. 10 15
- D. 15 15
Show answerHide answer
Answer: D. 15 15
r is a reference to the vector a, so the change is visible through both names.Q34. What does this code print?
auto a = make_shared<int>(5); auto b = a; cout << a.use_count() << endl;
- A. 3
- B. 1
- C. 0
- D. 2
Show answerHide answer
Answer: D. 2
Two shared_ptrs own the same object.Q35. What does this code print?
try { throw runtime_error("bad"); } catch (const exception& e) { cout << "Caught: " << e.what() << endl; }- A. Caught: bad
- B. bad
- C. Crash
- D. Nothing
Show answerHide answer
Answer: A. Caught: bad
The thrown runtime_error is caught by the matching catch block.Q36. What does this code print?
int k = 4; auto mul = [k](int x) { return x * k; }; cout << mul(3) << endl;- A. 12
- B. Error
- C. 34
- D. 7
Show answerHide answer
Answer: A. 12
The lambda captures k = 4 by value and multiplies: 3 × 4 = 12.Q37. What does this code print?
vector<int> v = {1, 2, 3, 4, 5}; cout << accumulate(v.begin(), v.end(), 0) << endl;- A. 10
- B. Error
- C. 5
- D. 15
Show answerHide answer
Answer: D. 15
accumulate adds every element to the starting value 0.Q38. What does this code print?
#include <iostream> using namespace std; struct Base { Base() { cout << "Base "; } }; struct Derived : Base { Derived() { cout << "Derived" << endl; } }; int main() { Derived d; return 0; }- A. Base Derived
- B. Base
- C. Derived
- D. Derived Base
Show answerHide answer
Answer: A. Base Derived
The base class part is always constructed before the derived part.Q39. What is the difference between new and malloc?
- A. They are identical
- B. new calls constructors and is type-safe; malloc only allocates raw memory
- C. malloc calls constructors
- D. new allocates on the stack
Show answerHide answer
Answer: B. new calls constructors and is type-safe; malloc only allocates raw memory
Pair new with delete, malloc with free.Q40. What is a namespace?
- A. A type of class
- B. A named scope that prevents name clashes
- C. A file
- D. A memory region
Show answerHide answer
Answer: B. A named scope that prevents name clashes
std is the standard library's namespace.Q41. What is an inline function?
- A. A virtual function
- B. A function defined in one line
- C. A function the compiler may expand at the call site
- D. A function without parameters
Show answerHide answer
Answer: C. A function the compiler may expand at the call site
Avoids call overhead for small functions.Q42. What is the 'this' pointer?
- A. A pointer to the object a member function was called on
- B. A global pointer
- C. A pointer to the class
- D. A pointer to main()
Show answerHide answer
Answer: A. A pointer to the object a member function was called on
Used to tell members apart from parameters.Q43. What is a friend function?
- A. A public member function
- B. A non-member function allowed to access private members
- C. A function in a friend class only
- D. A virtual function
Show answerHide answer
Answer: B. A non-member function allowed to access private members
Declared with the friend keyword inside the class.Q44. What is the rule of three?
- A. If a class needs a destructor, copy constructor or copy assignment, it likely needs all three
- B. A class can inherit from at most three classes
- C. Every class needs three constructors
- D. Use at most three pointers
Show answerHide answer
Answer: A. If a class needs a destructor, copy constructor or copy assignment, it likely needs all three
The rule of five adds move constructor and move assignment.Q45. What is move semantics?
- A. Moving files on disk
- B. Moving a function between classes
- C. Changing the order of members
- D. Transferring resources from a temporary object instead of copying them
Show answerHide answer
Answer: D. Transferring resources from a temporary object instead of copying them
std::move and && (rvalue references), since C++11.Q46. What is a lambda capture [&]?
- A. Capture all by value
- B. Capture nothing
- C. Capture all used variables by reference
- D. Capture only this
Show answerHide answer
Answer: C. Capture all used variables by reference
[=] captures by value.Q47. What is the difference between std::map and std::unordered_map?
- A. map is sorted (tree, O(log n)); unordered_map is hashed (average O(1))
- B. They are identical
- C. map doesn't allow string keys
- D. unordered_map is sorted
Show answerHide answer
Answer: A. map is sorted (tree, O(log n)); unordered_map is hashed (average O(1))
Pick unordered_map unless you need order.Q48. What does std::vector::reserve do?
- A. Sorts the vector
- B. Adds elements
- C. Shrinks the vector
- D. Allocates capacity in advance without changing size
Show answerHide answer
Answer: D. Allocates capacity in advance without changing size
Avoids repeated reallocations.Q49. What is iterator invalidation?
- A. Iterators becoming unusable after the container changes, e.g. vector reallocation
- B. An iterator reaching end()
- C. An iterator pointing to the first element
- D. A compile error
Show answerHide answer
Answer: A. Iterators becoming unusable after the container changes, e.g. vector reallocation
Erasing or inserting into a vector can invalidate iterators.Q50. What is an abstract class in C++?
- A. A template class
- B. A class with only static members
- C. A class with at least one pure virtual function
- D. A class with no members
Show answerHide answer
Answer: C. A class with at least one pure virtual function
virtual void draw() = 0;Q51. What does the override keyword do?
- A. Hides a base function
- B. Makes a function static
- C. Makes the compiler check that the function really overrides a base virtual function
- D. Prevents overriding
Show answerHide answer
Answer: C. Makes the compiler check that the function really overrides a base virtual function
Catches signature mistakes.Q52. What does the final keyword do on a virtual function?
- A. Prevents further overriding in derived classes
- B. Makes it static
- C. Makes it inline
- D. Makes it pure virtual
Show answerHide answer
Answer: A. Prevents further overriding in derived classes
On a class, it prevents inheritance.Q53. What is a const reference parameter (const T&) good for?
- A. Passing large objects without copying and without allowing changes
- B. Making the object global
- C. Passing by value
- D. Allowing the function to change the object
Show answerHide answer
Answer: A. Passing large objects without copying and without allowing changes
Common for strings and vectors.Q54. What is the STL?
- A. A string library only
- B. A graphics library
- C. The Standard Template Library of containers, iterators and algorithms
- D. A testing framework
Show answerHide answer
Answer: C. The Standard Template Library of containers, iterators and algorithms
vector, map, sort, find…Q55. What is a deque?
- A. A stack only
- B. A hash table
- C. A sorted list
- D. A double-ended queue with fast insert/remove at both ends
Show answerHide answer
Answer: D. A double-ended queue with fast insert/remove at both ends
std::deque supports random access too.Q56. What does std::pair hold?
- A. A key and a hash
- B. Two values, accessed as first and second
- C. An array of two strings
- D. Two pointers only
Show answerHide answer
Answer: B. Two values, accessed as first and second
make_pair(1, "a").Q57. What is the purpose of std::sort's third argument?
- A. The starting index
- B. The number of elements
- C. The sorting algorithm name
- D. A custom comparison function or lambda
Show answerHide answer
Answer: D. A custom comparison function or lambda
sort(v.begin(), v.end(), greater<int>()).Q58. What is undefined behaviour?
- A. A runtime exception
- B. Code whose result the C++ standard doesn't specify, like reading out of bounds
- C. A warning from the compiler
- D. Code that doesn't compile
Show answerHide answer
Answer: B. Code whose result the C++ standard doesn't specify, like reading out of bounds
Anything may happen; avoid it.Q59. What does nullptr replace?
- A. NULL / 0 as a null pointer
- B. void
- C. delete
- D. this
Show answerHide answer
Answer: A. NULL / 0 as a null pointer
nullptr has its own type and avoids overload ambiguities.Q60. What is a static member variable?
- A. A private variable
- B. A constant
- C. A variable shared by all objects of the class
- D. A variable on the stack
Show answerHide answer
Answer: C. A variable shared by all objects of the class
Defined once outside the class (before C++17 inline).Q61. What happens if an exception is thrown and not caught?
- A. The exception is ignored
- B. std::terminate is called and the program ends
- C. The program restarts
- D. It returns 0
Show answerHide answer
Answer: B. std::terminate is called and the program ends
Catch exceptions you can handle.Q62. What is a template specialization?
- A. An abstract template
- B. A virtual template
- C. A template with no parameters
- D. A custom implementation of a template for a specific type
Show answerHide answer
Answer: D. A custom implementation of a template for a specific type
template<> class Box<bool> { … };Q63. What is the difference between struct and class inheritance by default?
- A. struct inherits publicly by default; class inherits privately
- B. They are the same
- C. class inherits publicly
- D. structs can't inherit
Show answerHide answer
Answer: A. struct inherits publicly by default; class inherits privately
Write public explicitly to be clear.Q64. What is object slicing?
- A. Using pointers to members
- B. Copying a derived object into a base object, losing the derived part
- C. Deleting part of an object
- D. Splitting an object into two
Show answerHide answer
Answer: B. Copying a derived object into a base object, losing the derived part
Use references or pointers to avoid it.Q65. Which smart pointer breaks reference cycles with shared_ptr?
- A. auto_ptr
- B. raw pointer
- C. weak_ptr
- D. unique_ptr
Show answerHide answer
Answer: C. weak_ptr
weak_ptr observes without owning.Q66. What is constexpr?
- A. A runtime constant only
- B. An exception type
- C. A constant pointer
- D. A value or function that can be evaluated at compile time
Show answerHide answer
Answer: D. A value or function that can be evaluated at compile time
constexpr int square(int x) { return x * x; }Q67. What does std::string::npos represent?
- A. 'Not found' or 'until the end' (the largest size_t value)
- B. Position 0
- C. The string's length
- D. A null character
Show answerHide answer
Answer: A. 'Not found' or 'until the end' (the largest size_t value)
find() returns npos when nothing is found.Q68. Why use emplace_back instead of push_back?
- A. It is the only way to add objects
- B. It sorts the vector
- C. It constructs the element in place, avoiding a temporary copy
- D. It inserts at the front
Show answerHide answer
Answer: C. It constructs the element in place, avoiding a temporary copy
v.emplace_back("Asha", 21) for a vector of structs.Q69. What does this code print?
queue<int> q; for (int x : {1, 2, 3}) q.push(x); for (int i = 0; i < 3; i++) { cout << q.front() << (i < 2 ? " " : "\n"); q.pop(); }- A. Error
- B. 2 1 3
- C. 1 2 3
- D. 3 2 1
Show answerHide answer
Answer: C. 1 2 3
A queue returns items in insertion order.Q70. What does this code print?
map<string, int> m = {{"b", 2}, {"a", 1}}; string out; for (auto& [k, v] : m) out += k + to_string(v) + " "; out.pop_back(); cout << out << endl;- A. a1 b2
- B. b2 a1
- C. 1a 2b
- D. Error
Show answerHide answer
Answer: A. a1 b2
A map iterates in sorted key order.Q71. What does this code print?
vector<int> v; v.reserve(100); for (int i = 0; i < 5; i++) v.push_back(i); cout << v.size() << endl;
- A. 3
- B. 6
- C. 5
- D. 4
Show answerHide answer
Answer: C. 5
reserve changes capacity, not size; 5 elements were added.Q72. What does this code print?
vector<int> v = {2, 5, 2, 7, 2}; cout << count(v.begin(), v.end(), 2) << endl;- A. 3
- B. 1
- C. 0
- D. Error
Show answerHide answer
Answer: A. 3
count returns how many times 2 appears.Q73. What does this code print?
vector<int> v = {4, 9, 1, 5}; cout << *max_element(v.begin(), v.end()) << endl;- A. 9
- B. 1
- C. 5
- D. Error
Show answerHide answer
Answer: A. 9
max_element returns an iterator to the largest value.Q74. What does this code print?
vector<int> v = {2, 4, 1, 3}; sort(v.begin(), v.end(), greater<int>()); for (size_t i = 0; i < v.size(); i++) cout << v[i] << (i + 1 < v.size() ? " " : "\n");- A. 4 3 2 1
- B. Error
- C. 4 1 2 3
- D. 1 2 3 4
Show answerHide answer
Answer: A. 4 3 2 1
sort with greater<int>() sorts descending.Q75. What does this code print?
string s = "hello"; reverse(s.begin(), s.end()); cout << s << endl;
- A. hello
- B. olleh
- C. h
- D. Error
Show answerHide answer
Answer: B. olleh
reverse works on strings too.Q76. What does this code print?
string s = "hello"; cout << s.find('l') << endl;- A. Error
- B. 1
- C. 2
- D. -1
Show answerHide answer
Answer: C. 2
find returns the index of the first 'l'.Q77. What does this code print?
string s = "hello"; cout << (s.find("xyz") == string::npos ? "Not found" : "Found") << endl;- A. Error
- B. Found
- C. 0
- D. Not found
Show answerHide answer
Answer: D. Not found
find returns string::npos when the text isn't present.Q78. What does this code print?
pair<int, int> p = {3, 1}; cout << p.first << " " << p.second << endl;- A. 1 1
- B. 3 1
- C. 1 3
- D. 3 3
Show answerHide answer
Answer: B. 3 1
pair members are first and second.Q79. What does this code print?
int x = 5; auto get = [&x]() { return x; }; x = 10; cout << get() << endl;- A. 5
- B. Error
- C. 20
- D. 10
Show answerHide answer
Answer: D. 10
The lambda captures x by reference, so it sees the updated value.Q80. What does this code print?
int x = 5; auto get = [x]() { return x; }; x = 10; cout << get() << endl;- A. Error
- B. 0
- C. 10
- D. 5
Show answerHide answer
Answer: D. 5
Capture by value copies x when the lambda is created.Q81. What does this code print?
#include <iostream> using namespace std; struct Shape { virtual string draw() const { return "Shape"; } virtual ~Shape() = default; }; struct Circle : Shape { string draw() const override { return "Circle"; } }; void show(const Shape& s) { cout << s.draw() << endl; } int main() { Circle c; show(c); return 0; }- A. Circle
- B. Shape
- C. Nothing
- D. Error
Show answerHide answer
Answer: A. Circle
draw is virtual, so the Circle version runs through the reference.Q82. What does this code print?
#include <iostream> using namespace std; struct Shape { virtual string draw() const { return "Shape"; } virtual ~Shape() = default; }; struct Circle : Shape { string draw() const override { return "Circle"; } }; void show(Shape s) { cout << s.draw() << endl; } int main() { Circle c; show(c); return 0; }- A. Error
- B. Nothing
- C. Shape
- D. Circle
Show answerHide answer
Answer: C. Shape
Passing by value slices the Circle into a Shape (object slicing).Q83. What does this code print?
auto a = make_unique<int>(7); auto b = move(a); cout << (a == nullptr) << endl;
- A. 0
- B. 1
- C. Error
- D. 2
Show answerHide answer
Answer: B. 1
unique_ptr has a single owner; moving transfers it, leaving a empty.Q84. What does this code print?
#include <iostream> using namespace std; struct Base { ~Base() { cout << "Base" << endl; } }; struct Derived : Base { ~Derived() { cout << "Derived "; } }; int main() { { Derived d; } return 0; }- A. Base Derived
- B. Derived
- C. Base
- D. Derived Base
Show answerHide answer
Answer: D. Derived Base
Destructors run in reverse order of construction: derived first.Q85. What does this code print?
#include <iostream> using namespace std; struct C { static int n; C() { n++; } }; int C::n = 0; int main() { C a, b, c; cout << C::n << endl; return 0; }- A. 3
- B. Error
- C. 1
- D. 2
Show answerHide answer
Answer: A. 3
Each object increments the shared static counter.Q86. What does this code print?
#include <iostream> using namespace std; template <typename T> T add(T a, T b) { return a + b; } int main() { cout << add(3, 4) << endl; return 0; }- A. 3.5
- B. Error
- C. 3
- D. 7
Show answerHide answer
Answer: D. 7
The template is instantiated for int: 3 + 4.Q87. What does this code print?
set<int> s = {1, 2}; s.insert(2); cout << s.size() << " "; s.insert(5); cout << s.size() << endl;- A. 2 2
- B. 2 3
- C. 3 2
- D. Error
Show answerHide answer
Answer: B. 2 3
set::insert ignores duplicates; size goes 2 → 2 → 3.Q88. What does this code print?
#include <iostream> using namespace std; void say(string s = "Default") { cout << s << endl; } int main() { say(); return 0; }- A. Default
- B. Value
- C. Error
- D. 0
Show answerHide answer
Answer: A. Default
The default argument is used when none is passed.Q89. What does this code print?
int x = 10; int& r = x; r += 5; cout << x << endl;
- A. 10
- B. Error
- C. 15
- D. 5
Show answerHide answer
Answer: C. 15
The reference r is another name for x, so x becomes 15.Q90. What does this code print?
vector<int> v = {1, 2, 3}, out(3); transform(v.begin(), v.end(), out.begin(), [](int x) { return x * x; }); cout << out[0] << " " << out[1] << " " << out[2] << endl;- A. 2 4 6
- B. 1 2 3
- C. Error
- D. 1 4 9
Show answerHide answer
Answer: D. 1 4 9
transform squares every element into the output vector.Q91. What does this code print?
vector<int> v = {1, 2, 3, 4, 5}; cout << count_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; }) << endl;- A. 1
- B. 2
- C. Error
- D. 3
Show answerHide answer
Answer: B. 2
count_if counts the even numbers 2 and 4.Q92. What does this code print?
string a = "a", b = "b"; cout << a + b << endl;
- A. Error
- B. ba
- C. ab
- D. a
Show answerHide answer
Answer: C. ab
string + concatenates.Q93. What does this code print?
vector<int> v = {1, 3, 5, 7, 9}; cout << lower_bound(v.begin(), v.end(), 8) - v.begin() << endl;- A. 4
- B. 5
- C. 3
- D. Error
Show answerHide answer
Answer: A. 4
lower_bound returns the first element not less than 8, which is 9 at index 4.Q94. What does this code print?
priority_queue<int, vector<int>, greater<int>> pq; for (int x : {3, 1, 2}) pq.push(x); for (int i = 0; i < 3; i++) { cout << pq.top() << (i < 2 ? " " : "\n"); pq.pop(); }- A. 1 3 2
- B. 1 2 3
- C. Error
- D. 3 2 1
Show answerHide answer
Answer: B. 1 2 3
A min-heap via greater<int> pops the smallest first.Q95. What does this code print?
vector<int> v = {1, 2, 3}; cout << accumulate(v.begin(), v.end(), 0) << endl;- A. 7
- B. 6
- C. 5
- D. Error
Show answerHide answer
Answer: B. 6
accumulate adds 1 + 2 + 3 to the starting value 0.
Practised these? Now prove it.
Take a timed mock test with new questions every attempt, earn a verified certificate at 70%+, and get noticed by employers.
Start a mock test