C++ frequently asked interview questions | Short – Real

This section contains list of real C++ short interview questions and answers frequently asked in IT industries for freshers and experienced.


Q-Tell me one important properties of “delete” operator except de-allocation of memory.

Answer – “delete” operator calls the destructor of a class.


Q – Can we overload destructor of a class?

Answer No, we can’t overload the destructor, we don’t have this provision in C++.


Q- In which statement assignment operator will be called?

  1. A Obj1(5);
  2. A Obj2(Obj1);
  3. A Obj2 = Obj1

Answer- None, for every statement copy constructor will get called.

Assignment operator will be called for the following

Obj2 = Obj1; // note this is different than A Obj2= Obj1 as here, we are initializing new object at the time of creation with existing object.


Q- How would you initialize and delete the memory, allocated using “char* p = new char[length]” by 0, where int length =512;

Answer:

All the memory slot of characters can be initialized by 0 using function “memset()”

memset(p,0, length);

Since, this is the array of characters, it should be de-allocated using

delete[] p;


Q-Can we create array of references in C++?

Answer – No, We cannot have array of references.


Q- Write the syntax for copy constructor. Consider class name is A.

Answer – A(const A& obj)


Q-What is difference between structure and class?

Answer- There is only one difference i.e. members of a class are private and members of structures are public by default.

e.g. if we don’t put any access specifier i.e public, protected or private

class A{

int a; // private

}

Struct A{

Int a; // public, can be accessible outside from the class.

}


Q- What is size of empty class? ( a class doesn’t have any data member and member function)

Answer- 1 byte; Just for an identifier of a class in the memory.


Q- What is the size of void?

Answer- sizeof(void) is not allowed in c++.


Q- What is the size of void* pointer?

Answer- On a 32-bit arch, sizeof(void *) is 4 bytes.

Note – Whatever be the type of pointer i.e char, int or void. Every pointer has size is of 4 bytes on 32 bit.

Related Posts