Showing posts with label pointers. Show all posts
Showing posts with label pointers. Show all posts

February 12, 2021

Top 20 C++ language Interview Questions & Answers

 

Ques. 1): What is ‘this’ pointer?

Answer: The ‘this’ pointer is passed as a hidden argument to all non-static member function calls and is available as a local variable within the body of all non-static functions. ‘this’ pointer is a constant pointer that holds the memory address of the current object. ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name).

 

Ques. 2): What are VTABLE and VPTR?

Answer: vtable is a table of function pointers. It is maintained per class.

vptr is a pointer to vtable. It is maintained per object.

Compiler adds additional code at two places to maintain and use vtable and vptr.

1) Code in every constructor. This code sets vptr of the object being created. This code sets vptr to point to vtable of the class.

2) Code with polymorphic function call (e.g. bp->show()). Wherever a polymorphic call is made, compiler inserts code to first look for vptr using base class pointer or reference, for example, since pointed or referred object is of derived type, vptr of derived class is accessed). Once vptr is fetched, vtable of derived class can be accessed. Using vtable, address of derived derived class function show() is accessed and called.

 

Ques. 3): What do you mean by ‘void’ return type?

Answer: All functions should return a value as per the general syntax.

However, in case, if we don’t want a function to return any value, we use “void” to indicate that. This means that we use “void” to indicate that the function has no return value or it returns “void”.

Example:

void myfunc()

{

Cout<<”Hello,This is my function!!”;

}

int main()

{

myfunc();

return 0;

}

 

Ques. 4): What is an Inline function in C++?

Answer: Inline function is a function that is compiled by the compiler as the point of calling the function and the code is substituted at that point. This makes compiling faster. This function is defined by prefixing the function prototype with the keyword “inline”.

Such functions are advantageous only when the code of the inline function is small and simple. Although a function is defined as Inline, it is completely compiler dependent to evaluate it as inline or not.

 

Ques. 5): What is a Reference Variable in C++?

Answer: A reference variable is an alias name for the existing variable. This means that both the variable name and the reference variable point to the same memory location. Hence, whenever the variable is updated, the reference is updated too.

Example:

int a=10;

 int& b = a;

Here, b is the reference of a.

 

Ques. 6): When to use “const” reference arguments in a function?

Answer: Using “const” reference arguments in a function is beneficial in several ways:

   “const” protects from programming errors that could alter data.

   As a result of using “const”, the function is able to process both const and non-const actual arguments, which is not possible when “const” is not used.

   Using a const reference will allow the function to generate and use a temporary variable in an appropriate manner.

 

Ques. 7): What is the main difference between Class and Structure.

Answer:

Structure: In C language, the structure is used to bundle different types of data types together. The variables inside a structure are called the members of the structure. These members are by default public and can be accessed by using the structure name followed by a dot operator and then the member name.

Class: Class is a successor of the Structure. C++ extends the structure definition to include the functions that operate on its members. By default, all the members inside the class are private.

 

Ques. 8): What is Namespace?

Answer: Namespace allows us to group a set of global classes, objects and/or functions under a specific name.

The general form to use namespaces is:

namespace identifier { namespace-body }

Where identifier is any valid identifier and the namespace-body is the set of classes, objects, and functions that are included within the namespace. Namespaces are especially useful in the case where there is a possibility for more than one object to have the same name, resulting in name clashes.

 

Ques. 9): What is Name Mangling?

Answer: C++ compiler encodes the parameter types with function/method into a unique name. This process is called name mangling. The inverse process is called as demangling.

Example:

A::b(int, long) const is mangled as ‘b__C3Ail’.

For a constructor, the method name is left out.

That is A:: A(int, long) const is mangled as ‘C3Ail’.

 

Ques. 10): What is a COPY CONSTRUCTOR and when is it called?

Answer: A copy constructor is a constructor that accepts an object of the same class as its parameter and copies its data members to the object on the left part of the assignment. It is useful when we need to construct a new object of the same class.

Example:

class A{

int x; int y;

public int color;

public A() : x(0) , y(0) {} //default (no argument) constructor

public A( const A& ) ;

};

A::A( const A & p )

{

this->x = p.x;

this->y = p.y;

this->color = p.color;

}

main()

{

A Myobj;

Myobj.color = 345;

A Anotherobj = A( Myobj ); // now Anotherobj has color = 345

}

 

Ques. 11): What is the main difference between Declaration and Definition of a variable?

Answer: The declaration of a variable is merely specifying the data type of a variable and the variable name. As a result of the declaration, we tell the compiler to reserve the space for a variable in the memory according to the data type specified.

Example:

int Result;

char c;

int a,b,c;

All the above are valid declarations. Also, note that because of the declaration, the value of the variable is undetermined.

Whereas, a definition is an implementation/instantiation of the declared variable where we tie up appropriate value to the declared variable so that the linker will be able to link references to the appropriate entities.

From above Example,

Result = 10;

C = ‘A’;

These are valid definitions.

 

Ques. 12): What is Local and Global scope of a variable in C++?

Answer: The scope of a variable is defined as the extent of the program code within which the variable remains active i.e. it can be declared, defined or worked with.

There are two types of scope in C++:

1. Local Scope: A variable is said to have a local scope or is local when it is declared inside a code block. The variable remains active only inside the block and is not accessible outside the code block.

2. Global Scope: A variable has a global scope when it is accessible throughout the program. A global variable is declared on top of the program before all the function definitions.

Example:

#include <iostream.h>

Int globalResult=0; //global variable

int main()

{

Int localVar = 10; //local variable.

}

 

Ques. 13): What is the precedence when there are a Global variable and a Local variable in the program with the same name?

Answer: Whenever there is a local variable with the same name as that of a global variable, the compiler gives precedence to the local variable.

Example:

#include <iostream.h>

int globalVar = 2;

int main()

{

int globalVar = 5;

cout<<globalVar<<endl;

}

The output of the above code is 5. This is because, although both the variables have the same name, the compiler has given preference to the local scope.

 

Ques. 14): What is a Constant? Explain with an example.

Answer: A constant is an expression that has a fixed value. They can be divided into integer, decimal, floating-point, character or string constants depending on their data type.

Apart from the decimal, C++ also supports two more constants i.e. octal (to the base 8) and hexadecimal (to the base 16) constants.

Examples of Constants:

      75 //integer (decimal)

      0113 //octal

      0x4b //hexadecimal

      3.142 //floating point

      ‘c’ //character constant

      “Hello, World” //string constant

 

Ques. 15): What is the difference between equal to (==) and Assignment Operator (=)?

Answer: In C++, equal to (==) and assignment operator (=) are two completely different operators.

Equal to (==) is an equality relational operator that evaluates two expressions to see if they are equal and returns true if they are equal and false if they are not.

The assignment operator (=) is used to assign a value to a variable. Hence, we can have a complex assignment operation inside the equality relational operator for evaluation.

 

Ques. 16): what is the difference between Pre and Post Increment/Decrement Operations in C++?

Answer: C++ allows two operators i.e ++ (increment) and –(decrement), that allow you to add 1 to the existing value of a variable and subtract 1 from the variable respectively. These operators are in turn, called increment (++) and decrement (–).

Example:

a=5;

a++;

The second statement, a++, will cause 1 to be added to the value of a. Thus a++ is equivalent to

a = a+1; or

a += 1;

A unique feature of these operators is that we can prefix or suffix these operators with the variable. Hence, if a is a variable and we prefix the increment operator it will be

++a;

This is called Pre-increment. Similarly, we have pre-decrement as well.

If we prefix the variable a with an increment operator, we will have,

a++;

This is the post-increment. Likewise, we have post-decrement too.

The difference between the meaning of pre and post depends upon how the expression is evaluated and the result is stored.

In the case of the pre-increment/decrement operator, the increment/decrement operation is carried out first and then the result passed to an lvalue. Whereas for post-increment/decrement operations, the lvalue is evaluated first and then increment/decrement is performed accordingly.

Example:

a = 5; b=6;

++a;       #a=6

b–;         #b=6

–a;         #a=5

b++;      #6

 

Ques. 17): What is Data Abstraction in C++?

Answer: Data Abstraction is a technique of providing only essential information of any application to the user and hiding its implementation, thus making the data more secure.

Example: Consider WhatsApp, we as users can only have a look at the elements useful for us and operate using the same but we never know what happens in the background.

 

Ques. 18): What is a destructor, and can there be more than one destructor in a class?

Answer: Destructors are used to de-allocate the memory that has been allocated for an object by the constructor. It has the same name as of the class name with a tilde symbol in front of the class name.

Syntax: class_name() { }

Also, there should be only one destructor in a class even if there are more than one constructors in a single class with no parameters and no return type.

 

Ques. 19): What is Pure Virtual Function?

Answer: A pure virtual function is a virtual function which does not contain any definition. The normal virtual function is preceded with a keyword virtual. Whereas the pure virtual function is preceded with the keyword virtual and ended with the value 0.

Example: virtual void add() = 0;

 

Ques. 20): Are exceptions and errors the same?

Answer: No. Errors occur because of any mistakes in the syntax of the program. i.e. errors occur during compile time. Whereas exceptions during run time of the program.

Example: If you forget to give semicolon at the end of an assignment statement, then it is an error. If you give 6 input values to an array which is declared only to store 5 elements, then that is an exception.