InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
How do you allocate and deallocate memory in C++? |
|
Answer» The new operator is used for memory allocation and DELETES operator is used for memory deallocation in C++. For example- INT value=new int; //allocates memory for storing 1 integerdelete value; // deallocates memory TAKEN by valueint *ARR=new int[10]; //allocates memory for storing 10 intdelete []arr; // deallocates memory occupied by arrAdditional Resources Practice Coding C++ MCQ C++ Tutorials C Interview Questions Difference Between C and C++ Difference Between C++ and Java Online C++ Compiler Features of C++ |
|
| 2. |
What is this pointer in C++? |
|
Answer» The MEMBER functions of every object have a pointer named this, which points to the object itself. The value of this is set to the address of the object for which it is called. It can be used to access the DATA in the object it points to. Example class A{ private: int value; public: void setvalue(int x){ this->value=x; }};int main(){ A a; a.setvalue(5); RETURN 0;} |
|
| 3. |
What are void pointers? |
|
Answer» A VOID pointer is a pointer which is having no datatype associated with it. It can HOLD ADDRESSES of any TYPE. For example- void *ptr; char *str;p=str; // no error str=p; // error because of type mismatchWe can ASSIGN a pointer of any type to a void pointer but the reverse is not true unless you typecast it as str=(char*) ptr; |
|
| 4. |
Can we call a virtual function from a constructor? |
|
Answer» Yes, we can call a virtual function from a constructor. But the behavior is a little different in this case. When a virtual function is called, the virtual call is RESOLVED at runtime. It is always the member function of the current CLASS that GETS called. That is the virtual MACHINE doesn’t work within the constructor. For example- class base{ private: int value; public: base(int x){ value=x; } virtual void fun(){ }}class derived{ private: int a; public: derived(int x, int y):base(x){ base *B; b=this; b->fun(); //calls derived::fun() } void fun(){ cout<<”fun inside derived class”<<endl; }} |
|
| 5. |
If class D is derived from a base class B. When creating an object of type D in what order would the constructors of these classes get called? |
|
Answer» The derived CLASS has two parts, a base part, and a derived part. When C++ constructs derived objects, it does so in phases. First, the most-base class(at the top of the INHERITANCE TREE) is constructed. Then each child class is constructed in order until the most-child class is constructed last. During the destruction EXACTLY reverse order is followed. That is destructor starts at the most-derived class and works its way down to base class. |
|
| 6. |
What is the difference between virtual functions and pure virtual functions? |
|
Answer» A virtual function is a member function in the base CLASS that you redefine in a derived class. It is declared using the virtual keyword. Example- class base{public: virtual VOID fun(){ }};A pure virtual function is a function that has no IMPLEMENTATION and is declared by assigning 0. It has no body. Example- class base{public: virtual void fun()=0;};Here, = sign has got nothing to do with the assignment, and value 0 is not assigned to anything. It is used to SIMPLY tell the compiler that a function will be pure and it will not have anybody. |
|
| 7. |
What is the difference between shallow copy and deep copy? |
||||||||
|
Answer» The DIFFERENCE between shallow copy and a deep copy is given below:
|
|||||||||
| 8. |
What is a copy constructor? |
|
Answer» A copy CONSTRUCTOR is a member function that initializes an OBJECT using another object of the same class. Example- class A{int x,y;A(int x, int y){ this->x=x; this->y=y;}};int main(){A A1(2,3);A a2=a1; //DEFAULT copy constructor is calledreturn 0;}We can define our copy constructor. If we don’t define a copy constructor then the default copy constructor is called. |
|