Here is a listing of C problems on “Storage Management” along with answers, explanations and/or solutions:
1. The function ____ obtains a block of memory dynamically.
a) calloc
b) malloc
c) both calloc & malloc
d) free
Answer: c
Clarification: None.
2. void * malloc(size_t n) returns?
a) Pointer to n bytes of uninitialized storage
b) NULL if the request can be satisfied
c) Nothing
d) None of the mentioned
Answer: a
Clarification: None.
3. calloc() returns storage that is initialized to.
a) Zero
b) Null
c) Nothing
d) One
Answer: a
Clarification: None.
4. In function free(p), p is a _______
a) int
b) pointer returned by malloc()
c) pointer returned by calloc()
d) pointer returned by malloc() & calloc()
Answer: d
Clarification: None.
5. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
char *p = calloc(100, 1);
-
p = "welcome";
-
printf("%sn", p);
-
}
a) Segmentation fault
b) Garbage
c) Error
d) welcome
Answer: d
Clarification: None.
6. Memory allocation using malloc() is done in _________
a) Static area
b) Stack area
c) Heap area
d) Both Stack & Heap area
Answer: c
Clarification: None.
7. Why do we write (int *) before malloc?
int *ip = (int *)malloc(sizeof(int));
a) It is for the syntax correctness
b) It is for the type-casting
c) It is to inform malloc function about the data-type expected
d) None of the mentioned
Answer: b
Clarification: None.
8. Which of the following is used during memory deallocation in C?
a) remove(p);
b) delete(p);
c) free(p);
d) terminate(p);
Answer: c
Clarification: None.