C++ MCQs with Explanations (Constructors and Destructors)

CONSTRUCTORS AND DESTRUCTORS

1. Which of the following is NOT TRUE about constructors and destructors?

Explanation ≡ Correct answer: option d) They are called automatically.
Constructors are special member functions that are automatically called when an object is created, and they have the same name as the class. They cannot return values and can be invoked multiple times in a program, depending on how many objects are created.

2. Which one of the following statements does correctly describe one of the many characteristics of constructors?

Explanation ≡ Correct answers: options a) They can be inherited and d) They can have default arguments.
Constructors cannot be inherited like regular member functions. However, base class constructors are automatically called when a derived class object is created. Additionally, constructors can have default arguments, which allow object creation with fewer arguments than the number of parameters defined in the constructor.

3. A constructor for the class Area having two data members length and breadth is declared as follows:

Area (int length = 15, int breadth = 10);

Which one of the following object declarations is illegal?

Explanation ≡ Correct answer: option d) Area A;
The constructor Area(int length = 15, int breadth = 10); provides default arguments for length and breadth. When an object of the class Area is declared without any arguments, the default values are used. So, the declaration Area A; is legal and uses the default values of 15 and 10 for length and breadth, respectively.

4. Which one of the following is an implicit constructor for the class ROOM having a data member area?

Explanation ≡ Correct answer: option b) Room(int a) { area = a; }
An implicit constructor is a default constructor generated by the compiler when no constructor is explicitly defined in the class. In this case, the constructor Room(int a) { area = a; } is an implicit constructor as it initializes the data member area with the value passed to the constructor.

5. Which one of the following declarations represents the legal copy constructor for the class X?

Explanation ≡ Correct answer: option c) X(X&);
The copy constructor is a special constructor that is used to create a new object by copying the data of an existing object of the same class. It takes a reference to the object to be copied as its parameter. The correct syntax for the copy constructor is X(X&);.

6. Identify the error, if any, in the following code segment:

 class Sample
 {
     int m;
     Sample();
     Sample(int);
 };
        
Explanation ≡ Correct answer: option e) Line 6 should not have a semicolon
In C++, when declaring the constructor within a class definition, you don't need to end the declaration with a semicolon. The constructor definition will be provided separately outside the class definition. So, line 6 should not have a semicolon.

7. Which one of the following is TRUE about a copy constructor?

Explanation ≡ Correct answer: option b) It declares and initializes an object from another object.
A copy constructor is used to create a new object by copying the data of an existing object of the same class. It declares the new object and initializes it with the values of the existing object. The copy constructor is not invoked automatically when an object is declared; it is explicitly called when a copy of an object is required.

8. Which of the following statements is TRUE?

Explanation ≡ Correct answer: option c) A default constructor can only be supplied by the compiler.
A class can have multiple constructors with different parameter lists. A constructor needs to be defined only if it is explicitly required, and the compiler provides a default constructor if one is not defined in the class. The constructor is automatically called when an object is created, but it's not automatically called each time an object is created; it's called only once for each object.

9. Which of the following statements is TRUE?

Explanation ≡ Correct answer: option d) Destructors are not useful when the class contains pointer data members.
Destructors are special member functions that are automatically called when an object goes out of scope or is explicitly deleted. They are useful for releasing resources and performing cleanup tasks. If a class contains pointer data members, a destructor can be used to deallocate memory and perform other necessary cleanup operations.

10. Which of the following statements is FALSE?

Explanation ≡ Correct answer: option b) A default constructor can only be supplied by the compiler.
A default constructor is a constructor that can be called with no arguments. It can be supplied by the compiler if no constructor is explicitly defined in the class. However, a class can have multiple constructors with different parameter lists, and a constructor can have default arguments, allowing objects to be created with fewer arguments.


Your Score is 10

THE STUDENT FRIENDLY BOOK TO LEARN C++. 
Let us C++ by Famous author Yashavant Kanetkar
Indian writer


Topic wise C++ MCQs Index ≡ (Click to open)
Click on particular topic to visit that page
  1. Principles of Object-Oriented Programming 
  2. Beginning with C++
  3. Tokens, Expressions, and Control Structures
  4. Functions in C++
  5. Classes and Objects
  6. Constructors and Destructors
  7. Operator Overloading
  8. Inheritance
  9. Pointers, Virtual Functions, and Polymorphism
  10. Managing Console I/O Operations
  11. Working with Files
  12. Templates
  13. Exception Handling
  14. Standard Template Library
  15. Manipulating Strings
  16. New Features in ANSI C++ Standards
  17. Object-Oriented System Development

Post a Comment

0 Comments