Memory Allocation(C Programming ) Questions and Answers

Question 1.

When we dynamically allocate memory is there any way to free memory during run time?


  1.    Yes
  2.    No
Explanation:-
Answer: Option A. -> Yes

Using free()

Question 2.

Can I increase the size of statically allocated array?


  1.    Yes
  2.    No
Explanation:-
Answer: Option B. -> No



Question 3.

Can I increase the size of dynamically allocated array?


  1.    Yes
  2.    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.


  1.    True
  2.    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.


  1.    True
  2.    False
Explanation:-
Answer: Option B. -> False



Question 6.

malloc() allocates memory from the heap and not from the stack.


  1.    True
  2.    False
Explanation:-
Answer: Option A. -> True



Question 7.

malloc() returns a NULL if it fails to allocate the requested memory.


  1.    True
  2.    False
Explanation:-
Answer: Option A. -> True



Question 8.

Which of the following statement is correct prototype of the malloc() function in c ?


  1.    int* malloc(int);
  2.    char* malloc(char);
  3.    unsigned int* malloc(unsigned int);
  4.    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;
}
  1.    free(p); , free(p->s);
  2.    free(p->s); , free(p);
  3.    free(p->s);
  4.    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
  1.    p = (int*) malloc(3, 4);
  2.    p = (int*) malloc(3*sizeof(int));
  3.    p = malloc(3*4*sizeof(int));
  4.    p = (int*) malloc(3*4*sizeof(int));
Explanation:-
Answer: Option D. -> p = (int*) malloc(3*4*sizeof(int));