Are you looking to test and expand your understanding of C++ programming? Whether you’re a beginner aiming to solidify your foundational knowledge or an experienced developer seeking to challenge yourself, this blog post is here to help you on your journey. In this comprehensive compilation, we present 100 multiple-choice questions and answers on C++ programming.
C++ is a powerful and widely used programming language known for its efficiency, flexibility, and extensive application domains. It’s crucial for developers to have a strong grasp of its concepts, syntax, and features. This blog post aims to serve as an interactive learning resource, allowing you to assess your understanding of C++ while also reinforcing important concepts.
Question | Option A | Option B | Option C | Option D | Answer |
---|---|---|---|---|---|
1. What is the purpose of the new operator in C++? | To dynamically allocate memory for objects | To perform arithmetic operations | To compare two variables | To print output to the console | To dynamically allocate memory for objects |
2. Which of the following is NOT a fundamental data type in C++? | boolean | char | int | float | boolean |
3. What is the purpose of the static keyword in C++? | To declare a variable or function with static storage duration | To define a class template | To perform type casting | To control the flow of a loop | To declare a variable or function with static storage duration |
4. Which of the following is the correct syntax to define a class in C++? | class MyClass { // class definition } | class { // class definition } MyClass; | class = { // class definition } MyClass; | class = MyClass { // class definition } | class MyClass { // class definition } |
5. What is the purpose of the const keyword in C++? | To declare a constant variable | To perform bitwise operations | To define a class constructor | To convert a data type to a string | To declare a constant variable |
6. Which of the following is the correct way to declare a reference variable in C++? | int& refVar = num; | &int refVar = num; | refVar int& = num; | int refVar& = num; | int& refVar = num; |
7. What is the purpose of the virtual keyword in C++? | To enable polymorphic behavior in derived classes | To specify a base class | To perform logical operations | To read input from the user | To enable polymorphic behavior in derived classes |
8. Which of the following is NOT a valid access specifier in C++? | public | private | protected | internal | internal |
9. What is the purpose of the this pointer in C++? | To refer to the current object | To perform string manipulation | To perform type casting | To exit a loop or switch statement | To refer to the current object |
10. Which of the following is the correct syntax to define a pure virtual function in C++? | virtual void myFunction() = 0; | void virtual myFunction() = 0; | void myFunction() virtual = 0; | void myFunction() = virtual 0; | virtual void myFunction() = 0; |
Question | Option A | Option B | Option C | Option D | Answer |
---|---|---|---|---|---|
11. What is the purpose of the iostream library in C++? | To provide input/output functionality | To perform mathematical operations | To manage memory allocation | To define classes and objects | To provide input/output functionality |
12. Which of the following is the correct way to define a constructor in C++? | MyClass::MyClass() { // constructor code } | constructor MyClass() { // constructor code } | MyClass() { // constructor code } | MyClass::constructor() { // constructor code } | MyClass::MyClass() { // constructor code } |
13. What is the purpose of the try-catch block in C++? | To handle exceptions and perform error handling | To compare two strings | To control the flow of a loop | To declare and define a function | To handle exceptions and perform error handling |
14. Which of the following is the correct way to declare a constant member function in C++? | void myFunction() const; | const void myFunction(); | const myFunction() void; | myFunction() void const; | void myFunction() const; |
15. What is the purpose of the dynamic_cast operator in C++? | To perform safe downcasting between polymorphic classes | To perform logical operations | To define a derived class | To read input from the user | To perform safe downcasting between polymorphic classes |
16. Which of the following is NOT a valid storage class specifier in C++? | auto | static | register | constant | constant |
17. What is the purpose of the namespace keyword in C++? | To define a named scope | To specify the return type of a function | To perform type casting | To exit a loop or switch statement | To define a named scope |
18. Which of the following is the correct way to define a destructor in C++? | MyClass::~MyClass() { // destructor code } | ~MyClass::MyClass() { // destructor code } | MyClass()~ { // destructor code } | destructor MyClass() { // destructor code } | MyClass::~MyClass() { // destructor code } |
19. What is the purpose of the const_cast operator in C++? | To remove the constness of an object | To compare two variables | To perform bitwise operations | To convert a data type to a string | To remove the constness of an object |
20. Which of the following is the correct syntax to define a friend function in C++? | friend void myFunction(); | void myFunction() friend; | myFunction() friend void; | void friend myFunction(); | friend void myFunction(); |
Question | Option A | Option B | Option C | Option D | Answer |
---|---|---|---|---|---|
21. What is the purpose of the typeid operator in C++? | To retrieve the type information of an object | To perform arithmetic operations | To compare two variables for equality | To print output to the console | To retrieve the type information of an object |
22. Which of the following is NOT a valid C++ access modifier? | default | public | private | protected | default |
23. What is the purpose of the inline keyword in C++? | To suggest the compiler to perform inline expansion of a function | To define a class template | To perform type casting | To control the flow of a loop | To suggest the compiler to perform inline expansion of a function |
24. Which of the following is the correct syntax to declare a template class in C++? | template <class T> class MyClass { // class definition } | class template <T> MyClass { // class definition } | class <T> template MyClass { // class definition } | class MyClass <T> { // class definition } | template <class T> class MyClass { // class definition } |
25. What is the purpose of the explicit keyword in C++? | To prevent implicit type conversions during object construction | To perform bitwise operations | To read input from the user | To define a base class | To prevent implicit type conversions during object construction |
26. Which of the following is the correct syntax to declare a static member variable in C++? | static int myVar; | int static myVar; | myVar static int; | int myVar static; | static int myVar; |
27. What is the purpose of the throw keyword in C++? | To manually throw an exception | To compare two strings | To perform mathematical calculations | To exit a loop or switch statement | To manually throw an exception |
28. Which of the following is NOT a valid pointer type in C++? | nullptr | void* | int* | char* | nullptr |
29. What is the purpose of the decltype keyword in C++? | To deduce the type of an expression at compile-time | To perform logical operations | To define a derived class | To perform string manipulation | To deduce the type of an expression at compile-time |
30. Which of the following is the correct way to define an enumeration in C++? | enum MyEnum { Value1, Value2, Value3 }; | enum { Value1, Value2, Value3 } MyEnum; | enum = { Value1, Value2, Value3 } MyEnum; | enum = MyEnum { Value1, Value2, Value3 }; | enum MyEnum { Value1, Value2, Value3 }; |
Question | Option A | Option B | Option C | Option D | Answer |
---|---|---|---|---|---|
31. What is the purpose of the sizeof operator in C++? | To determine the size of a data type or object | To perform arithmetic operations | To compare two variables for equality | To print output to the console | To determine the size of a data type or object |
32. Which of the following is NOT a valid C++ type qualifier? | const | volatile | mutable | abstract | abstract |
33. What is the purpose of the default keyword in C++11 and later versions? | To define a default constructor or assignment operator | To define a virtual function | To perform type casting | To exit a loop or switch statement | To define a default constructor or assignment operator |
34. Which of the following is the correct syntax to define a lambda function in C++? | [captures](parameters) { // function body } | {captures}(parameters) -> // function body | parameters [captures] { // function body } | {parameters}(captures) -> // function body | [captures](parameters) { // function body } |
35. What is the purpose of the override keyword in C++11 and later versions? | To indicate that a virtual function overrides a base class function | To compare two strings | To perform bitwise operations | To define a derived class | To indicate that a virtual function overrides a base class function |
36. Which of the following is the correct way to access a member of a class using a pointer in C++? | pointer->member; | member.pointer; | pointer::member; | member::pointer; | pointer->member; |
37. What is the purpose of the decltype(auto) specifier in C++14 and later versions? | To deduce the type of an expression, including reference and cv-qualifiers | To perform logical operations | To read input from the user | To define a base class | To deduce the type of an expression, including reference and cv-qualifiers |
38. Which of the following is NOT a valid C++ standard library container? | List | Vector | Map | Set | List |
39. What is the purpose of the std::move function in C++? | To enable efficient move semantics and transfer ownership of resources | To compare two variables | To perform mathematical calculations | To exit a loop or switch statement | To enable efficient move semantics and transfer ownership of resources |
40. Which of the following is the correct syntax to define a template function in C++? | template <class T> void myFunction(T param) { // function body } | void template <T> myFunction(T param) { // function body } | void myFunction(T param) template <class T> { // function body } | void myFunction(T param) <class T> template { // function body } | template <class T> void myFunction(T param) { // function body } |
Question | Option A | Option B | Option C | Option D | Answer |
---|---|---|---|---|---|
41. What is the purpose of the std::thread class in C++11 and later versions? | To create and manage concurrent execution of tasks | To perform string manipulation | To define a derived class | To control the flow of a loop | To create and manage concurrent execution of tasks |
42. Which of the following is NOT a valid C++ access specifier? | public | private | protected | internal | internal |
43. What is the purpose of the nullptr keyword in C++11 and later versions? | To represent a null pointer | To perform logical operations | To define a class constructor | To convert a data type to a string | To represent a null pointer |
44. Which of the following is the correct way to define a constant member variable in a C++ class? | const int myVar; | int myVar const; | int const myVar; | myVar const int; | const int myVar; |
45. What is the purpose of the std::array container in C++? | To represent a fixed-size array with bounds checking | To perform bitwise operations | To read input from the user | To enable polymorphic behavior in derived classes | To represent a fixed-size array with bounds checking |
46. Which of the following is NOT a valid C++ type specifier? | int | char | bool | string | string |
47. What is the purpose of the std::move function in C++11 and later versions? | To enable efficient move semantics and transfer ownership of resources | To compare two variables | To perform arithmetic operations | To print output to the console | To enable efficient move semantics and transfer ownership of resources |
48. Which of the following is the correct syntax to define a template specialization in C++? | template <> class MyClass<int> { // class definition } | class MyClass template <> <int> { // class definition } | class MyClass<int> template <> { // class definition } | class MyClass<int> { // class definition } template <> | template <> class MyClass<int> { // class definition } |
49. What is the purpose of the std::unique_ptr class in C++11 and later versions? | To manage the ownership of dynamically allocated objects | To perform type casting | To define a base class | To control the flow of a loop | To manage the ownership of dynamically allocated objects |
50. Which of the following is the correct syntax to define a destructor in C++? | ~MyClass() { // destructor code } | ~MyClass::MyClass() { // destructor code } | MyClass()~ { // destructor code } | destructor MyClass() { // destructor code } | ~MyClass() { // destructor code } |
Question | Option A | Option B | Option C | Option D | Answer |
---|---|---|---|---|---|
51. What is the purpose of the static_assert keyword in C++11 and later versions? | To perform compile-time assertions | To compare two strings | To perform bitwise operations | To exit a loop or switch statement | To perform compile-time assertions |
52. Which of the following is NOT a valid C++ standard library algorithm? | sort | find | print | transform | print |
53. What is the purpose of the std::unordered_map container in C++? | To represent an unordered collection of key-value pairs | To perform arithmetic operations | To define a derived class | To control the flow of a loop | To represent an unordered collection of key-value pairs |
54. Which of the following is the correct way to declare a pure virtual function in C++? | virtual void myFunction() = 0; | void myFunction() virtual = 0; | void virtual myFunction() = 0; | myFunction() = 0 virtual void; | virtual void myFunction() = 0; |
55. What is the purpose of the std::initializer_list in C++11 and later versions? | To provide a convenient way to initialize a container or object with a list of values | To perform logical operations | To read input from the user | To define a base class | To provide a convenient way to initialize a container or object with a list of values |
56. Which of the following is NOT a valid C++ type qualifier? | const | volatile | explicit | mutable | explicit |
57. What is the purpose of the std::mutex class in C++11 and later versions? | To provide mutual exclusion and synchronization in multithreaded programs | To compare two variables | To perform mathematical calculations | To define a class template | To provide mutual exclusion and synchronization in multithreaded programs |
58. Which of the following is the correct syntax to define a friend class in C++? | friend class MyClass; | class friend MyClass; | class MyClass friend; | class MyClass; friend | friend class MyClass; |
59. What is the purpose of the std::unique_lock class in C++11 and later versions? | To provide a flexible mechanism for locking and unlocking mutexes | To perform bitwise operations | To specify the return type of a function | To exit a loop or switch statement | To provide a flexible mechanism for locking and unlocking mutexes |
60. Which of the following is the correct syntax to define a copy constructor in C++? | MyClass::MyClass(const MyClass& other) { // copy constructor code } | MyClass::copy() { // copy constructor code } | MyClass(const MyClass& other) { // copy constructor code } | copy::MyClass(const MyClass& other) { // copy constructor code } | MyClass::MyClass(const MyClass& other) { // copy constructor code } |
Question | Option A | Option B | Option C | Option D | Answer |
---|---|---|---|---|---|
61. What is the purpose of the constexpr keyword in C++11 and later versions? | To indicate that a function or variable can be evaluated at compile-time | To compare two variables | To perform arithmetic operations | To print output to the console | To indicate that a function or variable can be evaluated at compile-time |
62. Which of the following is NOT a valid C++ standard library container adapter? | stack | queue | deque | set | set |
63. What is the purpose of the std::function class in C++? | To represent a callable object or function | To perform string manipulation | To define a base class | To control the flow of a loop | To represent a callable object or function |
64. Which of the following is the correct way to define a move constructor in C++? | MyClass::MyClass(MyClass&& other) { // move constructor code } | MyClass::move() { // move constructor code } | MyClass(MyClass&& other) { // move constructor code } | move::MyClass(MyClass&& other) { // move constructor code } | MyClass::MyClass(MyClass&& other) { // move constructor code } |
65. What is the purpose of the std::tuple class in C++11 and later versions? | To create a heterogeneous collection of values | To perform bitwise operations | To read input from the user | To define a derived class | To create a heterogeneous collection of values |
66. Which of the following is NOT a valid C++ exception handling mechanism? | try | catch | throw | finally | finally |
67. What is the purpose of the std::atomic class in C++11 and later versions? | To provide atomic operations on shared variables in multithreaded programs | To compare two strings | To perform bitwise operations | To define a class template | To provide atomic operations on shared variables in multithreaded programs |
68. Which of the following is the correct syntax to define a conversion operator in C++? | operator int() const { // conversion code } | int operator() const { // conversion code } | int() operator const { // conversion code } | int() const operator { // conversion code } | operator int() const { // conversion code } |
69. What is the purpose of the std::bitset class in C++? | To represent a fixed-size sequence of bits | To perform logical operations | To perform mathematical calculations | To exit a loop or switch statement | To represent a fixed-size sequence of bits |
70. Which of the following is the correct way to define an alias for a type in C++? | using NewType = OldType; | type NewType = OldType; | alias NewType = OldType; | OldType as NewType; | using NewType = OldType; |
Question | Option A | Option B | Option C | Option D | Answer |
---|---|---|---|---|---|
71. What is the purpose of the const_iterator in C++? | To provide read-only access to the elements of a container | To perform arithmetic operations | To define a derived class | To control the flow of a loop | To provide read-only access to the elements of a container |
72. Which of the following is NOT a valid C++ standard library algorithm? | for_each | transform | find_first_of | execute | execute |
73. What is the purpose of the std::shared_ptr class in C++? | To manage shared ownership of dynamically allocated objects | To compare two variables | To perform type casting | To print output to the console | To manage shared ownership of dynamically allocated objects |
74. Which of the following is the correct way to define a virtual destructor in C++? | virtual ~MyClass() { // destructor code } | virtual MyClass::~MyClass() { // destructor code } | ~MyClass virtual() { // destructor code } | destructor ~MyClass() { // destructor code } | virtual ~MyClass() { // destructor code } |
75. What is the purpose of the std::vector container in C++? | To represent a dynamic-size array | To perform bitwise operations | To read input from the user | To enable polymorphic behavior in derived classes | To represent a dynamic-size array |
76. Which of the following is NOT a valid C++ type qualifier? | const | volatile | explicit | static | explicit |
77. What is the purpose of the std::function class template in C++11 and later versions? | To store and invoke callable objects with different types | To perform string manipulation | To define a base class | To control the flow of a loop | To store and invoke callable objects with different types |
78. Which of the following is the correct syntax to define a friend function in C++? | friend void myFunction(); | myFunction() friend void; | void myFunction() friend; | void friend myFunction(); | friend void myFunction(); |
79. What is the purpose of the std::queue container adapter in C++? | To represent a first-in, first-out (FIFO) data structure | To perform arithmetic operations | To define a derived class | To exit a loop or switch statement | To represent a first-in, first-out (FIFO) data structure |
80. Which of the following is the correct way to define a template specialization in C++? | template <> class MyClass<T> { // class definition } | class MyClass<T> template <> { // class definition } | class MyClass<T> template { // class definition } | template <> class<T> MyClass { // class definition } | template <> class MyClass<T> { // class definition } |
Question | Option A | Option B | Option C | Option D | Answer |
---|---|---|---|---|---|
81. What is the purpose of the decltype keyword in C++? | To deduce the type of an expression at compile-time | To compare two strings | To perform bitwise operations | To exit a loop or switch statement | To deduce the type of an expression at compile-time |
82. Which of the following is NOT a valid C++ standard library container? | list | array | sequence | map | sequence |
83. What is the purpose of the std::unordered_set container in C++? | To represent an unordered collection of unique elements | To perform logical operations | To read input from the user | To define a base class | To represent an unordered collection of unique elements |
84. Which of the following is the correct way to define a default constructor in C++? | MyClass() { // constructor code } | MyClass::MyClass() { // constructor code } | constructor MyClass() { // constructor code } | default MyClass() { // constructor code } | MyClass() { // constructor code } |
85. What is the purpose of the std::list container in C++? | To represent a doubly-linked list | To perform bitwise operations | To perform mathematical calculations | To enable polymorphic behavior in derived classes | To represent a doubly-linked list |
86. Which of the following is NOT a valid C++ type qualifier? | const | volatile | explicit | final | explicit |
87. What is the purpose of the std::condition_variable class in C++11 and later versions? | To synchronize the execution of multiple threads | To compare two variables | To perform arithmetic operations | To define a class template | To synchronize the execution of multiple threads |
88. Which of the following is the correct syntax to define a lambda function in C++? | [capture-list](parameters) { // lambda body } | (parameters) [capture-list] { // lambda body } | { // lambda body } (parameters) [capture-list] | { // lambda body } [capture-list] (parameters) | [capture-list](parameters) { // lambda body } |
89. What is the purpose of the std::priority_queue container adapter in C++? | To represent a priority queue | To perform bitwise operations | To specify the return type of a function | To control the flow of a loop | To represent a priority queue |
90. Which of the following is the correct syntax to define a template function in C++? | template <typename T> void myFunction(T value) { // function code } | void myFunction<template T>(T value) { // function code } | void myFunction<T template>(T value) { // function code } | void template <typename T> myFunction(T value) { // function code } | template <typename T> void myFunction(T value) { // function code } |
Question | Option A | Option B | Option C | Option D | Answer |
---|---|---|---|---|---|
91. What is the purpose of the std::set container in C++? | To represent a sorted set of unique elements | To perform arithmetic operations | To read input from the user | To define a base class | To represent a sorted set of unique elements |
92. Which of the following is NOT a valid C++ standard library algorithm? | reverse | accumulate | count_if | execute | execute |
93. What is the purpose of the std::unique_ptr class in C++? | To manage exclusive ownership of dynamically allocated objects | To compare two variables | To perform type casting | To print output to the console | To manage exclusive ownership of dynamically allocated objects |
94. Which of the following is the correct way to define an abstract base class in C++? | class AbstractClass { virtual void myFunction() = 0; }; | class AbstractClass { abstract void myFunction(); }; | abstract class AbstractClass { void myFunction() = 0; }; | class AbstractClass { void myFunction() abstract = 0; }; | class AbstractClass { virtual void myFunction() = 0; }; |
95. What is the purpose of the std::array container in C++? | To represent a fixed-size array | To perform bitwise operations | To perform mathematical calculations | To exit a loop or switch statement | To represent a fixed-size array |
96. Which of the following is NOT a valid C++ type qualifier? | const | volatile | explicit | virtual | explicit |
97. What is the purpose of the std::atomic_flag class in C++11 and later versions? | To provide atomic flag-based synchronization in multithreaded programs | To compare two strings | To perform bitwise operations | To define a class template | To provide atomic flag-based synchronization in multithreaded programs |
98. Which of the following is the correct syntax to define a member function of a class in C++? | return_type ClassName::functionName(parameters) { // function code } | ClassName return_type::functionName(parameters) { // function code } | functionName(return_type) ClassName::parameters { // function code } | parameters ClassName::functionName(return_type) { // function code } | return_type ClassName::functionName(parameters) { // function code } |
99. What is the purpose of the std::bitset class in C++? | To represent a fixed-size sequence of bits | To perform logical operations | To perform mathematical calculations | To enable polymorphic behavior in derived classes | To represent a fixed-size sequence of bits |
100. Which of the following is the correct way to define a lambda function with capture in C++? | [capture-list](parameters) { // lambda body } | (parameters) {capture-list} { // lambda body } | { // lambda body } (capture-list) (parameters) | {capture-list} (parameters) { // lambda body } | [capture-list](parameters) { // lambda body } |