C Programming Multiple choice Questions and Answers on Storage Management (Dynamic Memory Allocation) for Freshers

https://www.computersprofessor.com/2018/01/c-programming-multiple-choice-questions_9.html
1. The function ____ obtains block of memory dynamically.
a) calloc
b) malloc
c) Both calloc & malloc
d) free
Answer: c
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
3. calloc() returns a storage that is initialized to.
a) Zero
b) Null
c) Nothing
d) One
Answer: a
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
5. What is the output of this C code?
#include
void main()
{
char *p = calloc(100, 1);
p = "welcome";
printf("%s\n", p);
}
a) Segmentation fault
b) Garbage
c) Error
d) welcome
b) Garbage
c) Error
d) welcome
Answer: d
6. Memory allocation using malloc() is done in?
a) Static area
b) Stack area
c) Heap area
d) Both Stack & Heap area
Answer: c
7. Why do we write (int *) before malloc?
int *ip = (int *)malloc(sizeof(int));
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
8. Which one is used during memory deallocation in C?
a) remove(p);
b) delete(p);
c) free(p);
d) terminate(p);
Answer: c
9. Which of the following will return a result most quickly for searching a given key?
a) Unsorted Array
b) Sorted Array
c) Sorted linked list
d) Binary Search Tree
Answer: d
10. On freeing a dynamic memory, if the pointer value is not modified, then the pointer points to.
a) NULL
b) Other dynamically allocated memory
c) The same deallocated memory location
d) It points back to location it was initialized with
Answer: c
11. For the following program, Which of the following should be used for freeing the memory allocated?
#include
struct p
{
struct p *next;
int x;
};
int main()
{
struct p *p1 = (struct p*)malloc(sizeof(struct p));
p1->x = 1;
p1->next = (struct p*)malloc(sizeof(struct p));
return 0;
}
a) free(p1);
free(p1->next)
b) free(p1->next);
free(p1);
c) free(p1);
d) all of the mentioned
free(p1->next)
b) free(p1->next);
free(p1);
c) free(p1);
d) all of the mentioned
Answer: b
12. What is the output of this C code?
#include
struct p
{
struct p *next;
int x;
};
int main()
{
struct p *p1 = calloc(1, sizeof(struct p));
p1->x = 1;
p1->next = calloc(1, sizeof(struct p));
printf("%d\n", p1->next->x);
return 0;
}
a) Compile time error
b) 1
c) Somegarbage value
d) 0
b) 1
c) Somegarbage value
d) 0
Answer: d
13. What is the output of this C code?
#include
struct p
{
struct p *next;
int x;
};
int main()
{
struct p* p1 = malloc(sizeof(struct p));
p1->x = 1;
p1->next = malloc(sizeof(struct p));
printf("%d\n", p1->next->x);
return 0;
}
a) Compile time error
b) 1
c) Somegarbage value
d) 0
b) 1
c) Somegarbage value
d) 0
Answer: c
14. calloc initialises memory with all bits set to zero.
a) True
b) False
c) Depends on the compiler
d) Depends on the standard
Answer: a
15. realloc(ptr, size), where size is zero means
a) Allocate a memory location with zero length
b) Free the memory pointed to by ptr
c) Undefined behaviour
d) Doesn’t do any reallocation of ptr i.e. no operation
Answer: b