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 141–150 of 1,000
Topics in C Programming
Q.141 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.142 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.143 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.144 Easy Dynamic Memory
Which of the following correctly allocates memory for an array of 5 integers and initializes to zero?
A int *p = malloc(5 * sizeof(int));
B int *p = calloc(5, sizeof(int));
C int *p = realloc(NULL, 5);
D int p[5] = {0};
Correct Answer:  B. int *p = calloc(5, sizeof(int));
EXPLANATION

calloc(5, sizeof(int)) allocates memory for 5 integers and initializes all to zero.

Take Test
Q.145 Easy Dynamic Memory
What is the primary difference between malloc() and calloc()?
A malloc() is faster than calloc()
B calloc() initializes memory with zeros, malloc() does not
C malloc() allocates from stack, calloc() from heap
D calloc() requires only one argument
Correct Answer:  B. calloc() initializes memory with zeros, malloc() does not
EXPLANATION

calloc() allocates memory and initializes all bytes to zero, while malloc() leaves the allocated memory uninitialized.

Take Test
Q.146 Easy Dynamic Memory
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().

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

Take Test
Q.148 Hard Dynamic Memory
What is the typical behavior if malloc() is called in a loop without corresponding free() calls?
A Program automatically clears memory
B Memory leaks accumulate, eventually causing program crash
C Compiler optimizes and prevents leaks
D Memory is recycled automatically
Correct Answer:  B. Memory leaks accumulate, eventually causing program crash
EXPLANATION

Without freeing, allocated memory accumulates until system runs out of heap space, causing allocation failure or crash.

Take Test
Q.149 Hard Dynamic Memory
For a program handling 10^6 integers dynamically, which allocation is most appropriate?
A int arr[1000000];
B int *arr = malloc(1000000 * sizeof(int));
C static int arr[1000000];
D auto int arr[1000000];
Correct Answer:  B. int *arr = malloc(1000000 * sizeof(int));
EXPLANATION

Stack allocation (option A) will cause stack overflow. Dynamic allocation on heap is required for large data.

Take Test
Q.150 Hard Dynamic Memory
What distinguishes a dangling pointer?
A A pointer that points to deallocated memory
B A pointer initialized to NULL
C A pointer that hasn't been malloc'd
D A pointer that points to stack variable
Correct Answer:  A. A pointer that points to deallocated memory
EXPLANATION

Dangling pointer points to memory that has been freed. Accessing it causes undefined behavior.

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