Twenty C++ Interview Questions

So, you?ve scheduled an interview for a job requiring knowledge of C++. Even under the best of circumstances, the interview process can be nerve-wracking, which means you need to be ready for anything. Thorough preparation will allow you to stay calm both in the run-up to being interviewed as well as during the interview itself.

Given that, what do you need to know? What are you likely to be asked? Every interview is different, but if you?re prepared to answer these C++ interview questions below, you should have a head start on your competition.

Who Uses C++?

C++ is a powerful and versatile programming language used to create desktop and mobile applications in a wide variety of industries.

For example, game developers and publishers like Valve Corporation and Blizzard Entertainment use C++. It?s also used at aerospace and defense companies such as L3 Technologies, which supplies communications, surveillance, command and control, and other electronics equipment to governments around the world.    

writing in paper

Typical C++ job titles include entry-level programmer, applications developer, senior developer, and software engineer. Or, if you?re a technical writer who specializes in programming languages, you?ll need a working knowledge of C++ to produce application programming interface (API) documentation, end-user instructions and manuals, or even white papers.  

Why You Should Prepare Answers to Interview Questions

Sure, you don?t want to sound like a robot monotoning answers by rote when you?re at an interview. At the same time, you can?t afford to hem and haw as you work out every answer in your head from scratch. That?s why practice is paramount.

men talking

While you may know the answer to the question being asked, it?s easy to be tripped up by its exact phrasing or how it?s being asked by an interviewer. You want to hit the sweet spot: have answers for the questions being asked and sound confident and polished with your delivery.

20 C++ Interview Questions

1. What?s the difference between C++ and C?

The key difference is C++ allows both object-oriented programming (OOP) and procedural programming, while C only supports procedural programming. In addition, C++ can be considered a superset of C which means most ? but not all! ? C programs will also work in C++.

2. What?s the difference between pointers and references?

Ultimately, references are less robust in their uses than pointers. For example, after a reference has been created, it can never reference a different object. This is, however, commonly done with pointers. In addition, references can never be NULL while pointers will often be NULL.

3. What is inheritance?

Inheritance is when the properties of an existing class are acquired by a new class. The existing class is referred to as the parent/base class while the new, inherited class is the child/derived class.

4. How many different types of inheritance are there and what are they?

There are five types of inh??eritance: hierarchical, hybrid, multilevel, multiple, and single.

5. What is an iterator and what are the different types?

An iterator is like a pointer in that it also points to a specific object within a container. An iterator, however, can perform additional operations depending on the type being used:

  • Bidirectional: can be further decremented
  • Forward: can be incremented
  • Output: can add objects to a container
  • Random Access: can be moved by an offset

6. What is ?undefined behavior? and how is it different from ?unspecified behavior??

serious conversation

Undefined behavior means there is absolutely no standard guarantee regarding how the program will work. That is, the program might or might not perform as intended. (That?s why undefined behavior must be avoided!) In contrast, unspecified behavior simply means behavior is well defined, but the actual definition is done by compiler implementation.

7. What are examples of undefined behavior?

Some typical examples include:

  • Arithmetic errors such as dividing by zero
  • Dereferencing a wild or null pointer
  • Going past the limits of an array

8. What is a class and how many can be used?

A class is a named collection of data and functions. You can have as many classes as necessary in a C++ program. You can create a class with the keyword class followed by the name you want to use. 

9. What are the basic variable types and how are they used in C++?

  • Bool: stores Boolean values
  • Char: stores character types
  • Float and double: stores floating point and large values
  • Int: stores integral values

10. What is the loop function and what are the different types?

The loop function executes an action over and over until a predefined condition has been met. There are three types used in C++:

  • Do-while loop: tests the condition after the loop is executed.
  • For loop: executes a loop a predefined number of times.
  • While loop: tests the condition before the loop is executed.

11. What is the difference between class and struct?

woman doing a talk show

Access modifiers are used differently with class and struct. That is, class members are private by default while struct members are public. That?s why you should employ classes with an object with methods. Structs should be used with a simple data object.  

12. What is the volatile keyword and how is it used?

guy behind magnifying glass

The volatile keyword tells the compiler about a variable which may change without the compiler knowing it. Volatile objects will not be optimized because code outside the current code being executed can change their values.

13. What is the difference between deep copy and shallow copy?

budgeting

Deep copy performs a field-by-field copy of an object to another one using overloading assignment operator or copy constructor. Shallow copy dumps a bit-by-bit memory copy of an object into another one.

14. What are the types of C++ multithreading and how are they used?

Multithreading is a form of multitasking where two or more programs execute at the same time. C++ uses two kinds of multithreading:

  • Thread-based: simultaneous execution of different parts of the same program
  • Process-based: simultaneous execution of different programs

15. What is the octal numbering system? Is 8 a valid octal number?

Octal digits use a base-8 numbering system. The numeral 8, however, is not a valid octal number because the eight digits in this base-8 system are 0-7.

16. What is the distinction between formal and actual parameters?

Parameters sent to the receiving end of a function definition are formal. Actual parameters are sent to the calling end of a function.

17. What?s the difference between a header file inside double quotation marks (??) versus greater-than, less-than signs (<>)?  

laptop and a notebook

When a header file is inside double quotation marks, the compiler first searches the current working directory for the specific header. If the header is not found, the compiler will then search the built-in include path. When a header file is inside greater-than, less-than signs, the compiler looks for the specific header only in the built-in include path.

18. How many ways are there ? and when do you use them ? to pass parameters to the functions?

There are three ways to do this:

  • Call by address: instead of sending values, this sends the actual parameters to the function.
  • Call by reference: actual parameters are received as formal parameters with new C++ reference variables.
  • Call by value: only values are sent as parameters.

19. What are tokens and how are they used in C++?

A token is the most granular unit of information in a C++ program which the compiler can find meaningful. C++ programs are built with tokens which include:

  • constants
  • keywords
  • identifiers
  • string literals
  • symbols

20. When should the register storage specifier be used?

When a variable is used heavily, it should be stored in the register via the register storage specifier. This has the potential to enhance performance as the CPU does not have to continually look up a frequently used variable.

Be Ready for Your Interview

suit and tie

Of course, employers will be most interested in how you answer technical C++ interview questions. You do need to be prepared as well for questions about who you are as a person, employee, and team member. Here are some additional ones ? which you?ll have to answer on your own! ? to be on the lookout for:

  • Why do you want to work for this company?
  • Tell us about a project that went well and why you think that was the case.
  • Describe a project which was unsuccessful and what you learned from it.
  • What kinds of coworkers do you prefer and don?t prefer working with?
  • Give us an example of an interpersonal conflict you had while working on a team and how you resolved it.

Every interview is different, and it can often be difficult after the fact to determine exactly how it went. All you can do is your best and see how things work out in the end. Knowing the answers to the C++ interview questions above will help you be confident and relaxed, and that will always improve your odds for success.

Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.