Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Which of the following functions allocates multiple blocks of memory, each block of the same size?(a) malloc()(b) realloc()(c) calloc()(d) free()I had been asked this question in a job interview.My question is from DMA Functions, Memory Leak, Dangling Pointers in section Dynamic Memory Allocation in C of C

Answer»

Correct option is (c) calloc()

The BEST explanation: MALLOC() ALLOCATES a single block of MEMORY WHEREAS calloc() allocates multiple blocks of memory, each block with the same size.

2.

In the function malloc(), each byte of allocated space is initialized to zero.(a) True(b) FalseI got this question by my school teacher while I was bunking the class.This intriguing question comes from DMA Functions, Memory Leak, Dangling Pointers topic in portion Dynamic Memory Allocation in C of C

Answer»

The correct OPTION is (b) False

The best explanation: In the function MALLOC(), allocated space is initialized to junk values. In calloc(), each BYTE of allocated space is initialized to zero.

3.

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 programI have been asked this question during an online exam.This question is from Static vs Dynamic Memory Allocation in section Dynamic Memory Allocation in C of C

Answer»

The correct answer is (c) Execution of the PROGRAM is faster than that of STATIC memory ALLOCATION

The best I can explain: 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.

4.

Garbage collector frees the programmer from worrying about ___________(a) Dangling pointers(b) Creating new objects(c) Memory leak(d) Segmentation errorsThe question was posed to me in an online quiz.Origin of the question is DMA Functions, Memory Leak, Dangling Pointers topic in division Dynamic Memory Allocation in C of C

Answer»

The correct answer is (c) MEMORY leak

Explanation: A garbage collector is a program that automatically removes unwanted DATA held TEMPORARILY in the memory during processing. Hence it FREES the programmer from WORRYING about memory leaks.

5.

Which of the following is an example of static memory allocation?(a) Linked list(b) Stack(c) Queue(d) ArrayThis question was posed to me in class test.My doubt stems from Static vs Dynamic Memory Allocation in division Dynamic Memory Allocation in C of C

Answer»

Correct ANSWER is (d) Array

To explain I would say: Array is an example of STATIC MEMORY allocation whereas LINKED LIST, queue and stack are examples for dynamic memory allocation.

6.

Local variables are stored in an area called ___________(a) Heap(b) Permanent storage area(c) Free memory(d) StackThis question was addressed to me in an interview for internship.The above asked question is from Static vs Dynamic Memory Allocation topic in chapter Dynamic Memory Allocation in C of C

Answer»

The correct answer is (d) Stack

Easy 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.

7.

The incorrect statement with respect to dangling pointers is ___________(a) Pointer pointing to non-existent memory location is called dangling pointer(b) When a dynamically allocated pointer references the original memory after it has been freed, a dangling pointer arises(c) If memory leak occurs, it is mandatory that a dangling pointer arises(d) Dangling pointer may result in segmentation faults and potential security risksThe question was posed to me in homework.I'm obligated to ask this question of DMA Functions, Memory Leak, Dangling Pointers topic in chapter Dynamic Memory Allocation in C of C

Answer»

The CORRECT option is (C) If memory leak occurs, it is mandatory that a dangling pointer arises

The explanation is: Memory leak and dangling pointers are not INTER dependent. HENCE, when memory leak occurs, it is not mandatory that a dangling pointer arises

8.

Array is preferred over linked list for the implementation of ________(a) Radix sort(b) Insertion sort(c) Binary search(d) Polynomial evaluationThe question was posed to me in an online quiz.My question is based upon Static vs Dynamic Memory Allocation in chapter Dynamic Memory Allocation in C of C

Answer»

The correct ANSWER is (c) Binary search

The explanation is: When we try to IMPLEMENT binary search using linked list, the traversal steps per ELEMENT increases in order to find the middle element. Thus, this process is slow and inefficient. This process is much faster using an array, hence it is preferable to use an array for the IMPLEMENTATION of binary search.

9.

The number of arguments taken as input which allocating memory dynamically using malloc() is ___________(a) 0(b) 1(c) 2(d) 3This question was posed to me in an internship interview.The question is from DMA Functions, Memory Leak, Dangling Pointers topic in portion Dynamic Memory Allocation in C of C

Answer»

Correct choice is (B) 1

The best I can explain: An example of MEMORY allocated USING malloc():

(INT*)malloc(3*sizeof(int)

It is clear from the above example that malloc() takes only one argument as input, that is the number of bytes to be allocated.

10.

In the function realloc(), if the new size of the memory block is larger than the old size, then the added memory ___________(a) is initialized to junk values(b) is initialized to zero(c) results in an error(d) is not initializedI got this question in an online quiz.My doubt stems from DMA Functions, Memory Leak, Dangling Pointers topic in portion Dynamic Memory Allocation in C of C

Answer»

Correct answer is (d) is not initialized

To explain: The function REALLOC() changes the size of a particular memory block. If the new size is LARGER than the OLD size, the added memory is not initialized.

11.

A condition where in memory is reserved dynamically but not accessible to any of the programs is called _____________(a) Memory leak(b) Dangling pointer(c) Frozen memory(d) Pointer leakI have been asked this question in examination.I'm obligated to ask this question of DMA Functions, Memory Leak, Dangling Pointers topic in chapter Dynamic Memory Allocation in C of C

Answer»

The CORRECT option is (a) MEMORY leak

Explanation: If we allocate memory dynamically in a function (malloc, calloc, realloc), the allocated memory will not be de-allocated automatically when the control comes out of the function. This allocated memory cannot be accessed and hence cannot be used. This unused inaccessible memory RESULTS in a memory leak.

12.

Queue data structure works on the principle of ____________(a) Last In First Out (LIF0)(b) First In Last Out (FILO)(c) First In First Out (FIFO)(d) Last In Last Out (LILO)This question was addressed to me in semester exam.My question is based upon Static vs Dynamic Memory Allocation topic in section Dynamic Memory Allocation in C of C

Answer»

Right option is (c) FIRST In First Out (FIFO)

Easy explanation - QUEUE is a linear DATA structure which works on the principle of first in first out. This means that the first ELEMENT to be inserted in a queue will be the first one to be REMOVED.

13.

Which of the following is an example for non linear data type?(a) Tree(b) Array(c) Linked list(d) QueueThis question was posed to me in an online interview.This is a very interesting question from Static vs Dynamic Memory Allocation in division Dynamic Memory Allocation in C of C

Answer»

Correct option is (a) Tree

For 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.

14.

Which of the following header files must necessarily be included to use dynamic memory allocation functions?(a) stdlib.h(b) stdio.h(c) memory.h(d) dos.hI had been asked this question in an online interview.The origin of the question is Static vs Dynamic Memory Allocation topic in portion Dynamic Memory Allocation in C of C

Answer» CORRECT answer is (a) stdlib.h

Easiest explanation - stdlib.h is a header FILE which stands for the standard library. It consists of the declaration for dynamic memory allocation functions such as MALLOC(), CALLOC(), realloc() and free.
15.

The type of linked list in which the node does not contain any pointer or reference to the previous node is _____________(a) Circularly singly linked list(b) Singly linked list(c) Circular doubly linked list(d) Doubly linked listI have been asked this question in an interview.The doubt is from Static vs Dynamic Memory Allocation in chapter Dynamic Memory Allocation in C of C

Answer» RIGHT choice is (b) Singly LINKED list

The BEST I can explain: 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.
16.

The size of both stack and heap remains the same during run time.(a) True(b) FalseThis question was addressed to me by my college director while I was bunking the class.The doubt is from Static vs Dynamic Memory Allocation in portion Dynamic Memory Allocation in C of C

Answer»

The correct option is (B) False

The best explanation: Memory can be allocated or de-allocated during the run time in the HEAP region. Memory BINDINGS (allocation and de-allocation) are established and destroyed during the EXECUTION of the program. Hence we can see that the size of heap does not remain same during run time. However, the size of stack remains the same.