C Programming
C language from basics to advanced placement prep
303 Questions 10 Topics Take Test
Advertisement
Showing 31–40 of 303 questions
Q.31 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.32 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.33 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.34 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
Q.35 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.36 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.37 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.38 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.39 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
Q.40 Easy Dynamic Memory
What is the primary difference between malloc() and calloc()?
A malloc() is faster than calloc()
B calloc() initializes memory with zeros, malloc() does not
C malloc() allocates from stack, calloc() from heap
D calloc() requires only one argument
Correct Answer:  B. calloc() initializes memory with zeros, malloc() does not
EXPLANATION

calloc() allocates memory and initializes all bytes to zero, while malloc() leaves the allocated memory uninitialized.

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