Interview Questions for C++ Programmers

Even if you know C++ syntax and have memorized STL components, you could stumble during a technical interview since some managers like to explore the boundary of a programmer’s C++ knowledge. To make sure you land on your feet, we asked Dice C++ Community Guide David Bolton to share a few standard questions, as well as some posed by these boundary explorers.

Interview QsIs it legal and moral for a member function to say delete this?

  • What Most People Say: No, you should never tell an object to delete itself.
  • What You Should Say: It’s OK, but you need to be careful. The object must have been created with new and you mustn’t access any non-static members once you delete the object.
  • Why You Should Say It: Deleting an object just feels wrong, Bolton says. However, it’s appropriate under some circumstances and it’s important to show that you understand the ramifications, and how and when it’s appropriate to do it.

What three default methods are provided by the compiler if the programmer doesn’t specify them?

  • What Most People Say: I’m familiar with default constructor.
  • What You Should Say: The three methods are: default constructor, default copy constructor and assignment operator.
  • Why You Should Say It: Naming all three default methods shows that you understand how to manage objects. By the way, you’ll get extra points for mentioning that you’re familiar with the destructor in C++03.

What’s the difference between C type strings char *ptr and std::string? Can they be used together?

  • What Most People Say: I’m familiar with standard strings, but I don’t know how to convert them to a char pointer.
  • What You Should Say: Char * is a pointer to a 0-terminated array of chars. An std::string is a class that is type-safe and can be initialized from a C type string. Also, an std::string can be converted to a C type string using the c_str() method.
  • Why You Should Say It: If you’re only familiar with standard strings, then chances are you’ve programmed in C++ and don’t know C.

Should a destructor always be virtual?

  • What Most People Say: Yes.
  • What You Should Say: While it does no harm, your destructor should only be virtual when the class is a base class from which others inherit and has a non-virtual destructor.
  • Why You Should Say It: A more complete answer shows that you understand when a destructor should be virtual. This is a big issue in C++. You can rise above the competition by acknowledging the challenge and demonstrating your expertise.

What is a pure virtual function?

  • What Most People Say: I don’t know.
  • What You Should Say: It’s a virtual member function defined in a base class with the =0 notation. Derived classes from this base class must implement pure virtual functions.
  • Why You Should Say It: Many programmers are familiar with a pure virtual function but they don’t know how to use it. You need to explain not only what a pure virtual function is, but how it’s applied.

What’s an incomplete type?

  • What Most People Say: I don’t know.
  • What You Should Say: Incomplete type is another name for an uninitialized pointer.
  • Why You Should Say It: “This is not a trick question,” Bolton says. “Interviewers ask questions like these to assess your level of proficiency with C++, since inexperienced programmers usually don’t know the answer.”

What’s an orthogonal base class?

  • What Most People Say: I’m not sure.
  • What You Should Say: When two or more base classes do not share common methods or data — and don’t have the same names — they are said to be orthogonal or independent of each other. They can all be inherited into a derived class without issues.
  • Why You Should Say It: This is an advanced question, explained Bolton. Explaining how two or more derived classes can be inherited shows that you’re an expert in base classes.

Which members of the base class are visible in a derived class?

  • What Most People Say: Members defined as public.
  • What You Should Say: Members defined as public or protected.
  • Why You Should Say It: Most programmers are familiar with access control in a public base class, but you should know that derived-class members can refer to public and protected members of the base class.

Comments

  1. BY Janusz Szpilewski says:

    I assume those questions are intended for graduates and other beginners as in the case of a regular C++ programmer some “I don’t know”s would be quite ridiculous.

    Nevertheless there are also some corner issues addressed there like the question about the incomplete type. Saying that it an uninitiated pointer is an oversimplification also suggesting that the implication in the opposite direction might be true as well. While to refer to an incomplete type one must use an uninitiated pointer (or at least declared that way) it should also be remembered that pointer is a variable and incomplete type just a type not particularly tied to the notion of pointer.

    But actually this is a corner issue unlikely to be asked in the same interview along with questions about strings or pure virtual functions.

  2. BY MC says:

    A lot of these are flat wrong. An incomplete type is NOT an uninitialized pointer. It’s a type that has been forward-declared (e.g. “class Foo;”) but not yet defined (“class Foo {…..};”).

    Your guidelines for making destructors virtual (to the extent they are even correct english) are also wrong. If your class is derived from at all, it needs a virtual destructor.

Post a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>