Pages

Gin Rummy Indian Rummy Nine Men's Morris and more Air Attack

Tuesday 14 April 2015

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?

4 comments:

  1. Hello There,


    I’ve often thought about this Advanced c++ interview questions and answers 1. Nice to have it laid out so clearly. Great eye opener.

    How can milli second / micro second / nano second be used in gcc platform? What is the process, syntax?

    It was cool to see your article pop up in my google search for the process yesterday. Great Guide.

    Keep up the good work!


    Ciao,
    Neha

    ReplyDelete
  2. Coding was great. Thanks for spending time for this post. Feeling happy to read this
    to get more information follow the links...
    jsp interview questions
    c++ interview questions
    salesforce interview questions

    ReplyDelete