Q: 1 What happens if a programmer forgets to release memory that was allocated using malloc() in C?
Compilation Error
Segmentation Fault
Memory Leak
Pointer Corruption
[ Option C ]
If memory allocated using malloc() is not released using free(), the program causes a Memory Leak. A memory leak occurs when dynamically allocated heap memory is no longer needed but is not deallocated, and there is no pointer left to access it.
As a result, that memory remains reserved and cannot be reused during the program’s execution. Over time, repeated memory leaks can consume large amounts of RAM and may eventually Slow Down or Crash the system.
Q: 2 Consider the following C program:
#include<stdio.h>
#include<stdlib.h>
void main()
{
int *ptr = (int *)malloc(3*sizeof(int));
for(int i = 0; i < 3; i++)
ptr[i] = i + 1;
ptr = (int *)realloc(ptr,5*sizeof(int));
for(int i = 3; i < 5; i++)
ptr[i] = i + 1;
free(ptr);
free(ptr);
}
The program runs correctly and prints values 1 to 5.
The program causes memory leak.
The program causes undefined behavior.
The program gives compilation error.
[ Option C ]
In the given program, memory is first allocated using malloc() for three integers and initialized correctly. Then realloc() is used to increase the memory size to store five integers, and the additional elements are assigned values properly. Up to this point, the program works correctly.
However, the problem occurs at the end where free(ptr); is called twice. The first free() correctly deallocates the dynamically allocated heap memory, but the second free() attempts to release the same memory again. This results in Undefined Behavior, meaning the program may crash, corrupt memory, or behave unpredictably.
Q: 3 What is the output of the following C code?
# include<stdio.h>
# include<stdlib.h>
void fun(int *a)
{
a = (int*)malloc(sizeof(int));
}
int main()
{
int *p;
fun(p);
*p = 9;
printf("%d",*p);
return(0);
}
It always works and prints 9
It always prints 0
It has undefined behaviour because p is not initialized to the allocated memory in fun()
It gives a compile-time error because malloc cannot be used inside a function
[ Option C ]
Here, the pointer p in main() is declared but not initialized. When fun(p) is called, the pointer is passed by value, meaning a copy of p is sent to the function. Inside fun(), memory is allocated to the local parameter a, but this change does not affect the original pointer p in main() because only a copy was modified. Therefore, after returning from fun(), the pointer p in main() still contains a garbage address.
When the statement *p=9; is executed, it attempts to dereference an uninitialized pointer, which results in undefined behaviour. The program may crash, may print some garbage value, or may behave unpredictably or may work on some systems.
Q: 4
Match the following:
| Group-A | Group-B |
|---|---|
| (X) m=malloc(5); m= NULL; | (1) Using Dangling Pointers. |
| (Y) free(n); n->value=5; | (2) Using Uninitialized Pointers. |
| (Z) char *p; *p=’a’; | (3) Memory Lost. |
(X)—(1) (Y)—(3) (Z)—(2)
(X)—(2) (Y)—(1) (Z)—(3)
(X)—(3) (Y)—(1) (Z)—(2)
(X)—(3) (Y)—(2) (Z)—(1)
[ Option C ]
In statement (X), memory is allocated using malloc(5), but immediately the pointer m is assigned NULL without freeing the allocated memory. This results is memory leak because the allocated heap memory no longer has any reference.
In statement (Y), memory is first freed using free(n);, but after freeing, the pointer is still used (n->value=5;). This is situation of Dangling Pointer, because the pointer refers to memory that has already been deallocated.
In statement (Z), a pointer p is declared but not initialized to any valid memory location. When *p='a'; is executed, it attempts to access memory through an uninitialized pointer, which leads to Undefined Behavior.
Q: 5 What is the output of following program?
#include<stdio.h>
#include<stdlib.h>
void main()
{
char *ptr=(char*)calloc(100,1);
ptr="Suraku Academy";
printf("%s",ptr);
}
Suraku
Suraku Academy
Address of ptr
Compile Time Error
[ Option B ]
Here first memory is allocated dynamically using calloc(100,1) and the returned address is stored in the pointer ptr.
However, in the next statement, ptr = "Suraku Academy"; the pointer is reassigned to point to a string literal instead of the allocated memory. This means the previously allocated memory becomes unused, but the pointer now correctly points to the string "Suraku Academy".
When printf("%s",ptr); is executed, it prints the string stored at the address pointed to by ptr, which is "Suraku Academy".
Q: 6 Consider the following three C functions:
[P1]
int* fun(void)
{
int x=10;
return(&x);
}
[P2]
int* fun(void)
{
int *px;
*px=10;
return px;
}
[P3]
int* fun(void)
{
int *px;
px = (int *)malloc(sizeof(int));
*px=10;
return px;
}
Which of the above three functions are likely to cause problems with pointers?
Only P3
Only P1 and P2
Only P1 and P3
Only P1, P2 and P3
[ Option B ]
[P1]
int* fun(void)
{
int x=10;
return(&x);
}
Here, x is a local variable stored on the stack. Once the function returns, x goes out of scope and its memory becomes invalid. Returning the address of such a local variable results in a Dangling Pointer, which can cause undefined behavior.
Hence, P1 causes a pointer problem.
[P2]
int* fun(void)
{
int *px;
*px=10;
return px;
}
In this case, px is an uninitialized pointer. No memory is allocated before dereferencing it using *px=10. This leads to undefined behavior and may cause a Segmentation Fault.
Hence, P2 causes a pointer problem.
[P3]
int* fun(void)
{
int *px;
px=(int *)malloc(sizeof(int));
*px=10;
return px;
}
Here, memory is correctly allocated using malloc(), the value is assigned properly, and the pointer returned is valid as long as the allocated memory is not freed.
Hence, P3 does NOT cause a pointer problem.
Q: 7 What is the problem with following code?
#include<stdio.h>
int main()
{
int *ptr = (int *)malloc(sizeof(int));
ptr=NULL;
free(ptr);
}
Memory Leak.
Compile Time Error.
Dangling Pointer.
Wild Pointer.
[ Option A ]
In the given code, memory is dynamically allocated using malloc() and its address is stored in the pointer ptr. Immediately after allocation, the pointer is set to NULL using ptr=NULL;. This causes the original memory address returned by malloc() to be lost, meaning there is no way to access or free that allocated memory anymore.
As a result, the allocated memory remains unused and cannot be reclaimed, which leads to a Memory Leak.
Calling free(ptr) after setting ptr to NULL has no effect, because freeing a NULL pointer is safe but does nothing.
Q: 8 Consider the following code?
int *p=malloc(5*sizeof(int));
p=realloc(p,0);
What happens?
Memory Remains Unchanged
Memory is Freed
Compilation Error
Stack Overflow
[ Option B ]
In C, when realloc(ptr, 0) is called, it behaves like free(ptr). This means the previously allocated memory block is deallocated, and the memory is returned to the heap.
According to the C standard, if the new size passed to realloc() is zero and the pointer is not NULL, the memory is released.
Now, consider the one more special case, if ptr is NULL and size is not 0, then realloc() behaves exactly like malloc(size). That means it allocates a new block of memory of the given size and returns a pointer to it.
| FUNCTION CALL | CONDITION | WHAT HAPPENS | EQUIVALENT TO |
|---|---|---|---|
| realloc(ptr, new_size); | ptr ≠ NULL and new_size > 0 | Resizes the existing memory block. Old data is preserved up to the minimum of old and new size. | Resize operation |
| realloc(ptr, 0); | ptr ≠ NULL | Frees the allocated memory block. | free(ptr); |
| realloc(NULL, size); | size > 0 | Allocates a new memory block of given size. | malloc(size); |
Q: 9 What is returned by malloc() if the allocation fails?
0
-1
NULL
Run-Time Error
[ Option C ]
In C, the malloc() function attempts to allocate the requested amount of memory from the heap. If the memory allocation is successful, it returns a pointer to the beginning (base address) of the allocated block. However, if the allocation fails for any reason, malloc() returns NULL.
The same rule applies to other dynamic memory allocation functions such as calloc() and realloc().
If calloc() fails to allocate memory, it also returns NULL. Similarly, realloc() returns NULL if it cannot resize the memory block.
Q: 10 What happens if free() is called on a pointer that was never dynamically allocated?
Nothing Happen
Compiler Warning
Undefined Behavior
Graceful Termination
[ Option C ]
In C, the free() function is used to release memory that was allocated using malloc(), calloc(), or realloc(). If free() is called on a pointer that was not dynamically allocated, the program results in Undefined Behavior.
Undefined behavior means there is no guarantee what will happen. The program may crash, corrupt memory, behave unpredictably, or sometimes even appear to work correctly. Since C gives full control to the programmer, it does not perform automatic safety checks in such situations.
Q: 11 What is the return type of malloc() function?
void*
int*
Pointer of allocated memory type
Depends on compiler
[ Option A ]
In C, the malloc() function returns a pointer of type void*, which represents a generic pointer to the allocated memory block. This pointer can be typecast to any data type pointer as required.
| DMA FUNCTION | DESCRIPTION | RETURN TYPE |
|---|---|---|
| malloc() | Allocates a single block of memory and uninitialized. | void* |
| calloc() | Allocates multiple blocks and initializes to zero. | void* |
| realloc() | Resizes previously allocated memory. May return a new address. | void* |
| free() | Deallocates allocated memory. Does not return any value. | void |
Q: 12 Which of the following is used for static allocation of memory in C?
malloc
realloc
calloc
None of the above
[ Option D ]
In C, Static Memory Allocation is done at compile time using fixed-size variables or arrays, whereas malloc(), calloc(), and realloc() are used for Dynamic Memory Allocation at runtime, so none of the given options represent static allocation.
Q: 13 The heap is used for?
Static Memory Allocation
To Share Data Between Controllers
Dynamic Memory Allocation
To Store Permanent Data
[ Option C ]
Memory allocation in programming divides into static (compile-time, fixed size) and dynamic (runtime, flexible size).
The heap is a region of memory used for dynamic memory allocation, where memory is allocated and deallocated at runtime as per program requirements.
In languages like C, functions such as malloc(), calloc(), realloc(), free() are used to manage heap memory.
Q: 14 Consider the following C program. In which memory segments are the variables a, b, c, and *d stored?
int a;
int b=22;
void main()
{
int c;
int *d=(int *)malloc(sizeof(int));
}
a, b, c, and *d are stored in stack segment.
a, b, and c are stored in stack segment. *d is stored on heap.
a is stored in BSS part of data segment, b is stored in DATA part of data segment and c is stored in stack segment. *d is stored on heap.
b is stored in BSS part of data segment, a is stored in DATA part of data segment and c is stored in stack segment. *d is stored on heap.
[ Option C ]
In C, memory is divided mainly into following segments:
(1) Text or Code Segment.
(2) Data Segment.
(3) Heap Segment.
(4) Free Space Segment.
(5) Stack segment.
(6) Unmapped Segment.
The variable a is declared globally but not initialized. Global uninitialized variables are stored in the BSS (Block Started by Symbol) segment. Therefore, a is stored in the BSS part of the data segment.
The variable b is also global but initialized with value 22. Global initialized variables are stored in the DATA segment. Hence, b is stored in the DATA part of the data segment.
Inside the main() function, c is a local variable. Local variables are always stored in the stack segment. Therefore, c is stored in the stack.
The pointer variable d itself is stored in the stack because it is a local variable. However, the memory allocated using malloc() (*d) is stored in the heap segment.
Therefore, finally:
Q: 15 Which function is used to delete the allocated memory space?
delete();
free();
dealloc();
remove();
[ Option B ]
In C, the free() function used to deallocate dynamically allocated memory. It releases the memory that was previously allocated using malloc(), calloc(), or realloc(), making it available for future use.
Q: 16 Which of the following statements is equivalent to the C statement:
ptr = calloc(m, n);
ptr = malloc(m * n);
ptr = malloc(m * n);
memset(ptr, 0, m * n);
ptr = malloc(m);
memset(ptr, 0, m);
ptr = malloc(n);
memset(ptr, 0, n);
[ Option B ]
The function calloc(m, n) allocates memory for m blocks, each of size n bytes, so the total memory allocated is m*n bytes. In addition to allocation, calloc() also initializes all the allocated memory to zero. Therefore, it performs two operations:
(1) Memory Allocation and
(2) Zero Initialization.
The statement ptr = malloc(m * n); memset(ptr, 0, m * n); does exactly the same work. First, malloc(m * n) allocates the required total memory, and then memset(ptr, 0, m * n) sets all the bytes to zero.
Option (A) is incorrect because malloc() does not initialize memory, it contains garbage values.
Options (C) and (D) are incorrect because they allocate only m or n bytes instead of m*n bytes.
THE memset() FUNCTION:
This function is used to fill a block of memory with a specific value. It is commonly used with malloc(), calloc(), arrays and structures.
The prototype of the function is void *memset(void *pointer, int value, size_t num); and available in <string.h> header file. It takes three arguments:
The function sets the first num bytes of the memory block pointed to by pointer with the given value and returns the same pointer.
The memset() works byte by byte. Even if you give an integer value like 5, it converts it to unsigned char and fills memory byte-wise.
#include<stdio.h>
#include<string.h>
void main()
{
int arr[5],i;
memset(arr,0,sizeof(arr));
for(i=0;i<5;i++)
printf("%d ",arr[i]);
}
OUTPUT
0 0 0 0 0
Here sizeof(arr) is 10 (5*2=10 bytes (assuming int occupy 2 bytes)). So, all 10 bytes are set to 0.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
int *arr,i;
arr=(int*)malloc(5*sizeof(int));
memset(arr,0,5*sizeof(int));
for(i=0;i<5;i++)
printf("%d ",arr[i]);
free(arr);
}
OUTPUT
0 0 0 0 0
This makes malloc() behave like calloc().
Q: 17 What is the output of the below code?
void main()
{
int *ptr;
ptr=(int *)calloc(1,sizeof(int));
*ptr=101;
printf("%d",*ptr);
}
10
0
101
5
[ Option C ]
Here, memory for one integer is allocated dynamically using calloc(1, sizeof(int)). Although calloc() initializes the allocated memory to zero, the next statement assigns the value 101 to the allocated memory using *ptr = 101;.
Therefore, when printf("%d",*ptr); is executed, it prints the value stored at that memory location, which is 101.
Q: 18 Which of the following is/are true?
calloc() allocates the memory and also initializes the allocates memory to zero, while memory allocated using malloc() has random data.
calloc() takes two arguments, but malloc takes only 1 argument.
Both malloc() and calloc() return void* pointer.
All of the above
[ Option D ]
The function calloc() not only allocates memory but also initializes all allocated bytes to zero, whereas malloc() only allocates memory and leaves it uninitialized, meaning it may contain garbage values.
Secondly, calloc() takes two arguments, the number of elements and the size of each element while malloc() takes only one argument, which is the total number of bytes to allocate.
Finally, both malloc() and calloc() return a void* pointer, which can be typecast to the required pointer type.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.