-
August 2nd, 2012, 09:19 AM
#1
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.
Code:
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 by gulHK; August 2nd, 2012 at 09:26 AM.
-
August 2nd, 2012, 10:02 AM
#2
Re: Interview Questions
You are not in an interview, and neither are we (presumably). So, make use of the resources at your disposal. You can pretty much search the Web for the answers. The "mutable" one might be hard to find specific stuff on, but if you search it with "C++", you will find the answer.
-
August 2nd, 2012, 11:55 AM
#3
Re: Interview Questions
Originally Posted by gulHK
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.
...
7) Layers of TCP/IP
...
That is not a C++ question.
Technically, this isn't a C++ question either:
4) Problems normally faced in multithreading
You can be an ace in C++ and not know a single thing about multithreading. I know that many companies will refuse to hire a C++ programmer if their software uses multithreading and the coder has no experience in these types of applications, regardless of how much C++ they know.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; August 2nd, 2012 at 12:01 PM.
-
August 3rd, 2012, 06:09 PM
#4
Re: Interview Questions
Originally Posted by gulHK
in an easiest way
You must understand that your question is rubbing programmers the wrong way. They've spent lots of time and effort learning C++ to get a good job. And here comes some lazy dude who not only knows very little C++ but also has the nerve to ask them to silverspoon him into their company to become their new deadweight collegue. It's not going to happen.
There are books to prepare you for the interview situation with focus both on C++ and general programming. But in my view for this to work you must already have the knowledge and only need a refresher to summarize and bring things to the top of your head for fast recollection and coherent presentation. If you cram such books you only end up with shallow and superficial knowledge that an experienced interviewer will expose as bull in no time.
Instead use the flunked interview to identify your weak areas and get the proper books and get down to some serious studying and coding. You're wellcome here if you get stuck with something.
Last edited by nuzzle; August 4th, 2012 at 01:15 AM.
-
August 4th, 2012, 06:01 AM
#5
Re: Interview Questions
1) volatile is a compiler hint. The correct answer is compiler dependant.
The fact they are asking this probably means they either don't fully understand the meaning/concept behind it. Or they're trying to trick you into a false/incomplete/incorrect answer. (i.e. ther eis no right answer until they can be more specific)
6) mostly irrelevant, other than trivial examples and handling plain text. Every other form of (de)serialization is implementation specific.
Again. See 1). either they have wrong ideas of it, or it's a trick question.
7) look up "OSI model" on wikipedia.
-
August 4th, 2012, 09:04 AM
#6
Re: Interview Questions
Thank you all
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
-
August 4th, 2012, 10:13 AM
#7
Re: Interview Questions
Originally Posted by gulHK
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?
If you attempt to delete an object of a derived class through a base class pointer that points to it, and the base class destructor is not virtual, then the behaviour is undefined.
Originally Posted by gulHK
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
I shall not explain with an example. Rather, I shall explain with a book: Accelerated C++.
-
August 4th, 2012, 02:56 PM
#8
Re: Interview Questions
Thanks laserlight . Virtual destructor concept is now clear to me
-
August 5th, 2012, 06:17 AM
#9
Re: Interview Questions
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.
-
August 5th, 2012, 06:51 AM
#10
Re: Interview Questions
Originally Posted by gulHK
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
Is that what these "websites" taught you? Then it's time to get a book.
Code:
class foo
{
};
int main()
{
foo f;
foo f2 = f;
foo f3(f2);
foo f4;
f4 = f;
}
So after going through these "websites" as you claimed, you should know which lines of code invoke the copy constructor of foo and which ones invoke the assignment operator, right?
There is a big difference in the two -- the copy constructor constructs a new object, while the assignment operator assigns to an existing object. That's why one is called a constructor and the other one is called assignment.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; August 5th, 2012 at 10:26 AM.
-
August 5th, 2012, 07:00 AM
#11
Re: Interview Questions
Paul I know which one uses copy constructor and which one uses assignment but 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?
-
August 5th, 2012, 07:04 AM
#12
Re: Interview Questions
Originally Posted by gulHK
I know which one uses copy constructor and which one uses assignment but what are the uses of each of these two ways?
Please answer these questions:
- Which of them use the copy constructor and which of them uses the copy assignment operator?
- What is the difference between constructing an object and assigning to it?
-
August 5th, 2012, 07:35 AM
#13
Re: Interview Questions
That's what's written in one of the websites
Code:
Cents cMark(5); // calls Cents constructor
Cents cNancy; // calls Cents default constructor
cNancy = cMark; // calls Cents assignment operator
Cents cMark(5); // calls Cents constructor
Cents cNancy = cMark; // calls Cents copy constructor!
-
August 5th, 2012, 10:34 AM
#14
Re: Interview Questions
Originally Posted by gulHK
Paul I know which one uses copy constructor and which one uses assignment
OK, so tell us -- which ones invoke the copy constructor and which ones invoke the assignment operator?
why not we just always use a copy constructor or why not always an assignment?
I already stated it to you:
A copy constructor constructs a brand new object from an existing object. An assignment object takes an existing object and turns it into a copy of another object. It can't be explained more simply than that.
Regards,
Paul McKenzie
-
August 5th, 2012, 10:35 AM
#15
Re: Interview Questions
Originally Posted by gulHK
That's what's written in one of the websites
Code:
Cents cMark(5); // calls Cents constructor
Cents cNancy; // calls Cents default constructor
cNancy = cMark; // calls Cents assignment operator
Cents cMark(5); // calls Cents constructor
Cents cNancy = cMark; // calls Cents copy constructor!
So what's the question?
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|