100 Multiple Choice Questions & Answers on C++

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.

QuestionOption AOption BOption COption DAnswer
1. What is the purpose of the new operator in C++?To dynamically allocate memory for objectsTo perform arithmetic operationsTo compare two variablesTo print output to the consoleTo dynamically allocate memory for objects
2. Which of the following is NOT a fundamental data type in C++?booleancharintfloatboolean
3. What is the purpose of the static keyword in C++?To declare a variable or function with static storage durationTo define a class templateTo perform type castingTo control the flow of a loopTo 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 variableTo perform bitwise operationsTo define a class constructorTo convert a data type to a stringTo 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 classesTo specify a base classTo perform logical operationsTo read input from the userTo enable polymorphic behavior in derived classes
8. Which of the following is NOT a valid access specifier in C++?publicprivateprotectedinternalinternal
9. What is the purpose of the this pointer in C++?To refer to the current objectTo perform string manipulationTo perform type castingTo exit a loop or switch statementTo 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;
QuestionOption AOption BOption COption DAnswer
11. What is the purpose of the iostream library in C++?To provide input/output functionalityTo perform mathematical operationsTo manage memory allocationTo define classes and objectsTo 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 handlingTo compare two stringsTo control the flow of a loopTo declare and define a functionTo 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 classesTo perform logical operationsTo define a derived classTo read input from the userTo perform safe downcasting between polymorphic classes
16. Which of the following is NOT a valid storage class specifier in C++?autostaticregisterconstantconstant
17. What is the purpose of the namespace keyword in C++?To define a named scopeTo specify the return type of a functionTo perform type castingTo exit a loop or switch statementTo 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 objectTo compare two variablesTo perform bitwise operationsTo convert a data type to a stringTo 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();
QuestionOption AOption BOption COption DAnswer
21. What is the purpose of the typeid operator in C++?To retrieve the type information of an objectTo perform arithmetic operationsTo compare two variables for equalityTo print output to the consoleTo retrieve the type information of an object
22. Which of the following is NOT a valid C++ access modifier?defaultpublicprivateprotecteddefault
23. What is the purpose of the inline keyword in C++?To suggest the compiler to perform inline expansion of a functionTo define a class templateTo perform type castingTo control the flow of a loopTo 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 constructionTo perform bitwise operationsTo read input from the userTo define a base classTo 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 exceptionTo compare two stringsTo perform mathematical calculationsTo exit a loop or switch statementTo manually throw an exception
28. Which of the following is NOT a valid pointer type in C++?nullptrvoid*int*char*nullptr
29. What is the purpose of the decltype keyword in C++?To deduce the type of an expression at compile-timeTo perform logical operationsTo define a derived classTo perform string manipulationTo 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 };
QuestionOption AOption BOption COption DAnswer
31. What is the purpose of the sizeof operator in C++?To determine the size of a data type or objectTo perform arithmetic operationsTo compare two variables for equalityTo print output to the consoleTo determine the size of a data type or object
32. Which of the following is NOT a valid C++ type qualifier?constvolatilemutableabstractabstract
33. What is the purpose of the default keyword in C++11 and later versions?To define a default constructor or assignment operatorTo define a virtual functionTo perform type castingTo exit a loop or switch statementTo 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 bodyparameters [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 functionTo compare two stringsTo perform bitwise operationsTo define a derived classTo 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-qualifiersTo perform logical operationsTo read input from the userTo define a base classTo deduce the type of an expression, including reference and cv-qualifiers
38. Which of the following is NOT a valid C++ standard library container?ListVectorMapSetList
39. What is the purpose of the std::move function in C++?To enable efficient move semantics and transfer ownership of resourcesTo compare two variablesTo perform mathematical calculationsTo exit a loop or switch statementTo 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 }
QuestionOption AOption BOption COption DAnswer
41. What is the purpose of the std::thread class in C++11 and later versions?To create and manage concurrent execution of tasksTo perform string manipulationTo define a derived classTo control the flow of a loopTo create and manage concurrent execution of tasks
42. Which of the following is NOT a valid C++ access specifier?publicprivateprotectedinternalinternal
43. What is the purpose of the nullptr keyword in C++11 and later versions?To represent a null pointerTo perform logical operationsTo define a class constructorTo convert a data type to a stringTo 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 checkingTo perform bitwise operationsTo read input from the userTo enable polymorphic behavior in derived classesTo represent a fixed-size array with bounds checking
46. Which of the following is NOT a valid C++ type specifier?intcharboolstringstring
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 resourcesTo compare two variablesTo perform arithmetic operationsTo print output to the consoleTo 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 objectsTo perform type castingTo define a base classTo control the flow of a loopTo 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 }
QuestionOption AOption BOption COption DAnswer
51. What is the purpose of the static_assert keyword in C++11 and later versions?To perform compile-time assertionsTo compare two stringsTo perform bitwise operationsTo exit a loop or switch statementTo perform compile-time assertions
52. Which of the following is NOT a valid C++ standard library algorithm?sortfindprinttransformprint
53. What is the purpose of the std::unordered_map container in C++?To represent an unordered collection of key-value pairsTo perform arithmetic operationsTo define a derived classTo control the flow of a loopTo 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 valuesTo perform logical operationsTo read input from the userTo define a base classTo 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?constvolatileexplicitmutableexplicit
57. What is the purpose of the std::mutex class in C++11 and later versions?To provide mutual exclusion and synchronization in multithreaded programsTo compare two variablesTo perform mathematical calculationsTo define a class templateTo 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; friendfriend 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 mutexesTo perform bitwise operationsTo specify the return type of a functionTo exit a loop or switch statementTo 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 }
QuestionOption AOption BOption COption DAnswer
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-timeTo compare two variablesTo perform arithmetic operationsTo print output to the consoleTo 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?stackqueuedequesetset
63. What is the purpose of the std::function class in C++?To represent a callable object or functionTo perform string manipulationTo define a base classTo control the flow of a loopTo 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 valuesTo perform bitwise operationsTo read input from the userTo define a derived classTo create a heterogeneous collection of values
66. Which of the following is NOT a valid C++ exception handling mechanism?trycatchthrowfinallyfinally
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 programsTo compare two stringsTo perform bitwise operationsTo define a class templateTo 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 bitsTo perform logical operationsTo perform mathematical calculationsTo exit a loop or switch statementTo 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;
QuestionOption AOption BOption COption DAnswer
71. What is the purpose of the const_iterator in C++?To provide read-only access to the elements of a containerTo perform arithmetic operationsTo define a derived classTo control the flow of a loopTo provide read-only access to the elements of a container
72. Which of the following is NOT a valid C++ standard library algorithm?for_eachtransformfind_first_ofexecuteexecute
73. What is the purpose of the std::shared_ptr class in C++?To manage shared ownership of dynamically allocated objectsTo compare two variablesTo perform type castingTo print output to the consoleTo 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 arrayTo perform bitwise operationsTo read input from the userTo enable polymorphic behavior in derived classesTo represent a dynamic-size array
76. Which of the following is NOT a valid C++ type qualifier?constvolatileexplicitstaticexplicit
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 typesTo perform string manipulationTo define a base classTo control the flow of a loopTo 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 structureTo perform arithmetic operationsTo define a derived classTo exit a loop or switch statementTo 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 }
QuestionOption AOption BOption COption DAnswer
81. What is the purpose of the decltype keyword in C++?To deduce the type of an expression at compile-timeTo compare two stringsTo perform bitwise operationsTo exit a loop or switch statementTo deduce the type of an expression at compile-time
82. Which of the following is NOT a valid C++ standard library container?listarraysequencemapsequence
83. What is the purpose of the std::unordered_set container in C++?To represent an unordered collection of unique elementsTo perform logical operationsTo read input from the userTo define a base classTo 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 listTo perform bitwise operationsTo perform mathematical calculationsTo enable polymorphic behavior in derived classesTo represent a doubly-linked list
86. Which of the following is NOT a valid C++ type qualifier?constvolatileexplicitfinalexplicit
87. What is the purpose of the std::condition_variable class in C++11 and later versions?To synchronize the execution of multiple threadsTo compare two variablesTo perform arithmetic operationsTo define a class templateTo 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 queueTo perform bitwise operationsTo specify the return type of a functionTo control the flow of a loopTo 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 }
QuestionOption AOption BOption COption DAnswer
91. What is the purpose of the std::set container in C++?To represent a sorted set of unique elementsTo perform arithmetic operationsTo read input from the userTo define a base classTo represent a sorted set of unique elements
92. Which of the following is NOT a valid C++ standard library algorithm?reverseaccumulatecount_ifexecuteexecute
93. What is the purpose of the std::unique_ptr class in C++?To manage exclusive ownership of dynamically allocated objectsTo compare two variablesTo perform type castingTo print output to the consoleTo 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 arrayTo perform bitwise operationsTo perform mathematical calculationsTo exit a loop or switch statementTo represent a fixed-size array
96. Which of the following is NOT a valid C++ type qualifier?constvolatileexplicitvirtualexplicit
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 programsTo compare two stringsTo perform bitwise operationsTo define a class templateTo 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 bitsTo perform logical operationsTo perform mathematical calculationsTo enable polymorphic behavior in derived classesTo 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 }

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Python is a versatile and widely-used programming language known for its simplicity and readability. Whether you’re a beginner or an...
Are you ready to test your knowledge and sharpen your skills in C# programming? Whether you’re a beginner looking to...
Are you ready to test your knowledge of Java? Whether you’re a beginner seeking to solidify your understanding of the...