C++ questions

OOP, virtual functions, STL, templates and output questions.

1. Which feature lets two functions share a name but differ in parameters?

  1. Abstraction
  2. Function overloading
  3. Encapsulation
  4. 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:

  1. A static class
  2. A friend class
  3. A final class
  4. 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?

  1. internal
  2. protected
  3. private
  4. public
Answer

C. private class members are private by default; struct members are public.

4. Which keyword releases memory allocated with new?

  1. remove
  2. delete
  3. dispose
  4. 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?

  1. Friend functions
  2. Function overriding
  3. Function overloading
  4. Templates
Answer

B. Function overriding Overriding with virtual functions enables runtime polymorphism.

6. What is a constructor?

  1. A special member function called when an object is created
  2. A type of loop
  3. A function that deletes objects
  4. 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++?

  1. +
  2. ==
  3. []
  4. :: (scope resolution)
Answer

D. :: (scope resolution) The scope resolution operator cannot be overloaded.

8. Which STL container stores unique keys in sorted order?

  1. std::vector
  2. std::deque
  3. std::set
  4. 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?

  1. std::list
  2. std::map
  3. std::set
  4. std::unordered_map
Answer

D. std::unordered_map unordered_map is a hash table.

10. What does the "this" pointer point to?

  1. The current object
  2. The main function
  3. The base class
  4. 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?

  1. Name hiding
  2. The diamond problem
  3. Memory leaks
  4. 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":

  1. Is always static
  2. Can access private members of the class
  3. Cannot take arguments
  4. 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?

  1. ~Box
  2. Box~
  3. !Box
  4. delete Box
Answer

A. ~Box Destructors are named ~ClassName.

14. Which keyword makes a member function unable to modify the object?

  1. static
  2. mutable
  3. final
  4. const
Answer

D. const A const member function promises not to modify the object.

15. Templates in C++ are used for:

  1. Exception handling
  2. Memory management only
  3. Generic programming
  4. File handling
Answer

C. Generic programming Templates let one definition work with many types.

16. Which block catches exceptions thrown in a try block?

  1. finally
  2. catch
  3. except
  4. handle
Answer

B. catch C++ uses try / catch / throw.

17. What does std::vector::push_back do?

  1. Adds an element at the front
  2. Removes the last element
  3. Adds an element at the end
  4. Sorts the vector
Answer

C. Adds an element at the end push_back appends to the end.

18. A reference in C++ must be:

  1. Initialised when declared
  2. Declared as a pointer
  3. Global
  4. 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?

  1. Macros
  2. Virtual functions
  3. Templates
  4. Function overloading
Answer

B. Virtual functions Virtual dispatch happens at runtime.

20. What does RAII stand for?

  1. Random Access Index Iterator
  2. Runtime Allocation In Instances
  3. Resource Acquisition Is Initialization
  4. Reference And Inheritance Interface
Answer

C. Resource Acquisition Is Initialization Resources are tied to object lifetime.

21. Which smart pointer allows only one owner?

  1. std::shared_ptr
  2. std::weak_ptr
  3. std::unique_ptr
  4. std::auto_owner
Answer

C. std::unique_ptr unique_ptr has exclusive ownership.

22. What is "name mangling"?

  1. Deleting unused names
  2. A type of exception
  3. Encoding function names with parameter types so overloads can coexist
  4. 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?

  1. <cout>
  2. <string>
  3. <iostream>
  4. <stdio.h>
Answer

C. <iostream> iostream declares cin and cout.

24. Which access specifier lets derived classes, but not outside code, use a member?

  1. protected
  2. friend
  3. private
  4. public
Answer

A. protected protected members are visible to derived classes.

25. What is the time complexity of inserting into a std::map?

  1. O(log n)
  2. O(n log n)
  3. O(1)
  4. O(n)
Answer

A. O(log n) std::map is usually a balanced binary search tree.

26. An object of an abstract class:

  1. Cannot be created
  2. Must be static
  3. Can be created normally
  4. 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?

  1. alloc int[10]
  2. new int[10]
  3. malloc int[10]
  4. 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;
}
  1. 5
  2. Address of a
  3. Compile error
  4. 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;
}
  1. ABab
  2. BAab
  3. BAba
  4. 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;
}
  1. Shape
  2. ShapeCircle
  3. Circle
  4. 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;
}
  1. Shape
  2. Compile error
  3. Square
  4. 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;
}
  1. 6 6
  2. 6 5
  3. 5 6
  4. 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;
}
  1. 41
  2. 34
  3. 37
  4. 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;
}
  1. 51531
  2. 15
  3. 531
  4. 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;
}
  1. a1b2
  2. b2a1b7
  3. b7a1
  4. 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. 1
  2. 3
  3. Compile error
  4. 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;
}
  1. 9x
  2. 4c
  3. 9c
  4. 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;
}
  1. 42
  2. caught 42
  3. caught
  4. 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;
}
  1. 0
  2. 1
  3. 100
  4. 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;
}
  1. inter5
  2. intervi9
  3. interv9
  4. 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;
}
  1. 21
  2. 81
  3. 18
  4. 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;
}
  1. 34
  2. Compile error
  3. 7
  4. 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;
}
  1. 10
  2. 20
  3. Compile error
  4. 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;
}
  1. 9
  2. 5
  3. 15
  4. 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;
}
  1. 12
  2. 22
  3. 21
  4. 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”?

  1. user-defined type with members
  2. standard input stream
  3. generic code parameterized by types or values
  4. standard output stream
Answer

D. standard output stream cout is used to standard output stream.

47. In C++, which description best fits “cin”?

  1. initialize an object when created
  2. standard input stream
  3. user-defined type with members
  4. standard output stream
Answer

B. standard input stream cin is used to standard input stream.

48. In C++, which description best fits “vector”?

  1. generic code parameterized by types or values
  2. resizable sequence container
  3. alias for an existing object
  4. standard output stream
Answer

B. resizable sequence container vector is used to resizable sequence container.

49. In C++, which description best fits “reference”?

  1. alias for an existing object
  2. enable runtime dispatch through a base-class interface
  3. standard output stream
  4. 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”?

  1. standard output stream
  2. scope for grouping names
  3. user-defined type with members
  4. 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”?

  1. generic code parameterized by types or values
  2. cleanup when an object is destroyed
  3. resizable sequence container
  4. 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”?

  1. resizable sequence container
  2. standard input stream
  3. cleanup when an object is destroyed
  4. 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”?

  1. standard output stream
  2. generic code parameterized by types or values
  3. initialize an object when created
  4. 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”?

  1. user-defined type with members
  2. generic code parameterized by types or values
  3. standard output stream
  4. 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”?

  1. user-defined type with members
  2. scope for grouping names
  3. generic code parameterized by types or values
  4. 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”?

  1. template
  2. cout
  3. constructor
  4. class
Answer

B. cout The definition describes cout.

57. Which C++ concept matches this description: “standard input stream”?

  1. cin
  2. class
  3. cout
  4. vector
Answer

A. cin The definition describes cin.

58. Which C++ concept matches this description: “resizable sequence container”?

  1. vector
  2. cout
  3. cin
  4. destructor
Answer

A. vector The definition describes vector.

59. Which C++ concept matches this description: “alias for an existing object”?

  1. reference
  2. cout
  3. class
  4. virtual
Answer

A. reference The definition describes reference.

60. Which C++ concept matches this description: “enable runtime dispatch through a base-class interface”?

  1. virtual
  2. class
  3. destructor
  4. constructor
Answer

A. virtual The definition describes virtual.

61. Which C++ concept matches this description: “user-defined type with members”?

  1. template
  2. cin
  3. reference
  4. class
Answer

D. class The definition describes class.

62. Which C++ concept matches this description: “initialize an object when created”?

  1. reference
  2. template
  3. destructor
  4. constructor
Answer

D. constructor The definition describes constructor.

63. Which C++ concept matches this description: “cleanup when an object is destroyed”?

  1. namespace
  2. vector
  3. destructor
  4. class
Answer

C. destructor The definition describes destructor.

64. Which C++ concept matches this description: “generic code parameterized by types or values”?

  1. cout
  2. template
  3. class
  4. vector
Answer

B. template The definition describes template.

65. Which C++ concept matches this description: “scope for grouping names”?

  1. namespace
  2. virtual
  3. vector
  4. reference
Answer

A. namespace The definition describes namespace.

Mock test

More Technical MCQs topics