Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

1,000 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 171–180 of 1,000
Topics in C Programming
Q.171 Medium Dynamic Memory
Which allocation method is most suitable for a dynamically growing linked list?
A Static array allocation
B malloc() for each node as needed
C calloc() all nodes upfront
D Stack allocation
Correct Answer:  B. malloc() for each node as needed
EXPLANATION

Each node should be allocated individually with malloc() to allow dynamic growth and efficient memory usage.

Take Test
Q.172 Hard Dynamic Memory
What is a common mistake in this code?
void func() {
int *ptr;
ptr = malloc(sizeof(int) * 5);
func2(ptr);
free(ptr);
}
void func2(int *p) {
free(p);
}
A Double free error - memory freed in both functions
B Memory leak - malloc without free
C Buffer overflow
D Segmentation fault guaranteed
Correct Answer:  A. Double free error - memory freed in both functions
EXPLANATION

Memory is freed in func2(), then again in func(), causing double free error.

Take Test
Q.173 Medium Dynamic Memory
Consider reallocating memory:
int *p = malloc(10);
p = realloc(p, 20);
A The original 10 bytes are lost
B realloc() preserves existing data and allocates additional memory
C realloc() always returns a new address
D Memory at address p is deallocated first
Correct Answer:  B. realloc() preserves existing data and allocates additional memory
EXPLANATION

realloc() extends the existing allocation, preserving original data, and may return the same or different address.

Take Test
Q.174 Medium Dynamic Memory
What is the output of this code?
int *p = calloc(3, sizeof(int));
printf("%d %d %d", p[0], p[1], p[2]);
A Random values
B 0 0 0
C Garbage, Garbage, Garbage
D Compilation error
Correct Answer:  B. 0 0 0
EXPLANATION

calloc() initializes all allocated bytes to 0, so all integers are 0.

Take Test
Q.175 Easy Dynamic Memory
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.

Take Test
Q.176 Easy Dynamic Memory
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.

Take Test
Q.177 Medium Dynamic Memory
Analyze the memory leak in this code:
void func() {
int *p = malloc(100);
if(condition) return;
free(p);
}
A No memory leak; free() is called before return
B Memory leak occurs when condition is true (p is not freed)
C Memory leak occurs in all cases
D No leak, malloc() automatically frees memory
Correct Answer:  B. Memory leak occurs when condition is true (p is not freed)
EXPLANATION

When condition is true, the function returns without calling free(), causing a memory leak.

Take Test
Q.178 Medium Dynamic Memory
What is the purpose of typecasting in malloc()?
int *p = (int*)malloc(sizeof(int));
A It is mandatory in C
B It improves memory allocation speed
C It improves code readability and is good practice (optional in C, mandatory in C++)
D It prevents memory leaks
Correct Answer:  C. It improves code readability and is good practice (optional in C, mandatory in C++)
EXPLANATION

Typecasting malloc() is optional in C but improves readability. It's mandatory in C++.

Take Test
Q.179 Medium Dynamic Memory
Which of the following correctly allocates memory for an array of 5 strings (each max 20 characters)?
A char *str = malloc(5 * 20);
B char **str = malloc(5 * sizeof(char*)); for(int i=0; i
C char str[5][20];
D Both B and C are valid approaches
Correct Answer:  D. Both B and C are valid approaches
EXPLANATION

Option B uses dynamic allocation (2D array), Option C uses static allocation. Both are valid but for different scenarios.

Take Test
Q.180 Easy Dynamic Memory
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.

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