5. Inventor of C++ language is
a) John Dell
b) Bjarne Stroustrup
c) Thomusn Steve
d) Karl Thomus
b) Bjarne Stroustrup
a) It is called as forward declaration
d) All of above
10. In C++, symbolic constants created using:
a) const
b) enum
c) Both of above
d) None of above
c) Both of above
a) public, private, or protected
a) private and protected
b) Pointer to a constant
a) constant pointer
16. C++ supports ….
a) constant pointer
b) pointer to a constant
c) None of above
d) Both of above
d) Both of above
a) char str1[3] = "ab"
b) float avg[5]
a) <iostring>
b) as many as you like
c) both a) and b)
b) #include<iostream.h> #define LEFT 1
28. 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;
}
29. 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;
}
30. 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.
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.
32. 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.