C++ MCQs with answers Page - 102

Here, you will find a collection of MCQ questions on C++. Go through these questions to enhance your preparation for upcoming examinations and interviews.

To check the correct answer, simply click the View Answer button provided for each question.

Have your own questions to contribute? Click the button below to share your MCQs with others!

+ Add Question

A

Admin • 833.24K Points
Coach

Q. What is the output of the following C++ program?

Code:
#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
";
}

Point::Point(const Point &p) {
   x = p.x;
   y = p.y;
   cout << "Copy constructor called
";
}

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

Admin • 833.24K Points
Coach

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

  • (A) Polymorphisme
  • (B) Encapsulation
  • (C) Inheritance
  • (D) Both A and C are true.

A

Admin • 833.24K Points
Coach

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

  • (A) :
  • (B) ;
  • (C) #
  • (D) ,

A

Admin • 833.24K Points
Coach

Q. What is the output of the following C++ program?

Code:
#include<iostream> 
using namespace std; 
  
class MyClass { 
    int val; 
public: 
    MyClass(int v); 
}; 
  
MyClass::MyClass(int v) { 
    val = v; 
    cout << "Constructor called
";
} 
  
int main() { 
    MyClass arr[100]; 
    return 0; 
}
  • (A) Display “Constructor called”
  • (B) Does not display anything
  • (C) Compilation error
  • (D) None of the above

A

Admin • 833.24K Points
Coach

Q. 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

A

Admin • 833.24K Points
Coach

Q. 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

A

Admin • 833.24K Points
Coach

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

  • (A) <ifstream>
  • (B) <ofstream>
  • (C) <fstream>
  • (D) None of the above

A

Admin • 833.24K Points
Coach

Q. What is the output of the following C++ program?

Code:
#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

A

Admin • 833.24K Points
Coach

Q. What is the output of the following C++ code?

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

A

Admin • 833.24K Points
Coach

Q. What is the output of the following C++ code?

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

Add MCQ in this Category

If you want to share an MCQ question in this category, it's a great idea! It will be helpful for many other students using this website.

Share Your MCQ