Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

490 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 71–80 of 490
Topics in C Programming
Q.71 Medium Dynamic Memory
What happens if you call free() twice on the same pointer?
A Program immediately crashes
B Double free error - undefined behavior
C Memory is freed twice successfully
D No effect, second free is ignored
Correct Answer:  B. Double free error - undefined behavior
EXPLANATION

Double free is undefined behavior and can cause program crash or corruption. After first free(), the pointer should not be used.

Test
Q.72 Medium Dynamic Memory
Consider a program that repeatedly allocates memory in a loop but never frees it. What problem occurs?
A Stack overflow
B Memory leak - heap memory is exhausted
C Segmentation fault immediately
D Integer overflow
Correct Answer:  B. Memory leak - heap memory is exhausted
EXPLANATION

Allocating without freeing causes memory leak, where allocated memory is not returned to the system, eventually exhausting heap.

Test
Q.73 Medium Dynamic Memory
A competitive programmer allocates a 2D array dynamically. What is the correct approach?
A int **arr = (int**)malloc(rows * sizeof(int*)); for(i=0; i
B int arr[rows][cols] = (int**)malloc(...);
C int **arr = calloc(rows*cols, sizeof(int));
D int arr[][] = malloc(rows * cols);
Correct Answer:  A. int **arr = (int**)malloc(rows * sizeof(int*)); for(i=0; i
EXPLANATION

2D arrays are allocated as array of pointers, where each row pointer points to a dynamically allocated array.

Test
Q.74 Medium Dynamic Memory
What is the correct way to allocate a dynamic array of struct for 'n' elements?
struct Node { int data; char name[20]; };
A struct Node *arr = malloc(n);
B struct Node *arr = malloc(n * sizeof(struct Node));
C struct Node arr = malloc(n);
D struct Node *arr = calloc(n, 1);
Correct Answer:  B. struct Node *arr = malloc(n * sizeof(struct Node));
EXPLANATION

Must allocate n * sizeof(struct Node) bytes to hold all n structures.

Test
Q.75 Medium Dynamic Memory
In competitive exams, why is dynamic allocation preferred over static arrays for unknown input sizes?
A It's faster than static arrays
B Allows allocation only as much memory as needed, preventing stack overflow
C Static arrays don't work in C
D It's automatically freed by compiler
Correct Answer:  B. Allows allocation only as much memory as needed, preventing stack overflow
EXPLANATION

Dynamic allocation allocates memory on heap based on runtime input, preventing fixed-size limitations and stack overflow.

Test
Q.76 Medium Dynamic Memory
If realloc() cannot expand memory at the current location, what does it do?
A Fails and returns NULL
B Allocates new memory, copies old data, frees old memory, returns new pointer
C Extends memory anyway
D Keeps old pointer valid
Correct Answer:  B. Allocates new memory, copies old data, frees old memory, returns new pointer
EXPLANATION

realloc() finds new space if needed, copies data, deallocates old block, and returns new pointer.

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

calloc() initializes all allocated memory to zero, so all elements will be 0.

Test
Q.78 Medium Dynamic Memory
Which scenario best demonstrates a memory leak?
A char *s = malloc(10); free(s);
B char *s = malloc(10); s = malloc(20);
C char *s = malloc(10); free(s); free(s);
D char *s = NULL; free(s);
Correct Answer:  B. char *s = malloc(10); s = malloc(20);
EXPLANATION

Original malloc(10) pointer is lost before freeing. The 10 bytes are now inaccessible - a memory leak.

Test
Q.79 Medium Dynamic Memory
What happens if you use a pointer after calling free() on it (without reallocating)?
A Program runs normally
B Pointer becomes NULL
C Use-after-free: undefined behavior, possible crash or data corruption
D Compiler automatically prevents it
Correct Answer:  C. Use-after-free: undefined behavior, possible crash or data corruption
EXPLANATION

After free(), accessing the pointer causes undefined behavior as memory is returned to the heap and may be reused.

Test
Q.80 Medium Dynamic Memory
In dynamic 2D array creation:
int **arr = (int**)malloc(m * sizeof(int*));
what does the first malloc() allocate?
A m rows of integers
B An array of m pointers to int
C m*n integers
D Memory for entire 2D array at once
Correct Answer:  B. An array of m pointers to int
EXPLANATION

First malloc allocates an array of m pointers. Each pointer must be individually allocated in a loop for actual row data.

Test
IGET
IGET AI
Online · Exam prep assistant
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