Thursday, July 21, 2011

C++ interview questions

Everyone who works in software industry as developer or tester would have come across interview in C++ - The most powerful Object Oriented Programming Language. I would like to list some of the interesting C++ questions in this column which I have come across.

1. What is the size of below structures and how the size is calculated?
a. struct st{
int i;
char c;
double d;
};

b. struct st{ };


2. When will you use "static global" variable in a file?

3. Where initialized static and uninitialized extern variable stored in application memory layout?

4. Let's say your application is crashed before hitting main()'s breakpoint, what will be the possible reasons for crash?

5. What is the significance of using "const obj&" in copy constructor? Whyn't can I pass object in other ways?

6. What is the size of below structures and how the size is calculated?
a. class A{
virtual vfuc1() = 0;
};

class A1{
virtual void vfunc2(){printf("I'm class A1's vfunc2\n");}
};

b. class B : class A, class A1 {
virtual vfunc1() {printf("I'm class B's vfunc1\n");}
virtual vfunc2() {printf("I'm class B's vfunc2\n");}
};

7. How many vtptr(virtual table pointer) and vtable(virtual function table) would have been created for below definition?
A1 array[10]; // refer A1's declaration in Q8.

8. How virtual key in virtual destructor take care of calling derived class dtor(destructor) first then base dtor when the derived class object is deleted through base class pointer?

9. What is the size of below class B and why?
class A{ public: virtual void vfuc(){} };
class B: virtual class A {};

10. Give me some example when ctor has to be added explicitly for a class?

11. Give me some scenario why you need virtual functions?

12. What is the word 'virtual' significance in virtual base class?
say: class B: virtual class A {};

13. Do you achieve function overriding without virtual keyword?

14. What is deep-copy and how will you achieve it?

15. When will you implement your own Copy Ctor and assignment operator?

16. When copy ctor and assignment operator get called below?
class Fred {}
Fred obj1, obj2, obj3;
obj3 = obj1;
Fred obj3 = obj2;
Fred obj5(obj2);
Fred obj9();

18. When copy ctor will be called by compiler?

17. Why const &Ref is the parameter for copy ctor, why not const obj?


18. How to avoid my objects getting copied?

19. How can you avoid my objects getting copied with my object?

20. How do you know whether the given class is given for inheritance or not?

21. What are the things you consider while designing your class?

22. How do you decide whether your ctor has to be inline or not?

23. How do you avoid your object creation on stack?

24. Can your destructor be pure virtual to make your class ABC. If yes then does dtor should have body or not?

25. What is function object?

26. What are the differences between set and map?

27. Can I convert map into set?

28. What is association?

29. What is the difference between aggregation and composition?

30. why function overloading is not done using different return types in C++?

- to be continued




No comments: