C++ New And Delete Interview Preparation Guide
Strengthen your C++ New And Delete interview skills with our collection of 33 important questions. These questions will test your expertise and readiness for any C++ New And Delete interview scenario. Ideal for candidates of all levels, this collection is a must-have for your study plan. Download the free PDF now to get all 33 questions and ensure youre well-prepared for your C++ New And Delete interview. This resource is perfect for in-depth preparation and boosting your confidence.33 C++ New And Delete Questions and Answers:
1 :: Can you please explain the difference between new and malloc and delete and free()
Delete is assocated with new and free(0 is associated with malloc()
New
Its an operator
delete is associated with new
It creates an object
It throws exceptions if memory is unavailable
Operator new can be overloaded
You can state the number of objects to be created.
malloc()
It’s a function
free() is associated with malloc()
It does not create objects
It returns NULL
This cannot be overloaded
You need to specify the number of bytes to be allocated.
New
Its an operator
delete is associated with new
It creates an object
It throws exceptions if memory is unavailable
Operator new can be overloaded
You can state the number of objects to be created.
malloc()
It’s a function
free() is associated with malloc()
It does not create objects
It returns NULL
This cannot be overloaded
You need to specify the number of bytes to be allocated.
2 :: Explain realloc()?
An existing block of memory which was allocated by malloc() subroutine, will be freed by free() subroutine. In case , an invalid pointer parameter is passed, unexpected results will occur. If the parameter is a null pointer, then no action will occur.
3 :: Explain the difference between realloc() and free()?
An existing block of memory which was allocated by malloc() subroutine, will be freed by free() subroutine. In case , an invalid pointer parameter is passed, unexpected results will occur. If the parameter is a null pointer, then no action will occur.
Where as the realloc() subroutine allows the developer to change the block size of the memory which was pointed to by the pointer parameter, to a specified bytes size through size parameter and a new pointer to the block is returned. The pointer parameter specified must have been created by using malloc(),calloc() or realloc() sub routines and should not deallocated with realloc() or free() subroutines. If the pointer parameter is a null pointer, then no action will occur.
Where as the realloc() subroutine allows the developer to change the block size of the memory which was pointed to by the pointer parameter, to a specified bytes size through size parameter and a new pointer to the block is returned. The pointer parameter specified must have been created by using malloc(),calloc() or realloc() sub routines and should not deallocated with realloc() or free() subroutines. If the pointer parameter is a null pointer, then no action will occur.
4 :: What is new operator and delete operator?
new and delete operators are provided by C++ for runtime memory management. They are used for dynamic allocation and freeing of memory while a program is running.
The new operator allocates memory and returns a pointer to the start of it. The delete operator frees memory previously allocated using new. The general form of using them is:
p_var = new type;
delete p_var;
new allocates memory on the heap. If there is insufficient memory, then new will fail and a bad_alloc exception will be generated. The program should handle this exception.
Consider following program:
#include <iostream>
#include <new>
using namespace std;
int main()
{
int *p;
try
{
p = new int; //dynamic allocation of memory
}
catch (bad_alloc x)
{
cout << “Memory allocation failed”;
return 1;
}
*p = 100;
cout <<”P has value”<<*p;
delete p; //free the dynamically allocated memory
return 0;
}
The new operator allocates memory and returns a pointer to the start of it. The delete operator frees memory previously allocated using new. The general form of using them is:
p_var = new type;
delete p_var;
new allocates memory on the heap. If there is insufficient memory, then new will fail and a bad_alloc exception will be generated. The program should handle this exception.
Consider following program:
#include <iostream>
#include <new>
using namespace std;
int main()
{
int *p;
try
{
p = new int; //dynamic allocation of memory
}
catch (bad_alloc x)
{
cout << “Memory allocation failed”;
return 1;
}
*p = 100;
cout <<”P has value”<<*p;
delete p; //free the dynamically allocated memory
return 0;
}
5 :: What is dynamic memory management for array?
Using the new and delete operators, we can create arrays at runtime by dynamic memory allocation. The general form for doing this is:
p_var = new array_type[size];
size specifies the no of elements in the array
To free an array we use:
delete[ ]p_var; // the [ ] tells delete that an array is being freed.
Consider following program:
#include <iostream>
#include <new>
using namespace std;
int main()
{
int *p, i;
try
{
p = new int(10); //allocate array of 10 integers
}
catch (bad_alloc x)
{
cout << “Memory allocation failed”;
return 1;
}
for (i = 0; i < 10; i++)
p[i] = i;
for (i = 0; i < 10; i++)
cout <<p[i]<<”\n”;
delete [ ] p; //free the array
return 0;
}
p_var = new array_type[size];
size specifies the no of elements in the array
To free an array we use:
delete[ ]p_var; // the [ ] tells delete that an array is being freed.
Consider following program:
#include <iostream>
#include <new>
using namespace std;
int main()
{
int *p, i;
try
{
p = new int(10); //allocate array of 10 integers
}
catch (bad_alloc x)
{
cout << “Memory allocation failed”;
return 1;
}
for (i = 0; i < 10; i++)
p[i] = i;
for (i = 0; i < 10; i++)
cout <<p[i]<<”\n”;
delete [ ] p; //free the array
return 0;
}
6 :: Following is the not a correct statement for preprocessor directive declaration?
a) #include<iostream.h>
b) #include<iostream.h> #define LEFT 1
c) #define LEFT 1
d) #define ABS(a) (a)<0 ? -(a) : (a)
b) #include<iostream.h> #define LEFT 1
7 :: In C++ if return statements is written in a non-void function without any specified value, what will be return value of the function?
a) undefined
b) 1
c) 0
d) -1
a) undefined
8 :: In C++ if program executed successfully, following value will be returned to the calling process, if nothing specified in return statement?
a) 0
b) 1
c) -1
d) 2
a) 0
9 :: Individual characters in a string are accessed as following:
a) cout << S.at(i);
b) cout << S[i];
c) both a) and b)
d) None
c) both a) and b)
10 :: Which of the following keyword is not used in exception handling:
a) Try
b) allow
c) Catch
d) Throw
b) allow