C++ OOPs MCQ Questions and Answers – Part 1
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 does a class contain in C ++?
A Data
B Functions
C Data and Functions
D Arrays
2. How many access specifiers are there?
A 1
B 2
C 3
D 4
3. ___ is used to define a member of a class externally?
A #
B ::
C !! $
D :
4. What operator does a pointer object of a class use to access its data members and member functions?
A ::
B .
C ->
D :
5. Suppose the integers take 4 bytes, what is the output of the following code?
#include<iostream> using namespace std; class MyClass { static int a; int b; }; int MyClass::a; int main() { cout << sizeof(MyClass); return 0; }
A 4
B 8
C 16
D None of the above
6. What is the output of the following C++ code?
#include <iostream> using namespace std; class MyClass { int var; public: int write(int i) const { var = i; } int read() const { return var; } }; int main(int argc, char const *argv[]) { MyClass obj; obj.write(2); cout << obj.read(); }
A 2
B 3
C Compilation error
D None of the above
7. What is the output of the following C++ code?
#include <iostream> using namespace std; class calculate { int x, y; public: void val(int, int); int sum() { return (x + y); } }; void calculate::val(int a, int b) { x = a; y = b; } int main() { calculate calculate; calculate.val(5, 10); cout << "The sum = " << calculate.sum(); return 0; }
A The sum = 5
B The sum = 10
C The sum = 15
D Error because calculate is used as class name and variable name in line 19.
8. Which one is a valid class declaration?
A public classe A {}
B classe A {}
C classe A {int x;};
D object A {int x;};
9. The members of a class in C++ are by default __________
A private
B protected
C public
D public and protected
10. What is the correct syntax to access a static member of a class?
class A { public: static int val; }
A A->val
B A.val
C A::val
D A^val