Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

499 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 91–100 of 499
Topics in C Programming
Q.91 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.92 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.93 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.94 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.95 Medium Dynamic Memory
What is the output?
int *p = malloc(sizeof(int) * 3);
p[0] = 1; p[1] = 2; p[2] = 3;
printf("%d", *(p+2));
A 1
B 2
C 3
D Compilation error
Correct Answer:  C. 3
EXPLANATION

*(p+2) is equivalent to p[2], which contains 3. Pointer arithmetic works with dynamic arrays.

Take Test
Q.96 Medium Dynamic Memory
Analyze this code:
char *str = malloc(5);
strcpy(str, "Hello");
How many bytes should malloc allocate for safety?
A 5
B 6
C 4
D 7
Correct Answer:  B. 6
EXPLANATION

String "Hello" has 5 characters plus 1 null terminator (\0), requiring 6 bytes total.

Take Test
Q.97 Medium Dynamic Memory
What is double free error?
A Allocating memory twice
B Calling free() on already freed memory
C Using two pointers to same memory
D Allocating memory twice and freeing once
Correct Answer:  B. Calling free() on already freed memory
EXPLANATION

Double free error occurs when free() is called on the same memory block twice, causing undefined behavior and program crash.

Take Test
Q.98 Medium Dynamic Memory
Which of the following is correct for dynamic array allocation?
A int arr[n]; where n is variable
B int *arr = malloc(n * sizeof(int));
C int arr = malloc(n);
D int *arr[n];
Correct Answer:  B. int *arr = malloc(n * sizeof(int));
EXPLANATION

Dynamic array allocation requires pointer and malloc() with proper size calculation. VLAs are not standard C.

Take Test
Q.99 Medium Dynamic Memory
What is the purpose of realloc() function?
A To allocate new memory
B To resize previously allocated memory block
C To free memory
D To initialize memory
Correct Answer:  B. To resize previously allocated memory block
EXPLANATION

realloc() resizes a previously allocated memory block. It can increase or decrease the size and preserves existing data.

Take Test
Q.100 Medium Dynamic Memory
What happens when you try to access memory after calling free()?
A Automatic reallocation
B Use-after-free error (undefined behavior)
C Returns NULL
D Program terminates gracefully
Correct Answer:  B. Use-after-free error (undefined behavior)
EXPLANATION

Accessing freed memory results in undefined behavior and is a critical bug. The memory may be reallocated for other purposes.

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