This comprehensive set of MCQs on C++ is designed to cover all essential topics necessary for mastering the principles and practices of this powerful programming language. Focused on key subjects such as object-oriented programming, data types, control structures, functions, and standard libraries, these MCQs aim to help students and professionals build a strong foundation in C++, which is widely used in software development, game programming, and systems design.
Who should practice C++ MCQs?
• Students pursuing degrees in computer science, software engineering, or information technology who need to understand the fundamentals of C++.
• Individuals preparing for exams or certifications that include C++ programming as part of their curriculum.
• Anyone aiming to strengthen their coding skills and proficiency in object-oriented programming concepts.
• Candidates looking to improve their problem-solving abilities and algorithmic thinking using C++.
• Professionals seeking to enhance their programming skills and explore advanced features of C++ for practical applications.
• Suitable for all students eager to develop their technical skills and gain confidence in using C++ for various programming challenges.
 1. Which of the following is the correct syntax for a C++ comment?
A. // Comment
B. /* Comment */
C. Both A and B
D. None of the above
View AnswerC
2. What is the size of an int data type in C++?
A. 2 bytes
B. 4 bytes
C. 8 bytes
D. Depends on the system/compiler
View AnswerD
3. Which of the following is used to define a constant in C++?
A. #define
B. const
C. Both A and B
D. None of the above
View AnswerC
4. Which of the following is not a valid C++ identifier?
A. variable_1
B. _variable
C. 1_variable
D. variable1
View AnswerC
5. Which keyword is used to declare a class in C++?
A. struct
B. class
C. object
D. typedef
View AnswerB
6. What is the correct syntax for a for loop in C++?
A. for (initialization; condition; increment)
B. for (condition; initialization; increment)
C. for (initialization; increment; condition)
D. for (increment; condition; initialization)
View AnswerA
7. Which function is used to read a single character from the user in C++?
A. getchar()
B. get()
C. cin.get()
D. scanf()
View AnswerC
8. What will be the output of the following code?
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 10;
cout << a + b << endl;
return 0;
}
A. 5
B. 10
C. 15
D. 20
View AnswerC
9. Which of the following is used to allocate memory dynamically in C++?
A. malloc
B. alloc
C. new
D. create
View AnswerC
10. What is the default return type of the main function in C++?
A. void
B. int
C. float
D. char
View AnswerB
11. Which of the following operators cannot be overloaded in C++?
A. +
B. =
C. >
D. ::
View AnswerD
12. What is the output of the following code?
#include <iostream>
using namespace std;
void foo(int &a) {
a = a + 10;
}
int main() {
int x = 5;
foo(x);
cout << x << endl;
return 0;
}
A. 5
B. 10
C. 15
D. 20
View AnswerC
13. Which header file is needed to use the std::vector class?
A. <vector>
B. <list>
C. <map>
D. <queue>
View AnswerA
14. Which function is used to find the length of a string in C++?
A. strlen()
B. length()
C. size()
D. Both B and C
View AnswerD
15. Which of the following is not a fundamental data type in C++?
A. int
B. float
C. string
D. double
View AnswerC
16. What is the output of the following code?
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
cout << (a > b ? a : b) << endl;
return 0;
}
A. 10
B. 20
C. 30
D. None of the above
View AnswerB
17. Which of the following is a valid function prototype in C++?
A. int func();
B. int func(int a);
C. void func(double a, int b);
D. All of the above
View AnswerD
18. Which of the following is the correct syntax for a destructor in C++?
A. ~ClassName()
B. ClassName()
C. #ClassName()
D. *ClassName()
View AnswerA
19. What is the correct way to declare a constant pointer to an integer in C++?
A. int* const ptr;
B. const int* ptr;
C. const int* const ptr;
D. int const* ptr;
View AnswerA
20. Which of the following access specifiers is used as default in a class in C++?
A. public
B. private
C. protected
D. friend
View AnswerB
21. Which of the following statements is true about inline functions in C++?
A. They improve performance by eliminating function call overhead.
B. They increase the size of the binary file.
C. They can be defined inside the class definition.
D. All of the above
View AnswerD
22. Which of the following keywords is used to inherit a class in C++?
A. inherits
B. extends
C. implements
D. :
View AnswerD
23. What is the correct syntax to declare a friend function in C++?
A. friend return_type function_name();
B. friend function_name();
C. return_type friend function_name();
D. friend return_type function_name;
View AnswerA
24. Which of the following is not a valid type of inheritance in C++?
A. Single inheritance
B. Multiple inheritance
C. Multilevel inheritance
D. Hybrid inheritance
View AnswerD
25. What is the output of the following code?
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() {
cout << "Base class" << endl;
}
};
class Derived : public Base {
public:
void show() {
cout << "Derived class" << endl;
}
};
int main() {
Base* b;
Derived d;
b = &d;
b->show();
return 0;
}
A. Base class
B. Derived class
C. Compiletime error
D. Runtime error
View AnswerB
26. Which of the following is true about virtual functions in C++?
A. They can be overridden in derived classes.
B. They must be declared in the base class.
C. They ensure dynamic binding.
D. All of the above
View AnswerD
27. Which of the following operators is used to access members of a structure through a pointer in C++?
A. .
B. >
C. *
D. &
View AnswerB
28. What is the output of the following code?
#include <iostream>
using namespace std;
void func(int a, int b = 20) {
cout << a << " " << b << endl;
}
int main() {
func(10);
return 0;
}
A. 10 20
B. 10 0
C. 10 10
D. Compiletime error
View AnswerA
29. Which of the following is used to terminate a loop in C++?
A. break
B. exit
C. return
D. All of the above
View AnswerA
30. Which of the following is not a member of the iostream library in C++?
A. cin
B. cout
C. cerr
D. printf
View AnswerD
31. Which of the following is not a standard exception class in C++?
A. std::bad_alloc
B. std::out_of_range
C. std::bad_exception
D. std::not_found
View AnswerD
32. Which of the following functions is used to generate random numbers in C++?
A. random()
B. srand()
C. rand()
D. generate_random()
View AnswerC
33. What is the output of the following code?
#include <iostream>
using namespace std;
class Test {
public:
Test() {
cout << "Constructor called" << endl;
}
~Test() {
cout << "Destructor called" << endl;
}
};
int main() {
Test t;
return 0;
}
A. Constructor called
B. Destructor called
C. Constructor called Destructor called
D. Runtime error
View AnswerC
34. Which of the following is the correct way to create an array of 10 integers in C++?
A. int arr[10];
B. int arr = new int[10];
C. int arr[] = new int[10];
D. int arr[10] = {0};
View AnswerA
35. Which of the following statements is true about C++ arrays?
A. The size of the array must be determined at compile time.
B. The size of the array can be determined at runtime.
C. The size of the array can be changed dynamically.
D. Arrays can store elements of different types.
View AnswerA
36. What will be the output of the following code?
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40, 50};
cout << arr[2] << endl;
return 0;
}
A. 10
B. 20
C. 30
D. 40
View AnswerC
37. Which of the following is not a storage class in C++?
A. auto
B. register
C. static
D. local
View AnswerD
38. What is the purpose of the friend keyword in C++?
A. To declare a class as a friend of another class
B. To allow a function or class to access private or protected members of another class
C. To create a new data type
D. To create a global variable
View AnswerB
39. Which of the following is the correct syntax for a switch statement in C++?
A. switch (expression) { case value1: statement; break; }
B. switch { case value1: statement; break; }
C. switch (expression) { value1: statement; break; }
D. switch (value1) { case: statement; break; }
View AnswerA
40. What is the output of the following code?
#include <iostream>
using namespace std;
int main() {
int a = 10;
int* p = &a;
cout << *p << endl;
return 0;
}
A. 10
B. Address of a
C. Compiletime error
D. Runtime error
View AnswerA
41. Which of the following best describes encapsulation in OOP?
A. Combining data and methods into a single unit
B. Deriving a new class from an existing class
C. Hiding implementation details from the user
D. Using a common interface for multiple implementations
View AnswerA
42. What is polymorphism in OOP?
A. The ability to create multiple classes from a base class
B. The ability to process objects differently based on their data type or class
C. The ability to bind data and methods into a single unit
D. The ability to hide implementation details from the user
View AnswerB
43. Which of the following is not a principle of OOP?
A. Encapsulation
B. Inheritance
C. Polymorphism
D. Compilation
View AnswerD
44. What is inheritance in OOP?
A. The ability to create multiple classes from a base class
B. The ability to process objects differently based on their data type or class
C. The ability to bind data and methods into a single unit
D. The ability to hide implementation details from the user
View AnswerA
45. Which keyword is used to inherit a class in C++?
A. inherits
B. extends
C. :
D. implements
View AnswerC
46. Which of the following is true about abstract classes in C++?
A. They cannot be instantiated.
B. They can have both pure virtual functions and nonpure virtual functions.
C. They are intended to be base classes.
D. All of the above
View AnswerD
47. What is the purpose of a constructor in a class?
A. To initialize the object of the class
B. To destroy the object of the class
C. To allocate memory for the object
D. To define the interface of the class
View AnswerA
48. Which of the following is true about destructors in C++?
A. They are automatically called when an object is destroyed.
B. They can be overloaded.
C. They can have parameters.
D. They must be explicitly called.
View AnswerA
49. What is the output of the following code?
#include <iostream>
using namespace std;
class Base {
public:
virtual void display() {
cout << "Base class" << endl;
}
};
class Derived : public Base {
public:
void display() {
cout << "Derived class" << endl;
}
};
int main() {
Base* b;
Derived d;
b = &d;
b->display();
return 0;
}
A. Base class
B. Derived class
C. Compiletime error
D. Runtime error
View AnswerB
50. Which of the following is used to achieve runtime polymorphism in C++?
A. Function overloading
B. Operator overloading
C. Virtual functions
D. Inline functions
View AnswerC
51. Which of the following is not a type of inheritance in OOP?
A. Single inheritance
B. Multiple inheritance
C. Multilevel inheritance
D. Hierarchical inheritance
View AnswerD
52. Which of the following is an example of function overloading?
A. Two functions with the same name but different parameters
B. Two functions with different names but same parameters
C. Two functions with the same name and same parameters
D. Two functions with different return types
View AnswerA
53. What is the main purpose of operator overloading in C++?
A. To provide new implementations for existing operators
B. To allow operators to be used with userdefined data types
C. To increase the performance of operators
D. To create new operators
View AnswerB
54. Which of the following best describes a pure virtual function in C++?
A. A function with no implementation in the base class
B. A function that can be overridden in derived classes
C. A function that must be implemented in derived classes
D. Both A and C
View AnswerD
55. Which of the following is not a characteristic of an abstract class in C++?
A. It cannot be instantiated.
B. It can have constructors.
C. It can have pure virtual functions.
D. It can have only pure virtual functions.
View AnswerD
56. What is the output of the following code?
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() {
cout << "Base" << endl;
}
};
class Derived : public Base {
public:
void show() {
cout << "Derived" << endl;
}
};
int main() {
Base* b = new Derived();
b->show();
delete b;
return 0;
}
A. Base
B. Derived
C. Compiletime error
D. Runtime error
View AnswerB
Â
57. What is the purpose of the this pointer in C++?
A. To refer to the current object
B. To refer to the base class
C. To refer to the derived class
D. To refer to a global object
View AnswerA
58. Which of the following best describes inheritance in OOP?
A. It allows a class to use the properties and methods of another class.
B. It allows a class to hide its implementation details.
C. It allows a class to implement multiple interfaces.
D. It allows a class to override methods of another class.
View AnswerA
59. Which of the following best describes encapsulation in OOP?
A. It allows a class to use the properties and methods of another class.
B. It allows a class to hide its implementation details.
C. It allows a class to implement multiple interfaces.
D. It allows a class to override methods of another class.
View AnswerB
60. Which of the following is not a type of polymorphism in OOP?
A. Compiletime polymorphism
B. Runtime polymorphism
C. Virtual polymorphism
D. Both A and B
View AnswerC
61. What is the purpose of a virtual destructor in C++?
A. To prevent memory leaks when deleting a base class pointer pointing to a derived class object
B. To allow the destructor to be overridden in derived classes
C. To improve performance of destructors
D. To provide a default destructor implementation
View AnswerA
62. Which of the following is true about multiple inheritance in C++?
A. A class can inherit from multiple classes.
B. A class can only inherit from one class.
C. Multiple inheritance is not supported in C++.
D. Multiple inheritance is supported but not recommended.
View AnswerA
63. What is the output of the following code?
#include <iostream>
using namespace std;
class A {
public:
void show() {
cout << "Class A" << endl;
}
};
class B : public A {
public:
void show() {
cout << "Class B" << endl;
}
};
int main() {
B obj;
obj.show();
return 0;
}
A. Class A
B. Class B
C. Compiletime error
D. Runtime error
View AnswerB
64. Which of the following is not a benefit of using OOP?
A. Reusability
B. Scalability
C. Security
D. Speed
View AnswerD
65. What is the output of the following code?
#include <iostream>
using namespace std;
class Base {
public:
virtual void display() = 0;
};
class Derived : public Base {
public:
void display() {
cout << "Derived class" << endl;
}
};
int main() {
Derived d;
d.display();
return 0;
}
A. Base class
B. Derived class
C. Compiletime error
D. Runtime error
View AnswerB
66. Which of the following is not a feature of OOP?
A. Encapsulation
B. Inheritance
C. Polymorphism
D. Concurrency
View AnswerD
67. Which of the following is true about constructors in C++?
A. They can be overloaded.
B. They are automatically called when an object is created.
C. They cannot be virtual.
D. All of the above
View AnswerD
68. Which of the following is true about destructors in C++?
A. They are automatically called when an object is destroyed.
B. They can be overloaded.
C. They can have parameters.
D. They must be explicitly called.
View AnswerA
69. Which of the following best describes a pure virtual function in C++?
A. A function with no implementation in the base class
B. A function that can be overridden in derived classes
C. A function that must be implemented in derived classes
D. Both A and C
View AnswerD
70. What is the output of the following code?
#include <iostream>
using namespace std;
class A {
public:
virtual void show() = 0;
};
class B : public A {
public:
void show() {
cout << "Class B" << endl;
}
};
int main() {
A* a = new B();
a->show();
delete a;
return 0;
}
A. Class A
B. Class B
C. Compiletime error
D. Runtime error
View AnswerB
71. Which of the following is true about abstract classes in C++?
A. They cannot be instantiated.
B. They can have both pure virtual functions and nonpure virtual functions.
C. They are intended to be base classes.
D. All of the above
View AnswerD
72. Which of the following is not a type of inheritance in C++?
A. Single inheritance
B. Multiple inheritance
C. Multilevel inheritance
D. Hybrid inheritance
View AnswerD
73. Which of the following best describes polymorphism in OOP?
A. The ability to create multiple classes from a base class
B. The ability to process objects differently based on their data type or class
C. The ability to bind data and methods into a single unit
D. The ability to hide implementation details from the user
View AnswerB
74. Which of the following is not a principle of OOP?
A. Encapsulation
B. Inheritance
C. Polymorphism
D. Compilation
View AnswerD
75. Which of the following is true about virtual functions in C++?
A. They can be overridden in derived classes.
B. They must be declared in the base class.
C. They ensure dynamic binding.
D. All of the above
View AnswerD
76. What is the purpose of the this pointer in C++?
A. To refer to the current object
B. To refer to the base class
C. To refer to the derived class
D. To refer to a global object
View AnswerA
77. Which of the following is true about inheritance in OOP?
A. It allows a class to use the properties and methods of another class.
B. It allows a class to hide its implementation details.
C. It allows a class to implement multiple interfaces.
D. It allows a class to override methods of another class.
View AnswerA
78. Which of the following is true about encapsulation in OOP?
A. It allows a class to use the properties and methods of another class.
B. It allows a class to hide its implementation details.
C. It allows a class to implement multiple interfaces.
D. It allows a class to override methods of another class.
View AnswerB
79. Which of the following is not a type of polymorphism in OOP?
A. Compiletime polymorphism
B. Runtime polymorphism
C. Virtual polymorphism
D. Both A and B
View AnswerC
80. What is the purpose of a virtual destructor in C++?
A. To prevent memory leaks when deleting a base class pointer pointing to a derived class object
B. To allow the destructor to be overridden in derived classes
C. To improve performance of destructors
D. To provide a default destructor implementation
View AnswerA