Constructor and Destructor

  1. Which of the following is NOT TRUE about constructors and destructors?
    • They must the same names as class.
    • They cannot return values.
    • They can be invoked only once in a program.
    • They are called automatically.
  2. Which one of the following statements does not correctly describe one of the many characteristics of constructors?
    • They can be inherited.
    • They can have default arguments.
    • They can not return values, like other functions.
    • They can be declared anywhere in the class.
  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?
    • Area A(20, 10);
    • Area A();
    • Area A = Area (20, 10);
    • Area A;
  4. Which one of the following is an implicit constructor for the class ROOM having a data member area?
    • Room ( ) { }
    • Room (int a) {area = a}
    • Room (Room & a) {area = a.area}
    • None of the above
  5. Which one of the following declarations represents the legal copy constructor for the class X?
    • X (X);
    • X (&X);
    • X (X&);
    • X ( );
  6. Identify error, if any, in the following code segment:
    1. class Sample
    2. {
    3. int m;
    4. Sample ( );
    5. Sample (int);       
    6. };
      • Line 6 should not have semicolon
      • In line 5 parameter name is missing
      • In line 4, argument should be of type void
      • Constructors should be declared in public section
  7. Which one of the following is TRUE about a copy constructor?
    • It is used for passing address of data members
    • It declares and initializes an object from another object
    • It is invoked automatically when an object the class is declared
    • We can pass the argument by value to it
  8. Which of the following statements is TRUE?
    • A class can have only one constructor
    • A constructor must be declared for each class
    • A default constructor can only be supplied by the compiler
    • A constructor is automatically called each time an object is created
  9. Which of the following statements is TRUE?
    • A destructor must be declare for each class
    • A class can have only one destructor
    • A destructor can have default arguments
    • Destructors are not useful when the class contains pointer data members
  10. Which of the following statements is FALSE?
    • A class can have only default constructor
    • A default constructor can only be supplied by the compiler
    • A default constructor may be declared without any parameters
    • All parameters of a default constructor can have default values

Answers:

  1. They can be invoked only once in a program.
  2. They can be declared anywhere in the class.
  3. Area A();
  4. Room (int a) {area = a}
  5. X (&X);
  6. Constructors should be declared in public section
  7. It is used for passing address of data members
  8. A constructor is automatically called each time an object is created
  9. A class can have only one destructor
  10. A default constructor can only be supplied by the compiler

Related posts