Interview Questions

Hello All

Below are the few questions that were asked in an interview. Can anyone please explain to me the answers in an easiest way with examples.

1
2
3
4
5
6
7
8
9
10
11
1)	Volatile object
3)	Can constructor/destructor be private?
4)	Problems normally faced in multithreading
6)	Data serialization/de-serialization
7)	Layers of TCP/IP
12)	Mutable
13)	Why do we use virtual destructors?
14)	what is a copy constructor and why do we use it?
15)	Smart pointers
16)	Case where exception handling is not safe
17) Static and non-Static member variables/functions


Thanks
Last edited on
closed account (o1vk4iN6)
Why not research it yourself ?
Well, open the C++standard and read in it for example section 7.1.1 Storage class specifiers about the storage class specifier mutable. Take into account that the standard contains examples.

By the way some questions are very simple and do not require additional reading. For example any declaration in a class can be private including a constructor and the destructor.
Last edited on
Hello Vlad

I still have few confusions.

1) If any of the functions of the base class is virtual then destructor should also be virtual. What will happen if destructor is not virtual?

2) And I have always found Copy Constructor and copying an object through assignment operator. Will you please explain to me what are these and in what situations are these used? When to use which one? Please with an example

Thanks
closed account (o1vk4iN6)
The destructor doesn't need to be virtual (it really depends on the situation), if the class is already virtual than there is no increase in overhead (other than when the destructor is called). I believe some people always make the destructor virtual, even when it isn't needed. This than makes the class virtual and adds overhead of the vtable, which may not be needed for a simple class with no inheritance.

When you pass a variable by value, the copy constructor is used to create that copy.
Last edited on
@vlad from moscow

Well, open the C++standard and read in it for example section 7.1.1...


Sorry fir post intrusion.

Could you please share a link to that documentation as i cant find it with google?

Thanks for your time.
Last edited on
Thanks xerzi for a detailed reply

I read this in a C++ book that that's kind of a rule that if a function in the base class is virtual then destructor should also be virtual but the book doesn't say anything about the situation where the destructor is not virtual and this is what confuses me.
if the class is already virtual
how do you mean by that? is it
if the class is virtually inherited
? I am sorry for asking these questions again and again but I want to learn it and remove my confusions.

And that copy constructor concept is still not clear. Below is the link I have been following for learning about copy constructor

http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/
Having a virtual destructor is really a good idea when the class contains virtual functions. And this is why. If a class has virtual functions then it's safe to assume that the objects of this class will be used via a pointer to a base class (otherwise why having virtual functions?). So all the user of the class will have is a pointer to base class - perhaps obtained using a factory. But when the time comes to destroy the object, deleting it without a virtual destructor will lead to a call to a base-class destructor, potentially leading to memory leaks and/or unexpected behavior and/or crashes.

Thanks KRAkatau, now I understand :)

so these memory leaks and/or unexpected behavior and/or crashes are few examples of the unexpected behavior
I have now gone through few of the websites to clear my concepts about copy constructor and assignment and have found out that these are just two different ways that serve the same purpose, one through copy constructor and one through assignment. Then why are there two different ways when one will do? Please correct me if I am wrong. what are the uses of each of these two ways? why not we just always use a copy constructor or why not always an assignment? Are there certain situations where copy constructor works best or where assignment works best. Does the usage depend on any situation?
Last edited on
Having a virtual destructor is really a good idea when the class contains virtual functions.

A class can be a base class without having virtual functions. Destructors should be virtual whenever a class is intended to be used as a base class, otherwise not.

why not we just always use a copy constructor or why not always an assignment?

Because they do different things:

copy construction *creates* a new object by copying an existing one

copy assignment *replaces* the contents of one existing object with a copy of the contents of another existing object.

PS: Most of the questions in your original post are conversation starters, rather than problems that have specific answers. If I'm asking someone about multithreading problems, smart pointers, or exception safety, I want to know what they are familiar with, to what degree, what they've encountered, etc, not just a terse list of terms. Those are book-length topics.
Thanks Cubbi

1
2
3
Cents cMark(5); // calls Cents constructor
Cents cNancy; // calls Cents default constructor
cNancy = cMark; // calls Cents assignment operator 


AND

1
2
Cents cMark(5); // calls Cents constructor
Cents cNancy = cMark; // calls Cents copy constructor! 


The value of cNancy in both of the above cases is eventually equal to that of cMark. Is this correct?
Last edited on
Topic archived. No new replies allowed.