Pages

getteammates.com

Tuesday, 14 April 2015

Advanced c++ interview questions and answers 3


1. How to define an postfix increment operator function. How to call it explicitly?

SHOW/HIDE ANSWER
Declaration,
    1 Integer operator++(int)
    2 {
    3  Integer v = *this;
    4  ++mV;
    5  return v;
    6 }
Calling it explicitly,
    1 Integer v1(2);
    2 Integer v2 = v1.operator++(0); 
 
 

2. What is the output of the following program?

    1 #include <iostream>
    2 
    3 class Base
    4 {
    5 public:
    6     void func()
    7     {
    8         std::cout << "Base::func" << std::endl;
    9     }
   10 };
   11 
   12 class Derived: public Base
   13 {
   14 public:
   15     void func(int i)
   16     {
   17         std::cout << "Derived::func" << std::endl;
   18     }
   19 };
   20 
   21 int main(int argc, char **argv)
   22 {
   23     Derived derived;
   24     derived.func();
   25     return 0;
   26 } 
SHOW/HIDE ANSWER
Compilation error.

3. What is the output of the following program?

    1 #include <iostream>
    2 
    3 class Base
    4 {
    5     int i;
    6 public:
    7     Base(int v)
    8      : i(v)
    9     {}
   10 
   11     virtual void print()
   12     {
   13         std::cout << i << std::endl;
   14     }
   15 };
   16 
   17 class Derived: public Base
   18 {
   19     int j;
   20 
   21 public:
   22     Derived(int v)
   23      : Base(v),
   24        j(v)
   25     {}
   26 
   27     void print()
   28     {
   29         Base::print();
   30         std::cout << j << std::endl;
   31     }
   32 };
   33 
   34 int main(int argc, char **argv)
   35 {
   36     Derived d1(1);
   37     Derived d2(2);
   38 
   39     Base &b1 = d2;
   40 
   41     b1 = d1;
   42 
   43     d1.print();
   44     d2.print();
   45 
   46     return 0;
   47 } 

4. What is the output of the following program?

    1 #include <iostream>
    2 
    3 class Base
    4 {
    5     int i;
    6 public:
    7     Base(int v)
    8     {}
    9 
   10     virtual void print()
   11     {
   12         std::cout << i << std::endl;
   13     }
   14 };
   15 
   16 class Derived: public Base
   17 {
   18     int j;
   19 
   20 public:
   21     Derived(int v)
   22      : Base(v),
   23        j(v)
   24     {}
   25 
   26     void print()
   27     {
   28         Base::print();
   29         std::cout << j << std::endl;
   30     }
   31 };
   32 
   33 void func(Base b)
   34 {
   35     if (dynamic_cast<Derived *>(&b) == 0) {
   36         std::cout << "It is not an instance of Derived";
   37     } else {
   38         std::cout << "It is an instance of Derived";
   39     }
   40 }
   41 
   42 int main(int argc, char **argv)
   43 {
   44     Derived d(1);
   45 
   46     func(d);
   47 
   48     return 0;
   49 } 

SHOW/HIDE ANSWER
It is not an instance of Derived

Slicing removed the Derived class part from the object.

5. Where is the location of vptr in an object?

SHOW/HIDE ANSWER
At the start of the object. In case of multiple inheritance there will be multiple vptr so other vptr will be with an offset.


6. What is the output of the following program?

    1 #include <iostream>
    2 
    3 class A
    4 {
    5     int i;
    6 public:
    7     virtual ~A()
    8     {}
    9 
   10 };
   11 
   12 class B
   13 {
   14     int j;
   15 public:
   16     virtual ~B()
   17     {}
   18 };
   19 
   20 class C: public A, public B
   21 {
   22     int k;
   23 };
   24 
   25 int main(int argc, char **argv)
   26 {
   27     C obj;
   28 
   29     std::cout << "A:" << (A *)&obj << "\nB:" << (B *)&obj << "\nC:" << (C *)&obj << std::endl;
   30     return 0;
   31 }
SHOW/HIDE ANSWER

Sample output for a 32 bit build:
A:0xffc338dc
B:0xffc338e4
C:0xffc338dc

7. What is the output of the following program?

    1 #include <iostream>
    2 
    3 class A
    4 {
    5 public:
    6     virtual ~A()
    7     {}
    8 };
    9 
   10 class B: protected A
   11 {
   12 public:
   13 };
   14 
   15 int main(int argc, char **argv)
   16 {
   17     B b;
   18     A *a = &b;
   19     return 0;
   20 }
SHOW/HIDE ANSWER
Compilation error. b can be converted in to a within the scope of B only in this case. So the following program will work fine.

    1 #include <iostream>
    2 
    3 class A
    4 {
    5 public:
    6     virtual ~A()
    7     {}
    8 
    9 };
   10 
   11 class B: protected A
   12 {
   13 public:
   14     static A * toA(B * b);
   15 };
   16 
   17 A * B::toA(B * b)
   18 {
   19     return b;
   20 }
   21 
   22 int main(int argc, char **argv)
   23 {
   24     B b;
   25 
   26     A *a = B::toA(&b);
   27 
   28     return 0;
   29 } 
 

8. Is there a way to know if operator[] is used for reading(rhs) or writing(lhs)?

SHOW/HIDE ANSWER
    1 #include <iostream>
    2 #include <algorithm>
    3 
    4 class Test
    5 {
    6     int mData[16];
    7 public:
    8     class Proxy
    9     {
   10         int &mCell;
   11     public:
   12         Proxy(int &cell)
   13          : mCell(cell)
   14         {}
   15 
   16         Proxy& operator=(int i)
   17         {
   18             std::string pass;
   19             std::cout << "password for writting :) ";
   20             std::cin >> pass;
   21             mCell = i;
   22         }
   23 
   24         operator int() const
   25         {
   26             return mCell;
   27         }
   28     };
   29 
   30     class ConstProxy: public Proxy
   31     {
   32     public:
   33         ConstProxy(const int &cell)
   34          : Proxy(const_cast<int &>(cell))
   35         {}
   36     };
   37 
   38 public:
   39     Test()
   40     {
   41         std::fill(mData, mData+16, 0);
   42     }
   43 
   44     ConstProxy operator[](int index) const
   45     {
   46         return ConstProxy(mData[index]);
   47     }
   48 
   49     Proxy operator[](int index)
   50     {
   51         return Proxy(mData[index]);
   52     }
   53 
   54     friend std::ostream& operator<<(std::ostream& os, const Test& t);
   55 };
   56 
   57 std::ostream& operator<<(std::ostream& os, const Test& t)
   58 {
   59     os << "[";
   60     for (int i=0; i<16; ++i) {
   61         os << t[i] << (i<15?", ":"");
   62     }
   63     os << "]";
   64     return os;
   65 }
   66 
   67 int main(int argc, char **argv)
   68 {
   69     Test t;
   70     t[0] = 1;
   71     std::cout << t << std::endl;
   72 }
   73 

9. How to create a non-derivable class?

SHOW/HIDE ANSWER
    1 class Sealed;
    2 
    3 class Lock
    4 {
    5 private:
    6     Lock() {}
    7     Lock(const Lock&) {}
    8     friend class Sealed;
    9 };
   10 
   11 class Sealed: public virtual Lock
   12 {
   13 public:
   14     Sealed() {}
   15 };
 
 
PREVIOUS HOME

Advanced c++ interview questions and answers 2


1. What is the difference between calling just throw and throw with an object in a catch block?

SHOW/HIDE ANSWER
A copy of the object is created if we throw with an object. With just throw, no copy is created.

2. What is a possible replacement for c static function in c++?

SHOW/HIDE ANSWER
Unnamed namespaces.

3. What is the size of an empty class, or class with only normal functions?

SHOW/HIDE ANSWER
Not zero, 1 for most compilers. The reason for this is to have different address for different object.

4.  What is the size of class with only virtual functions?

SHOW/HIDE ANSWER
4 with most of the compilers for a 32bit binary.

5. Which class name the following program will print?

    1 #include <typeinfo>
    2 #include <iostream>
    3 
    4 class B
    5 {
    6 };
    7 
    8 class D: public B
    9 {
   10 };
   11 
   12 int main(int argc, char **argv)
   13 {
   14     B *t = new D();
   15     std::cout << typeid(*t).name() << std::endl;
   16 }
SHOW/HIDE ANSWER
B
For RTTI to work correctly we need atleast one virtual function in the base class.

6. What is the output of the following program?

    1 #include <iostream>
    2 
    3 void print(double v)
    4 {
    5     std::cout << v << std::endl;
    6 }
    7 
    8 void print(long v)
    9 {
   10     std::cout << v << std::endl;
   11 }
   12 
   13 int main(int argc, char **argv)
   14 {
   15     print(1);
   16 }
SHOW/HIDE ANSWER
Compilation error: ambiguous function call : print(long(1)) or print(double(1))?

7. What is the output of the following program?

    1 #include <iostream>
    2 
    3 void print(double v)
    4 {
    5     void print(long v);
    6     print(v);
    7 }
    8 
    9 void print(long v)
   10 {
   11     std::cout << v << std::endl;
   12 }
   13 
   14 int main(int argc, char **argv)
   15 {
   16     print(1.0);
   17 } 

8. What is the output of the following program?

    1 #include <iostream>
    2 
    3 class Base
    4 {
    5 public:
    6     void print(int v = 1)
    7     {
    8         std::cout << "Base : " << v << std::endl;
    9     }
   10 };
   11 
   12 class Derived: public Base
   13 {
   14 public:
   15     void print(int v = 10)
   16     {
   17         std::cout << "Derived : " << v << std::endl;
   18     }
   19 };
   20 
   21 
   22 int main(int argc, char **argv)
   23 {
   24     Derived o1;
   25     Base * o2 = &o1;
   26 
   27     o1.print();
   28     o2->print();
   29 
   30     return 0;
   31 }
SHOW/HIDE ANSWER

Derived : 10
Base : 1

9. How to declare a namespace alias?

SHOW/HIDE ANSWER
namespace MyLongNameSpaceName
{
...
}

namespace MLNSN = MyLongNameSpaceName;

10. Which is the macro that can be used to identify that we are using a c++ compiler?

SHOW/HIDE ANSWER
__cplusplus

11. How to declare c function in c++?

SHOW/HIDE ANSWER
By using extern "C".

extern "C" void print();

or

extern "C" {
    void print();
}

12. What is the difference between exit and abort?

SHOW/HIDE ANSWER
exit does a graceful process termination, it calls the destructors for all the constructed objects, with abort they are not called.
With exit the local With variables of the calling function and its callers will not have their destructors invoked.

13. Can I have static members in an union?

14. Which are the operators that cannot be overloaded?

15. Suppose we have an Integer class as shown below, how do I support 2+ obj?

    1 #include <iostream>
    2 
    3 class Integer
    4 {
    5     int mV;
    6 
    7 public:
    8     explicit Integer(int v)
    9      : mV(v)
   10     {}
   11 
   12     Integer operator+(int v) const
   13     {
   14         return Integer(mV+v);
   15     }
   16 
   17     int value() const
   18     {
   19         return mV;
   20     }
   21 };
   22 
   23 int main(int argc, char **argv)
   24 {
   25     Integer v1(2);
   26 
   27     std::cout << (v1+2).value() << std::endl;
   28     std::cout << (2+v1).value() << std::endl;
   29 }
SHOW/HIDE ANSWER
Add a global operator+ function which takes int as first argument and Integer as the second argument.

    1 Integer operator+(int v, Integer iv)
    2 {
    3     return iv+v;
    4 } 
 

16. Can I overload destructor?


17. Can I call destructor explicitly?

SHOW/HIDE ANSWER
Yes, but you only want to do that when you have used placement new. 

18. What is the output of the following program?


    1 #include <iostream>
    2 
    3 class Shape
    4 {
    5 public:
    6     virtual ~Shape()
    7     {};
    8     virtual void draw() = 0;
    9 };
   10 
   11 class Circle: public Shape
   12 {
   13 public:
   14     virtual void draw()
   15     {
   16         std::cout << "circle drawn" << std::endl;
   17     }
   18 };
   19 
   20 int main(int argc, char **argv)
   21 {
   22     Circle c;
   23     Shape *sp = &c;
   24 
   25     Circle *cp = &c;
   26     Shape **spp = &cp;
   27     
   28     (*spp)->draw();
   29 
   30     return 0;
   31 }
SHOW/HIDE ANSWER
Compilation error at 26; invalid conversion from ‘Circle**’ to ‘Shape**’

19. Where virtual inheritance should be used in a hierarchy?

SHOW/HIDE ANSWER
If we have a diamond class hierarchy we should use the virtual inheritance just below the top of the diamond

20. What is the output of the following program?

    1 
    2 #include <iostream>
    3 
    4 class Base
    5 {
    6 public:
    7     virtual ~Base()
    8     {}
    9 
   10     virtual void func1() = 0;
   11     virtual void func2() = 0;
   12 };
   13 
   14 class DerivedA: public virtual Base
   15 {
   16 public:
   17     void func1()
   18     {
   19         func2();
   20     }
   21 };
   22 
   23 class DerivedB: public virtual Base
   24 {
   25 public:
   26     void func2()
   27     {
   28         std::cout << "DerivedB::func2()" << std::endl;
   29     }
   30 };
   31 
   32 class Join: public DerivedA, public DerivedB
   33 {};
   34 
   35 int main(int argc, char **argv)
   36 {
   37     Join * j = new Join();
   38 
   39     DerivedA * da = j;
   40     DerivedB * db = j;
   41 
   42     da->func1();
   43     db->func1();
   44     
   45     delete j;
   46 
   47     return 0;
   48 }
SHOW/HIDE ANSWER
DerivedB::func2()
DerivedB::func2()

It is sometimes called "cross delegation". DerivedA ended up calling function of its sibling class DerivedB.


PREVIOUS HOME NEXT(Advanced c++ interview questions and answers 3)

Advanced c++ interview questions and answers 1


Let me assemble the c++ questions and answers so that I don't have to go elsewhere searching for questions for my next interview!

1. Can I call constructor from another constructor in the same class?

SHOW/HIDE ANSWER
No you can not do this. So the below given code snippet won't work.
    1 class Test
    2 {
    3 private:
    4     int mI;
    5 
    6 public:
    7     Test()
    8      : Test(0)
    9     {}
   10 
   11     Test(int i)
   12      : mI(i)
   13     {}
   14 };

However you can very much do this in c++11.

So is there a way to achieve it? Well yes. Its by using placement new, modified code is shown below.

    1 class Test
    2 {
    3 private:
    4     int mI;
    5 
    6 public:
    7     Test()
    8     {
    9         new (this) Test(100);
   10     }
   11     Test(int i)
   12      : mI(i)
   13     {}
   14 };
 

 

2. Do we need to call the base class operator= function from derived class, and how to do it?

SHOW/HIDE ANSWER
Yes you would need to call the base class assignment operator function, otherwise base class part of the variable won't set correctly. How to do it! see below,
    1 #include <iostream>
    2 
    3 class Base
    4 {
    5 private:
    6     int mBI;
    7 
    8 public:
    9     Base()
   10      : mBI(100)
   11     {}
   12 
   13     void setBValue(int i)
   14     {
   15         mBI = i;
   16     }
   17 
   18     void print() 
   19     {
   20         std::cout << "Base I " << mBI << std::endl;
   21     }
   22 };
   23 
   24 class Derived: public Base
   25 {
   26 private:
   27     int mDI;
   28 
   29 public:
   30     Derived()
   31      : mDI(1000)
   32     {}
   33 
   34     Derived & operator=(Derived const & o)
   35     {
   36         if (this != &o) {
   37             static_cast<Base &>(*this) = o;
   38             mDI = o.mDI;
   39         }
   40 
   41  return *this;
   42     }
   43 
   44     void setDValue(int i)
   45     {
   46         mDI = i;
   47     }
   48 
   49     void print() 
   50     {
   51         Base::print();
   52         std::cout << "Derived I " << mDI << std::endl;
   53     }
   54 };
   55 
   56 int main(int argc, char **argv)
   57 {
   58     Derived d1;
   59     Derived d2;
   60 
   61     d1.setBValue(1);
   62     d1.setDValue(1);
   63 
   64     d2 = d1;
   65 
   66     d1.print();
   67     d2.print();
   68 
   69     return 0;
   70 }

 

 3. Why do I need to return *this in an assignment operator function?

SHOW/HIDE ANSWER
To make assignment such as (obj3 = obj2) = obj1; to work.

 

4. How to declare and use pointer to member variable?

SHOW/HIDE ANSWER
    1 #include <iostream>
    2 
    3 class Test
    4 {
    5 public:
    6     int mI;
    7 };
    8 
    9 int main(int argc, char **argv)
   10 {
   11     Test d1;
   12     d1.mI = 100;
   13 
   14     int Test::*pmI = &Test::mI;
   15     d1.*pmI = 20;
   16 
   17     std::cout << "I " << d1.mI << std::endl;
   18 
   19     return 0;
   20 }

5. How to declare and use pointer to member function?

SHOW/HIDE ANSWER
    1 #include <iostream>
    2 
    3 class Test
    4 {
    5 public:
    6     int print() {
    7         std::cout << "Test::print" << std::endl;
    8     }
    9 };
   10 
   11 int main(int argc, char **argv)
   12 {
   13     Test d;
   14 
   15     int (Test::*pPrint)();
   16 
   17     pPrint = &Test::print;
   18     (d.*pPrint)();
   19 
   20     return 0;
   21 } 
 

6. What is the issue with the given program?

    1 #include <iostream>
    2 
    3 class Base
    4 {
    5 public:
    6     Base()
    7     {
    8         print();
    9     }
   10 
   11     virtual ~Base()
   12     {
   13         print();
   14     }
   15 
   16     virtual void print() = 0;
   17 };
   18 
   19 class Derived: public Base
   20 {
   21 public:
   22     Derived()
   23     {}
   24 
   25     void print()
   26     {
   27         std::cout << "Derived " << std::endl;
   28     }
   29 };
   30 
   31 int main(int argc, char **argv)
   32 {
   33     Derived d;
   34     return 0;
   35 } 
  
SHOW/HIDE ANSWER
Pure virtual function called from constructor and destructor!

7. What is the output of the following program?

    1 #include <iostream>
    2 
    3 class Test
    4 {
    5 private:
    6     int m1;
    7     int m2;
    8 
    9 public:
   10     Test()
   11      : m2(1),
   12        m1(m2)
   13     {}
   14 
   15     void print() const
   16     {
   17         std::cout << m1 << ", " << m2 << std::endl;
   18     }
   19 };
   20 
   21 
   22 int main(int argc, char **argv)
   23 {
   24     Test t;
   25     
   26     t.print();
   27 
   28     return 0;
   29 }
 
 
SHOW/HIDE ANSWER
Garbage value, 1
The problem is with the order of initialization list

8.How to initialize constant and reference member variable?

SHOW/HIDE ANSWER
Using initialization list.

9. What is constant in a const function?

SHOW/HIDE ANSWER
Variable 'this'.

10. How to modify member variable from a const funciton?

SHOW/HIDE ANSWER
Declare the member variables as mutable or use const_cast as shown below.
 
    1 #include <iostream>
    2 
    3 class Test
    4 {
    5 private:
    6     int m1;
    7     int m2; 
    8 
    9 public:
   10 
   11     void print() const
   12     {
   13         const_cast<Test *>(this)->m1 = 100;
   14         const_cast<Test *>(this)->m2 = 200;
   15         std::cout << m1 << ", " << m2 << std::endl;
   16     }
   17 };
   18 
   19 
   20 int main(int argc, char **argv)
   21 {
   22     Test t;
   23     t.print();
   24 
   25     return 0;
   26 }
 

11. What is the issue in the following program?

    1 #include <iostream>
    2 
    3 int main(int argc, char **argv)
    4 {
    5     const int & r1 = 100;
    6     int v = 200;
    7     int &r2 = v;
    8     int & r3 = 200;
    9     return 0;
   10 }
 
 
SHOW/HIDE ANSWER
Issue is in the initialization of r3 at line 8, rvalue should be a variable.

12. Can the destructor be pure virtual function?

SHOW/HIDE ANSWER
Yes, but you still have to define it!

 

13. What is the output of the following program?

    1 #include <iostream>
    2 
    3 class Base
    4 {
    5 public:
    6     Base()
    7     {
    8         print();
    9     }
   10 
   11     virtual ~Base()
   12     {
   13         print();
   14     }
   15 
   16     virtual void print() = 0;
   17 };
   18 
   19 class Derived: public Base
   20 {
   21 public:
   22     Derived()
   23     {}
   24 
   25     void print()
   26     {
   27         std::cout << "Derived" << std::endl;
   28     }
   29 };
   30 
   31 void Base::print()
   32 {
   33     std::cout << "Base" << std::endl;
   34 }
   35 
   36 int main(int argc, char **argv)
   37 {
   38     Derived *d = new Derived();
   39     d->print();
   40     delete d;
   41 
   42     return 0;
   43 }
SHOW/HIDE ANSWER
Base
Derived
Base

Some compiler will throw some warning on calling pure virtual function from constructor and destructor.

14. What is the output of the following program?

    1 #include <iostream>
    2 
    3 class Base
    4 {
    5 };
    6 
    7 class Derived: public Base
    8 {
    9 };
   10 
   11 int main(int argc, char **argv)
   12 {
   13     try {
   14         throw Derived();
   15     } catch (Base const & d) {
   16         std::cout << "Base" << std::endl;
   17     } catch (Derived const & d) {
   18         std::cout << "Derived" << std::endl;
   19     }
   20 
   21     return 0;
   22 } 
 
SHOW/HIDE ANSWER
Base

Exception will be handled by the first possible catch caluse.

15. What is the output of the following program?

    1 #include <iostream>
    2 
    3 class Exception
    4 {
    5 public:
    6     Exception()
    7     {
    8         std::cout << "Exception" << std::endl;
    9     }
   10     Exception(const Exception & o)
   11     {
   12         std::cout << "Exception Copy " << std::endl;
   13     }
   14 
   15     Exception & operator=(Exception const & o)
   16     {
   17         std::cout << "Exception Assign" << std::endl;
   18     }
   19 };
   20 
   21 void func(int i) {
   22     try
   23     {
   24         throw Exception();
   25     } catch (Exception const & e) {
   26         if (i) 
   27             throw e;
   28         else
   29             throw;
   30     }
   31 }
   32 
   33 int main(int argc, char **argv)
   34 {
   35     for (int i=0; i<2; ++i) {
   36         try {
   37             func(i);
   38         } catch (Exception const & d) {
   39             std::cout << "Caught Exception" << std::endl;
   40         }
   41     }
   42     return 0;
   43 }
 
SHOW/HIDE ANSWER
Exception
Caught Exception
Exception
Exception Copy
Caught Exception

16. What is the memory structure of an object?

SHOW/HIDE ANSWER
Usually C++ objects are made by concatenating member variables.
For example;


    1 class Test
    2 {
    3  int i;
    4  float j;
    5 };


is represented by an int  followed by a float.

    1 class TestSub: public Test
    2 {
    3  int k;
    4 };

The above class is represented by Test and then an int(for int k). So finally it will be int,  float and int.

In addition to this each object will have the vptr(virtual pointer) if the class has virtual function, usually as the first element in a class.


17. What is the difference between std::vector<int> x; and std::vector<int> x();?

SHOW/HIDE ANSWER
 First one declares a variable x of type std::vector<int>. Second one declares a function x which returns std::vector<int>.

18. What is a default constructor?

SHOW/HIDE ANSWER
1) A constructor which takes no argument
2) A constructor which has argument(s) but is(are) with default value

19. Can I use this pointer in the constructor?

SHOW/HIDE ANSWER
Yes, but try to avoid calling virtual function from the constructor and passing this pointer from the initialization list to other classes.
 

20. Does friends are inherited?