Basic C++ Interview Questions

Basic-C++-Interview-Questions

C++ is general-purpose, object-oriented programming (OOP) language that was developed by Bjarne Stroustrup. An extension of the C programming language, C++ combines the features of both low-level and high-level languages and is hence considered to be a hybrid, intermediate level programming language.

Even today, C++ is considered to be a requisite skill for CS students and engineers. It is one of the most challenging programming languages since when writing programs with C++, one has to do extensive thinking. Hence, to crack a C++ interview successfully, you must be well prepared.

To help you crack your next C++ interview, we’ve created a list of some of the most commonly asked questions during interviews.

  1. Name the basic components of OOP language?

The essential components of an object-oriented programming language are:

  • Objects
  • Classes
  • Inheritance
  • Polymorphism
  • Message passing
  • Dynamic Binding
  • Data abstraction and encapsulation
  1. How is C++ different from C?

Since C++ is an extension of C, most programs written in C are compatible with C++ too. The primary differences between the two languages are:

  • While C is a procedural programming language, C++ supports both procedural and OOP features.
  • As C++ is an OOP language, it supports features like inheritance, templates, classes, function overloading, and virtual functions, to name a few. C programming language is devoid of these functions.
  • While in C exception handling is done in traditional ‘if-else’ technique, in C++ exception handling is accomplished at the language level.
  • In C++ input and output operations are performed through streams (cin and cout), whereas in C, scanf() and printf() are used for input and output functions.
  1. What is inheritance?

Inheritance is the technique of deriving a new class known as ‘derived’ from an old class known as ‘base class.’ With the help of this process, you don’t need to write a code from scratch; you can reuse an existing code by modifying it.

  1. What are virtual functions?

Virtual functions are integrated with inheritance to ensure that the correct function has been deployed to point to a particular object. Instead of naming them according to the type of reference or pointer, virtual functions are named according to the kind of object being referred to. The functions are named with a virtual keyword in the base class.

  1. What are the C++ access specifiers?

Access specifiers define how functions and variables (members) can be accessed outside of a given class. They can be classified into three types:

  • Private: The functions and variables that are labeled as ‘private’ can be accessed only within the same class and not outside their declared class.
  • Public: When members are labeled as ‘public,’ they can be accessed from anywhere.
  • Protected: Protected members can be accessed only by the declared class its child classes.
  1. Define Friend class and Friend function in C++.

A friend class can access both private and protected members of other classes in which it is specified as a friend. For instance, a LinkedList class may be declared as a friend of Node to access the private members of Node.

Like a Friend class, a Friend function can be granted access to private and protected members of other classes. A friend function can either be a method of another class or a global function. It should be kept in mind that Friend classes and functions should be used only for specific purposes to prevent the drop in the encapsulation value of different classes in OOP.

  1. What is s Static Member?

Static Member is a keyword in C++ that is used to bestow unique characteristics to a specific element. Static members cannot be virtual, and they also do not possess ‘this’ pointer. Static elements are allotted a storage space in the static storage area. While they need to be stored only once in a program lifetime, their life scope equals the program lifetime.

  1. What is a default constructor?

Default constructors are those that do not accept any parameters. For instance, if a default constructor is needed in a particular class (say, class A) where there exists no user-defined constructor, the compiler will implicitly declare a default constructor without any parameter – A::A(). This constructor is devoid of any constructor initializer and a null body.

  1. What are tokens?

Tokens are the smallest individual units of a program written in C++. They are as follows:

  • Identifiers
  • Constants
  • Keywords
  • Strings
  • Operators
  1. What is an iterator class?

Iterator class allows you to access classes that are inside containers that hold data structures, classes, and abstract data types. Iterators are crucial to understanding the functioning of C++ Standard Template Library (STL) since it offers a way to access data stored in the container classes, for example, maps, list, vector, and so on.

Want to get an in-depth understanding of C++ and its underlying concepts? Join Coding Ninjas today! Our trained and dedicated team of instructors will ensure that by the time you finish the course, you’ve mastered the art of C++.