MCQ

C++ OOPs MCQ Questions and 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

D
An object represents an instance of a class, i.e. a variable of this class type having access to its data members and member functions from the outside if it is authorized.

 

 

2. How many objects can be in the same class?

A 1

B 2

C 3

D As much as possible

D
A class can contain any number of objects depending on its conformity.

 

 
 

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

A
See the following comments for more details:

Point *p1, *p2;   // No constructor call
p1 = new Point(5, 10);  // Normal constructor called
p2 = new Point(*p1);   // Copy constructor called
Point p3 = *p1;  // Copy constructor called
Point p4;   // Normal constructor called
p4 = p3;   // Call the assignment operator

The output of the above program :

 

 

4. Code reuse can be achieved in a C++ program using ______.

A Polymorphisme

B Encapsulation

C Inheritance

D Both A and C are true.
 

 
C
Inheritance is a process of defining a new class based on an existing class by inheriting its common members and methods. Inheritance allows us to reuse code, it improves the reusability in your C++ code.

 

 

5. Which character is used to indicate the end of a class?

A :

B ;

C #

D ,

B
As at the end of a statement, a class also ends with a semicolon (;).

 

 

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

C
The class MyClass has a user-defined constructor “MyClass (int v)” which expects an argument. It does not have a constructor without argument because the compiler does not create the default constructor if the user has already defined a constructor, so the program will display an error.
 

 

 

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

D

 

 

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.

C
A static member is shared by all objects of the class. All static data are initialized to zero when the first object is created, if no other initialization is present.

 

 
 

9. To perform file-based I/O operations, you must use the __________.

A <ifstream>

B <ofstream>

C <fstream>

D None of the above

C
To perform processing on C++ files, the header files <iostream> and <fstream> must be included in your C++ source file.

 

 

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

C
The program works correctly because ‘x’ is static. Since ‘x’ is a static variable, its location in memory remains valid even after f() returns. Thus, we can return a reference of a static variable.
 

 

 
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *