C++ Interview Questions and Answers

Last Updated : 6 May, 2026

C++ is a widely used programming language known for its performance, flexibility, and object-oriented features. It remains highly relevant and is used by top tech companies for system-level and high-performance applications. This article presents the top 50+ C++ interview questions to help you prepare effectively.

  • Covers beginner, intermediate, and advanced-level questions
  • Helps you build confidence for technical interviews
  • Designed for quick revision and placement preparation

C++ Interview Questions for Freshers

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

C++ is a general-purpose, object-oriented programming language developed by Bjarne Stroustrup. It is an extension of C that supports both procedural and object-oriented programming, making it suitable for system-level as well as application-level development.

Advantages of C++:

  • High Performance: Close to hardware, making it fast and efficient
  • Object-Oriented: Supports concepts like encapsulation, inheritance, and polymorphism
  • Portability: Can run on multiple platforms with minimal changes
  • Memory Control: Allows direct memory management using pointers
  • Rich Standard Library: Provides STL (Standard Template Library) for efficient data structures and algorithms

2. What are the different data types present in C++?

Data types define the kind of data a variable can store.

  • In C++, when a variable is declared, the compiler allocates memory for it based on its data type.
  • Each data type may require a different amount of memory.

Following is the list of data types in C++:

Data-Type-in-C-2
Data Types

3. Define 'std'?

'std' is also known as Standard or it can be interpreted as a namespace. The command "using namespace std" informs the compiler to add everything under the std namespace and inculcate them in the global namespace. This all inculcation of global namespace benefits us to use "cout" and "cin" without using "std::_operator_".

4. What are references in C++?

In C++, references are an alternative way to create an alias for another variable. A reference acts as a synonym for a variable, allowing you to access the variable directly without any additional syntax. They must be initialized when created and cannot be changed to refer to another variable afterward. This feature makes it easier to manipulate variables in functions while avoiding the overhead of copying large objects. A reference variable is preceded with a '&' symbol.

Syntax:

int GFG = 10;
// reference variable
int& ref = GFG;

5. What do you mean by Call by Value and Call by Reference?

In this programming language to call a function we have 2 methods: Call by Value and Call by Reference

Call by Value

Call by Reference

A copy of a variable is passed.A variable itself is passed fundamentally.
Calling a function by sending the values by copying variables.Calling a function by sending the address of the passed variable.
The changes made in the function are never reflected outside the function on the variable. In short, the original value is never altered in Call by Value.The changes made in the functions can be seen outside the function on the passed function. In short, the original value is altered in Call by reference.
Passed actual and formal parameters are stored in different memory locations. Therefore, making Call by Value a little memory inefficient. Passed actual and formal parameters are stored in the same memory location. Therefore, making Call by Reference a little more memory efficient.

6. Define token in C++

A token is the smallest individual element of a program that is understood by a compiler. A token comprises the following:

Tokens-in-C
C++ Tokens
  • Keywords - That contain a special meaning to the compiler
  • Identifiers - That hold a unique value/identity 
  • Constants - That never change their value throughout the program 
  • Strings - That contains the homogenous sequence of data 
  • Special Symbols - They have some special meaning and cannot be used for another purpose; eg: [] () {}, ; * = # 
  • Operators - Who perform operations on the operand

7. What is the difference between C and C++?

The following table lists the major differences between C and C++:

C

C++

It is a procedural programming language. In simple words, it does not support classes and objectsIt is a mixture of both procedural and object-oriented programming languages. In simple words, it supports classes and objects.
It does not support any OOPs concepts like polymorphism, data abstraction, encapsulation, classes, and objects.It supports all concepts of data 
It does not support Function and Operator OverloadingIt supports Function and Operator Overloading respectively
It is a function-driven languageIt is an object-driven language

8. What is the difference between struct and class?

Following table lists the primary difference between struct and class:

Aspectstructclass
Default Access ModifierMembers are public by default.Members are private by default.
Memory AllocationCan be allocated on the stack or heap.Can be allocated on the stack or heap.
InheritanceSupports inheritance (with public, protected, or private access).Supports inheritance (with public, protected, or private access).
Use CaseOften used for Plain Old Data (POD) structures, or simple data grouping.Suitable for complex objects that may include methods, constructors, and destructors.

9. What is the difference between reference and pointer?

Following are the main difference between reference and pointer:

Reference

Pointer

The value of a reference cannot be reassignedThe value of a pointer can be reassigned
It can never hold a null value as it needs an existing value to become an alias of                     It can hold or point at a null value and be termed as a nullptr or null pointer
To access the members of class/struct it uses a ' . 'To access the members of class/struct it uses a ' -> ' 
The memory location of reference can be accessed easily or it can be used directlyThe memory location of a pointer cannot be accessed easily as we have to use a dereference ' *

10. What is the difference between function overloading and operator overloading?

Following is the main difference operator overloading and function overloading:

Function Overloading

Operator Overloading

It is basically defining a function in numerous ways such that there are many ways to call it or in simple terms you have multiple versions of the same functionIt is basically giving practice of giving a special meaning to the existing meaning of an operator or in simple terms redefining the pre-redefined meaning                          
Parameterized Functions are a good example of Function Overloading as just by changing the argument or parameter of a function you make it useful for different purposes Polymorphism is a good example of an operator overloading as an object of allocations class can be used and called by different classes for different purposes

Example of Function Overloading:

  1. int GFG(int X, int Y);
  2. int GFG(char X, char Y);

Example of Operator Overloading:

  1. int GFG() = X() + Y();
  2. int GFG() = X() - Y();

11. What is the difference between an array and a list?

The major differences between the arrays and lists are:

Arrays

Lists

Array are contiguous memory locations of homogenous data types stored in a fixed location or size.    Lists are classic individual elements that are linked or connected to each other with the help of pointers and do not have a fixed size.
Arrays are static in nature.Lists are dynamic in nature
Uses less memory than linked lists.Uses more memory as it has to store the value and the pointer memory location

12. What is the difference between const and #define?

  • const: A typed constant with scope rules, checked by the compiler.
  • #define: A preprocessor macro replaced before compilation, no type checking.

const int x = 10; // Type safe
#define y 10 // No type checking

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

Following are the major difference between while and do-while loop:

Featurewhile loopdo-while loop
Condition CheckChecks condition before executionChecks condition after execution
Execution GuaranteeMay not execute even onceExecutes at least once
Syntax Structurewhile(condition) { ... }do { ... } while(condition);
Use CaseWhen number of iterations is unknownWhen loop must run at least once
Semicolon RequirementNo semicolon after conditionEnds with a semicolon (;)

14. What is the difference between a reference and a pointer?

  • Reference is an alias for a variable. It cannot be NULL and must be initialized.
  • Pointer is a variable storing address. It can be reassigned and can be NULL.
C++
#include <iostream>
using namespace std;

int main() {
    int a = 10;

    int& ref = a;    // reference
    int* ptr = &a;   // pointer

    cout << "ref: " << ref << endl;
    cout << "ptr: " << *ptr << endl;

    *ptr = 20;
    cout << "After changing via pointer, a = " << a << endl;

    ref = 30;
    cout << "After changing via reference, a = " << a << endl;

    return 0;
}

15. Define storage class in C++ and name some

Storage class is used to defines the scope (visibility), lifetime, and linkage of variables or functions. These features usually help in tracing the existence of a variable during the runtime of a program.

Syntax:

storage_class var_data_type var_name;

Some types of storage classes:

CStorageClass1
Define storage

16. Discuss the difference between prefix and postfix?

Following are the major difference between prefix and postfix:

prefix

postfix

It simply means putting the operator before the operandIt simply means putting the operator after the operand
It executes itself before '; '  It executes itself after '; ' 
Associativity of prefix ++ is right to leftAssociativity of postfix ++ is left to right

17. What is the difference between new and malloc()?

Following are the major difference between new and malloc()

new

malloc()

new is an operator which performs an operation   malloc is a function that returns and accepts values
new calls the constructorsmalloc cannot call a constructor
new is faster than malloc as it is an operatormalloc is slower than new as it is a function
new returns the exact data typemalloc returns void*

18. What is the difference between virtual functions and pure virtual functions?

Following are the major difference between virtual functions and pure virtual functions

Virtual Function

Pure Virtual Function

A Virtual Function is a member function of a base class that can be redefined in another derived class.A Pure Virtual Function is a member function of a base class that is only declared in a base class and defined in a derived class to prevent it from becoming an abstract class.
A virtual Function has its definition in its respective base class.              There is no definition in Pure Virtual Function and is initialized with a pure specifier (= 0).
The base class has a virtual function that can be represented or instanced; In simple words, its object can be made.A base class having pure virtual function becomes abstract that cannot be represented or instanced; In simple words, it means its object cannot be made.

19. What are classes and objects in C++?

A class is a user-defined data type where all the member functions and data members are tailor-made according to demands and requirements in addition to which these all can be accessed with the help of an object. To declare a user-defined data type we use a keyword class.

An object is an instance of a class and an entity with value and state; In simple terms, it is used as a catalyst or to represent a class member. It may contain different parameters or none.

Note: A class is a blueprint that defines functions which are used by an object.

Java
#include <iostream>
using namespace std;

class Student {
public:
    string name;    // data member

    void display_name() {   // member function
        cout<<"Name: "<<name<< endl;
    }
};

int main() {
    
    // Creating object of class
    Student s;
    s.name = "Rahul";
    s.display_name();
}

20. What is Function Overriding?

When a function of the same name, same arguments or parameters, and same return type already present/declared in the base class is used in a derived class is known as Function Overriding. It is an example of Runtime Polymorphism or Late Binding which means the overridden function will be executed at the run time of the execution.

21. What are the various OOPs concepts in C++?

Following are the OOPs concepts in C++:

  • Classes: It is a user-defined datatype
  • Objects: It is an instance of a class
  • Abstraction: It is a technique of showing only necessary details
  • Encapsulation: Wrapping of data in a single unit
  • Inheritance: The capability of a class to derive properties and characteristics from another class
  • Polymorphism: Polymorphism is known as many forms of the same thing

22. What is inheritance in C++?

Inheritance is a mechanism in which a class (derived class) acquires the properties and behaviors of another class (base class). It promotes code reuse and helps represent relationships between classes.

inheritance
Different classes inheriting from Animal Class

23. What is encapsulation in C++?

Encapsulation is the concept of wrapping data members and member functions together inside a class and restricting direct access to the data. It is usually implemented using access specifiers such as private, protected, and public. Sensitive data is kept private and accessed through public member functions.

Encapsulation
Encapsulation

24. What is virtual inheritance?

Virtual inheritance is a technique that ensures only one copy of a base class's member variables is inherited by grandchild-derived classes. Or in simple terms, virtual inheritance is used when we are dealing with a situation of multiple inheritances but want to prevent multiple instances of the same class from appearing in the inheritance hierarchy.

25. What is polymorphism in C++?

Polymorphism means one interface, multiple implementations.
It allows the same function or operator to behave differently depending on the context.

In C++, polymorphism can be achieved through:

  • Compile-time polymorphism (function overloading, operator overloading)
  • Run-time polymorphism (function overriding using virtual functions)
polymorphism

26. What are the different types of polymorphism in C++?

There is 2 type of polymorphism

1. Compile Time Polymorphism or Static Binding: This type of polymorphism is achieved during the compile time of the program which results in it making a bit faster than Run time. Also, Inheritance is not involved in it. It is comprised of 2 further techniques:

Function Overloading: When there are multiple functions with the same name but different parameters then this is known as function overloading.

C++
// same name different arguments
int GFG() {}
int GFG(int a) {}
float GFG(double a) {}
int GFG(int a, double b) {}


Operator Overloading: It is basically giving practice of giving a special meaning to the existing meaning of an operator or in simple terms redefining the pre-redefined meaning

C++
class GFG {
    // private and other modes
    statements public returnType
    operator symbol(arguments){ statements } statements
};

 Run-Time Polymorphism or Late Binding: Run-time polymorphism takes place when functions are invoked during run time. 
 
Function Overriding: Function overriding occurs when a base class member function is redefined in a derived class with the same arguments and return type.

C++
// C++ program to demonstrate
// Function overriding
#include
<iostream> using namespace std;
class GFG {
public:
    virtual void display()
    {
        cout << "Function of base class" << endl;
    }
};
class derived_GFG : public GFG {
public:
    void display()
    {
        cout << "Function of derived class" << endl;
    }
};
int main()
{
    derived_GFG dg;
    dg.display();
    return 0;
}

Output:

Function of derived class

27. Compare compile-time polymorphism and Runtime polymorphism

Following are the major differences between the runtime and compile time polymorphism:

Compile-Time Polymorphism

Runtime Polymorphism

It is also termed static binding and early binding.It is also termed Dynamic binding and Late binding.
It is fast because execution is known early at compile time.It is slow as compared to compile-time because execution is known at runtime.
It is achieved by function overloading and operator overloading.It is achieved by virtual functions and function overriding.

28. Explain the constructor in C++.

A constructor is a special type of member function of a class, whose name is the same as that of the class by whom it is invoked and initializes value to the object of a class. 

There are 3 types of constructors:

1. Default constructor: It is the most basic type of constructor which accepts no arguments or parameters. Even if it is not called the compiler calls it automatically when an object is created.

Example:

C++
class Class_name {
public:
    Class_name() { cout << "I am a default constructor"; }
};


2. Parameterized constructor: It is a type of constructor which accepts arguments or parameters. It has to be called explicitly by passing values in the arguments as these arguments help initialize an object when it is created. It also has the same name as that of the class. 

Also, It is used to overload constructors.

Example:

C++
// CPP program to demonstrate
// parameterized constructors
#include
<iostream> using namespace std;
class GFG {
private:
    int x, y;

public:
    // Parameterized Constructor
    GFG(int x1, int y1)
    {
        x = x1;
        y = y1;
    }
    int getX() { return x; }
    int getY() { return y; }
};
int main()
{
    // Constructor called
    GFG G(10, 15);
    // Access values assigned by constructor
    cout << "G.x = " << G.getX() << ", G.y = " << G.getY();
    return 0;
}

Output

G.x = 10, G.y = 15

3. Copy Constructor: A copy constructor is a member function that initializes an object using another object of the same class. Also, the Copy constructor takes a reference to an object of the same class as an argument.

Example:

C++
Sample(Sample& t) { id = t.id; }

29. What are destructors in C++?

Destructors are members of functions in a class that delete an object when an object of the class goes out of scope. Destructors have the same name as the class preceded by a tilde (~) sign. Also, destructors follow a down-to-top approach, unlike constructors which follow a top-to-down.

Syntax:

~constructor_name(); // tilde sign signifies that it is a destructor

30. What is a virtual destructor?

When destroying instances or objects of a derived class using a base class pointer object, a virtual destructor is invoked to free up memory space allocated by the derived class object or instance.

Virtual destructor guarantees that first the derived class' destructor is called. Then the base class's destructor is called to release the space occupied by both destructors in the inheritance class which saves us from the memory leak. It is advised to make your destructor virtual whenever your class is polymorphic.

31. Is destructor overloading possible? If yes then explain and if no then why?

The simple answer is NO we cannot overload a destructor. It is mandatory to only destructor per class in C++. Also to mention, Destructor neither take arguments nor they have a parameter that might help to overload.

32. When should we use multiple inheritance?

  • Use multiple inheritance when a class logically needs to derive features or behavior from more than one base class.
  • It is useful when a class needs to combine independent functionalities (e.g., a class that is both Printable and Serializable).
  • It can model real-world scenarios where an object belongs to multiple categories (e.g., Teacher who is also a Researcher).
  • However, it should be used carefully because it can lead to issues like the diamond problem, so prefer it only when there’s a clear and justifiable relationship.
Multiple_inheritance

Example:

Java
#include <iostream>
using namespace std;

// Parent class A
class A {
public:
    void showA() {
        cout << "Function from class A" << endl;
    }
};

// Parent class B
class B {
public:
    void showB() {
        cout << "Function from class B" << endl;
    }
};

// Child class C inherits from both A and B
class C : public A, public B {
public:
    void showC() {
        cout << "Function from class C" << endl;
    }
};

int main() {
    C obj;       // Object of derived class
    obj.showA(); // Access from class A
    obj.showB(); // Access from class B
    obj.showC(); // Own function
    return 0;
}

C++ Interview Questions - Intermediate Level

32. 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:

  • Increment/Decrement of a Pointer
  • Addition and Subtraction of integer to a pointer
  • Comparison of pointers of the same type

33. 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;

34. 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

35. 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;
}

36. 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.

37. 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

38. 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 

39. 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("Geeksforgeeks");
    return 0;
}

40. 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

41. What are STL containers? Explain different types with examples.

STL containers are predefined data structures in C++ that store collections of data in various ways. They are categorized into three types:

1. Sequence Containers: Maintain the order of insertion.

  • vector: Dynamic array
  • list: Doubly linked list
  • deque: Double-ended queue

Example:

vector<int> v = {1, 2, 3};

2. Associative Containers: Store elements using keys (ordered).

  • set, multiset: Store unique values
  • map, multimap: Store key-value pairs

Example:

map<string, int> age;
age["Alice"] = 25;

3. Unordered Associative Containers: Use hash tables instead of trees.

  • unordered_set, unordered_map

Example:

unordered_map<string, int> scores;

4. Container Adapters: Built on other containers with restricted interface

  • stack: based on last-in-first-out (LIFO).
  • queue: based on first-in-first-out (FIFO).
  • priority_queue: implemented using min or max heap

Containers save time and effort by providing built-in operations like insertion, deletion, and search with optimal complexity.

42. What are iterators in STL? Describe types of iterators with examples.

An iterator is an object (like a pointer) used to traverse containers. STL uses iterators to access elements in a uniform manner, regardless of the container. Iterators help in writing generic algorithms that work across different container types.

Types of Iterators:

TypeDescription
Input IteratorRead-only access, single pass
Output IteratorWrite-only access, single pass
Forward IteratorRead/write, multiple passes
BidirectionalTraverse both directions
Random AccessDirect jump to any position (e.g., vector)

43. What are lambda expressions in C++? How do they relate to STL?

Lambda expressions are anonymous functions defined inline using the [] syntax. They are commonly used with STL algorithms to simplify code.

Syntax:

[ capture ] (parameters) -> return_type { body }

Example:

C++
#include <bits/stdc++.h>
#include<vector>
using namespace std;

int main()
{

    vector<int> v = {1, 2, 3, 4, 5};

    int count = count_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; });
    cout<<count;
    return 0;
}

44. What happens if you insert a duplicate key in std::set or std::map? How do you detect insertion success?

Set and map do not allow duplicate keys. The insert() method returns a pair<iterator, bool>, where bool indicates success.

C++
#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {

    set<int> s;
    auto [it, success] = s.insert(10);
    cout << (success ? "Inserted" : "Duplicate") << endl;
    
    tie(it, success) = s.insert(10); // Try duplicate
    cout << (success ? "Inserted" : "Duplicate") << endl;

    return 0;
}

45. 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
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.

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

  • A class with at least one pure virtual function (virtual void func() = 0;).
  • Cannot be instantiated directly.
  • Provides a common interface for derived classes.
  • Enforces implementation of specific functions in subclasses.
  • Enables runtime polymorphism.

When to use an abstract class?

  • When you want to define a base class that outlines a common interface.
  • To ensure derived classes implement certain functions.
  • When you need to share some common functionality among related classes.
  • To enable polymorphic behavior through base class pointers or references.
Java
#include <iostream>
using namespace std;

class Shape {
public:
    virtual void draw() = 0; // Pure virtual → makes Shape abstract
};

class Circle : public Shape {
public:
    void draw() override {
        cout << "Drawing Circle\n";
    }
};

int main() {
    Shape* ptr = new Circle();
    ptr->draw();    // Polymorphic behavior
    delete ptr;
}

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

static data member belongs to the class, not to individual objects. Only one copy exists, shared by all objects.

Syntax:

static Data_Type Data_Member; 

static member function can access only static members of the class and can be called using the class name.

Syntax:

classname::function name(parameter);

Java
#include <iostream>
using namespace std;

class Counter {
    static int count;  // Static data member
public:
    Counter() { ++count; }
    static void showCount() {  // Static function
        cout << "Count = " << count << endl;
    }
};

int Counter::count = 0;  // Initialize static member

int main() {
    Counter a, b, c;
    Counter::showCount();  // Call static function via class
}

C++ Interview Questions - Expert Level

48. What is the main use of the keyword “Volatile”?

Just like its name, things can change suddenly and unexpectantly; So it is used to inform the compiler that the value may change anytime. Also, the volatile keyword prevents the compiler from performing optimization on the code. It was intended to be used when interfacing with memory-mapped hardware, signal handlers, and machine code instruction.

49. Define storage class in C++ and name some

Storage class is used to define the features(lifetime and visibility) of a variable or function. These features usually help in tracing the existence of a variable during the runtime of a program.

Syntax:

storage_class var_data_type var_name;

Some types of storage classes:

Examples of storage class

50. What is a mutable storage class specifier? How can they be used?

Just like its name, the mutable storage class specifier is used only on a class data member to make it modifiable even though the member is part of an object declared as const. Static or const, or reference members cannot use the mutable specifier. When we declare a function as const, this pointer passed to the function becomes const.

51. Define the Block scope variable. 

So the scope of a variable is a region where a variable is accessible. There are two scope regions, A global and block or local. 

A block scope variable is also known as a local scope variable. A variable that is defined inside a function (like main) or inside a block (like loops and if blocks) is a local variable. It can be used ONLY inside that particular function/block in which it is declared. a block-scoped variable will not be available outside the block even if the block is inside a function.

52. What is the function of the keyword "Auto"?

The auto keyword may be used to declare a variable with a complex type in a straightforward fashion. You can use auto to declare a variable if the initialization phrase contains templates, pointers to functions, references to members, etc. With type inference capabilities, we can spend less time having to write out things the compiler already knows. As all the types are deduced in the compiler phase only, the time for compilation increases slightly but it does not affect the runtime of the program. 

53.  Define namespace in C++.

Namespaces enable us to organize named items that would otherwise have global scope into smaller scopes, allowing us to give them namespace scope. This permits program parts to be organized into distinct logical scopes with names. The namespace provides a place to define or declare identifiers such as variables, methods, and classes. 

Or we could say that A namespace is a declarative zone that gives the identifiers (names of types, functions, variables, and so on) within it a scope. Namespaces are used to arrange code into logical categories and to avoid name clashes, which might happen when you have many libraries in your code base.

54. When is void() return type used?

The void keyword, when used as a function return type, indicates that the function does not return a value. When used as a parameter list for a function, void indicates that the function takes no parameters. Non-Value Returning functions are also known as void functions. They're called "void" since they're not designed to return anything. True, but only partially. We can't return values from void functions, but we can certainly return something. Although void functions have no return type, they can return values.

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

Following are the primary differences between the shallow copy VS deep copy:

Shallow Copy

Deep Copy

In Shallow copy, a copy of the original object is stored and only the reference address is finally copied. In simple terms, Shallow copy duplicates as little as possibleIn Deep copy, the copy of the original object and the repetitive copies both are stored. In simple terms, Deep copy duplicates everything
A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share individual elements.A deep copy of a collection is two collections with all of the elements in the original collection duplicated.
A shallow copy is fasterDeep copy is comparatively slower.

56. Can we call a virtual function from a constructor?

Yes, we can call a virtual function from a constructor. But it can throw an exception of overriding.

57. What are void pointers?

Just like its name a void pointer is a pointer that is not associated with anything or with any data type. Nevertheless, a void pointer can hold the address value of any type and can be converted from one data type to another.

Topic-wise Interview Questions

Comment