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 191–200 of 1,000
Topics in C Programming
Q.191 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.192 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.193 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
Q.194 Medium Dynamic Memory
What is a memory leak in C?
A Accessing freed memory
B Allocated memory that is never freed
C Memory overflow
D Stack overflow
Correct Answer:  B. Allocated memory that is never freed
EXPLANATION

A memory leak occurs when allocated memory is not freed, consuming system resources without releasing them back.

Take Test
Q.195 Easy Dynamic Memory
Which function is used to free dynamically allocated memory?
A delete()
B release()
C free()
D unalloc()
Correct Answer:  C. free()
EXPLANATION

free() is the standard function to deallocate memory previously allocated by malloc(), calloc(), or realloc().

Take Test
Q.196 Medium Dynamic Memory
What will be the output of the following code?
int *ptr = malloc(sizeof(int));
if(ptr == NULL) printf("Failed");
else printf("Success");
A Failed
B Success
C Compilation Error
D Runtime Error
Correct Answer:  B. Success
EXPLANATION

In typical scenarios, malloc() successfully allocates memory and returns non-NULL pointer, printing 'Success'. NULL check is good practice for error handling.

Take Test
Q.197 Easy Dynamic Memory
What does calloc() do differently from malloc()?
A Allocates more memory
B Initializes memory to zero
C Allocates memory faster
D Returns integer instead of void pointer
Correct Answer:  B. Initializes memory to zero
EXPLANATION

calloc() takes two arguments (number of elements and size) and initializes all allocated bytes to zero, unlike malloc().

Take Test
Q.198 Easy Dynamic Memory
Which header file is required for dynamic memory allocation functions?
A
B
C
D
Correct Answer:  B.
EXPLANATION

The <stdlib.h> header file contains declarations for malloc(), calloc(), realloc(), and free() functions.

Take Test
Q.199 Easy Dynamic Memory
What is the return type of malloc() function?
A int*
B void*
C char*
D NULL
Correct Answer:  B. void*
EXPLANATION

malloc() returns a void pointer (void*) that can be cast to any pointer type.

Take Test
Q.200 Easy Dynamic Memory
Which function is used to allocate memory dynamically in C?
A malloc()
B alloc()
C allocate()
D memalloc()
Correct Answer:  A. malloc()
EXPLANATION

malloc() is the standard C function for dynamic memory allocation. It returns a void pointer to the allocated memory.

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