Back-End Programming Exam  >  Back-End Programming Tests  >  Test: Constructor And Destructor - 3 - Back-End Programming MCQ

Test: Constructor And Destructor - 3 - Back-End Programming MCQ


Test Description

15 Questions MCQ Test - Test: Constructor And Destructor - 3

Test: Constructor And Destructor - 3 for Back-End Programming 2024 is part of Back-End Programming preparation. The Test: Constructor And Destructor - 3 questions and answers have been prepared according to the Back-End Programming exam syllabus.The Test: Constructor And Destructor - 3 MCQs are made for Back-End Programming 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests for Test: Constructor And Destructor - 3 below.
Solutions of Test: Constructor And Destructor - 3 questions in English are available as part of our course for Back-End Programming & Test: Constructor And Destructor - 3 solutions in Hindi for Back-End Programming course. Download more important topics, notes, lectures and mock test series for Back-End Programming Exam by signing up for free. Attempt Test: Constructor And Destructor - 3 | 15 questions in 30 minutes | Mock test for Back-End Programming preparation | Free important questions MCQ to study for Back-End Programming Exam | Download free PDF with solutions
Test: Constructor And Destructor - 3 - Question 1

A constructor has the same …………….. as that of class.

Test: Constructor And Destructor - 3 - Question 2

Which of the followings is/are automatically added to every class, if we do not write our own.

Detailed Solution for Test: Constructor And Destructor - 3 - Question 2

In C++, if we do not write our own, then compiler automatically creates a default constructor, a copy constructor and a assignment operator for every class.

1 Crore+ students have signed up on EduRev. Have you? Download the App
Test: Constructor And Destructor - 3 - Question 3

Can destuctors be private in C++?

Detailed Solution for Test: Constructor And Destructor - 3 - Question 3

Destructors can be private. 

Test: Constructor And Destructor - 3 - Question 4

A constructor that accepts no parameters is called the …………….

Test: Constructor And Destructor - 3 - Question 5

Output of following program?
#include<iostream>
using namespace std;
class Point {
    Point() { cout << "Constructor called"; }
};
 
int main()
{
   Point t1;
   return 0;
}

Detailed Solution for Test: Constructor And Destructor - 3 - Question 5

By default all members of a class are private. Since no access specifier is there for Point(), it becomes private and it is called outside the class when t1 is constructed in main.

Test: Constructor And Destructor - 3 - Question 6

Predict the output of following C++ program
#include <iostream>
using namespace std;
  
int i;
  
class A
{
public:
    ~A()
    {
        i=10;
    }
};
  
int foo()
{
    i=3;
    A ob;
    return i;
}
  
int main()
{
    cout << foo() << endl;
    return 0;
}

Detailed Solution for Test: Constructor And Destructor - 3 - Question 6

While returning from a function, destructor is the last method to be executed. The destructor for the object “ob” is called after the value of i is copied to the return value of the function. So, before destructor could change the value of i to 10, the current value of i gets copied & hence the output is i = 3. See this for more details.

Test: Constructor And Destructor - 3 - Question 7

State whether the following statements about  the constructor are True or False.
i) constructors should be declared in the private section.
ii) constructors are invoked automatically when the objects are created.

Test: Constructor And Destructor - 3 - Question 8

Output of following program?
#include<iostream>
using namespace std;
 
class Point {
public:
    Point() { cout << "Normal Constructor calledn"; }
    Point(const Point &t) { cout << "Copy constructor calledn"; }
};
 
int main()
{
   Point *t1, *t2;
   t1 = new Point();
   t2 = new Point(*t1);
   Point t3 = *t1;
   Point t4;
   t4 = t3;
   return 0;
}

Detailed Solution for Test: Constructor And Destructor - 3 - Question 8

Point *t1, *t2; // No constructor call
t1 = new Point(10, 15); // Normal constructor call
t2 = new Point(*t1); // Copy constructor call
Point t3 = *t1; // Copy Constructor call
Point t4; // Normal Constructor call
t4 = t3; // Assignment operator call

Test: Constructor And Destructor - 3 - Question 9

Like constructors, can there be more than one destructors in a class?

Detailed Solution for Test: Constructor And Destructor - 3 - Question 9

There can be only one destructor in a class. Destructor's signature is always ~ClassNam() and they can not be passed arguments.

Test: Constructor And Destructor - 3 - Question 10

When an object is created and initialized at the same time, a ………………. gets called.

Test: Constructor And Destructor - 3 - Question 11

What is the output of following program?
#include <iostream>
using namespace std;
 
class Point
{
    int x, y;
public:
   Point(const Point &p) { x = p.x; y = p.y; }
   int getX() { return x; }
   int getY() { return y; }
};
 
int main()
{
    Point p1;
    Point p2 = p1;
    cout << "x = " << p2.getX() << " y = " << p2.getY();
    return 0;
}

Detailed Solution for Test: Constructor And Destructor - 3 - Question 11

There is compiler error in line "Point p1;". The class Point doesn't have a constructor without any parameter. If we write any constructor, then compiler doesn't create the default constructor. It is not true other way, i.e., if we write a default or parameterized constructor, then compiler creates a copy constructor. 

Test: Constructor And Destructor - 3 - Question 12

What is the output of following program?

#include <iostream>
using namespace std; 
class A
{
    int id;
    static int count;
public:
    A() {
        count++;
        id = count;
        cout << "constructor for id " << id << endl;
    }
    ~A() {
        cout << "destructor for id " << id << endl;
    }
};
  
int A::count = 0;
  
int main() {
    A a[3];
    return 0;
}

Detailed Solution for Test: Constructor And Destructor - 3 - Question 12

In the above program, id is a static variable and it is incremented with every object creation. Object a[0] is created first, but the object a[2] is destroyed first. Objects are always destroyed in reverse order of their creation. The reason for reverse order is, an object created later may use the previously created object. For example, consider the following code snippet.
A a;
B b(a);
In the above code, the object ‘b’ (which is created after ‘a’), may use some members of ‘a’ internally. So destruction of ‘a’ before ‘b’ may create problems. Therefore, object ‘b’ must be destroyed before ‘a’.

Test: Constructor And Destructor - 3 - Question 13

…………….. constructor will not do anything and defined just to satisfy the compiler

Test: Constructor And Destructor - 3 - Question 14

Predict the output of following program.
#include<iostream>
#include<stdlib.h>
using namespace std;
 
class Test
{
public:
   Test()
   { cout << "Constructor called"; }
};
 
int main()
{
    Test *t = (Test *) malloc(sizeof(Test));
    return 0;
}

Detailed Solution for Test: Constructor And Destructor - 3 - Question 14

Unlike new, malloc() doesn't call constructor (See this) If we replace malloc() with new, the constructor is called, see this.

Test: Constructor And Destructor - 3 - Question 15

Can destructors be virtual in C++?

Information about Test: Constructor And Destructor - 3 Page
In this test you can find the Exam questions for Test: Constructor And Destructor - 3 solved & explained in the simplest way possible. Besides giving Questions and answers for Test: Constructor And Destructor - 3, EduRev gives you an ample number of Online tests for practice
Download as PDF