C++ OOPs MCQ Questions with Answers – Part 4
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. Choose another definition for objects:
A Class Member
B Class associate
C Class attribute
D Instance of a class
2. How many objects can be in the same class?
A 1
B 2
C 3
D As much as possible
3. What is the output of the following C++ program?
#include<iostream> using namespace std; class Point { private: int x; int y; public: Point(int a = 0, int b = 0); // Normal constructor Point(const Point &p); // Copy Constructor }; Point::Point(int a, int b) { x = a; y = b; cout << "Normal constructor called\n"; } Point::Point(const Point &p) { x = p.x; y = p.y; cout << "Copy constructor called\n"; } int main() { Point *p1, *p2; p1 = new Point(5, 10); p2 = new Point(*p1); Point p3 = *p1; Point p4; p4 = p3; return 0; }
A
Normal constructor called Copy constructor called Copy constructor called Normal constructor called
B
Normal constructor called Normal constructor called Normal constructor called Copy constructor called Copy constructor called Normal constructor called
C
Copy constructor called Normal constructor called Copy constructor called Copy constructor called Normal constructor called
D None of the above
4. Code reuse can be achieved in a C++ program using ______.
A Polymorphisme
B Encapsulation
C Inheritance
D Both A and C are true.
5. Which character is used to indicate the end of a class?
A :
B ;
C #
D ,
6. What is the output of the following C++ program?
#include<iostream> using namespace std; class MyClass { int val; public: MyClass(int v); }; MyClass::MyClass(int v) { val = v; cout << "Constructor called\n"; } int main() { MyClass arr[100]; return 0; }
A Display “Constructor called”.
B Does not display anything
C Compilation error
D None of the above
7. Which of the following statements is/are valid for dynamically allocating memory for an integer in C++?
A int *ptr = new int(100);
B int *ptr; ptr=new int; *ptr=100;
C int *ptr=NULL; ptr=new int; *ptr=100;
D All the answers are true
8. Static variables in a class are initialized when _____.
A each object of the class is created.
B the last object of the class is created.
C the first object of the class is created.
D No need to initialize a static variable.
9. To perform file-based I/O operations, you must use the __________.
A <ifstream>
B <ofstream>
C <fstream>
D None of the above
10. What is the output of the following C++ program?
#include<iostream> using namespace std; int &f() { static int x = 10; return x; } int main() { int &y = f(); y = y +5; cout << f(); return 0; }
A 10
B 5
C 15
D Compilation error