Explore topic-wise MCQs in Memory Allocation.

This section includes 63 Mcqs, each offering curated multiple-choice questions to sharpen your Memory Allocation knowledge and support exam preparation. Choose a topic below to get started.

1.

On freeing a dynamic memory, if the pointer value is not modified, then the pointer points to?

A. NULL
B. Other dynamically allocated memory
C. The same deallocated memory location
D. It points back to location it was initialized with
Answer» D. It points back to location it was initialized with
2.

Which of the following will return a result most quickly for searching a given key?

A. Unsorted Array
B. Sorted Array
C. Sorted linked list
D. Binary Search Tree
Answer» E.
3.

Which one is used during memory deallocation in C?

A. remove(p);
B. delete(p);
C. free(p);
D. terminate(p);
Answer» D. terminate(p);
4.

Why do we write (int *) before malloc? int *ip = (int *)malloc(sizeof(int));

A. It is for the syntax correctness
B. It is for the type-casting
C.
D. D.
Answer» C.
5.

Memory allocation using malloc() is done in?

A. Static area
B. Stack area
C. Heap area
D. Both b & c
Answer» D. Both b & c
6.

What is the output of this C code? void main() { char *p = calloc(100, 1); p = "welcome"; printf("%s n", p); }

A. Segmentation fault
B. garbage
C. Error
D. welcome
Answer» E.
7.

In function free(p), p is a:

A. int
B. Pointer returned by malloc()
C. Pointer returned by calloc()
D. Both b & c
Answer» E.
8.

calloc() returns a storage that is initialized to.

A. Zero
B. Null
C. Nothing
D.
Answer» B. Null
9.

Which function is used to delete the allocated memory space?

A. Dealloc()
B. free()
C. Both A and B
D. None of the above
Answer» C. Both A and B
10.

Among 4 header files, which should be included to use the memory allocation functions like malloc(), calloc(), realloc() and free()?

A. #include
B. #include
C. #include
D. Both b and c
Answer» C. #include
11.

What is the return type of malloc() or calloc()?

A. int *
B. int **
C. void *
D. void **
Answer» D. void **
12.

Point out the error of the following code #include #include #include int main() { char* ptr; *ptr = (int*)malloc(30); strcpy(ptr, "ABC"); printf("%s", ptr); free(ptr); }

A. Error in strcpy() statement
B. Error in *ptr = (int*)malloc(30);
C. Error in free(ptr)
D. No Error
Answer» C. Error in free(ptr)
13.

Purpose of using fflush() function

A. To flush all streams and specified streams
B. To flush only specified streams
C. To flush input-output buffer
D. This is invalid library function
Answer» B. To flush only specified streams
14.

What will be the output of the following code #include #include int main() { union my_union { int i; float f; char c; }; union my_union* u; u = (union my_union*)malloc(sizeof(union my_union)); u->f = 20.60f; prin

A. Garbage Value
B. 20.600000
C. Syntax Error
D. 20.6
Answer» C. Syntax Error
15.

What is the correct sequence of the compilation Process

A. Assembler, Compiler, Preprocessor, Linking
B. Compiler, Assembler, Preprocessor, Linking
C. Preprocessor, Compiler, Assembler, Linking
D. Assembler, Compiler, Linking, Preprocessor
Answer» D. Assembler, Compiler, Linking, Preprocessor
16.

Which of the following is an example of static memory allocation?

A. Linked list
B. Stack
C. Queue
D. Array
Answer» E.
17.

Array is preferred over linked list for the implementation of ________

A. Radix sort
B. Insertion sort
C. Binary search
D. Polynomial evaluation
Answer» D. Polynomial evaluation
18.

We use malloc and calloc for

A. Dynamic memory allocation
B. Static memory allocation
C. Both dynamic and static memory allocation
D. None of the above
Answer» B. Static memory allocation
19.

What is the problem with following code? #include int main() { int *p = (int *)malloc(sizeof(int)); p = NULL; free(p); }

A. Compiler Error: free can't be applied on NULL pointer
B. Memory Leak
C. Dangling Pointer
D. The program may crash as free() is called for NULL pointer
Answer» C. Dangling Pointer
20.

Consider the following program, where are i, j and k are stored in memory? int i; int main() { int j; int *k = (int *) malloc (sizeof(int)); }

A. i, j and *k are stored in stack segment
B. i and j are stored in stack segment. *k is stored on heap
C. i is stored in BSS part of data segment, j is stored in stack segment. *k is stored on heap
D. j is stored in BSS part of data segment, i is stored in stack segment. *k is stored on heap
Answer» D. j is stored in BSS part of data segment, i is stored in stack segment. *k is stored on heap
21.

How many bytes of memory will the following code reserve? #include #include int main() { int *p; p = (int *)malloc(256 * 256); if(p == NULL) printf("Allocation failed"); return 0; }

A. 65536
B. Allocation failed
C. C.
D. D.
E.
Answer» C. C.
22.

What will be the output of the program? #include #include int main() { union test { int i; float f; char c; }; union test *t; t = (union test *)malloc(sizeof(union test)); t->f = 10.10f; printf("%f", t->f); return 0; }

A. 10
B. Garbage value
C. C.
D.
E. Error
Answer» D.
23.

What will be the output of the program? #include #include int main() { int *p; p = (int *)malloc(20); /* Assume p has address of 1314 */ free(p); printf("%u", p); return 0; }

A. 1314
B. Garbage value
C. 1316
D. Random address
Answer» B. Garbage value
24.

What will be the output of the program (16-bit platform)? #include #include int main() { int *p; p = (int *)malloc(20); printf("%d n", sizeof(p)); free(p); return 0; }

A. 4
B. 2
C. 7
D. Garbage value
Answer» C. 7
25.

What will be the output of the program? #include #include int main() { char *s; char *fun(); s = fun(); printf("%s n", s); return 0; } char *fun() { char buffer[30]; strcpy(buffer, "RAM"); return (buffer); }

A. 0xffff
B. Garbage value
C. 0xffee
D. Error
Answer» C. 0xffee
26.

What is the return type of malloc() or calloc()

A. void *
B. Pointer of allocated memory type
C. void **
D. int *
Answer» B. Pointer of allocated memory type
27.

Output? # include # include void fun(int *a) { a = (int*)malloc(sizeof(int)); } int main() { int *p; fun(p); *p = 6; printf("%dn",*p); return(0); }

A. May not work
B. Works and prints 6
Answer» B. Works and prints 6
28.

The most appropriate matching for the following pairs 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. lost memory is:

A. X 1 Y 3 Z-2
B. (X 2 Y 1 Z-3
C. X 3 Y 2 Z-1
D. X 3 Y 1 Z-2
Answer» E.
29.

Consider the following three C functions : [PI] int * g (void) { int x= 10; return (&x); } [P2] int * g (void) { int * px; *px= 10; return px; } [P3] int *g (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?

A. Only P3
B. Only P1 and P3
C. Only P1 and P2
D. P1, P2 and P3
Answer» D. P1, P2 and P3
30.

The function ____ obtains block of memory dynamically.

A. calloc
B. malloc
C. Both a & b
D. free
Answer» D. free
31.

Point out the error in the following program. #include #include int main() { char *ptr; *ptr = (char)malloc(30); strcpy(ptr, "RAM"); printf("%s", ptr); free(ptr); return 0; }

A. Error: in strcpy() statement
B. Error: in *ptr = (char)malloc(30);
C. Error: in free(ptr);
D. No Error
Answer» C. Error: in free(ptr);
32.

Consider the following program, where are i, j and k are stored in memory? #include #include int i; int main() { int j; int *k = (int *) malloc (sizeof(int)); }

A. i, j and *k are stored in stack segment
B. i and j are stored in stack segment. *k is stored on heap.
C. i is stored in BSS part of data segment, j is stored in stack segment. *k is stored on heap.
D. j is stored in BSS part of data segment, i is stored in stack segment. *k is stored on heap.
Answer» D. j is stored in BSS part of data segment, i is stored in stack segment. *k is stored on heap.
33.

How will you free the memory allocated by the following program? #include #include #define MAXROW 2 #define MAXCOL 3 int main() { int **p, i, j; p = (int **) malloc(MAXROW * sizeof(int*)); return 0; }

A. memfree(int p);
B. dealloc(p);
C. malloc(p, 0);
D. free(p);
Answer» E.
34.

What is the Error of this program? #include #include int main() { char *ptr; *ptr = (char)malloc(30); strcpy(ptr, "RAM"); printf("%s", ptr); free(ptr); return 0; }

A. Error: in strcpy() statement.
B. Error: in *ptr = (char)malloc(30);
C. Error: in free(ptr);
D. No error
Answer» C. Error: in free(ptr);
35.

Which statment is true about the given code ? #include #include int main() { int *a[5]; a = (int*) malloc(sizeof(int)*5); free(a); return 0; }

A. Error: unable to allocate memory
B. Error: We cannot store address of allocated memory in a
C. Error: unable to free memory
D. No error
Answer» C. Error: unable to free memory
36.

What is the output of this program? #include #include int main() { int *p; p = (int *)malloc(40); printf("%d", sizeof(p)); free(p); return 0; }

A. 40
B. 50
C. 30
D. 20
Answer» D. 20
37.

What is the output of this program? #include #include int main() { struct test { int i; float f; char c; }; struct test *ptr; ptr = (struct test *)malloc(sizeof(struct test)); ptr ->f = 2.5f; printf("%f", ptr->f); return 0; }

A. Compilation error
B. 2.5
C. Garbage value
Answer» C. Garbage value
38.

What is the output of this program? #include #include int main() { int i; char *ptr; char *fun(); ptr = fun(); printf(" %s", ptr); return 0; } char *fun() { char disk[30]; strcpy(disk, "letsfindcourse"); printf("%s ",disk); return disk; }

A. letsfindcourse
B. Compilation error
C. letsfindcourse letsfindcourse
D. garbage value
Answer» B. Compilation error
39.

What is the output of this program? #include void main() { int *ptr = (int *)malloc(sizeof(int)); *ptr = 10; free(ptr); p = 5; printf("%d", ptr); }

A. Compilation error
B. 5
C. Garbage value
Answer» C. Garbage value
40.

What is the output of this program? #include #include int main() { int *j = (int*)malloc(4 * sizeof(int)); *j = 15; free(j); printf("%d", *j); return 0; }

A. Compilation error
B. Some Garbage value
C. Nothing prints
Answer» C. Nothing prints
41.

What is the output of this program? #include #include int main() { int *numbers = (int*)calloc(4, sizeof(int)); numbers[0] = 2; free(numbers); printf("Stored integers are "); printf("numbers[%d] = %d ", 0, numbers[0]); return 0; }

A. Garbage value
B. 2
C. Compilation error
Answer» A. Garbage value
42.

What is the output of this program? #include #include int main() { int i, numbers[1]; numbers[0] = 15; free(numbers); printf("Stored integers are "); printf("numbers[%d] = %d ", 0, numbers[0]); return 0; }

A. 15
B. Compilation error
C. garbage value
Answer» B. Compilation error
43.

malloc() returns a float pointer if memory is allocated for storing float's and a double pointer if memory is allocated for storing double's. A.

A. TRUE
B. FALSE
C. May Be
D. Can't Say
Answer» C. May Be
44.

Which languages necessarily need heap allocation in the run time environment?

A. Those that support recursion
B. Those that use dynamic scoping
C. Those that use global variables
D. Those that allow dynamic data structures
Answer» E.
45.

Which of the following statement is correct prototype of the malloc() function in c ?

A. int* malloc(int);
B. Char* malloc(char);
C. unsigned int* malloc(unsigned int);
D. void* malloc(size_t);
Answer» E.
46.

Which of the following is/are true

A. calloc() allocates the memory and also initializes the allocates memory to zero, while memory allocated using malloc() has random data.
B. malloc() and memset() can be used to get the same effect as calloc()
C. Both malloc() and calloc() return 'void *' pointer
D. All of the above
Answer» E.
47.

What function should be used to free the memory allocated by calloc() ?

A. dealloc();
B. B.
C. C.
D.
E. memalloc(variable_name, 0)
Answer» D.
48.

Which is the correct sequence of compilation process?

A. Assembler -> Compiler -> Preprocessor -> Linking
B. Compiler -> Assenbler -> Preprocessor -> Linking
C. Preprocessor -> Compiler -> Assembler -> Linking
D. Assembler -> Compiler -> Linking -> Preprocessor
Answer» D. Assembler -> Compiler -> Linking -> Preprocessor
49.

Why is calloc() function used for?

A. allocates the specified number of bytes
B. allocates the specified number of bytes and initializes them to zero
C. increases or decreases the size of the specified block of memory and reallocates it if needed
D. calls the specified block of memory for execution
Answer» C. increases or decreases the size of the specified block of memory and reallocates it if needed
50.

Why to use fflush() library function?

A. To flush all streams and specified streams
B. To flush only specified stream
C. To flush input/output buffer
D. Invalid library function
Answer» B. To flush only specified stream