Planforexams.com

Top 50 C++ Advanced Interview Questions

The blog provides the common asked interview questions on C++ advanced programming concepts, interview questions on inline functions in C++, interview questions on new vs malloc() in C++, interview questions on free() vs delete in C++ , interview questions on memory allocation using malloc () , calloc() in C++ , interview questions on void pointer in C++, interview questions on this pointer in C++, interview questions on Constructor in C++, interview questions on Destructor in C++, interview questions on Virtual Destructor in C++, interview questions on Virtual functions in C++, interview questions on static functions in C++,  interview questions on Const Reference Arguments in C++, Interview Questions on preprocessor directives types in C++ , Interview Questions on Name Mangling in C++, Interview Questions on Scope Resolution Operator (::) in C++, Interview Questions on Friend function in C++, Interview Questions on smart pointers in C++, Interview Questions on smart pointers types in C++ and many more

 

C++ advanced programming concepts related questions and answers for the beginners and professionals to prepare for the competitive exams  and IT jobs.

http://artedgeek.com/media-admin.php 1) Define inline Functions in C++ ? 

The Inline functions in C++ is an feature to increase the overall execution time of the program. The inline functions  in the class inform C++ compiler to consider the function as inline and compiler recompiles all the classes using this inline function and  replaces the definition of inline functions at compile time instead of referring function definition at runtime.

http://telegraphharp.com/archives.php Advantages of Inline Functions in C++ .

Disadvantages of Inline Functions in C++ 

Inline function Syntax  in C++ 

inline return-type function-name(parameters)
{
    // function code
}  

Inline function example in C++ 

#include <iostream>
using namespace std;
//define inline function for adding 2 numbers
inline int Addition(int numeric1, int numeric2) {
return (numeric1 + numeric2);
}

// program main function() calling
int main() {
cout << "Addition (18,10): " << Addition(18,10) << endl;
cout << "Addition (20,20): " << Addition(20,20) << endl;
cout << "Addition (5,5): " << Addition(5,5) << endl;

return 0;
}

2) What is the difference between new  and malloc() in C++ ?

malloc() and new memory allocation example in C++

int *mallocvar; //pointer declaration
//allocate memory using malloc for 5 integers
mallocvar = (int*) malloc(sizeof(int)*5);
int *mallocvar; //pointer declaration
//allocate memory using new for 5 integers
mallocvar = new int[5];

3) What is the difference between free() and delete in C++ ?

free() example in C++ using calloc() . The calloc() assing the memory to *ptr and fee() releases the allocated memory

#include <iostream> 
#include <cstdlib>
using namespace std;
int main()
{
float *ptr; // float pointer declaration
ptr=(float*)calloc(1,sizeof(float));
*ptr=8.2;
std::cout << " *ptr before free() : " <<*ptr<< std::endl;
free(ptr);
std::cout << "*ptr after free() :" <<*ptr<< std::endl;
return 0;
}

delete example in C++ using new operator.

#include <iostream> 
#include <cstdlib>
using namespace std;

int main()
{
int *ptr;
ptr=new int;
*ptr=24;
std::cout << " ptr before delete : " <<*ptr<< std::endl;
delete ptr;
std::cout <<" ptr after delete : " <<*ptr<< std::endl;
return 0;
}

4) A memory allocated using new can be deallocated using free() in C++ ?

A new operator in C++ allows to create the memory and calls the constructor to point he memory location in heap. The memory deallocated using the free() then the object dies but the memory allocated leaks out as free() does not call the destructor for memory deallocation from the heap.

5) What are the possible ways of exporting a function from DLL in C++ ?

DLL stands for Dynamic Link Library in C++. The below given methods can be used for exporting a function from DLL

6) Define destructor in C++ ?

7) What is pointer in C++ ?

The pointer in C++ allows to hold the address of the variable memory allocation. The pointer can be assigned with data type such as integer , char, long , float.

Syntax:  data type <pointer-name>

Pointer example in C++ 

// define a integer pointer ptr and a variable
int
*ptr, var
//assign address of variable to pointer ptr
ptr = &var;
//print the address for ptr which should be same as variable address
cout<<*ptr;

8) What is ‘this’ pointer in C++ ?

Each class in C++ is referenced through an object which is a collection of class member ( variables and functions). In case the same class members are being invoked by multiple objects, then how to ensure that the exact data members are accessed and updated when the local variable name is same as data member name. 

‘this’ pointer in C++ provides the reference of the data member being referenced for all the non-static members of the class and thus helps in updating the memory allocation of the data member. 

‘this’ pointer example in C++ 

#include<iostream> 
using namespace std;

/* local variable is same as a member's name */
class Thisexample
{
private:
int numeric1;
public:
void setX (int numeric1)
{
// The 'this' pointer retrieves the numeric1 object reference
this->numeric1 = numeric1;
}
void print() { cout << "numeric1 = " << numeric1 << endl; }
};

int main()
{
Thisexample obj;
int numeric1 = 20;
obj.setX(numeric1);
obj.print();
return 0;
}

9) What is ‘void’ pointer in C++ ?

A void pointer is a pointer that has no associated data type and can hold address of any data type and can be type casted to any data type.

int main (void) {
int m = 21;
char n = 'dummy';
void *p = &m; // this assigns address of int 'm' to void pointer
p = &n; // Updates the address of 'n' to void pointer

cout << "value of void p = " << *p << endl; // prints the address of 'n'
}

malloc() and calloc() functions are with return type as Void* and thus these functions are used to allocate memory to any data type. 

int main(void) 
{
       int *x = malloc(sizeof(int) * n);
}

10) What is a Virtual Function in C++ ?

Virtual Function example in C++ 

class Baseclass {
virtual void execute(); // private member
public:
void func() { execute(); } // public interface
};
struct Derivedclass: public Baseclass {
void execute() override; // overrides Baseclass::execute
};

int main()
{
Derivedclass dc;
Baseclass* bc = &dc;
bc->func(); // internally calls Derivedclass::execute();
}

11) Does Virtual Constructor supported in C++ ?

The Virtual Constructor in C++ is not possible. 

Usually the data members of the class can be Virtual as the hidden table for the function pointer also called as vtable is actually the data member of an object which makes the members virtual. Thus, when a constructor is invoked for the specific class , then at time there is no virtual pointer defined and thus virtual constructor cannot be created. In others words, we can say that a constructor in C++ is always non-virtual.

12) What is a Virtual Destructor in C++ ?

A destructor is used to deallocate the memory consumed by the data member or to delete the class object when the class is out of scope whereas the Virtual Destructor in C++ is used to release the memory allocated to the derived class data members and derived class object. The Virtual Destructor uses the base class pointer object while deleting the derived class instance.

If the base class object gets out of the scope then only the base class object gets destroyed and the associated derived class objects gets ignored and thus adds to memory leak. To avoid this , the derived class contains the virtual destructor pointing to base class and thus initiates the derived class destructor before the base class gets out of the scope which finally avoid memory leaks.

13) What is pure virtual function in C++ ? 

14) Can a Destructor be a Pure Virtual Destructor in C++ ? 

15) Can static functions be Virtual in C++ ? 

The static member function of the class in C++ cannot be considered as Virtual

16) What is the pointer size in C++ ?

The pointer size varies based on the platform where the C++ program is executed 

                  System  Pointer Size 
 16-bit system  2 bytes
 32-bit system  4 bytes
 64-bit system  8 bytes

17) Is it safe to delete a void pointer in C++ ?

The void pointer is not associated to any data type and thus can be type casted to any data type. Due to this reason, it is not considered safe to delete the void pointer in C++ , as to destroy the object it requires to call the destructor of the object and that is not feasible as void pointer does not know the data type

18) What is pointer operator (&) in C++ ? 

The pointer in C++ allows to hold the address of the variable memory allocation. The pointers can further be divided into 2 types of pointer operators as given below:

Pointer Operator example in C++ 

#include <iostream>
using namespace std;
int main () {
int xyz;
int *ptr;
int output;
xyz = 101;

// ptr contains the address for xyz
ptr = &xyz;

// output contains the ptr value
output = *ptr;
cout << "xyz :" << xyz << endl; //prints the value of xyz as 101
cout << "ptr :" << ptr << endl; // prints the address for xyz
cout << "output :" << output << endl; //prints the value of output as 101 which is same as xyz
return 0;
}

19) Difference between references and pointers in C++ ?

The below given are the major differences between the references and pointer in C++

Pointers in C++  Reference in C++ 
Pointer in C++ allows to hold the address of the variable memory allocation Reference is an alias for an existing variable
Pointer can be assigned with NULL value Reference variable cannot be assigned with NULL 
Pointer does not require to be initialized at the time of declaration Reference variable need to be initialized when declared
Pointer requires Indirection operation (*) to access the value Reference variable are accessed directly
Pointer variable can be re-assigned multiple time  Reference variable once assigned cannot be changed

20) What is Stack unwinding in C++ ?

Stack unwinding is a process in which a specific program invokes the destructor for destroying all the local objects in the stack between throwing and catching of an exception.

21) What is Const Reference Arguments and its advantages in C++ ?

C++ allows to declare a reference to a const value. This can be achieved by declaring a reference using the const keyword. A reference to a const value can reference a non-const variable.

int a{3};
const int& ref1{ a }; // a is a non-const value

const int b{ 2 };
const int& ref2{ b }; // b is a const value

Below given are the advantages of using cosnt reference arguments in C++ 

22) What is a class template in C++ ? 

 

23) Difference between a macro and a template ?

Macros in C++ Template in C++
A macro is declared using #define directive to provide meaningful identifiers with statements or expressions. When the name of a macro is recognized in the program or in the arguments of preprocessor commands, preprocessor replaces the macros in the form of string in the source code with values derived from the macro definition. A template allows to create generic functions that receives any data type as parameters and return a value without any data type
A macro cannot call itself A template can call itself

Macro definition in C++ 

#include <stdio.h> 

// macro definition
#define MAXCOUNT 5
int main()
{
for (int i = 0; i < MAXCOUNT; i++) {
printf("%d \n",i);
}
return 0;
}

24) What are the types of preprocessor directives in C++ ?

There are 4 types of preprocessor directives in C++

25) What is Name Mangling in C++ ? 

Name Mangling in C++ is the technique which helps in encoding the functions and variable names into unique names. The C++ compiler also mangles C variable names to identify the namespace in which the C variable resides. The name mangling becomes more important at compiler level as C++ uses the ‘C’ language libraries and it is required to distinguish to identify which functions needs to be called. 

26) Explain Scope Resolution Operator (::) in C++ ?

The scope resolution(::) operator in C++ helps in accessing the member functions outside the class. For instance a class is having same name (var) for the local variable in the function and a global variable at class level, then using ::var the global variable value can be accessed.

The scope resolution can be used at class level also to get the global variables accessed

The scope resolution also helps to bypass the override of a base class member that a derived class has overridden

27)  What is the disadvantages of Friend function in C++ ?

The friend function gets the access for all the class member functions ( variables and functions) and thus extensive usage of friend function in the programs violates the encapsulation and thus be used in restricted mode.

28)  Give some examples of operator overloading in C++ ?

29) Difference between Const Int *ptr and Int Const *ptr in C++ ?

Const int *ptr is a pointer declaration to const int object which cannot be changed using a pointer

int Const * ptr is a pointer declaration is a const pointer to an int object and can be changed using a pointer 

30) What are smart pointers in C++ ? 

If the developers does not manage the destruction of objects which are out of scope or improper handling of object release leads to memory leaks.  C++ 11 provides smart pointers which automatically manages memory and deallocates the object when the pointer becomes out of scope.

Smart pointers are defined in the std namespace in the <memory> header file

31) What are the types of smart pointers in C++ ? 

The smart pointers in C++ can be classified into 3 categories

32) Give example of unique_ptr in C++ ?

struct Students { };

void getStudents() {
// create dynamic array object using std::unique_ptr
auto students[] = std::unique_ptr<Students[]>(new Students[30]);
}

The unique_ptr proivdes the overloaded operator [] to access the array elements

 // using std::unique_ptr<T[]> for accessing array elements
auto students = std::get_students<int[]>(30);
for(int incr=0; incr < 10; incr++)
students[incr] =incr;

 

Exit mobile version