C++ Interview Questions & Answers


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

C++ is an object-oriented programming language that was introduced to overcome the jurisdictions where C was lacking.By object-oriented we mean that it works with the concept of polymorphism, inheritance, abstraction, encapsulation, object, and class.
Advantages of C++:

  • C++ is an OOPs language that means the data is considered as objects.
  • C++ is a multi-paradigm language; In simple terms, it means that we can program the logic, structure, and procedure of the program.
  • Memory management is a key feature in C++ as it enables dynamic memory allocation
  • It is a Mid-Level programming language which means it can develop games, desktop applications, drivers, and kernels

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_”.

What are references in C++?

When a variable is described as a reference it becomes an alias of the already existing variable. In simple terms, a referenced variable is another named variable of an existing variable keeping in mind that changes made in the reference variable will be reflected in the already existing variable. A reference variable is preceded with a ‘&’ symbol.

Syntax:

    
        int GFG = 10;
        // reference variable
        int& ref = GFG;
    
What do you mean by Call by Value and Call by Reference?

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

  • Call by Value
    • A copy of a variable is passed.
    • Calling a function by sending the values by copying variables.
    • 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.
    • Passed actual and formal parameters are stored in different memory locations. Therefore, making Call by Value a little memory insufficient
  • Call by Reference
    • A variable itself is passed fundamentally.
    • Calling a function by sending the address of the passed variable.
    • 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 the same memory location. Therefore, making Call by Reference a little more memory efficient.
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:

  • 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
What is the difference between C and C++?
C C++
It is a procedural programming language. In simple words, it does not support classes and objects It 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 Overloading It supports Function and Operator Overloading respectively
It is a function-driven language It is an object-driven language

What is the difference between struct and class?

Struct

  • Members of the struct are always by default public mode
  • Structures are of the value type. They only hold value in memory.
  • The memory in structures is stored as stacks

Class

  • Members of the class can be in private, protected, and public modes.
  • Classes are of reference type. It holds a reference of an object in memory.
  • The memory in classes is stored as heaps.
What is the difference between reference and pointer?

Reference

  • The value of a reference cannot be reassigned
  • It can never hold a null value as it needs an existing value to become an alias of
  • It cannot work with arrays
  • To access the members of class/struct it uses a ‘ . ‘
  • The memory location of reference can be accessed easily or it can be used directly

Pointer

  • The value of a pointer can be reassigned
  • It can hold or point at a null value and be termed as a nullptr or null pointer
  • It can work with arrays
  • To access the members of class/struct it uses a ‘ -> ‘
  • The memory location of a pointer cannot be accessed easily as we have to use a dereference ‘ * ‘
What is the difference between function overloading and operator 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 function 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
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

What is the difference between an array and a list?

Arrays

  • Array are contiguous memory locations of homogenous data types stored in a fixed location or size.
  • Arrays are static in nature.
  • Uses less memory than linked lists.

Lists

  • 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.
  • Lists are dynamic in nature.
  • Uses more memory as it has to store the value and the pointer memory location.
What is the difference between a while loop and a do-while loop?

While Loop

  • While loop is also termed an entry-controlled loop.
  • If the condition is not satisfied the statements inside the loop will not execute.
  • Example of a while loop: while(condition) {statements to be executed;};

do-while Loop

  • The do-while loop is termed an exit control loop.
  • Even if the condition is not satisfied the statements inside the loop will execute for at least one time.
  • Example of a do-while loop: do {statements to be executed;} while(condition or expression);
Discuss the difference between prefix and postfix?

prefix

  • It simply means putting the operator before the operand
  • It executes itself before ‘; ‘
  • Associativity of prefix ++ is right to left

postfix

  • It simply means putting the operator after the operand
  • It executes itself after ‘; ‘
  • Associativity of postfix ++ is left to right
What is the difference between new and malloc()?

new

  • new is an operator which performs an operation
  • new calls the constructors
  • new is faster than malloc as it is an operator
  • new returns the exact data type

malloc()

  • malloc is a function that returns and accepts values
  • malloc cannot call a constructor
  • malloc is slower than new as it is a function
  • malloc returns void*
What is the difference between virtual functions and pure virtual functions?

Virtual Function

  • A Virtual Function is a member function of a base class that can be redefined in another derived class.
  • A virtual Function has its definition in its respective base class.
  • The base class has a virtual function that can be represented or instanced; In simple words, its object can be made.

Pure Virtual Function

  • 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.
  • There is no definition in Pure Virtual Function and is initialized with a pure specifier (= 0).
  • 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.
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.

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.

What are the various 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
Explain inheritance

The capability or ability of a class to derive properties and characteristics from another class is known as inheritance. In simple terms, it is a system or technique of reusing and extending existing classes without modifying them.

When should we use multiple inheritance?

Multiple inheritances mean that a derived class can inherit two or more base/parent classes. It is useful when a derived class needs to combine numerous attributes/contracts and inherit some, or all, of the implementation from these attributes/contracts. To take a real-life example consider your Parents where Parent A is your DAD Parent B is your MOM and Chid C is you.

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.

What is polymorphism in C++?

Polymorphism is known as many forms of the same thing. In simple terms, we can say that Polymorphism is the ability to display a member function in multiple forms depending on the type of object that calls them.

In other words, we can also say that a man can be an employee to someone, a son of someone, a father of someone, and a husband of someone; this is how polymorphism can be projected in real life.

There is 2 type of polymorphism:

Compile Time Polymorphism

  • Function Overloading
  • Operator Overloading

Run Time Polymorphism

  • Function Overriding
  • Virtual Function
What are the different types of polymorphism in C++?

There is 2 type of polymorphism

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.

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

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.

Compare compile-time polymorphism and Runtime polymorphism

Compile-Time Polymorphism

  • It is also termed static binding and early binding.
  • It is fast because execution is known early at compile time.
  • It is achieved by function overloading and operator overloading.

Runtime Polymorphism

  • It is also termed Dynamic binding and Late binding.
  • It is slow as compared to compile-time because execution is known at runtime.
  • It is achieved by virtual functions and function overriding.
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:

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

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

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

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

        
            Sample(Sample& t) { id = t.id; }
        
    
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.

        
            ~constructor_name(); // tilde sign signifies that it is a destructor
        
    
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.

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.