C Programming Memory Allocation MCQ
21. Consider the following program, where are i, j and k are stored in memory?
#include <stdio.h>
#include <stdlib.h>
int i;
int main()
{
int j;
int *k = (int *) malloc (sizeof(int));
}
A. i, j and *k are stored in stack segment
B. i and j are stored in stack segment. *k is stored on heap.
C. i is stored in BSS part of data segment, j is stored in stack segment. *k is stored on heap.
D. j is stored in BSS part of data segment, i is stored in stack segment. *k is stored on heap.
View Answer
Ans : C
Explanation: i is global variable and it is uninitialized so it is stored on BSS part of Data Segment, j is local in main() so it is stored in stack frame, *k is dynamically allocated so it is stored on Heap Segment.
22. What function should be used to free the memory allocated by calloc() ?
A. dealloc();
B. malloc(variable_name, 0)
C. free();
D. memalloc(variable_name, 0)
View Answer
Ans : C
Explanation: free(); function should be used to free the memory allocated by calloc().
23. Point out the error in the following 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: ptr = (char*)malloc(30);.
24. Where does the uninitialized data gets stored in memory?
A. Code segment
B. Data segment
C. BSS- Block started by symbol
D. Heap
View Answer
Ans : C
Explanation: BSS- Block started by symbol the uninitialized data gets stored in memory.
25. malloc() returns a NULL if it fails to allocate the requested memory.
A. TRUE
B. FALSE
C. May Be
D. Can't Say
View Answer
Ans : A
Explanation: No Explanation.
26. If malloc() successfully allocates memory it returns the number of bytes it has allocated.
A. TRUE
B. FALSE
C. May Be
D. Can't Say
View Answer
Ans : B
Explanation: No Explanation.
27. Can I increase the size of dynamically allocated array?
A. Yes
B. No
C. May Be
D. Can't Say
View Answer
Ans : A
Explanation: Use realloc(variable_name, value);
28. Can we increase the size of statically allocated array?
A. Yes
B. No
C. May Be
D. Can't Say
View Answer
Ans : B
Explanation: Yes we increase the size of statically allocated array
29. When we dynamically allocate memory is there any way to free memory during run time?
A. Yes
B. No
C. May Be
D. Can't Say
View Answer
Ans : A
Explanation: there any way to free memory during run time by Using free().
30. 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 = 5.5f;
printf("%f", ptr->f);
return 0;
}
A. 5.5
B. 5
C. 5.5
D. 0
View Answer
Ans : C
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 5.5f and then outputted as 5.500000.
Also check :
Discussion