C++ Programming - Constructors and Destructors

Exercise :: Constructors and Destructors - Programs

6. 

Which of the following statement is correct about the program given below?

#include<iostream.h> 
class IndiaBix
{
    int *p; 
    public:
    IndiaBix(int xx, char ch)
    {
        p  = new int(); 
        *p = xx + int(ch); 
        cout<< *p;
    }
    ~IndiaBix() 
    {
        delete p;
    }
};
int main()
{
    IndiaBix objBix(10, 'B'); 
    return 0;
}

A. The program will print the output 76.
B. The program will print the output 108.
C. The program will print the output garbage value.
D. The program will report compile time error.

Answer: Option A

Explanation:

No answer description available for this question. Let us discuss.

7. 

Which of the following constructor is used in the program given below?

#include<iostream.h> 
class IndiaBix
{
    int x, y; 
    public:
    IndiaBix(int xx = 10, int yy = 20 )
    {
        x = xx; 
        y = yy;
    }
    void Display()
    {
        cout<< x << " " << y << endl;
    } 
    ~IndiaBix()
    { } 
};
int main()
{
    IndiaBix objBix; 
    objBix.Display(); 
    return 0;
}

A. Copy constructor
B. Simple constructor
C. Non-parameterized constructor
D. Default constructor

Answer: Option D

Explanation:

No answer description available for this question. Let us discuss.

8. 

What will be the output of the following program?

#include<iostream.h>
class BixBase
{   
    public:
    BixBase()
    {
        cout<< "Base OK. "; 
    }
};
class BixDerived: public BixBase
{
    public:
    BixDerived()
    { 
        cout<< "Derived OK. "; 
    }
    ~BixDerived()
    { 
        cout<< "Derived DEL. "; 
    }
};
int main()
{
    BixBase    objB;
    BixDerived objD;
    objD.~BixDerived();
    return 0;
}

A. Base OK. Derived OK. Derived DEL.
B. Base OK. Base OK. Derived OK. Derived DEL.
C. Base OK. Derived OK. Derived DEL. Derived DEL.
D. Base OK. Base OK. Derived OK. Derived DEL. Derived DEL.
E. The program will report compile time error.

Answer: Option D

Explanation:

No answer description available for this question. Let us discuss.

9. 

What will be the output of the following program?

#include<iostream.h> 
class IndiaBix
{
    int x, y; 
    public:
    IndiaBix(int xx)
    {
        x = ++xx;
    } 
    ~IndiaBix()
    {
        cout<< x - 1 << " ";
    }
    void Display()
    {
        cout<< --x + 1 << " ";
    } 
};
int main()
{
    IndiaBix objBix(5);
    objBix.Display();
    int *p = (int*) &objBix;
    *p = 40;
    objBix.Display();
    return 0; 
}

A. 6 6 4
B. 6 6 5
C. 5 40 38
D. 6 40 38
E. 6 40 39

Answer: Option D

Explanation:

No answer description available for this question. Let us discuss.

10. 

What is the technical word for the function ~IndiaBix() defined in the following program?

#include<iostream.h> 
class IndiaBix
{
    int x, y; 
    public:
    IndiaBix(int xx = 10, int yy = 20 )
    {
        x = xx; 
        y = yy;
    }
    void Display()
    {
        cout<< x << " " << y << endl;
    } 
    ~IndiaBix()
    { } 
};
int main()
{
    IndiaBix objBix; 
    objBix.Display(); 
    return 0;
}

A. Constructor
B. Destructor
C. Default Destructor
D. Function Template

Answer: Option B

Explanation:

No answer description available for this question. Let us discuss.