C++ Interview Questions – Intermediate Level

29. Which operations are permitted on pointers?

Pointers are the variables that are used to store the address location of another variable. Operations that are permitted to a pointer are:

  1. Increment/Decrement of a Pointer
  2. Addition and Subtraction of integer to a pointer
  3. Comparison of pointers of the same type

30. What is the purpose of the β€œdelete” operator?

The delete operator is used to delete/remove all the characteristics/properties from an object by deallocating its memory; furthermore, it returns true or false in the end. In simple terms, it destroys or deallocates array and non-array(pointer) objects which are created by new expressions.

C++
int GFG = new int[100];
// uses GFG for deletion
delete[] GFG;

For more information, refer to Delete operator

31. How delete [] is different from delete?

delete[]

delete

It is used for deleting a whole arrayIt is used to delete only one single pointer
It is used for deleting the objects of new[]; By this, we can say that delete[] is used to delete an array of objectsIt is used for deleting the objects of new; By this, we can say that delete is used to delete a single object
It can call as many destructors it wantsIt can only call the destructor of a class once

32. What do you know about friend class and friend function?

A friend class is a class that can access both the protected and private variables of the classes where it is declared as a friend.

Example of friend class:

C++
class Class_1st {
    // ClassB is a friend class of ClassA
    friend class Class_2nd;
    statements;
} class Class_2nd {
    statements;
}


A friend function is a function used to access the private, protected, and public data members or member functions of other classes. It is declared with a friend keyword. The advantage of a friend function is that it is not bound to the scope of the class and once it is declared in a class, furthermore to that, it cannot be called by an object of the class; therefore it can be called by other functions. Considering all the mentioned points we can say that a friend function is a global function.

Example of friend function:

C++
class GFG {
    statements;
    friend dataype function_Name(arguments);
    statements;
} OR class GFG {
    statements' friend int divide(10, 5);
    statements;
}


For more information, refer to the friend function and friend class

33. What is an Overflow Error?

Overflow Error occurs when the number is too large for the data type to handle. In simple terms, it is a type of error that is valid for the defined but exceeds used the defined range where it should coincide/lie. 

For example, the range of int data type is –2,147,483,648 to 2,147,483,647 and if we declare a variable of size 2,247,483,648 it will generate a overflow error.

34. What does the Scope Resolution operator do?

A scope resolution operator is denoted by a β€˜::β€˜ symbol. Just like its name this operator resolves the barrier of scope in a program. A scope resolution operator is used to reference a member function or a global variable out of their scope furthermore to which it can also access the concealed variable or function in a program.

Scope Resolution is used for numerous amounts of tasks:

  1. To access a global variable when there is a local variable with the same name
  2. To define the function outside the class
  3. In case of multiple inheritances
  4. For namespace

For more information, refer to Scope resolution operator

35. What are the C++ access modifiers?

The access restriction specified to the class members( whether it is member function or data member) is known as access modifiers/specifiers. 

Access Modifiers are of 3 types:

  1. Private – It can neither be accessed nor be viewed from outside the class 
  2. Protected – It can be accessed if and only if the accessor is the derived class
  3. Public – It can be accessed or be viewed from outside the class 

For more information, refer to Access Modifiers

36. Can you compile a program without the main function?

Yes, it is absolutely possible to compile a program without a main(). For example Use Macros that defines the main

C++
// C++ program to demonstrate the
// a program without main()
#include
<stdio.h>
#define fun main
    int fun(void)
{
    printf("w3wiki");
    return 0;
}

37. What is STL?

STL is known as Standard Template Library, it is a library that provides 4 components like container, algorithms, and iterators.
 

C++ STL

For more information, refer to STL in C++

38. Define inline function. Can we have a recursive inline function in C++?

An inline function is a form of request not an order to a compiler which results in the inlining of our function to the main function body. An inline function can become overhead if the execution time of the function is less than the switching time from the caller function to called function. To make a function inline use the keyword inline before and define the function before any calls are made to the function.

Inline Function Explanation

Syntax:

inline data_type function_name()
{
Body;
}

The answer is No; It cannot be recursive.

An inline function cannot be recursive because in the case of an inline function the code is merely placed into the position from where it is called and does not maintain a piece of information on the stack which is necessary for recursion.

Plus, if you write an inline keyword in front of a recursive function, the compiler will automatically ignore it because the inline is only taken as a suggestion by the compiler.

For more information, refer to Inline Function

39. What is an abstract class and when do you use it?

An abstract class is a class that is specifically designed to be used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier(= 0) in the declaration of a virtual member function in the class declaration

You cannot use an abstract class as a parameter type, a function return type, or the type of an explicit conversion, nor can you declare an object of an abstract class. However, it can be used to declare pointers and references to an abstract class. 

An abstract class is used if you want to provide a common, implemented functionality among all the implementations of the component. Abstract classes will allow you to partially implement your class, whereas interfaces would have no implementation for any members whatsoever. In simple words, Abstract Classes are a good fit if you want to provide implementation details to your children but don’t want to allow an instance of your class to be directly instantiated.

40. What are the static data members and static member functions?

The static data member of a class is a normal data member but preceded with a static keyword. It executes before main() in a program and is initialized to 0 when the first object of the class is created. It is only visible to a defined class but its scope is of a lifetime.

Syntax:

static Data_Type Data_Member;  

The static member function is the member function that is used to access other static data members or other static member functions. It is also defined with a static keyword. We can access the static member function using the class name or class objects.

Syntax:

classname::function name(parameter);

C++ Interview Questions and Answers (2024)

C++ – the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent in C++. It is utilized by top IT companies such as Evernote, LinkedIn, Microsoft, Opera, NASA, and Meta because of its dependability, performance, and wide range of settings in which it may be used. So, to get into these companies, you need to be thorough in these top 50 C++ interview questions which can make you seem like an expert in front of recruiters.

To make you interview-ready, we have brought the Top 50 C++ interview questions for beginner, intermediate and experienced which you must definitely go through in order to get yourself placed at top MNCs.

Similar Reads

C++ Interview Questions For Freshers

1. What is C++? What are the advantages of C++?...

C++ Interview Questions – Intermediate Level

29. Which operations are permitted on pointers?...

C++ Interview Questions – Expert Level

41. What is the main use of the keyword β€œVolatile”?...

Bonus Question:

What is β€˜thisβ€˜ pointer in C++?...