C Programming MCQ - Memory Allocation
11. What is the output of this program?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, numbers[1];
numbers[0] = 15;
free(numbers);
printf("Stored integers are ");
printf("numbers[%d] = %d ", 0, numbers[0]);
return 0;
}
A. 15
B. Compilation error
C. 0
D. garbage value
View Answer
Ans : A
Explanation: he memory allocation function free() will not free or delete the content in the memory location, because free() can only free or delete the content in the memory, who's memory is allocated either by malloc() or calloc().
12. What is the output of this program?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *j = (int*)malloc(4 * sizeof(int));
*j = 15;
free(j);
printf("%d", *j);
return 0;
}
A. Compilation error
B. Some Garbage value
C. 0
D. Nothing prints
View Answer
Ans : B
Explanation: In this program, a pointer variable *j is declared and its memory space is allocated using malloc() and then an integer value 15 is set to a pointer variable *j.
Now free(j) is used to free or delete the memory space allocated to the pointer variable *j using malloc().
While freeing the memory space allocated using malloc(), all allocated space will be deleted and by default the deleted space will be denoted by some garbage value and is outputted.
13. What is the output of this program?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *numbers = (int*)calloc(4, sizeof(int));
numbers[0] = 2;
free(numbers);
printf("Stored integers are ");
printf("numbers[%d] = %d ", 0, numbers[0]);
return 0;
}
A. Garbage value
B. 2
C. 0
D. Compilation error
View Answer
Ans : C
Explanation: In this program, a pointer variable *numbers is declared and its memory space is allocated using calloc() and then an integer value 2 is set an array of index 0 ie numbers[0].
Now free(numbers) is used to free or delete the memory space allocated to the pointer variable *numbers using calloc().
While freeing the memory space allocated using calloc() ,all the space in a variable numbers are deleted and by default deleted spaces are denoted as 0 which is outputted.
14. What is the output of this program?
#include <stdio.h>
void main()
{
int *ptr = (int *)malloc(sizeof(int));
*ptr = 10;
free(ptr);
p = 5;
printf("%d", ptr);
}
A. Compilation error
B. 5
C. 0
D. Garbage value
View Answer
Ans : B
Explanation: free() will not deallocate the memory it just to delete all data's allocated to a variable (ptr).
In this program, first integer value 10 is assigned to the pointer variable *ptr and then its data is deleted using free(ptr) and then new integer value 5 is assigned to the variable ptr and then 5 is outputted.
15. What is the output of this program?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
char *ptr;
char *fun();
ptr = fun();
printf(" %s", ptr);
return 0;
}
char *fun()
{
char disk[30];
strcpy(disk, "letsfindcourse");
printf("%s ",disk);
return disk;
}
A. letsfindcourse
B. Compilation error
C. letsfindcourse letsfindcourse
D. garbage value
View Answer
Ans : A
Explanation: Here disk is an auto array, when array is used as return value, it will die when the control go back to main() function of a program. Thus memory in c alone prints.
16. What is the output of this program?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p;
p = (int *)malloc(40);
printf("%d", sizeof(p));
free(p);
return 0;
}
A. 40
B. 50
C. 30
D. 20
View Answer
Ans : C
Explanation: Since p is the pointer variable of integer type, thus the size of integer type is 4.
17. What is the output of this program?
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct test
{
int i;
float f;
char c;
};
struct test *ptr;
ptr = (struct test *)malloc(sizeof(struct test));
ptr ->f = 2.5f;
printf("%f", ptr->f);
return 0;
}
A. Compilation error
B. 2.5
C. Garbage value
D. 0
View Answer
Ans : B
Explanation: Here test is a structure name which consist of three variables of three different data types.
ptr is the pointer variable, for which the memory is allocated dynamically by using malloc() of function and then it is pointed towards the structure floating variable f.
Now f is initialized with a value 2.5f and then outputted as 2.500000.
18. Which statment is true about the given code ?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *a[5];
a = (int*) malloc(sizeof(int)*5);
free(a);
return 0;
}
A. Error: unable to allocate memory
B. Error: We cannot store address of allocated memory in a
C. Error: unable to free memory
D. No error
View Answer
Ans : B
Explanation: None
19. What is the Error of this program?
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *ptr;
*ptr = (char)malloc(30);
strcpy(ptr, "RAM");
printf("%s", ptr);
free(ptr);
return 0;
}
A. Error: in strcpy() statement.
B. Error: in *ptr = (char)malloc(30);
C. Error: in free(ptr);
D. No error
View Answer
Ans : B
Explanation: None
20. How will you free the memory allocated by the following program?
#include <stdio.h>
#include <stdlib.h>
#define MAXROW 2
#define MAXCOL 3
int main()
{
int **p, i, j;
p = (int **) malloc(MAXROW * sizeof(int*));
return 0;
}
A. memfree(int p);
B. dealloc(p);
C. malloc(p, 0);
D. free(p);
View Answer
Ans : D
Explanation: Dynamically allocated memory created with either calloc() or malloc() doesn't get freed on their own. You must explicitly use free() to release the space.
Also check :
Discussion