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.
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.
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.
31. Local variables are stored in an area called ___________
A. Heap
B. Permanent storage area
C. Free memory
D. Stack
View Answer
Ans : D
Explanation: Local variables are stored in an area called stack. Global variables, static variables and program instructions are stored in the permanent storage area. The memory space between these two regions is known a heap.
32. Choose the statement which is incorrect with respect to dynamic memory allocation.
A. Memory is allocated in a less structured area of memory, known as heap
B. Used for unpredictable memory requirements
C. Execution of the program is faster than that of static memory allocation
D. Allocated memory can be changed during the run time of the program based on the requirement of the program
View Answer
Ans : C
Explanation: Execution of the program using dynamic memory allocation is slower than that using static memory allocation. This is because in dynamic memory allocation, the memory has to be allocated during run time. This slows down the execution of the program.
33. The type of linked list in which the node does not contain any pointer or reference to the previous node:
A. Circularly singly linked list
B. Singly linked list
C. Circular doubly linked list
D. Doubly linked list
View Answer
Ans : B
Explanation: A singly linked list is one in which each node has two fields, namely data field and pointer field. Data field stores the data and the pointer field points to the address of the next node.
34. The advantage of using linked lists over arrays is that ________
A. Linked list is an example of linear data structure
B. Insertion and deletion of an element can be done at any position in a linked list
C. Linked list can be used to store a collection of homogenous and heterogeneous data types
D. The size of a linked list is fixed
View Answer
Ans : B
Explanation: Insertion and deletion in a linked list can be done at any position. On the other hand, in an array, to insert an element at a specific position, the rest of the elements have to be moved one position to the left and to delete an element, all the elements after the deleted element have to be moved one position to the right.
35. Which of the following is an example for non linear data type?
A. Tree
B. Array
C. Linked list
D. Queue
View Answer
Ans : A
Explanation: A data structure is said to be linear if its elements form a sequence or a linear list. For example array, linked list, queue, stack etc. Elements in a non linear data structure do not form a sequence. For example Trees, graphs etc
36. If malloc() successfully allocates memory it returns the number of bytes it has allocated.
# include<stdio.h>
#include<stdlib.h>
void fun(int *a)
{
a = (int*)malloc(sizeof(int));
}
int main()
{
int *p;
fun(p);
*p = 6;
printf("%dn",*p);
return(0);
}
A. May not work
B. Works and prints 6
C. Compiler Error
D. Runtime error
View Answer
Ans : A
Explanation: The program is not valid. Try replacing "int *p;" with "int *p = NULL;" and it will try to dereference a null pointer. This is because fun() makes a copy of the pointer, so when malloc() is called, it is setting the copied pointer to the memory location, not p. p is pointing to random memory before and after the call to fun(), and when you dereference it, it will crash. If you want to add memory to a pointer from a function, you need to pass the address of the pointer (ie. double pointer).
37. During preprocessing, the code #include gets replaced by the contents of the file stdio.h. Which is true?
A. During linking the code #include replaces by stdio.h
B. Yes
C. During execution the code #include replaces by stdio.h
D. During editing the code #include replaces by stdio.h
View Answer
Ans : B
Explanation: Preprocessing enlarges and boosts the C programming language by replacing preprocessing directive #include with the content of the file stdio.h.
38. Why to use fflush() library function?
A. To flush all streams and specified streams
B. To flush only specified stream
C. To flush input/output buffer
D. Invalid library function
View Answer
Ans : A
Explanation: As defined Use of fflush(stdin) in C : fflush() is typically used for output stream only. Its purpose is to clear (or flush) the output buffer and move the buffered data to console (in case of stdout) or disk (in case of file output stream).
39. Why is calloc() function used for?
A. allocates the specified number of bytes
B. allocates the specified number of bytes and initializes them to zero
C. increases or decreases the size of the specified block of memory and reallocates it if needed
D. calls the specified block of memory for execution.
View Answer
Ans : B
Explanation: allocates the specified number of bytes and initializes them to zero.
40. Which is the correct sequence of compilation process?
A. Assembler -> Compiler -> Preprocessor -> Linking
B. Compiler -> Assenbler -> Preprocessor -> Linking
C. Preprocessor -> Compiler -> Assembler -> Linking
D. Assembler -> Compiler -> Linking -> Preprocessor
View Answer
Ans : C
Explanation: No explanation.
Also check :
Discussion