50 C++ Interview Questions Every Developer Should Know in 2024 (2024)

C++ is still one of the most important programming languages as most schools/colleges still teach them to freshers as their first foray into coding. It still ranks in the top 5 rankings of the most popular programming languages.

So, for their first job, C++ is the skill they want to showcase. To ace their interviews, one must practice! So, we gathered 50 C++ Interview Questions, with Answers, which you can solve right now to brush up your knowledge.

50 C++ Interview Questions for Freshers

Here are 50 questions that are commonly asked in most technical interviews when testing the C++ coding skills of new developers:

1) How is C++ different from C?

CC++
C is a low-level language.C++ is a high-level language.
It does not support OOPS concepts like abstraction, encapsulation, inheritance, and polymorphism.It supports OOPS concepts like abstraction, encapsulation, inheritance, and polymorphism.
It is a procedural programming language that does not support classes and objects.It supports both a procedural programming language and object-oriented programming language.
C does not have a namespace feature which leads to ambiguity during collision.C++ has a namespace feature.

2) What are Classes in C++? Explain with an example.

A class is the blueprint of any object, a user-defined data type created for generating many objects with similar properties. For example, an animal serves as a blueprint encompassing common traits such as legs, colour, the sound they produce, and the food they consume.

 class Animal{ int legs; string color; string voice; public: Animal(){ } Animal(int a, string b, string c){ legs = a; color = b; voice = c; } };

3) What is an Object in C++?

An object is an instance of a class. For example, cows, cats, and dogs are all instances of animals, which is a class.

The basic syntax to construct an object of the above class Animal.

Animal* cat = new Animal(4, “black”, “meow”);

4) What is a Reference in C++?

A reference in C++ is like a nickname for a variable. The reference and the original variable share the same memory address, so any changes made to the reference variable also affect the original variable.

For example:// Original variable a.int a = 1;// Reference variable b.int& b = a;// Assigning the value of b to 2 also changes the value of a to 2.b = 2;// Now a equals 2.

5) What are Constructors?

A constructor is a special method in C++ that doesn’t have a return type. It’s designed to initialize data for an object. The constructor’s name must match the class name. For instance, in the given example, the method named Animal is a constructor.

 class Animal{ int legs; string color; string voice; public: Animal(int a, string b, string c){ legs = a; color = b; voice = c; } };

6) Define namespace in C++.

A namespace helps to avoid confusion between two variables or functions having the same name. For instance, if there are two functions with the same name but inside different namespaces, when you call one of these functions, the program automatically understands which one you mean. For example:

#include <iostream>using namespace std;namespace a {int fun() {return 1;}}namespace b {int fun() {return 0;}}using namespace a;int main() {cout << fun();return 0;}

7) What is the use of the keyword ‘using’?

The word ‘using’ is used with a namespace in C++. It is used to specify the use of a single namespace or multiple namespaces.

8) How to input strings in C++ with spaces?

To input strings with spaces in C++, we use the getline() function, which is part of the iostream library. For example:

Example:

#include<bits/stdc++.h>using namespace std;int main(){ cout<<"Enter Full Name"<<endl; string s; // input a long string with spaces getline(cin, s); cout<<"Your name is: "<<s; return 0;}

9) What is C++? What are the advantages of C++?

C++ is a programming language that supports OOP concepts, aiding programmers in reusing redundant code. It is a general-purpose programming language, enabling the creation of various types of projects. The basic syntax of C and C++ is almost identical, with the main difference being that C++ supports OOP while C does not.

10) What is ‘std’ in C++?

`std` is the standard library of C++. It contains functions, methods, classes, and objects. It helps prevent naming conflicts. For instance, if you attempt to use `cout` without specifying `std`, it will result in an error because the compiler doesn’t recognize `cout` without the use of the `std` namespace.

11) What are the different Data Types present in C++?

Data types are used to determine the type of a variable. There are three different types of data types:

  • Built-in or Primary Data Type: These are predefined data types, such as int, float, bool, double, char, void, etc.
  • Derived Data Type: These are data types derived from the primary data types. Examples include array, function, etc.
  • User-Defined Data Type: These are data types created by users. Examples include class, structure, union, etc.

12) What are Access Modifiers?

Access modifiers dictate whether data is accessible outside the class in C++. There are three types of access modifiers:

  • Public: If data members are defined as public, they can be used anywhere.
  • Private: If data members are defined as private, they can only be accessed within that class. By default, all data members are private.
  • Protected: Similar to private, but accessible in all subclasses of the parent class.

13) What is the difference between a while loop and a do-while loop?

While loopDo-while loop
Check the condition before entering into the loop.Check the condition after entering into the loop. So it will run at least once.
Syntax:
while(condition){// code}
Syntax:
do{// code}while(condition);

14) What is the size of the int data type?

The size of the `int` data type varies depending on the compiler. Generally, it is 4 bytes but it can be 2 bytes in 32-bit architectures.

15)What is operator overloading?

Operator overloading is a form of compile-time polymorphism used to extend the functionality of original operators. For instance, the ‘+’ operator, typically used to add built-in variables, can be overloaded to add two real numbers or user-defined variables.

16) Can we compile a program without the main function in C++?

No, we cannot compile a program if it does not have a main function because the main function is the entry point for any program in C++. So it will through a compile-time error.

17) What are Tokens in C++?

In a C++ program, any word is a token, serving as the smallest building block of the language. For instance, in the statement “int a = 5;”, tokens include int, a, =, 5, and ‘;’.

There are six main types of tokens.

  • Keywords
  • Identifiers
  • Operators
  • Special Symbols
  • Strings
  • Constants

18) What is the difference between an array and a list?

ArrayList
An array is a data structure used to store multiple variables simultaneously. It has a fixed size.The list is a data structure used to store multiple variables simultaneously. It has a variable size.
Once declared, you cannot resize it.You can resize it anytime you want.
They continuously assign the memory.It does not continuously allocate the memory.
You can access the data member by using indexesYou can access the data member only with the address.

19)What is the difference between new and malloc()?

Both `malloc` and `new` are used to allocate dynamic memory in C++. While `malloc` can also be used in C, `new` is exclusive to C++. The `new` keyword assigns a separate memory address to the created variable, whereas `malloc` returns a void pointer.

20) What is the difference between virtual functions and pure virtual functions?

Virtual functions are functions declared in the parent class that can be overridden in the derived class. Pure virtual functions, on the other hand, are defined in the parent class with nobody. They must be overridden by a derived class.

21) How is ‘struct’ different from class?

StructClass
It is generally used to group a lot of data.It is generally used for its properties like abstraction, inheritance, polymorphism, and encapsulation.
It is created using the ‘struct’ keyword.It is created using the ‘class’ keyword.
All members are public by default.All members are private by default.
Syntax:
Struct name_of_struct{// declare data members and methods
};
Syntax:
class name_of_class{// access specifier// data members// function};

22) What are OOPs?

OOP, or Object-Oriented Programming, is a programming paradigm used to minimize redundant data by creating classes, which serve as blueprints for objects. If we need to create many objects with similar properties and functions, we can utilize a class.

The four main pillars of OOP are:

  • Abstraction
  • Inheritance
  • Encapsulation
  • Polymorphism.

23) What is Polymorphism in C++?

It is one of the pillars of OOP. Polymorphism is one of them. Polymorphism means ‘having many forms’. For example, shapes have many forms like circles, squares, triangles, etc. Similarly, the same function may behave differently for different objects.

Polymorphism is of two types:

  • Compile-time Polymorphism
  • Run-time Polymorphism

24) What are Compile-time polymorphism and Runtime polymorphism?

Compile-time Polymorphism: This is known as compile-time polymorphism because, during compilation, it is decided which function will be executed. In this, you have more than one function with the same name in a class but with different prototypes.

Runtime Polymorphism: This is known as run-time polymorphism because, during runtime, it is decided which function will be executed. In this, you have the same function name in the parent and base class.

25) What is friend class and friend function?

A friend class can access private and protected members of the class in which it is declared. A friend function can access and modify private and protected members of the class in which it is declared.

Friend class example:

class parent{int a = 2;// declaring friend classfriend class s;};class s{void fun1(parent& var ){cout<<var->a<<endl;}};Friend function exampleclass parent{int a = 2;// declaring friend classpublic:friend void s(parent& var);};void s(parent& var){cout<<var->a<<endl;};

26)Define operator overloading and function overloading.

Operator Overloading: It is a compile-time polymorphism. It is used to provide special powers to the original operators. For example,’ +’ is used to add two inbuilt variables but by using the operator overloading we can add two real numbers or any user-defined variables.

Function Overloading: It is a compile-time polymorphism. It is used to define functions with the same name but different types of arguments. For example, if we have an add function that takes two integers and we have another add function that takes 3 integers or (1 integer & 1 decimal value) then when we call an add function it will depend on the arguments which you will pass.

27) What are the various OOP concepts in C++?

There are four main principles of OOPs:

  • Inheritance: It helps create a new class from an existing one.
  • Polymorphism: Polymorphism means ‘having many forms’. For example, shapes can have various forms like circles, squares, triangles, etc.
  • Abstraction: It hides unnecessary information from users and shows only the important ones.
  • Encapsulation: It binds data and functions together, like a capsule containing medicines. Similarly, it binds all the data and methods together.

28) What is Function Overriding?

As the name suggests, we are overriding a function in a child class that is already defined in a parent class. It’s runtime polymorphism because, during runtime, it will be decided which methods will be executed.

29) How delete [] is different from delete?

‘delete’ is used to remove and free space for a single variable. ‘delete[]’ is used to remove and free space for an array of objects. The difference lies in the fact that ‘delete’ calls only one destructor, while ‘delete[]’ needs to call the destructor for each object of the array.

30) What is a friend function?

Friend functions can access and modify private and protected members of the class in which they are declared. Here is an example of a friend function:

class parent{int a = 2;// declaring friend classpublic:friend void s(parent& var);};void s(parent& var){ cout<<var->a<<endl;};

31) What is Inheritance in C++?

Inheritance is a fundamental concept in OOP. It allows for the creation of a new class from an existing one. The new class is called a derived or child class, while the existing class is known as a base or parent class. The derived class inherits all the properties of the base class and can also have its properties.

32) What is a virtual function?

Virtual functions are functions declared in the parent class that can be replaced with a new implementation in the derived class.

33) What do you understand about pure virtual functions?

Pure virtual functions are functions declared in the parent class and must be defined in the child class. The pure virtual function has nobody in the parent class; it is merely used for declaration.

34) What does the Scope Resolution operator do?

The scope resolution operation is denoted by the symbol ‘::’. It allows users to use the same variable name inside a function if it’s already defined as a global variable. It’s also used to define functions outside the class. Moreover, it helps in determining which variable to call during multiple inheritance.

35) What are destructors in C++?

Destructors are special functions that are called automatically when an object’s use is finished or when it goes out of scope. Each class can have only one destructor, which mainly works to free up memory.

36) What is STL?

It’s a library in C++ called Standard Template Library (STL) which includes various algorithms and functions. For instance, the sort function is already included in the STL library. So, you can use it directly to sort arrays without needing to write the sorting code yourself.

37) What is a storage class in C++?

Storage class is used to define the characteristics of a variable. It defines the data type of the variable, such as int, string, char, etc. Additionally, it specifies the scope, storage location, default value, visibility, and lifetime of the variable.

38) What is the difference between shallow copy and deep copy?

Shallow CopyDeep Copy
It is used to copy the address of the original object.It is used to copy the values of the original object.
Any changes made in the shallow copy will reflect in the original object.Any changes made in the deep copy will not affect the original object.
It is faster to create as we just need to copy the address.It’s slower than the shallow copy.

39) What is Data binding and Abstraction?

Data Binding: As the name suggests, data are bound together with functions. This process is also known as encapsulation.

Abstraction: It is used to hide unnecessary information from users and show only important ones. For example, while you are driving a car, all you know is if you press the brakes the car will stop; you don’t know the actual mechanism behind it.

40) What are the various types of inheritance?

Inheritance is a key concept in OOPs, helping to create new classes from existing ones. There are five types of inheritance in C++.

  • Single Inheritance: It involves a child class derived from a single-parent class.
  • Multiple Inheritance: Here, a child class inherits properties from more than one parent class.
  • Hierarchical Inheritance: A parent class has multiple child classes, forming a tree-like structure.
  • Multilevel Inheritance: Inheritance occurs at multiple levels, with a child class derived from a parent class, and further inherited by another class.
  • Hybrid Inheritance: This combines both multiple and multilevel inheritance.

41) What are call-by values and call-by references in C++?

Call-by ValuesCall-by References
When a function is called by passing a copy of the original values.When a function is called by passing the address of the original value.
Any changes made in function do not affect the original value.Changes made within the function also reflect in the original value.
It consumes extra memory.It does not consume any extra memory.

42) What is an overflow error?

Overflow error happens when we exceed the limits of a data type, typically by trying to store a value larger than the data type can handle. This leads to an unexpected error.

43) Is deconstructor overloading possible?

No, overloading the destructor is not possible in C++. Each class can only have one destructor, which doesn’t take any arguments. It’s called automatically when an object goes out of scope.

44) What are access modifiers in C++?

Access modifiers are used to determine whether data members or functions are accessible outside the class. There are three types: private, public, and protected. Private means data members cannot be accessed outside the class, public means they can be accessed, and protected means they can be accessed only by the parent and its child class.

45) What is ‘cout’ in C++?

The ‘cout’ is a tool used to display output on the screen. It’s mainly used to show information or messages while running a program.

Syntax:

cout << "message";

46) Which is the best practice to increment the variable by 1, S++ or S = S+1?

Both are equally correct: you can increment the value by using S++ or S = S+1. However, if you only need to increment the value by one, you should use S++ as it is shorter and slightly more efficient.

47) What is the purpose of #undef preprocessor?

`#undef` is used to remove the previous definition of a macro. It’s commonly used when you need to change the value of a macro or if you don’t want to use it anymore. This allows you to use the macro name as a regular variable without causing any issues.

48) What is the order of objects destroyed in the memory?

The objects are destroyed in the reverse order in which they are created. So, the object which is created at first will be the last to be destroyed.

49) What is a stack memory?

Stack memory in C++ stores local variables, function inputs, and return types. When using recursive functions instead of iteration, it consumes more stack space. It operates on a Last-In-First-Out (LIFO) order, meaning the first item to enter the stack memory will be the last one to exit.

50) What are runtime and compile time errors?

Runtime errors are errors that occur during the execution of the program. They usually happen due to logical mistakes, like dividing by zero or accessing elements outside the bounds of an array. Compile-time errors, on the other hand, occur during compilation of the program. They typically result from syntax errors or not including the proper libraries.

Conclusion

After solving all these C++ questions, you are now prepared for a strong performance in your interview. You can connect with our Programming Tutors online to understand the concepts thoroughly or find errors in your answers.

50 C++ Interview Questions Every Developer Should Know in 2024 (2024)
Top Articles
Latest Posts
Article information

Author: Barbera Armstrong

Last Updated:

Views: 5587

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.