C Programming Multiple Choice Question - Memory Alloction
This section focuses on the "Memory Alloction" of the C programming. These Multiple Choice Questions (mcq) should be practiced to improve the C programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. What is the return type of malloc() or calloc()?
A. int *
B. int **
C. void *
D. void **
View Answer
Ans : C
Explanation: malloc() and calloc() return void *, without void * we may get warning in C if we don't type cast the return type to appropriate pointer.
2. Which function is used to delete the allocated memory space?
A. Dealloc()
B. free()
C. Both A and B
D. None of the above
View Answer
Ans : B
Explanation: free() is used to free the memory spaces allocated by malloc() and calloc().
3. Among 4 header files, which should be included to use the memory allocation functions like malloc(), calloc(), realloc() and free()?
A. #include<string.h>
B. #include<stdlib.h>
C. #include<memory.h>
D. Both b and c
View Answer
Ans : B
Explanation: #include <stdlib.h> is a header filer, which contains the inbuilt functions for all memory allocation functions.
4. Which of the following is/are true
A. calloc() allocates the memory and also initializes the allocates memory to zero, while memory allocated using malloc() has random data.
B. malloc() and memset() can be used to get the same effect as calloc()
C. Both malloc() and calloc() return 'void *' pointer
D. All of the above
View Answer
Ans : D
Explanation: None
5. Which of the following is true?
A. "ptr = calloc(m, n)" is equivalent to following
B. r = malloc(m * n);
C. "ptr = calloc(m, n)" is equivalent to following
D. r = malloc(m * n); memset(ptr, 0, m * n);
View Answer
Ans : B
Explanation: The name malloc and calloc() are library functions that allocate memory dynamically. It means that memory is allocated during runtime(execution of the program) from heap segment.
6. Which languages necessarily need heap allocation in the run time environment?
A. Those that support recursion
B. Those that use dynamic scoping
C. Those that use global variables
D. Those that allow dynamic data structures
View Answer
Ans : D
Explanation: Heap allocation is needed for dynamic data structures like tree, linked list, etc.
7. Which of the following statement is correct prototype of the malloc() function in c ?
A. int* malloc(int);
B. Char* malloc(char);
C. unsigned int* malloc(unsigned int);
D. void* malloc(size_t);
View Answer
Ans : D
Explanation: By defalut for malloc() function return type is void.
8. Specify the 2 library functions to dynamically allocate memory?
A. malloc() and memalloc()
B. alloc() and memalloc()
C. malloc() and calloc()
D. memalloc() and faralloc()
View Answer
Ans : C
Explanation: 2 library functions to dynamically allocate memory is malloc() and calloc().
9. 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.
A.
A. TRUE
B. FALSE
C. May Be
D. Can't Say
View Answer
Ans : B
Explanation: malloc() and calloc() return void pointer for using a particular data type we made explicite type casting.
10. malloc() allocates memory from the heap and not from the stack.
A. TRUE
B. FALSE
C. May Be
D. Can't Say
View Answer
Ans : A
Explanation: Heap area consist of hash codes .i.e. addreses while stack may or may not be that's why malloc() allocates memory from the heap.
Also check :
Discussion