Select chapter



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

Sponsored Links


  

c++ interview questions and answers

1
What is a destructor?
  1. destructor is a mandatory function in c++ to be implemented to take care of garbage collection.
  2. Destructor is a function which will be called when the object is a destroyed/ deleted or goes out of scope.
  3. option 2 is correct, but destructor will not be called if object goes out of scope.
  4. none
Show Answer
Share Facebook gmail Twitter
2
What is the purpose of using a destructor in C++?
  1. To take care of garbage collection in c++
  2. it is a standard to have destructor in every class, though the use is not much.
  3. if we want to delete an object using delete operator, then that class should have destructor, else it gives compile time error.
  4. if your class contains constructor to initialize some variables then it is mandatory to have destructor also.
Show Answer
Share Facebook gmail Twitter
3
What is the use of a constructor?
  1. Constructor is a special function which will be having same name as class name. Constructor is called at the time of creating object to your class. Constructor is used to initialize the instance variables of an object while creating it.
  2. Constructor is a special function which will be having same name as class name. Constructor is called at the time of creating object to your class. Constructor is used to initialize the instance variables of an object while creating it. Constructors can have return type.
  3. Constructor is a special function which will be having same name as class name, though its not mandatory . Constructor is called at the time of creating object to your class. Constructor is used to initialize the instance variables of an object while creating it. Constructor is also used to create virtual tables for virtual functions.
  4. Constructor is a special function which will be having same name as class name. Constructor is called at the time of creating object to your class. Constructor is used to initialize the instance variables of an object while creating it. Constructor is also used to create virtual tables for virtual functions.
Show Answer
Share Facebook gmail Twitter
4
What if I don't use copy constructor? Where does it create problem?
  1. There is no problem as such if we don't use copy constructors. It is just a mechanism to create a new object before initializing with another object.
  2. If we don't have copy constructor then Base b; Base c = b; this second line will not create a new object, rather it will do bit wise copy of object b to c.  And there by both the objects c,b having same memory copy. This can create problems in some situations. To avoid this we use copy constructor.
  3. both copy constructor and assignment operator are same. So it really doesn't matter if you have copy constructor or not. You can simulate copy constructor using assignment operator also.
  4. all are wrong.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
6
Is it possible to have virtual constructors? Explain the answer.
  1. yes,we can have virtual constructors.
  2. No. We can't have virtual constructors. It is a compile time error.
  3. It depends on the situation, some times it is possible.
  4. none
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
8
How does destructor looks like in C++?
  1. class Base{
        public:
        //below is the constructor
        Base(){
            cout<<"Base class constructor is called"<<endl;
        }
        //below is the syntax for destructor
        virtual ~Base(){
            cout<<"Base class destructor is called"<<endl;
        }
    };
  2. class Base{
        public:
        //below is the constructor
        Base(){
            cout<<"Base class constructor is called"<<endl;
        }
        //below is the syntax for destructor
        virtual Base(){
            cout<<"Base class destructor is called"<<endl;
        }
    };
  3. class Base{
        public:
        //below is the constructor
        Base(){
            cout<<"Base class constructor is called"<<endl;
        }
        //below is the syntax for destructor
        ~Base(){
            cout<<"Base class destructor is called"<<endl;
        }
    };
  4. class Base{
        public:
        //below is the constructor
        Base(){
            cout<<"Base class constructor is called"<<endl;
        }
        //below is the syntax for destructor
        !Base(){
            cout<<"Base class destructor is called"<<endl;
        }
    };
Show Answer
Share Facebook gmail Twitter
9
When does destructor will be called?
  1. destructor will be called only when we destroy an object using delete operator. Assuming that object is created using new operator.
  2. destructor will be called when a variable for an object created without pointers, goes out of scope.
  3. both options 1,2 are correct.
  4. option1 is wrong, even if we don't use delete operator for an object created using new operator, if it goes out of scope destructor will be called. Otherwise option 3 is correct.
Show Answer
Share Facebook gmail Twitter
10
What is the order of constructor execution in C++?
  1. base and then derived
  2. only derived
  3. derived and then base
  4. only base
Show Answer
Share Facebook gmail Twitter
11
What is the order of destructor execution in C++?
  1. base and then derived
  2. only derived
  3. derived and then base
  4. only base
Show Answer
Share Facebook gmail Twitter
12
What is a copy constructor?
  1. It is a special constructor, which initializes its object members with another object of same class.(by shallow copying)
  2. It is a special constructor, which initializes an existing object from another existing object.
  3. both copy constructor and assignment operator are same.
  4. It is a special constructor, which initializes its object members with another object of same class.(by deep copying)
Show Answer
Share Facebook gmail Twitter
13
Is it mandatory that a copy constructor should take only reference of an object, or it can take an object also with out using a reference operator?
  1. It is up to you. You can create copy constructor what ever the way you want to.
  2. It is mandatory that a copy constructor syntax says, it takes only reference of an object only as the first parameter.
  3. It is mandatory that a copy constructor syntax says, it takes only reference of an object only as the any  parameter.
  4. none
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
16
In what all the scenarios I can use copy constructors?
  1. when initializing a new object with an existing object.
  2. When passing an object to a function by value.
  3. when returning an object from a function by value.
  4. all three are correct.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
18
What is deep copy?
  1. A deep copy does not creates a copy of the dynamically 
    allocated objects.
  2. A deep copy just copies the 
    values of the data as they are
  3. A deep copy creates a copy 
    of the dynamically allocated 
    objects too
  4. A deep copy creates a 
    copy of the statically allocated objects too
Show Answer
Share Facebook gmail Twitter
19
Does constructor throws an exception? If so how to handle it?
  1. Constructor can never throw an exception.
  2. There is a possibility of constructor throwing an exception, so we have to handle it using try catch blocks.
  3. There is a possibility of constructor throwing an exception, but we can't handle it.
  4. none
Show Answer
Share Facebook gmail Twitter
20
Can we have virtual destructors? If so what is the use of virtual destructors.
  1. Just like we can't have virtual constructors, we can't have virtual destructors also.
  2. Though it is of no use having virtual destructors, still compiler won't give any error if we have virtual destructors.
  3. Yes, we can (in fact we should) have virtual destructors. This is to make sure that the correct class destructor will be called at run time when your object goes out of scope of if your object is deleted. Specifically when you are using base class pointer or reference to hold the derived class object.  If you don't have virtual destructor, then it will end up in calling only base class destructor.
  4. option 3 is wrong because whether you use virtual keyword to destructor or not, it is the responsibility of c++ to make sure that always it calls derived class destructor first and then base class destructor.
Show Answer
Share Facebook gmail Twitter
Read more papers :