C++ OOPs MCQ Questions and Answers – Part 3
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 output of the following C++ code?
#include <iostream> using namespace std; class A { static int val; public: void write(int i){ val = i; } void read(){ cout << val; } }; int main(int argc, char const *argv[]) { A a = A(); a.write(10); a.read(); return 0; }
A 10
B Segmentation fault
C Compilation error
D None of the above
[st_adsense]
2. What is the output of the following C++ code?
#include <iostream> using namespace std; class A { static int val; public: void write(int i){ val = i; } void read(){ cout << val; } }; int A::val = 55; int main(int argc, char const *argv[]) { A a = A(); a.write(10); a.read(); return 0; }
A 55
B 10
C Compilation error
D None of the above
3. What is the output of the following C++ code?
#include <iostream> using namespace std; class A { int val = 55; public: void write(int i){ val = i; } static void read(){ cout << val; } }; int main(int argc, char const *argv[]) { A a = A(); a.write(10); a.read(); return 0; }
A 55
B 10
C Compilation error
D None of the above
[st_adsense]
4. When allocating memory dynamically in C++, the new operator returns the value _________ if the memory allocation fails.
A False
B NULL
C Zero
D None of the above
5. We can create objects of type ‘abstract class’?
A True
B False
6. The default value of a static variable is _____.
A 1
B 0
C Depends on the compiler
D None of the above
[st_adsense]
7. What is the output of the following C++ code?
#include<iostream> using namespace std; int a = 55; void fun() { int a = 20; { int a = 10; cout << ::a << endl; } } int main() { fun(); return 0; }
A 10
B 20
C 55
D None of the above
8. Which class functions are called inline functions?
A All functions declared inside a class
B All functions defined outside the class of
C All functions defined inside or with the keyword inline
D All functions accessing static members of the class.
9. The object is created in _________
A A class
B Constructor
C Destructor
D Attributes
[st_adsense]
10. What is the output of the following C++ code?
#include<iostream> using namespace std; class Point { private: int x; int y; public: Point(int a, int b); }; Point::Point(int a = 0, int b = 0) { x = a; y = b; cout << "Constructor called"; } int main() { Point p1, *p2; return 0; }
A Displays “Constructor called” only once.
B Displays “Constructor called” twice.
C Does not display anything.
D Compilation error
[st_adsense]