Constructor and destructor - placement practice test

Constructor and destructor - placement practice test


1. A constructor that does not have any parameters is called____________ constructor.

a. Custom
b. dynamic
c. static
d. default
View Answer / Hide Answer

ANSWER: d. default




2. Is it mandatory to invoke/call a constructor for creating an object?

a. Yes
b. No
View Answer / Hide Answer

ANSWER: a. Yes




3. If default constructor is not defined, then how the objects of the class will be created?

a. The compiler will generate error
b. Error will occur at run-time.
c. Compiler provides its default constructor to build the object
d. None of these
View Answer / Hide Answer

ANSWER: c. Compiler provides its default constructor to build the object




4. Which of the followings are true about constructors?

1. A class can have more than one constructor
2. They can be inherited
3. Their address can be referred
4. Constructors cannot be declared in protected section of the class
5. Constructors cannot return values

a. Only 1,2,4
b. 1,2,4,5
c. 1,3,5
d. 1,4,5
View Answer / Hide Answer

ANSWER: d. 1,4,5




5. Can constructors be overloaded?

a. Yes
b. No
View Answer / Hide Answer

ANSWER: a. Yes
Explanation: Constructors are nothing but functions bearing the same name as that of the class. So, just like function overloading, constructor overloading is possible. We can have

constructor overloading through parameterized constructors differing in parameters and signature. We can also achieve it by having default constructor, parameterized constructor and copy

constructor in a class.




6. Assume class TEST. Which of the following statements is/are responsible to invoke copy constructor?

a. TEST T2(T1)
b. TEST T4 = T1
c. T2 = T1
d. both a and b
e. All of these
View Answer / Hide Answer

ANSWER: d. both a and b




7. Is it possible to define a constructor with default arguments?

a. Yes
b. No
View Answer / Hide Answer

ANSWER: a. Yes
Explanation: As we can have function with default arguments in CPP, likewise we can have constructors with default arguments as well. And finally, constructors are also

functions.




8. Which of the following statements are not true about destructor?

1. It is invoked when object goes out of the scope
2. Like constructor, it can also have parameters
3. It can be virtual
4. It can be declared in private section
5. It bears same name as that of the class and precedes Lambda sign

a. Only 2, 3, 5
b. Only 2, 3, 4
c. Only 2, 4, 5
d. Only 3, 4, 5
View Answer / Hide Answer

ANSWER: c. Only 2, 4, 5




9. The explicit keyword is an optional decoration for the constructors that takes exactly_____argument.

a. No argument
b. Two
c. Three
d. One
View Answer / Hide Answer

ANSWER: d. One




10. The purpose of explicit keyword is to tell the compiler that a certain constructor may not be used to implicitly cast an expression to its class type.

a. True
b. False
View Answer / Hide Answer

ANSWER: a. True




11. Match the following.



a. P – III, Q - II, R – I
b. P – V, Q - I, R – IV
c. P – III, Q - I, R – IV
d. P – V, Q - II, R – I
View Answer / Hide Answer

ANSWER: c. P – III, Q - I, R – IV



Post your comment

    Discussion

  • RE: Constructor and destructor - placement practice test -Ali ZAIN (06/06/15)
  • if a class is inherited from another class so the order inwhich constrctors are firstly the constructor of derived class is called after that the constructor of base class is called.
  • RE: Constructor and destructor - placement practice test -Stan (10/18/14)
  • 4. Constructors cannot be declared in protected section of the class

    Constructors CAN be declared in protected section of the class.

    class A {

    public:
    static A* Create(int x) {
    return new A(x);
    }

    protected:
    A(int x) {
    b = x;
    }

    private:
    int b;
    };

    A *a = A::Create(5);


  • RE: Constructor and destructor - placement practice test -Atish Shriniwar (06/06/14)
  • It is right that a destructor is having same name as that of the class and it precedes by tilde sign denoted by ~.
    And the last option is not correct because purposefully we have not shown symbol ~ .
    Instead we have written "lambda" thereat. If it could have been "tilde", then it is okey.
    You should know symbolic representation of Lambda and Tilde.
    Hope, this explanation solves your problem.
  • RE: Constructor and destructor - placement practice test -Shashi Kumar (06/05/14)
  • how the last option in question No:8 could be wrong.
    Destructor have same name as that of class and preceded by ~sign.