C Programming — Dynamic Memory
C language from basics to advanced placement prep
27 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 27 questions in Dynamic Memory
Q.1 Easy Dynamic Memory
In implementing a dynamic doubly-linked list, what additional field is required compared to singly-linked list?
A Data field
B Previous pointer
C Size field
D Head pointer
Correct Answer:  B. Previous pointer
EXPLANATION

Doubly-linked list nodes require both next and previous pointers to enable bidirectional traversal unlike singly-linked lists.

Take Test
Q.2 Easy Dynamic Memory
If malloc() returns NULL, what should a program do?
A Ignore and continue using the pointer
B Check for NULL and handle the error appropriately
C Always typecast the return value
D Use global variables instead
Correct Answer:  B. Check for NULL and handle the error appropriately
EXPLANATION

NULL return from malloc indicates allocation failure; ignoring causes dereferencing NULL which leads to crash.

Take Test
Q.3 Easy Dynamic Memory
In a dynamic queue implementation, if front pointer is NULL, what does it indicate?
A Queue is full
B Queue has only one element
C Queue is empty
D Memory allocation failed
Correct Answer:  C. Queue is empty
EXPLANATION

In queue implementations using linked lists, NULL front pointer indicates queue is empty as there are no nodes to dequeue.

Take Test
Q.4 Easy Dynamic Memory
What is the primary difference between calloc() and malloc() in C?
A calloc() allocates more memory than malloc()
B calloc() initializes allocated memory to zero, malloc() does not
C malloc() is faster than calloc()
D calloc() can only allocate arrays, malloc() allocates single blocks
Correct Answer:  B. calloc() initializes allocated memory to zero, malloc() does not
EXPLANATION

calloc(n, size) allocates n*size bytes and initializes all to 0, while malloc(size) allocates size bytes with garbage values.

Take Test
Q.5 Easy Dynamic Memory
How many bytes are allocated by malloc(sizeof(int) * 10) on a system where int is 4 bytes?
A 10 bytes
B 40 bytes
C 44 bytes
D 50 bytes
Correct Answer:  B. 40 bytes
EXPLANATION

malloc allocates exactly sizeof(int) * 10 = 4 * 10 = 40 bytes. sizeof() operator returns size in bytes.

Take Test
Advertisement
Q.6 Easy Dynamic Memory
What is the result of calling free() on a NULL pointer?
A It is safe and does nothing
B It causes undefined behavior
C It frees all allocated memory
D It returns -1
Correct Answer:  A. It is safe and does nothing
EXPLANATION

Calling free(NULL) is safe in C and does nothing. This is by design, allowing you to write code like free(ptr) without checking if ptr is NULL first.

Take Test
Q.7 Easy Dynamic Memory
What happens when malloc() fails to allocate memory?
A It returns NULL
B It throws an exception
C It allocates memory from stack
D It terminates the program
Correct Answer:  A. It returns NULL
EXPLANATION

When malloc() cannot allocate the requested memory, it returns NULL. The programmer must check for NULL before using the pointer to avoid undefined behavior.

Take Test
Q.8 Easy Dynamic Memory
Which header file must be included to use malloc() and free() functions?
A #include
B #include
C #include
D #include
Correct Answer:  A. #include
EXPLANATION

<stdlib.h> is the standard header file containing declarations for malloc(), calloc(), realloc(), and free() functions.

Take Test
Q.9 Easy Dynamic Memory
Which function in C is used to allocate memory dynamically at runtime?
A malloc()
B alloc()
C allocate()
D memalloc()
Correct Answer:  A. malloc()
EXPLANATION

malloc() is the standard C library function for dynamic memory allocation. It takes the number of bytes to allocate as an argument and returns a pointer to the allocated memory.

Take Test
Q.10 Easy Dynamic Memory
Which of the following correctly allocates memory for an array of 5 integers and initializes to zero?
A int *p = malloc(5 * sizeof(int));
B int *p = calloc(5, sizeof(int));
C int *p = realloc(NULL, 5);
D int p[5] = {0};
Correct Answer:  B. int *p = calloc(5, sizeof(int));
EXPLANATION

calloc(5, sizeof(int)) allocates memory for 5 integers and initializes all to zero.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips