Constructors and Destructors (1).docx

29
Constructors and Destructors MCQ 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 zero-argument constructor? A. Compile-time error. B. Preprocessing error. C. Runtime error. D. Runtime exception. Ans. A 3. Can a class have virtual destructor? A. Yes B. No Ans. A 4. Destructor has the same name as the constructor and it is preceded by ______ .

description

constructors and destructors

Transcript of Constructors and Destructors (1).docx

Page 1: Constructors and Destructors (1).docx

Constructors and Destructors MCQ

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 zero-argument constructor?

A. Compile-time error.

B. Preprocessing error.

C. Runtime error.

D. Runtime exception.

Ans. A

3. Can a class have virtual destructor?

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

Page 2: Constructors and Destructors (1).docx

5. For automatic objects, constructors and destructors are called each time the objects

A. enter and leave scope

B. inherit parent class

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. Which of the following statement is correct?

A. Constructor has the same name as that of the class.

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

Page 3: Constructors and Destructors (1).docx

8. Which of the following statement is incorrect?

A. Constructor is a member function of the class.

B. The compiler always provides a zero argument constructor.

C. It is necessary that a constructor in a class should always be public.

D. Both B and C.

Ans. D

9. When are the Global objects destroyed?

A. When the control comes out of the block in which they are being used.

B. When the program terminates.

C. When the control comes out of the function in which they are being used.

D. As soon as local objects die.

Ans. B

10. Copy constructor must receive its arguments by __________ .

A. either pass-by-value or pass-by-reference

B. only pass-by-value

C. only pass-by-reference

D. only pass by address

Ans. C

Page 4: Constructors and Destructors (1).docx

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 D. object

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

13. Which of the following statement is correct?

A. Destructor destroys only integer data members of the object.

B. Destructor destroys only float data members of the object.

C. Destructor destroys only pointer data members of the object.

D. Destructor destroys the complete object.

Ans. D

14. __________ used to make a copy of one class object from another class object of the same class type.

A. constructor

Page 5: Constructors and Destructors (1).docx

B. copy constructor

C. destructor

D. default constructor

Ans. B

15. Constructors __________ to allow different approaches of object construction.

A. cannot overloaded

B. can be overloaded

C. can be called

D. can be nested

Ans. B

16. Which of the following statement is correct?

A. A destructor has the same name as the class in which it is present.

B. A destructor has a different name than the class in which it is present.

C. A destructor always returns an integer.

D. A destructor can be overloaded.

Ans. A

17. Which of the following cannot be declared as virtual?

A. Constructor

B. Destructor

C. Data Members

D. Both A and C

Ans. D

Page 6: Constructors and Destructors (1).docx

18. If the copy constructor receives its arguments by value, the copy constructor would

A. call one-argument constructor of the class

B. work without any problem

C. call itself recursively

D. call zero-argument constructor

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

20. It is a __________ error to pass arguments to a destructor.

A. logical B. 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

Page 7: Constructors and Destructors (1).docx

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. A destructor has void return type.

B. A destructor has integer return type.

C. A destructor has no return type.

D. A destructors return type is always same as that of main().

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. Preprocessor B. Linker

C. Loader D. Compiler

Ans. D

26. A destructor takes __________ arguments.

A. one B. two

Page 8: Constructors and Destructors (1).docx

C. three D. no

Ans. D

27. Which of the following never requires any arguments?

A. Member function

B. Friend function

C. Default constructor

D. const function

Ans. C

28. Which of the following statement is correct?

A. A constructor of a derived class can access any public and protected member of the base class.

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

29. Which of the following statements are correct?

A. Constructor is always called explicitly.

B. Constructor is called either implicitly or explicitly, whereas destructor is always called implicitly.

C. Destructor is always called explicitly.

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?

Page 9: Constructors and Destructors (1).docx

A. Only once

B. Twice

C. Thrice

D. Depends on the way of creation of object

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

Output Based questions

1. What will be the output of the following program?

#include<iostream.h>

class lpu

{

int x;

public:

lpu(int xx, float yy)

{

cout<< char(yy);

}

};

int main()

Page 10: Constructors and Destructors (1).docx

{

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()

Page 11: Constructors and Destructors (1).docx

{

student obj;

return 0;

}

A. The program will print the output India.

B. The program will print the output win.

C. The program will print the output Indiawin.

D. The program will report compile time error.

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

{

Page 12: Constructors and Destructors (1).docx

cout<< x;

}

int main()

{

lpu objB;

objB.Show();

return 0;

}

A. The program will print the output 25.

B. The program will print the output Garbage-value.

C. The program will report compile time error.

D. The program will report runtime error.

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()

Page 13: Constructors and Destructors (1).docx

{

x = 5;

}

void section::Show() const

{

cout<< x;

}

int main()

{

section objB;

objB.Show();

return 0;

}

A. The program will print the output 5.

B. The program will print the output Garbage-value.

C. The program will report compile time error.

D. The program will report runtime error.

Answer: Option A

5. What will be the output of the following program?

#include<iostream.h>

int val = 0;

class cricket

{

public:

cricket()

Page 14: Constructors and Destructors (1).docx

{

cout<< ++val;

}

~cricket()

{

cout<< val--;

}

};

int main()

{

cricket obj1, obj2, obj3;

{

cricket obj4;

}

return 0;

}

A. 1234 B. 4321

C. 12344321 D. 12341234

E. 43211234

Answer: Option C

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

#include<iostream.h>

class IndiaBix

{

Page 15: Constructors and Destructors (1).docx

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.

Ans. A

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

Page 16: Constructors and Destructors (1).docx

#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

Page 17: Constructors and Destructors (1).docx

Ans. D

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;

Page 18: Constructors and Destructors (1).docx

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.

Ans. D

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 << " ";

}

Page 19: Constructors and Destructors (1).docx

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

Ans. D

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

#include<iostream.h>

class IndiaBix

{

Page 20: Constructors and Destructors (1).docx

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

Page 21: Constructors and Destructors (1).docx

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;

Page 22: Constructors and Destructors (1).docx

}

A. The program will print the output Short .

B. The program will print the output Int .

C. The program will print the output Char .

D. The program will print the output Final .

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;

}

Page 23: Constructors and Destructors (1).docx

~IndiaBix()

{

cout<< "Final";

}

};

int main()

{

IndiaBix *ptr = new IndiaBix('B');

return 0;

}

A. The program will print the output Short .

B. The program will print the output Int .

C. The program will print the output Float .

D. The program will print the output Final .

E. None of the above

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;

Page 24: Constructors and Destructors (1).docx

}

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. The program will print the output 25 25 25 .

B. The program will print the output 25 Garbage 25 .

C. The program will print the output Garbage 25 25 .

Page 25: Constructors and Destructors (1).docx

D. The program will report compile time error.

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()

{

Page 26: Constructors and Destructors (1).docx

cout<< x << " " << y;

}

};

int main()

{

IndiaBix objBix( new IndiaBix(20, 40) );

objBix.Display();

return 0;

}

A. The program will print the output 0 0 .

B. The program will print the output 20 40 .

C. The program will print the output Garbage Garbage .

D. The program will report compile time error.

Ans. B