Explore topic-wise InterviewSolutions in .

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.

What is delete operator in C++?

Answer»

The delete operator in C++ releases dynamically allocated memory.

2.

What is a delete operator in C++?

Answer»

The delete operator in C++ releases dynamically allocated memory.

3.

What is delete operator in C++? Give an example.

Answer»

The delete operator in C++ is used for releasing memory space when the object is no longer needed.

General syntax of delete operator in C++:

delete pointer_variable;
For example, delete cptr;
In the above example, delete is a keyword and the pointer variable cptr is the pointer that points to the objects already created in the new operator.

4.

What is new operator in C++? Give an example.

Answer»

The new operator in C++ is used for dynamic storage allocation. This operator can be used to create an object of any type.

General syntax of the new operator in C++:

The general syntax of a new operator in C++ is as follows:
pointer variable = new datatype;
In the above statement, new is a keyword and the pointer variable is a variable of type datatype.

For example:

int *a=new int;
In the above example, the new operator allocates sufficient memory to hold the object of datatype int and returns a pointer to its starting point. The pointer variable holds the address of memory space allocated.

5.

Illustrate the use of “self-referential structures” with the help of an example.

Answer»

A self-referential structure is used to create data structures like linked lists, stacks, etc. Following is an example of this kind of structure where pointer variable *next is of datatype ‘node’ which is the structure type itself:
struct node
{
int data;
node *next;
};

6.

What are the advantages of a pointer?

Answer»

The advantages of the pointer are:

  • For objects memory can be allocated dynamically during runtime.
  • When objects are no more required, then memory can be released.
  • Memory is efficiently used because memory is allocated when needed and freed when it is not needed for objects.
7.

How dynamic memory allocation is different from static memory allocation?

Answer»

Allocation of memory for data is done at the time of execution (run time) is known as dynamic memory allocation. Whereas allocation of memory is for data is done during compilation in fixed size is called static memory allocation.

8.

Explain with example by passing the pointers.

Answer»

#include <iostream>
// function declaration void swap(int *, int *);
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
cout << “Before swap, value of a :” << a << endl;
cout << “Before swap, value of b:”<< b << endl;
/* calling a function to swap the values.*/
swap(&a, &b);
cout << “After swap, value of a << a << endl;
cout <<“After swap, value of b << b << endl;
}

// function definition to swap the values, void swap(int *x, int *y)
{
int temp;
temp = *x; // assign to temp from the value at address x
*x = *y; // put value at address y into x
*y = temp; // put value of temp into value at the address y
}

The output of the above program is:
Before swap, value of a :100 Before swap, value of b :200
After swap, value of a :200
After swap, value of b :100

In the above program, swap() function is declared with two arguments of type integer pointers. During the function call memory address of variable, ‘a’ and variable ‘b’ is passed as actual arguments, and formal arguments are pointer ‘x’ and pointer Y takes the address of ‘a’ and ‘b’ variables respectively. In the body of the swap function, the pointer operator is used to access the value at the location. Here the memory location of ‘a’ and ‘b’ are accessed by the pointers V and Y and produces the output as shown above.

9.

What is the relationship between object and pointers? Give an example.

Answer»

The pointers pointing to objects are referred to as object pointers. There is a relationship between pointers and objects. Using pointers all the members of the class can be accessed. The following example shows various operations that can be performed on the members of the class through a pointer to objects.

Declaration : class name *object_pointer, object;
Initialisation object_pointer = & object;
Invoking function call to member of the class: object_pointer->memberfunction();
For example;
class emp

{
private:

int empno;
char name[15];
float salary;

public:

void readdata();
void printdata();
};
void main()
{

emp obj, *ptr;
ptr = &obj;
ptr-> readdata();
ptr->printdata();
}
In the above program, *ptr is a pointer to class ’emp’ and ‘obj’ is class ’emp’ type. The pointer is initialised with the address of object ‘obj’ of class ’emp’ type. Then the pointer ‘ptr’ is used to invoke function call to the member function ‘readdata()’ and ‘printdata()’using member access operator ->.

10.

Explain with example by passing the reference.

Answer»

#include <iostream>
// function declaration
void swap(int& x, int& y);
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
cout<< “Before swap, value of a << a << endl;
cout << “Before swap, value of b << b << endl;
/* calling a function to swap the values.*/

swap(a, b);
cout<< “After swap, value of a << a << endl;
cout << “After swap, value of b << b << endl;
}
// function definition to swap the values.
void swap(int& x, int& y)
{
int temp;
temp = x; /* save the value at address x */
x = y; /* put y into x */
y = temp; /* put x into y */
return;
}

The output of the above program is:
Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :200
After swap, value of b :100

In C++, reference variables are possible and works like alias to original memory locations. In the above program, the function void swap(int& x, int& y); is declared with two arguments with x and y as reference variables. During the function call swap() takes two arguments (formal arguments) ‘x’ and ‘y’ which are reference variables to variables of actual arguments ‘a’ and ‘b’ respectively. The interchange of value takes place between x and y in the body of swap() function. Here, variable ‘x’ and ‘y’ are used to manipulate the data values from the location ‘a’ and ‘b’ directly.