C++ OOPs MCQ Questions with Answers – Part 5
Multiple choice questions and answers (MCQs) on C++ to prepare for exams, tests, and certifications. These questions are taken from a real written exam and some parts are taken from an interview. So you will find questions on basic techniques such as Variables, Operators, Conditional Statement, Functions, and more. This quiz will easily prepare anyone to pass their online test.
1. What is the syntax to override the + operator for class A?
A int +(arguments){}
B int [+](arguments){}
C A operator+(arguments){}
D A operator[+](arguments){}
2. What is the output of the following C++ code?
#include<iostream> using namespace std; class MyClass { public: MyClass(); }; MyClass::MyClass() { cout << "Constructor is called\n"; } int main() { cout << " Before \n"; MyClass myclass(); cout << " After \n"; return 0; }
A Before
Constructor is called
After
B Before
After
C Constructor is called
Before
After
D None of the above.
3. What is the output of the following C++ code?
#include<iostream> using namespace std; class MyClass { int val; public: MyClass (int v = 0) {val = v;} int getValue() { return val; } }; int main() { const MyClass obj; cout << obj.getValue(); return 0; }
A 0
B Random value
C Compilation error
D None of the above
4. Which of the following operators cannot be overloaded?
A ? :
B +
C –
D %
5. Which of the following operators can be overloaded?
A ? :
B ::
C .
D ==
6. What is the output of the following C++ code?
#include<iostream> using namespace std; class MyClass { int &val; public: MyClass (int &v) { val = v; } int getValue() { return val; } }; int main() { int v = 10; MyClass obj(v); cout << obj.getValue() << " "; v = 20; cout << obj.getValue() << endl; return 0; }
A 10
B 20
C 10 20
D Compilation error
7. An exception is generated using the keyword __________
A throws
B throw
C threw
D Thrown
8. If the default constructor is not defined, how will the objects of the class be created?
A An error will occur at runtime.
B The compiler will generate an error
C The compiler provides its default constructor to build the object.
D None of the above
9. What is the output of the following code?
#include <iostream> using namespace std; class MyClass { private: static const int val = 10; public: static int getValue() { return val; } }; int main() { cout << MyClass::getValue() << endl; return 0; }
A 10
B 0
C Compilation error
D None of the above
10. Is it mandatory to call a constructor to create an object?
A Yes
B No