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 71–80 of 499
Topics in C Programming
Q.71 Medium Dynamic Memory
In a competitive programming scenario, you need to store strings dynamically. What is safe practice?
A char *str = malloc(100); without checking for NULL
B char *str = malloc(strlen(input)+1); with NULL check and strncpy
C char str[100] always on stack
D No allocation needed, use global arrays
Correct Answer:  B. char *str = malloc(strlen(input)+1); with NULL check and strncpy
EXPLANATION

Safe practice includes allocating exact size (input length + 1 for null terminator), checking for NULL, and using bounded string functions.

Take Test
Q.72 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.

Take Test
Q.73 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.

Take Test
Q.74 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.

Take Test
Q.75 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.

Take Test
Q.76 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.

Take Test
Q.77 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.

Take Test
Q.78 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.

Take Test
Q.79 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.

Take Test
Q.80 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.

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