C++ New And Delete Interview Questions And Answers
Download C++ New And Delete Interview Questions and Answers PDF
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 you're 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:
C++ New And Delete Job Interview Questions Table of Contents:
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.
Read MoreNew
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.
Read More3 :: 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.
Read MoreWhere 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;
}
Read MoreThe 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;
}
Read Morep_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
Read More7 :: 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
Read More8 :: 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
Read More9 :: 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)
Read More10 :: Which of the following keyword is not used in exception handling:
a) Try
b) allow
c) Catch
d) Throw
b) allow
Read More11 :: In C++ how many return statements are allowed in a non-void function?
a) 1
b) as many as you like
c) 0
d) 2
b) as many as you like
Read More12 :: Which of the following is invalid header file name?
a) <iostring>
b) <string>
c) <iostream >
d) <sstream>
a) <iostring>
Read More14 :: In the following statements:
class sports {};
class test : public student{};
class result : public test, public sports {};
//Here result class have implemented,
a) Hierarchical inheritance
b) Multiple inheritance
c) Multilevel inheritance
d) Both b) and c)
d) Both b) and c)
Read More15 :: Which of the following are valid array declaration?
a) int num(5)
b) float avg[5]
c) double[5] marks
d) counter int[5]
b) float avg[5]
Read More16 :: Which of the following are valid array declaration?
a) char str1[3] = "ab"
b) char str1[3] = "abc"
c) char str1[2] = "ab"
d) char str1[0] = "ab"
a) char str1[3] = "ab"
Read More17 :: C++ supports ….
a) constant pointer
b) pointer to a constant
c) None of above
d) Both of above
d) Both of above
Read More18 :: char * const ptr1 = "nice";
//this is example of ,
a) constant pointer
b) pointer to a constant
c) None of above
d) Both of above
a) constant pointer
Read More19 :: int const *ptr1 = &m;
// this is example of,
a) Constant pointer
b) Pointer to a constant
c) Both of above
d) None of above
b) Pointer to a constant
Read More20 :: In the following statement,
const char * const cp = "xyz";
a) Address assigned to pointer cp cannot be changed
b) contents it points to cannot be changed
c) Both of above
d) None of above
c) Both of above
Read More21 :: A friend class in C++, can access the "__________" members of the class in which it is declared as a friend:
a) private and protected
b) private and public
c) public and protected
d) Public
a) private and protected
Read More22 :: A friend function that is a "friend" of a given class is allowed access to ______data in that class.
a) public, private, or protected
b) public or private
c) public
d) protected
a) public, private, or protected
Read More23 :: In C++, symbolic constants created using:
a) const
b) enum
c) Both of above
d) None of above
c) Both of above
Read More24 :: Object can be used as a function argument by..
a) Pass by value
b) Pass by reference
c) None of above
d) All of above
d) All of above
Read More25 :: In the following statements,
class ABC;
class ABC
{
};
a) It is called as forward declaration
b) It is called as backward declaration
c) It is called as middle declaration
d) It is called as simple declaration
a) It is called as forward declaration
Read More