Showing 41–50 of 303 questions
What does free() function do in C?
A
Allocates memory from heap
B
Deallocates previously allocated memory
C
Copies memory from one location to another
D
Initializes memory with zeros
Correct Answer:
B. Deallocates previously allocated memory
EXPLANATION
free() is used to deallocate memory that was previously allocated using malloc(), calloc(), or realloc().
Which header file must be included to use dynamic memory allocation functions in C?
A
#include
B
#include
C
#include
D
#include
Correct Answer:
A. #include
EXPLANATION
stdlib.h contains declarations for malloc(), calloc(), realloc(), and free() functions.
How much memory in bytes does calloc(10, 4) allocate?
A
10 bytes
B
4 bytes
C
40 bytes
D
10.4 bytes
Correct Answer:
C. 40 bytes
EXPLANATION
calloc(num, size) allocates num * size = 10 * 4 = 40 bytes total.
What is the return type of malloc()?
A
void*
B
int*
C
char*
D
NULL
EXPLANATION
malloc() returns a generic pointer (void*) which can be cast to any data type pointer.
Which header file must be included for dynamic memory allocation functions in C?
A
#include
B
#include
C
#include
D
#include
Correct Answer:
A. #include
EXPLANATION
stdlib.h contains malloc(), calloc(), realloc(), and free() functions for dynamic memory management.
What is the correct way to allocate and initialize a structure dynamically?
struct Node { int data; int next; };
A
struct Node *n = malloc(sizeof(struct Node)); n->data = 5;
B
struct Node *n = (struct Node*)malloc(sizeof(struct Node)); n->data = 5;
C
struct Node n = malloc(sizeof(struct Node));
D
Both A and B are equally correct
Correct Answer:
D. Both A and B are equally correct
EXPLANATION
Both A and B allocate memory correctly; B includes explicit typecasting which is optional in C but good practice.
Which statement best describes dynamic memory allocation's advantage over static allocation?
A
Dynamic memory is always faster
B
Size is determined at compile time with dynamic allocation
C
Memory size can be determined at runtime based on actual needs
D
Dynamic memory never causes fragmentation
Correct Answer:
C. Memory size can be determined at runtime based on actual needs
EXPLANATION
Dynamic memory allows flexible sizing at runtime, which static arrays cannot provide.
What happens when you call free() on a NULL pointer?
A
Program crashes
B
It is safely ignored (no operation performed)
C
It returns false
D
Undefined behavior occurs
Correct Answer:
B. It is safely ignored (no operation performed)
EXPLANATION
According to C standard, free(NULL) is safe and does nothing.
What is the key difference between malloc() and calloc()?
A
malloc() allocates memory; calloc() deallocates it
B
calloc() initializes allocated memory to zero; malloc() does not
C
malloc() is faster than calloc()
D
calloc() allocates more memory than malloc()
Correct Answer:
B. calloc() initializes allocated memory to zero; malloc() does not
EXPLANATION
calloc() takes two parameters (count, size) and initializes memory to 0, while malloc() takes one parameter and leaves memory uninitialized.
Which header file must be included to use malloc(), calloc(), and free() functions?
A
#include
B
#include
C
#include
D
#include
Correct Answer:
B. #include
EXPLANATION
All memory allocation and deallocation functions are declared in <stdlib.h>.