Dynamic Memory Allocation MCQ
31. Local variables are stored in an area called ___________
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.
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:
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 ________
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?
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.
void fun(int *a)
a = (int*)malloc(sizeof(int));
*p = 6;
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?
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?
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?
Explanation: allocates the specified number of bytes and initializes them to zero.
40. Which is the correct sequence of compilation process?
Explanation: No explanation.
Also check :