Top 10 C++ Interview Questions
For those of you who applying for the dwindling number of pure C++ coding jobs, here seems to be the most popular questions interviewers like to ask. I guess some of these questions are rather general and can apply to other object oriented languages as well, but I try to focus on those that are more C++ specific. I've included some STL questions as well, because most C++ shops WILL expect you to have at least some familiarity with STL.
1. What is a virtual function? How is it implemented in C++?
Usually they would start out with the question, "What is polymorphism"? Which usually leads to the virtual function question.
2. What is the meaning of mutable?
This is a favorite question of interviewers because it leads into you having to explain const functions in more detail.
This is a favorite question of interviewers because it leads into you having to explain const functions in more detail.
3. What is the difference between a class and a struct?
Still a popular question after all these years.
Still a popular question after all these years.
4. Explain the different uses of the keyword "static".
There are several variations of this question. A popular version is "what is the difference between a static method and a regular method"? (Answer: static method lacks the "this" pointer).
There are several variations of this question. A popular version is "what is the difference between a static method and a regular method"? (Answer: static method lacks the "this" pointer).
5. Why do you need a virtual destructor?
This used to be the favorite question asked by interviewers, but I noticed a drop in popularity recently. Maybe they realized just how lame it is?
This used to be the favorite question asked by interviewers, but I noticed a drop in popularity recently. Maybe they realized just how lame it is?
6. Is it ok to call a virtual function from a constructor? Why or why not?
Sometimes they like to throw a more "advanced" question in there to trip you up. This is one of the more popular ones. It's not easy to explain, so make sure you read up on it.
Sometimes they like to throw a more "advanced" question in there to trip you up. This is one of the more popular ones. It's not easy to explain, so make sure you read up on it.
7. Name some STL containers and why you would choose one over the other.
Just make sure you know your associative containers from the sequenced ones. Also to really knock their socks off, read up on which data structures are used internally by the containers (for example sets are implemented as binary search trees.)
Just make sure you know your associative containers from the sequenced ones. Also to really knock their socks off, read up on which data structures are used internally by the containers (for example sets are implemented as binary search trees.)
8. Is it safe to use auto_ptrs in an STL container?
9. Explain the concept of "Resource Acquisition Is Initialization".
Questions 8 and 9 kind of tie in together. It's not surprising to get both of them in an interview.
Questions 8 and 9 kind of tie in together. It's not surprising to get both of them in an interview.
10. Explain what is the keyword "explicit".
Another favorite question of interviewers because it leads to discussions on copy constructors, and when/why you would need them.
Another favorite question of interviewers because it leads to discussions on copy constructors, and when/why you would need them.
Some extra questions to consider:
What is "volatile"? Will be most likely asked if your tasks involve threading.
What is "inline"? I don't get asked this very often anymore.
Difference between "const int *" and "int* const". Used to be a popular questions. The "trick" I use to keep myself from getting mixed up is looking if the const keyword is before or after the "*".
Explain the pros and cons of templates. This is actually a question I like, and don't let your lack of knowledge of templates scare you, it's actually a good chance for you to impress your interviewer. Just remember the important points like code bloat, debugging difficulty, type safety, etc.