C++ questions
OOP, virtual functions, STL, templates and output questions.
1. Which feature lets two functions share a name but differ in parameters?
- Abstraction
- Function overloading
- Encapsulation
- Function overriding
Answer
B. Function overloading Overloading is resolved at compile time by the parameter list.
2. A class with at least one pure virtual function is called:
- A static class
- A friend class
- A final class
- An abstract class
Answer
D. An abstract class Abstract classes cannot be instantiated.
3. What is the default access specifier for members of a class?
- internal
- protected
- private
- public
Answer
C. private class members are private by default; struct members are public.
4. Which keyword releases memory allocated with new?
- remove
- delete
- dispose
- free
Answer
B. delete new pairs with delete (and new[] with delete[]).
5. Which concept allows a derived class to provide its own version of a base-class virtual function?
- Friend functions
- Function overriding
- Function overloading
- Templates
Answer
B. Function overriding Overriding with virtual functions enables runtime polymorphism.
6. What is a constructor?
- A special member function called when an object is created
- A type of loop
- A function that deletes objects
- A static variable
Answer
A. A special member function called when an object is created Constructors initialise objects and have the class name.
7. Which of these cannot be overloaded in C++?
- +
- ==
- []
- :: (scope resolution)
Answer
D. :: (scope resolution) The scope resolution operator cannot be overloaded.
8. Which STL container stores unique keys in sorted order?
- std::vector
- std::deque
- std::set
- std::unordered_map
Answer
C. std::set std::set is ordered and holds unique elements.
9. Which STL container gives average O(1) lookup by key?
- std::list
- std::map
- std::set
- std::unordered_map
Answer
D. std::unordered_map unordered_map is a hash table.
10. What does the "this" pointer point to?
- The current object
- The main function
- The base class
- The next object
Answer
A. The current object this is the address of the object the member function was called on.
11. Which inheritance problem does virtual inheritance solve?
- Name hiding
- The diamond problem
- Memory leaks
- Stack overflow
Answer
B. The diamond problem Virtual inheritance keeps a single copy of the common base.
12. A function declared with the keyword "friend":
- Is always static
- Can access private members of the class
- Cannot take arguments
- Must be a member function
Answer
B. Can access private members of the class Friends are granted access to private and protected members.
13. What is a destructor's name for class Box?
- ~Box
- Box~
- !Box
- delete Box
Answer
A. ~Box Destructors are named ~ClassName.
14. Which keyword makes a member function unable to modify the object?
- static
- mutable
- final
- const
Answer
D. const A const member function promises not to modify the object.
15. Templates in C++ are used for:
- Exception handling
- Memory management only
- Generic programming
- File handling
Answer
C. Generic programming Templates let one definition work with many types.
16. Which block catches exceptions thrown in a try block?
- finally
- catch
- except
- handle
Answer
B. catch C++ uses try / catch / throw.
17. What does std::vector::push_back do?
- Adds an element at the front
- Removes the last element
- Adds an element at the end
- Sorts the vector
Answer
C. Adds an element at the end push_back appends to the end.
18. A reference in C++ must be:
- Initialised when declared
- Declared as a pointer
- Global
- Reassigned later
Answer
A. Initialised when declared References must bind to an object at declaration and cannot be reseated.
19. Which of these is used for dynamic (runtime) polymorphism?
- Macros
- Virtual functions
- Templates
- Function overloading
Answer
B. Virtual functions Virtual dispatch happens at runtime.
20. What does RAII stand for?
- Random Access Index Iterator
- Runtime Allocation In Instances
- Resource Acquisition Is Initialization
- Reference And Inheritance Interface
Answer
C. Resource Acquisition Is Initialization Resources are tied to object lifetime.
21. Which smart pointer allows only one owner?
- std::shared_ptr
- std::weak_ptr
- std::unique_ptr
- std::auto_owner
Answer
C. std::unique_ptr unique_ptr has exclusive ownership.
22. What is "name mangling"?
- Deleting unused names
- A type of exception
- Encoding function names with parameter types so overloads can coexist
- Renaming variables at runtime
Answer
C. Encoding function names with parameter types so overloads can coexist The compiler encodes signatures into symbol names.
23. Which header is needed for std::cout?
- <cout>
- <string>
- <iostream>
- <stdio.h>
Answer
C. <iostream> iostream declares cin and cout.
24. Which access specifier lets derived classes, but not outside code, use a member?
- protected
- friend
- private
- public
Answer
A. protected protected members are visible to derived classes.
25. What is the time complexity of inserting into a std::map?
- O(log n)
- O(n log n)
- O(1)
- O(n)
Answer
A. O(log n) std::map is usually a balanced binary search tree.
26. An object of an abstract class:
- Cannot be created
- Must be static
- Can be created normally
- Is created automatically
Answer
A. Cannot be created Abstract classes cannot be instantiated.
27. Which operator is used to allocate an array of 10 ints dynamically?
- alloc int[10]
- new int[10]
- malloc int[10]
- new int(10)
Answer
B. new int[10] new int[10] creates an array; new int(10) creates one int with value 10.
28. What is the output?
#include <iostream>
using namespace std;
int main() {
int a = 5;
int &r = a;
r = 12;
cout << a;
return 0;
}- 5
- Address of a
- Compile error
- 12
Answer
D. 12 r is another name for a.
29. What is the output?
#include <iostream>
using namespace std;
class A { public: A() { cout << "A"; } ~A() { cout << "a"; } };
class B : public A { public: B() { cout << "B"; } ~B() { cout << "b"; } };
int main() {
B obj;
return 0;
}- ABab
- BAab
- BAba
- ABba
Answer
D. ABba Constructors run base first; destructors run in reverse.
30. What is the output?
#include <iostream>
using namespace std;
class Shape { public: virtual void show() { cout << "Shape"; } };
class Circle : public Shape { public: void show() override { cout << "Circle"; } };
int main() {
Shape *s = new Circle();
s->show();
delete s;
return 0;
}- Shape
- ShapeCircle
- Circle
- Compile error
Answer
C. Circle A virtual call uses the actual object type.
31. What is the output?
#include <iostream>
using namespace std;
class Shape { public: void show() { cout << "Shape"; } };
class Square : public Shape { public: void show() { cout << "Square"; } };
int main() {
Shape *s = new Square();
s->show();
delete s;
return 0;
}- Shape
- Compile error
- Square
- ShapeSquare
Answer
A. Shape Without virtual, the call is bound to the pointer type at compile time.
32. What is the output?
#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(2, 3) << " " << add(2.0, 3.0);
return 0;
}- 6 6
- 6 5
- 5 6
- 5 5
Answer
C. 5 6 Integer arguments pick the int version; doubles pick the double version (which multiplies).
33. What is the output?
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v = {4, 1, 3};
v.push_back(7);
cout << v.size() << v.back();
return 0;
}- 41
- 34
- 37
- 47
Answer
D. 47 Four elements; the last is 7.
34. What is the output?
#include <iostream>
#include <set>
using namespace std;
int main() {
set<int> s = {5, 1, 5, 3, 1};
for (int x : s) cout << x;
return 0;
}- 51531
- 15
- 531
- 135
Answer
D. 135 A set keeps unique values in sorted order.
35. What is the output?
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> m;
m["b"] = 2; m["a"] = 1; m["b"] += 5;
for (auto &p : m) cout << p.first << p.second;
return 0;
}- a1b2
- b2a1b7
- b7a1
- a1b7
Answer
D. a1b7 map iterates in key order; b becomes 7.
36. What is the output?
#include <iostream>
using namespace std;
class Counter {
public:
static int count;
Counter() { count++; }
};
int Counter::count = 0;
int main() {
Counter a, b, c;
cout << Counter::count;
return 0;
}- 1
- 3
- Compile error
- 0
Answer
B. 3 A static member is shared by all objects; three were created.
37. What is the output?
#include <iostream>
using namespace std;
template <typename T>
T biggest(T a, T b) { return a > b ? a : b; }
int main() {
cout << biggest(4, 9) << biggest('x', 'c');
return 0;
}- 9x
- 4c
- 9c
- 4x
Answer
A. 9x The template returns the larger value for each type.
38. What is the output?
#include <iostream>
using namespace std;
int main() {
try {
throw 42;
} catch (int e) {
cout << "caught " << e;
}
return 0;
}- 42
- caught 42
- caught
- Program crashes
Answer
B. caught 42 The int exception is caught by catch (int).
39. What is the output?
#include <iostream>
using namespace std;
void change(int x) { x = 100; }
int main() {
int n = 1;
change(n);
cout << n;
return 0;
}- 0
- 1
- 100
- Compile error
Answer
B. 1 Passing by value changes only the local copy.
40. What is the output?
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "interview";
cout << s.substr(0, 5) << s.length();
return 0;
}- inter5
- intervi9
- interv9
- inter9
Answer
D. inter9 substr(0, 5) is "inter"; the length is 9.
41. What is the output?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<int> v = {5, 2, 8, 1};
sort(v.begin(), v.end());
cout << v[0] << v[3];
return 0;
}- 21
- 81
- 18
- 58
Answer
C. 18 After sorting: 1, 2, 5, 8.
42. What is the output?
#include <iostream>
using namespace std;
class P { int x; public: P(int v) : x(v) {} P operator+(const P &o) const { return P(x + o.x); } int get() const { return x; } };
int main() {
P a(3), b(4);
cout << (a + b).get();
return 0;
}- 34
- Compile error
- 7
- 12
Answer
C. 7 The overloaded + adds the members.
43. What is the output?
#include <iostream>
using namespace std;
int main() {
int x = 10;
auto f = [&x]() { x += 5; };
f(); f();
cout << x;
return 0;
}- 10
- 20
- Compile error
- 15
Answer
B. 20 The lambda captures x by reference and adds 5 twice.
44. What is the output?
#include <iostream>
using namespace std;
int main() {
int a[] = {1, 2, 3, 4, 5};
int s = 0;
for (int v : a) if (v % 2) s += v;
cout << s;
return 0;
}- 9
- 5
- 15
- 6
Answer
A. 9 Odd values 1 + 3 + 5 = 9.
45. What is the output?
#include <iostream>
using namespace std;
class Base { public: virtual ~Base() {} virtual int id() { return 1; } };
class Derived : public Base { public: int id() override { return 2; } };
int main() {
Derived d;
Base &b = d;
cout << b.id() << d.Base::id();
return 0;
}- 12
- 22
- 21
- 11
Answer
C. 21 b.id() is virtual (2); d.Base::id() calls the base version explicitly (1).
46. In C++, which description best fits “cout”?
- user-defined type with members
- standard input stream
- generic code parameterized by types or values
- standard output stream
Answer
D. standard output stream cout is used to standard output stream.
47. In C++, which description best fits “cin”?
- initialize an object when created
- standard input stream
- user-defined type with members
- standard output stream
Answer
B. standard input stream cin is used to standard input stream.
48. In C++, which description best fits “vector”?
- generic code parameterized by types or values
- resizable sequence container
- alias for an existing object
- standard output stream
Answer
B. resizable sequence container vector is used to resizable sequence container.
49. In C++, which description best fits “reference”?
- alias for an existing object
- enable runtime dispatch through a base-class interface
- standard output stream
- scope for grouping names
Answer
A. alias for an existing object reference is used to alias for an existing object.
50. In C++, which description best fits “virtual”?
- standard output stream
- scope for grouping names
- user-defined type with members
- enable runtime dispatch through a base-class interface
Answer
D. enable runtime dispatch through a base-class interface virtual is used to enable runtime dispatch through a base-class interface.
51. In C++, which description best fits “class”?
- generic code parameterized by types or values
- cleanup when an object is destroyed
- resizable sequence container
- user-defined type with members
Answer
D. user-defined type with members class is used to user-defined type with members.
52. In C++, which description best fits “constructor”?
- resizable sequence container
- standard input stream
- cleanup when an object is destroyed
- initialize an object when created
Answer
D. initialize an object when created constructor is used to initialize an object when created.
53. In C++, which description best fits “destructor”?
- standard output stream
- generic code parameterized by types or values
- initialize an object when created
- cleanup when an object is destroyed
Answer
D. cleanup when an object is destroyed destructor is used to cleanup when an object is destroyed.
54. In C++, which description best fits “template”?
- user-defined type with members
- generic code parameterized by types or values
- standard output stream
- enable runtime dispatch through a base-class interface
Answer
B. generic code parameterized by types or values template is used to generic code parameterized by types or values.
55. In C++, which description best fits “namespace”?
- user-defined type with members
- scope for grouping names
- generic code parameterized by types or values
- enable runtime dispatch through a base-class interface
Answer
B. scope for grouping names namespace is used to scope for grouping names.
56. Which C++ concept matches this description: “standard output stream”?
- template
- cout
- constructor
- class
Answer
B. cout The definition describes cout.
57. Which C++ concept matches this description: “standard input stream”?
- cin
- class
- cout
- vector
Answer
A. cin The definition describes cin.
58. Which C++ concept matches this description: “resizable sequence container”?
- vector
- cout
- cin
- destructor
Answer
A. vector The definition describes vector.
59. Which C++ concept matches this description: “alias for an existing object”?
- reference
- cout
- class
- virtual
Answer
A. reference The definition describes reference.
60. Which C++ concept matches this description: “enable runtime dispatch through a base-class interface”?
- virtual
- class
- destructor
- constructor
Answer
A. virtual The definition describes virtual.
61. Which C++ concept matches this description: “user-defined type with members”?
- template
- cin
- reference
- class
Answer
D. class The definition describes class.
62. Which C++ concept matches this description: “initialize an object when created”?
- reference
- template
- destructor
- constructor
Answer
D. constructor The definition describes constructor.
63. Which C++ concept matches this description: “cleanup when an object is destroyed”?
- namespace
- vector
- destructor
- class
Answer
C. destructor The definition describes destructor.
64. Which C++ concept matches this description: “generic code parameterized by types or values”?
- cout
- template
- class
- vector
Answer
B. template The definition describes template.
65. Which C++ concept matches this description: “scope for grouping names”?
- namespace
- virtual
- vector
- reference
Answer
A. namespace The definition describes namespace.
Mock test
- C++ Mock Test 15 questions · 15 min