Professional Documents
Culture Documents
Constructors and Destructors
Constructors and Destructors
Constructors and Destructors
1.
A constructor that accepts __________ parameters is called the default
constructor.
A.
one
B.
two
C.
no
D.
three
Ans. C
2. What happens when a class with parameterized constructors and having no
default constructor is used in a program and we create an object that needs a zeroargument constructor?
A.
Compile-time error.
B.
Preprocessing error.
C.
Runtime error.
D.
Runtime exception.
Ans. A
Yes
B.
No
Ans. A
4. Destructor has the same name as the constructor and it is preceded by ______ .
A.
B.
C.
D.
Ans. C
5.
For automatic objects, constructors and destructors are called each time the
objects
A.
B.
C.
are constructed
D.
are destroyed
Ans. A
6.
Which constructor function is designed to copy objects of the same class
type?
A.
Create constructor
B.
Object constructor
C.
Dynamic constructor
D.
Copy constructor
Ans. D
7.
A.
B.
Destructor has the same name as that of the class with a tilde symbol at the
beginning.
C.
Both A and B.
D.
Destructor has the same name as the first member function of the class.
Ans. C
8.
A.
B.
C.
D.
Both B and C.
Ans. D
9.
A.
When the control comes out of the block in which they are being used.
B.
C.
When the control comes out of the function in which they are being used.
D.
Ans. B
10.
A.
B.
only pass-by-value
C.
only pass-by-reference
D.
Ans. C
11. A function with the same name as the class, but preceded with a tilde character
(~) is called __________ of that class.
A.
constructor B.
destructor
C.
function
object
D.
Ans. B
12. Which of the following gets called when an object goes out of scope?
A.
constructor
B.
destructor
C.
main
D.
virtual function
Ans. B
B.
C.
D.
Ans. D
14. __________ used to make a copy of one class object from another class object of
the same class type.
A.
constructor
B.
copy constructor
C.
destructor
D.
default constructor
Ans. B
cannot overloaded
B.
can be overloaded
C.
can be called
D.
can be nested
Ans. B
B.
C.
D.
Ans. A
Constructor
B.
Destructor
C.
Data Members
D.
Both A and C
Ans. D
18.
If the copy constructor receives its arguments by value, the copy constructor
would
A.
B.
C.
D.
Ans. C
19. Which of the following are NOT provided by the compiler by default?
A.
Zero-argument Constructor
B.
Destructor
C.
Copy Constructor
D.
Copy Destructor
Ans. D
logicalB.
virtual
C.
syntax
D.
linker
Ans. C
21. If the programmer does not explicitly provide a destructor, then which of the
following creates an empty destructor?
A.
Preprocessor
B.
Compiler
C.
Linker
D.
main() function
Ans. B
22.
A __________ is a constructor that either has no parameters, or if it has
parameters, all the parameters have default values.
A.
default constructor
B.
copy constructor
C.
Both A and B
D.
None of these
Ans. A
23. How many default constructors per class are possible?
A.
Only one
B.
Two
C.
Three
D.
Unlimited
Ans. A
24. Which of the following statement is correct about destructors?
A.
B.
C.
D.
Ans. C
25. Which of the following implicitly creates a default constructor when the
programmer does not explicitly define at least one constructor for a class?
A.
PreprocessorB.
Linker
C.
Loader
Compiler
D.
Ans. D
26. A destructor takes __________ arguments.
A.
one
C.
three D.
Ans. D
B.
two
no
Member function
B.
Friend function
C.
Default constructor
D.
const function
Ans. C
Constructor cannot be inherited but the derived class can call them.
C.
A constructor of a derived class cannot access any public and protected
member of the base class.
D.
Both A and B.
Ans. D
B.
Constructor is called either implicitly or explicitly, whereas destructor is
always called implicitly.
C.
D.
Constructor and destructor functions are not called at all as they are always
inline.
Ans. B
30. How many times a constructor is called in the life-time of an object?
A.
Only once
B.
Twice
C.
Thrice
D.
Ans. A
31. To ensure that every object in the array receives a destructor call, always delete
memory allocated as an array with operator __________ .
A.
destructor
B.
delete
C.
delete[]
D.
kill[]
E.
free[]
Ans. C
1.
#include<iostream.h>
class lpu
{
int x;
public:
lpu(int xx, float yy)
{
cout<< char(yy);
}
};
int main()
{
lpu *p = new lpu(35, 99.50f);
return 0;
A.
99
B.
ASCII value of 99
C.
Garbage value
D.
99.50
Answer: Option B
2. Which of the following statement is correct about the program given below?
#include<iostream.h>
class student
{
public:
student()
{
cout<< "India";
}
~student()
{
cout<< "win";
}
};
int main()
{
student obj;
return 0;
A.
B.
C.
D.
Answer: Option C
3.
Which of the following statement is correct about the program given below?
#include<iostream.h>
class lpu
{
int x;
public:
lpu();
~lpu();
void Show() const;
};
lpu::lpu()
{
x = 25;
}
void lpu::Show() const
{
cout<< x;
}
int main()
{
lpu objB;
objB.Show();
return 0;
}
A.
B.
C.
D.
Answer: Option C
4. Which of the following statement is correct about the program given below?
#include<iostream.h>
class section
{
int x;
public:
section();
void Show() const;
~section(){}
};
section::section()
{
x = 5;
}
A.
B.
C.
D.
Answer: Option A
5.
#include<iostream.h>
int val = 0;
class cricket
{
public:
cricket()
{
cout<< ++val;
}
~cricket()
{
cout<< val--;
}
};
int main()
{
cricket obj1, obj2, obj3;
{
cricket obj4;
}
return 0;
}
A.
1234 B.
4321
C.
12344321
D.
E.
43211234
12341234
Answer: Option C
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.
B.
C.
D.
Ans. A
#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
Ans. D
#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.
B.
C.
D.
Base OK. Base OK. Derived OK. Derived DEL. Derived DEL.
E.
Ans. D
#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.
664
B.
665
C.
5 40 38
D.
6 40 38
E.
6 40 39
Ans. D
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
Ans. B
11. Which of the following statement is correct about the program given below?
#include<iostream.h>
class IndiaBix
{
int x;
public:
IndiaBix(short ss)
{
cout<< "Short" << endl;
}
IndiaBix(int xx)
{
cout<< "Int" << endl;
}
IndiaBix(char ch)
{
cout<< "Char" << endl;
}
~IndiaBix()
{
cout<< "Final";
}
};
int main()
{
IndiaBix *ptr = new IndiaBix('B');
return 0;
}
A.
B.
C.
D.
Ans. C
12. Which of the following statement is correct about the program given below?
#include<iostream.h>
class IndiaBix
{
int x;
public:
IndiaBix(short ss)
{
cout<< "Short" << endl;
}
IndiaBix(int xx)
{
cout<< "Int" << endl;
}
IndiaBix(float ff)
{
cout<< "Float" << endl;
}
~IndiaBix()
{
cout<< "Final";
}
};
int main()
{
IndiaBix *ptr = new IndiaBix('B');
return 0;
}
A.
B.
C.
D.
E.
Ans. B
13. Which of the following statement is correct about the program given below?
#include<iostream.h>
class IndiaBix
{
int x;
public:
IndiaBix()
{
x = 0;
}
IndiaBix(int xx)
{
x = xx;
}
IndiaBix(IndiaBix &objB)
{
x = objB.x;
}
void Display()
{
cout<< x << " ";
}
};
int main()
{
IndiaBix objA(25);
IndiaBix objB(objA);
IndiaBix objC = objA;
objA.Display();
objB.Display();
objC.Display();
return 0;
}
A.
B.
C.
D.
Ans. A
14. Which of the following statement is correct about the program given below?
#include<iostream.h>
class IndiaBix
{
int x, y;
public:
IndiaBix()
{
x = 0;
y = 0;
}
IndiaBix(int xx, int yy)
{
x = xx;
y = yy;
}
IndiaBix(IndiaBix *objB)
{
x = objB->x;
y = objB->y;
}
void Display()
{
cout<< x << " " << y;
}
};
int main()
{
IndiaBix objBix( new IndiaBix(20, 40) );
objBix.Display();
return 0;
}
A.
B.
C.
D.
Ans. B