Memory Allocation(C Programming ) Questions and Answers
Question 1.
When we dynamically allocate memory is there any way to free memory during run time?
Yes
No
Explanation:-
Answer: Option A. -> Yes
Using free()
Question 2.
Can I increase the size of statically allocated array?
Yes
No
Explanation:-
Answer: Option B. -> No
Question 3.
Can I increase the size of dynamically allocated array?
Yes
No
Explanation:-
Answer: Option A. -> Yes
Use realloc(variable_name, value);
Question 4.
If malloc() successfully allocates memory it returns the number of bytes it has allocated.
True
False
Explanation:-
Answer: Option B. -> False
Syntax: void *malloc(size_t size); The malloc() function shall allocate unused space for an object whose size in bytes is specified by size and whose value is unspecified. The order and contiguity of storage allocated by successive calls to malloc() is unspecified.
Question 5.
malloc() returns a float pointer if memory is allocated for storing float's and a double pointer
if memory is allocated for storing double's.
True
False
Explanation:-
Answer: Option B. -> False
Question 6.
malloc() allocates memory from the heap and not from the stack.
True
False
Explanation:-
Answer: Option A. -> True
Question 7.
malloc() returns a NULL if it fails to allocate the requested memory.
True
False
Explanation:-
Answer: Option A. -> True
Question 8.
Which of the following statement is correct prototype of the malloc() function in c ?
int* malloc(int);
char* malloc(char);
unsigned int* malloc(unsigned int);
void* malloc(size_t);
Explanation:-
Answer: Option D. -> void* malloc(size_t);
Question 9.
Point out the correct statement which correctly free the memory pointed to by 's' and 'p' in the following program?
#include<stdio.h> #include<stdlib.h> iint main() { struct ex { int i; float j; char *s }; struct ex *p; p = (struct ex *)malloc(sizeof(struct ex)); p->s = (char*)malloc(20); return 0; }
free(p); , free(p->s);
free(p->s); , free(p);
free(p->s);
free(p);
Explanation:-
Answer: Option B. -> free(p->s); , free(p);
Question 10.
Point out the correct statement which correctly allocates memory dynamically for 2D array following program?
#include<stdio.h> #include<stdlib.h> int main() { int *p, i, j; /* Add statement here */ for(i=0; i
p = (int*) malloc(3, 4);
p = (int*) malloc(3*sizeof(int));
p = malloc(3*4*sizeof(int));
p = (int*) malloc(3*4*sizeof(int));
Explanation:-
Answer: Option D. -> p = (int*) malloc(3*4*sizeof(int));