Home » Interview Questions and Answers » C++ Interview Questions and Answers for Beginners 2021

C++ Interview Questions and Answers for Beginners 2021

You may wonder how you can polish your current C++ programming skills and where you can go to look for the age-old question that haunts every coding professional, “How will I ace my programming interview?” Well, that is exactly the question that we aim to answer with this article that includes some of the most basic C++ interview questions and answers. 

One may find varied sets of questions from different sources based on different levels of expertise, however, further in this article, you will find C++ interview questions for beginners, as well as experienced professionals. Let us get started! 

Basic C++ Interview Questions and Answers:

1. What is C++?

C++ is a general-purpose, multi-paradigm, high-level programming language. C++ supports both Procedural, as well as Object-Oriented Programming (OOP). C++ is a superset of the C programming language; however, C cannot run C++ code. It is one of the most popular coding scripts for creating larger applications.

2. Who created C++?

The creator of C++ is Bjarne Stroustrup, who developed this programming language in 1979.

3. Name the OOP concepts:

The OOP Concepts include the following:

  • Object and Class, 
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction

4. Define a class:

Essentially, a class is a data-type that has data members and member functions. A class may also be defined in C++ using keyword classes followed by the name of the class itself.

5. Define and object:

An object within C++ is defined as an instance of a class. When the class is defined, only the speculation for the object is defined in this process.

6. Define the ‘*’ and ‘&’ operators in C++:

The ‘*’ operator is used as a pointer to a variable. For instance, * a where * acts as a pointer to the variable ‘a’. On the other hand, the ‘&’ operator is used to get the address of the variable. For instance, &a will give you the address of the variable ‘a’.

7. How many functions can be used to free the memory allocated by calloc()? Name them.

  • free();
  • memalloc(variable_name, 0)
  • malloc(variable_name, 0)
  • dealloc();

8. Define dynamic binding:

Dynamic binding (aka late binding) implies that the code associated with any specific procedure call is unknown until the time of the call at runtime.

9. Differentiate between class and structure:

By default, the members of structures are public while the members of classes are private. Structures do not provide something like data hiding, which classes do provide. Structures also contain only data while classes bind both data and member functions. 

10. What do you mean by a copy constructor?

A copy constructor can be defined as a method that accepts an object of the same class and further proceeds to copy its members to the object on the left of the assignment.

11. Differentiate between copy constructor and overload assignment operator?

A copy constructor basically constructs a new object by using the content of the argument object. On the other hand, an overload assignment operator assigns the contents of an existing object to another existing object of the same class.

12. Differentiate between method and message:

A method provides a response to a message and it is an implementation of an operation. Unlike a method, a message refers to when objects communicate with each other by sending messages.

13. Define a friend class:

Class members can gain accessibility over other class members by placing a class declaration prefixed with the keyword, ‘friend’ in the ‘class’ destination.

14. Explain the function of storage classes:

Storage classes are used to specify the visibility/ scope and lifetime of symbols (variables and functions). That means storage classes specify where all a variable or function can be accessed and till what time those variables will be available during the execution of a program.

15. Define a token in C++:

A C++ program includes multiple tokens. In C++, a token can either be a keyword, a string literal, a symbol, an identifier, or even a constant.

16. What happens when a variable becomes volatile?

Variables prefixed with the keyword ‘volatile’ act as a data type qualifier. The volatile keyword tries to change the given default method by which the variables are stored and the method by which the compiler handles these variables.

17. Define a virtual function:

A virtual function can be defined as a member function in the base class that you expect to get redefined within a derived class.

18. What are pointers? What do they do?

Pointers are a special type of variable that is used to store the memory address of the other variables. Pointers are declared normally as other variables with the difference of ‘*’ that is present in front of the pointer identifier.

19. What do you mean by a null pointer?

A null pointer is not the uninitialized pointer that can point anywhere. Null pointers are the ones that do not point anywhere, be it any object or function.

20. In the context of C++, define enumeration:

In C++, an enumeration is a user-defined data type that typically consists of integral constants. The ‘enum’ keyword is used in order to define an enumeration.

21. In the context of C++, define encapsulation:

In the context of C++, encapsulation is defined as a process in which data members and functions are combined in a single unit termed as ‘class’.

22. Within C++, what do you mean by abstraction?

In the context of C++, abstraction is a technique of displaying only essential details without representing the details that resulted in the implementation. In a case where members are defined with the ‘public’ keyword, then the members are accessible outside too.

23. Define data binding:

Data binding can be defined as a process of binding the application UI (User Interface) and business logic. Any change made in the business logic will reflect directly onto the application UI.

24. Why do we use ‘namespace’?

Namespaces are typically used to group entities like functions, objects, and classes under one cohesive name.

25. Define private, protected, and public, in the context of C++: 

Protected – Data functions and members are available to derive classes only.

Public – In public, the data members and functions are accessible outside the class.

Private – In private, data functions and members are not accessible outside any class.

26. How will one determine if a linked-list is a cycle or not?

One may find out if a linked-list is not a cycle by making use of two pointers. One of them goes in two nodes every single time, while the second one goes at 1 node each time. If there is a cycle, the one that goes in 2 nodes each time will meet the one that goes slower. If and when this happens, you can say that the linked-list is a cycle or not.

27. Define STL:

STL stands for Standard Template Library. STL refers to a library of container templates approved by the American National Standards Institute (ANSI) committee for inclusion within the general C++ specification.

28. Define stack unwinding:

Stack unwinding refers to the process during exception handling when the destructor is called for all of the local objects between the place where the exception was thrown and where it was caught.

29. What is the use of a PDB file?

In C++, a PDB (Program Database) file contains debugging and project state information that allows incremental linking of a Debug configuration of the program. This file is created when a coder compiles a C++ program with /ZI or /Zi or a Visual Basic/ Jscript.NET/ C# program while having the /debug option enabled.

30 What does OOPS stand for?

OOPs, in the context of programming languages stands for Object-Oriented Programming System.

31. What do you mean by an inline function?

A function prefixed with the keyword ‘inline’ before the function definition is known as an inline function in C++. The inline functions are much faster to execute as compared to normal functions as the compiler treats inline functions as macros.

32. Which are the different storage classes in C++?

The following storage classes are facilitated by C++:

  • Static
  • Auto
  • Register
  • Extern
  • Mutable

Advanced C++ Interview Questions and Answers for Experienced Professionals:

1. Differentiate between C and C++:

Some of the features of C are as follows:

  • C programming language was developed by Dennis Ritchie, sometime between 1969 and 1973 at AT&T Bell Labs.
  • As suggested earlier, C is a subset of C++ as C cannot run C++ code.
  • C does not support the ‘namespace’ feature.
  • C also does not support friend or virtual function.
  • C does not facilitate function and operator overloading.
  • C does not support reference variables.
  • C is a procedural programming script.
  • There is no support for exception handling in C.

However, in contrary to this, C++ fares as follows:

  • As mentioned earlier, C++ was created by Bjarne Stroustrup in 1979.
  • C++ is a superset of C as its code cannot be executed by C.
  • C++ is both, a procedural, as well as an object-oriented program language.
  • C++ makes use of the ‘namespace’ feature.
  • It supports function and operator overloading.
  • It also allows for smooth exception handling.
  • It facilitates the use of reference variables.
  • To top it all off, C++ also supports friend and virtual functions.

2. Differentiate between union and structs:

A struct is a piece of code that can have different types of data types. For instance, the following code has both, int and char types of data:

#include <iostream>
 
int main() {
struct a {
  int a;
  char b;
} a;
}

While a union can also have different types of data types inside it. The difference is that a struct can initialize different values for different variables. In unions, on the other hand, it stores the contents of a member variable at the exact same memory location.

3. Differentiate between #include “…” and #include <…>:

When ‘#include “…”’ is used, the preprocessor looks for the file to be included in the same directory where the current source file resides. On the other hand, when ‘#include <…>’ is used, the preprocessor searches for the file in directories pre-designated by the compiler – usually directories where the standard library header resides.

4. What are dangling pointers and how are they different from Memory Leaks?

Dangling pointers are those that point to memory locations that have already been freed and liberated. Whereas, Memory leaks happen when memory locations are not freed, however, there is no way to refer to them.

5. Differentiate between pre increment and post increment operator:

A pre increment operator is used to increment the value of the variable by 1 before assigning the value to the variable. On the contrary, a post increment operator is used to increment the value of the variable by 1 after assigning the value to the variable.

4. Why do we use the ‘goto’ statement?

The ‘goto’ statement is used in order to transfer the default flow of any program to a specified label within the given program. It is a one-way transfer of control to another line of code, in contrast, a function call usually returns control.

The following instance describes how it is coded:

{
.......
goto label;
....... 
.......
LABEL:
statements;
}

7. Define the static and extern function in C?

When we declare or define any function as static, these functions cannot be used in other files of the same project. On the other hand, by default, any function that is defined or declared in C++ is extern. 

8. Can a variable be both constant and volatile?

A constant modifier does not allow changing the value of the variable by an internal program. As suggested by its term, the value of a volatile variable may change at any point in time.

9. Differentiate between malloc and new:

Malloc allocates uninitialized memory. This allocated memory has to be released with ‘free’. On the other hand, new initializes calls the constructor in order to initialize the allocated memory. Memory allocated through new should be released with ‘delete’.

10. What do you mean by operator overloading?

To overload an operator refers to a process by which the coder provides the operator with a new meaning for user-defined types of data.

return_type [email protected](argument_list)
{
  //...definition
}

11. Define recursion:

A function that calls itself is known to be a recursive function. This process is known as recursion.

12. What do you mean by function overriding?

Function overriding is a way to implement polymorphism. Function overriding is a programming language feature that facilitates and supports child class or subclass to provide a particular implementation of any given method that has already been provided by any one of its parent classes or super classes.

13. How should one handle run time errors in C++?

Any run time error in C++ can be handled using exceptions. This mechanism of exception handling in C++ is developed to manage the errors in software and is made up of independently developed components operating in one process and under synchronous control.

14. Where are ‘longjmp’ and ‘setjmp’ used in C++?

‘longjmp’ and ‘setjmp’ should ideally, not be used while programming with C++. ‘longjmp’ jumps out of a given function without having to unwind the stack that it is in. Basically, this implies that the local objects generated are not destructed adequately. The more suitable alternative to this would be to use ‘throw/ catch/ try’ instead.

15. Explain the One Definition Rule (ODR) in C++:

According to the One Definition Rule (ODR), C++ constructs must be identically defined and declared in every compilation unit in which they are used. As per the rules of ODR, two definitions contained within varied source files are said to be identically defined if they match from token to token. Equivalence from character to character does not imply that they are identical in nature. Two definitions may have different whitespace or comments and still be identical to each other. 

14. Which types of inheritance are supported in C++ code?

The different types of inheritance that are supported by C++ in its code as Hybrid, Single, Multiple, Hierarchical, and Multilevel.

15. What function does a protected access specifier provide?

If a member of any class is protected, then it is accessible in the inherited class. However, both private and public members are not accessible. This happens because of the presence of a protected access specifier.

18. Why and how does code-bloating happen in C++?

Improper use of inline templates and functions may cause code-bloating. Multiple inheritances also leads to code bloating.

19. Why do we use realloc()?

realloc() is a function that is subroutine in nature. Realloc() changes the size of a specified block of memory, which has been pointed out by the pointer parameter to the exact specification of bytes as determined by the size parameter. It returns a brand-new pointer to the block in question. 

20. What is the function of auto storage class in C++?

An auto storage class is the default storage in C++. These variables are destroyed at the end of the block that defines the variable. These variables are not visible outside this block.

21. How do we use virtual functions in C++?

Virtual functions are implemented with the use of a table of functions, more commonly known as the ‘V-table’. There is one entry within the table per virtual function in the class. The table is created by the class constructor. When a derived class is constructed, its base class is constructed first, which created the V-table. The derived class may override some of the base class’s virtual functions. Such entries in the V-table are overwritten by the derived class constructor. This is why virtual functions should not be called from a constructor.

22. What are the varied sections of program memory?

The memory a program used is segregated into 4 areas, they are as follows:

  • The code area – This is where the compiled program sits within the program memory.
  • The global area – This is where global variables are stored.
  • The heap – This is where dynamically allocated variables are stored.
  • The stack – this is where parameters and local variables are allocated.

23. What do you mean by deep copy?

Deep copy involves using the contents of one object to create another instance for the same class. Here. The two objects may contain the same information but the target object will have its own resources. The destruction of any object will not affect the other objects.

24. What do you mean by shallow copy?

A shallow copy involves a process through which the contents of one object are copied into another instance of the same class. This creates an identical mirror object. The two objects share the exact same externally contained contents. This happens due to the direct copying of references, as well as pointers.

25. What is a scope resolution operator?

A scope resolution operator is used to define the member functions of a class outside a specific class. Usually, a scope resolution operator is required when a data member is redefined by a derived class or an overridden method of the derived class wishes to call the base class version of the same method.

26. What is the function served by templates?

Templates enable and facilitate the creation of general and basic functions that accept any data type as a parameter. They also return value without the need to overload the function with all the possible data types unless and until they satisfy the functionality of a macro.

27. What is a queue? How does one implement a queue in C++?

A queue is a type of linear data structure that acts as a collection of elements that are homogenous in nature. Within a queue, insertion and deletion happen at different ends of the queue. The end where insertion happens is termed as ‘rear’, whereas, the ned where deletion occurs is termed as ‘front’. 

A queue is also known as FiFo, which translates to First in, First out. This is because whichever element is inserted first within the queue is indefinitely the one that comes out first from the queue. One can implement a queue by the linked-list implementation of the stack, as well as array implementation of stack

Did you know?
1. Tableau interview questions
2. Python interview questions
3. Apache Spark interview questions
4. HTML interview questions
5. Java interview questions
6. SEO interview questions
7. C# interview questions
8. C++ Interview Questions

Pin It on Pinterest