Select chapter



constructors (2.papers)
inheritance (1.papers)
overriding (1.papers)
basics (4.papers)
pointers (2.papers)
Hide

Sponsored Links


  

c++ constructors interview questions and answers

1
what kind of constructor is this?

class Base{
    public:
        Base(int i);
};
Base b = 10;
  1. copy constructor
  2. it is wrong, it gives compile time error as we can't assign 10 to object b.
  3. it is a normal constructor
  4. it is a conversion constructor
Show Answer
Share Facebook gmail Twitter
2
What is a conversion constructor in c++?
  1. A constructor that can be called with a single argument is used for conversions from the type of the argument to the class type
  2. another name for copy constructor is conversion constructor.
  3. A constructor which converts narrow data type to wider type is called as conversion constructor.
  4. none
Show Answer
Share Facebook gmail Twitter
3
What is a default constructor in c++?
  1. A constructor with out any parameters is called as default constructor.
  2. A constructor if it is having all arguments with default values, then also it is called as default constructor.
  3. both options 1,2 are correct.
  4. all are wrong.
Show Answer
Share Facebook gmail Twitter
4
Calling one constructor from other: Is it possible to call one constructor from other constructor of same class in c++?
  1. No, it is not possible to call one constructor from other constructor of same class, at least in C++. It gives compile time error.
  2. We can call by using this()
  3. Though you can call, but it is of no use. When you jump to other constructor, your current object(this) this will not be carried over to next constructor.
  4. none
Show Answer
Share Facebook gmail Twitter
5
Is it possible to have pure virtual destructors in c++?
  1. no not possible.
  2. yes you can have pure virtual destructors in any class.
  3. It is possible to have pure virtual destructors only for abstract classes.
  4. there is nothing like pure virtual destructors. That concept itself is wrong.
Show Answer
Share Facebook gmail Twitter
6
In what order objects in an array will be destructed?
  1. last created first destructed.
  2. last created last destructed.
  3. we can't predict the order.
  4. none
Show Answer
Share Facebook gmail Twitter
7
Is it possible to overload destructors in c++?
  1. yes, just like you can overload constructors of your class.
  2. No, you can't
  3. can't say
  4. none
Show Answer
Share Facebook gmail Twitter
8
How to write a copy constructor in c++:
Assume that class name is Base, then how to write copy constructor for this class? What is the syntax?
  1. Base(Base b){
            cout<<"copy constructor of base called"<<endl;
        }
  2. Base(const Base *b){
            cout<<"copy constructor of base called"<<endl;
        }
  3. Base(const Base b){
            cout<<"copy constructor of base called"<<endl;
        }
  4. Base(const Base &b){
            cout<<"copy constructor of base called"<<endl;
        }
Show Answer
Share Facebook gmail Twitter
9
copy constructor in c++ with simple example:

What is the output of below C++ program?

class Base{
    int i;
    public:
    Base(){
        i = 10;
        cout<<"constructor of base called"<<endl;
    }
    Base(const Base &b){
        i = b.i;
        cout<<"copy constructor of base called"<<endl;
    }
    void set(int i){
        this->i = i;
    }
    void disp(){
        cout<<"val.."<<i<<endl;
    }
};
int main()
{
    Base b;
    b.disp();
    b.set(20);
    Base c=b;
    c.disp();
}
  1. constructor of base called
    val..10
    copy constructor of base called
    val..10
  2. constructor of base called
    val..10
    constructor of base called
    val..10
  3. constructor of base called
    val..10
    copy constructor of base called
    val..20
  4. constructor of base called
    val..10
    constructor of base called
    val..20
Show Answer
Share Facebook gmail Twitter
10
Example for default copy constructor in c++:

What is the output of below C++ program?

class Base{
    int i;
    public:
    Base(){
        i = 10;
        cout<<"constructor of base called"<<endl;
    }
    void set(int i){
        this->i = i;
    }
    void disp(){
        cout<<"val.."<<i<<endl;
    }
};
int main()
{
    Base b;
    b.disp();
    b.set(20);
    Base c=b;
    c.disp();
}
  1. constructor of base called
    val..10
    val..10
  2. constructor of base called
    val..10
    constructor of base called
    val..10
  3. constructor of base called
    val..10
    val..20
  4. constructor of base called
    val..10
    constructor of base called
    val..20
Show Answer
Share Facebook gmail Twitter
11
Do I need to call base class destructor from derived class explicitly?
  1. No
  2. Yes
  3. It depends, if you are using some pointers in base class members, then you have to call explicitly.
  4. can't say
Show Answer
Share Facebook gmail Twitter
12
What is the difference between copy constructor and overloaded assignment operator?
  1. A copy constructor constructs a new object and assigns member values of passed object to it.
  2. An overloaded assignment operator will copy the members of an existing object to another existing object. It will not create a new object.
  3. both options 1,2 are correct.
  4. both option 1 and 2 if reversed, then it is correct.
Show Answer
Share Facebook gmail Twitter
13
What happens if I call destructor explicitly on a class variable?
  1. Destructor will be called,and no harm.
  2. you are not supposed to do it, because once that variable goes out of scope destructor will be called automatically. If you are calling explicitly also, then its like you are calling destructor two times, side effects can be dangerous.
  3. option 2 is wrong, as destructor will not be called if the variable goes out of scope. Destructor will be called only when you use delete operator on a variable.
  4. it is a compile time error, if you try to call a destructor explicitly.
Show Answer
Share Facebook gmail Twitter
14
So can I call destructor explicitly if I create an object for a class using new operator?
  1. yes, in case of creating an object using new operator, you can call destructor explicitly.
  2. No, it is a compile time error.
  3. It is not advisable. Just like an automatic variables destructor will be called once it goes out of scope, similarly destructor of your newly created object (with new operator) will be called once you apply delete on your object. Until then your destructor will not be called. If you call programmatically also, again c++ system will also call when you apply delete operator on it.
  4. option 3 is wrong
Show Answer
Share Facebook gmail Twitter
15
If my class is having many objects with in it, then should I call destructors of each object in my main destructor of myclass?
  1. yes, in your class if you are creating any other objects, then it is the duty of programmer to call them explicitly from the main destructor.
  2. No. not required. A class destructor will automatically call all other member objects destructors in turn.
  3. we can't predict if the destructor will call member object's destructor, there is no guarantee from c++ documentation.
  4. option 2 is correct only partially. If you are using new operator to create your member objects, then unless you say delete on those objects, it won't call member objects destructors.
Show Answer
Share Facebook gmail Twitter
Read more papers :