Showing 31–40 of 303 questions
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.