Explore topic-wise MCQs in C Programming.

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

1.

Assume integer is 2 bytes wide. What will be the output of the following code?

A. 2, 8
B. 4, 16
C. 8, 24
D. 16, 32
Answer» B. 4, 16
2.

How many bytes of memory will the following code reserve?

A. 65536
B. Allocation failed
C. Error
D. No output
Answer» C. Error
3.

returns a pointer if memory is allocated for storing 's and a if memory is allocated for storing 's.

A. True
B. False
Answer» C.
4.

allocates memory from the heap and not from the stack.

A. True
B. False
Answer» B. False
5.

returns a if it fails to allocate the requested memory.

A. True
B. False
Answer» B. False
6.

If successfully allocates memory it returns the number of bytes it has allocated.

A. True
B. False
Answer» C.
7.

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

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

Point out the correct statement which correctly free the memory pointed to by 's' and 'p' in the following program?

A. free(p); , free(p->s);
B. free(p->s); , free(p);
C. free(p->s);
D. free(p);
Answer» C. free(p->s);
9.

What will be the output of the program (16-bit platform)?

A. 4
B. 2
C. 8
D. Garbage value
Answer» C. 8
10.

Assume integer is 2 bytes wide. How many bytes will be allocated for the following code?

A. 56 bytes
B. 128 bytes
C. 24 bytes
D. 12 bytes
Answer» D. 12 bytes
11.

Point out the correct statement which correctly allocates memory dynamically for 2D array following program?

A. p = (int*) malloc(3, 4);
B. p = (int*) malloc(3*sizeof(int));
C. p = malloc(3*4*sizeof(int));
D. p = (int*) malloc(3*4*sizeof(int));
Answer» E.
12.

Which header file should be included to use functions like and ?

A. memory.h
B. stdlib.h
C. string.h
D. dos.h
Answer» C. string.h
13.

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

A. dealloc();
B. malloc(variable_name, 0)
C. free();
D. memalloc(variable_name, 0)
Answer» D. memalloc(variable_name, 0)
14.

How will you free the memory allocated by the following program?

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

Point out the correct statement which correctly free the memory pointed to by 's' and 'p' in the following program?_x000D_ #include_x000D_ #include_x000D_ _x000D_ int main()_x000D_ {_x000D_ struct ex_x000D_ {_x000D_ int i;_x000D_ float j;_x000D_ char *s_x000D_ };_x000D_ struct ex *p;_x000D_ p = (struct ex *)malloc(sizeof(struct ex));_x000D_ p->s = (char*)malloc(20);_x000D_ return 0;_x000D_ }

A. free(p); , free(p->s);
B. free(p->s); , free(p);
C. free(p->s);
D. free(p);
Answer» C. free(p->s);
16.

Point out the error in the following program._x000D_ #include_x000D_ #include_x000D_ _x000D_ int main()_x000D_ {_x000D_ int *a[3];_x000D_ a = (int*) malloc(sizeof(int)*3);_x000D_ free(a);_x000D_ return 0;_x000D_ }

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
17.

Assume integer is 2 bytes wide. How many bytes will be allocated for the following code?_x000D_ #include_x000D_ #include_x000D_ #define MAXROW 3_x000D_ #define MAXCOL 4_x000D_ _x000D_ int main()_x000D_ {_x000D_ int (*p)[MAXCOL];_x000D_ p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));_x000D_ return 0;_x000D_ }

A. 56 bytes
B. 128 bytes
C. 24 bytes
D. 12 bytes
Answer» D. 12 bytes
18.

The advantage of using linked lists over arrays is that ________

A. Linked list is an example of linear data structure
B. Insertion and deletion of an element can be done at any position in a linked list
C. Linked list can be used to store a collection of homogenous and heterogeneous data types
D. The size of a linked list is fixed
Answer» C. Linked list can be used to store a collection of homogenous and heterogeneous data types
19.

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
20.

The free() function frees the memory state pointed to by a pointer and returns ___________

A. the same pointer
B. the memory address
C. no value
D. an integer value
Answer» D. an integer value
21.

Garbage collector frees the programmer from worrying about ___________

A. Dangling pointers
B. Creating new objects
C. Memory leak
D. Segmentation errors
Answer» D. Segmentation errors
22.

In the function realloc(), if the new size of the memory block is larger than the old size, then the added memory ___________

A. is initialized to junk values
B. is initialized to zero
C. results in an error
D. is not initialized
Answer» E.
23.

When the pointer is NULL, then the function realloc is equivalent to the function ___________

A. malloc
B. calloc
C. free
D. alloc
Answer» B. calloc
24.

In the function malloc(), each byte of allocated space is initialized to zero.

A. True
B. False
Answer» C.
25.

Suppose we have a one dimensional array, named 'x', which contains 10 integers. Which of the following is the correct way to allocate memory dynamically to the array 'x' using malloc()?

A. x=(int*)malloc(10);
B. x=(int*)malloc(10,sizeof(int));
C. x=malloc(int 10,sizeof(int));
D. x=(int*)malloc(10*sizeof(int));
Answer» E.
26.

If the space in memory allocated by malloc is not sufficient, then an allocation fails and returns ___________

A. NULL pointer
B. Zero
C. Garbage value
D. The number of bytes available
Answer» B. Zero
27.

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. Error
D. No output
Answer» C. Error
28.

Point out the correct statement which correctly allocates memory dynamically for 2D array following program? #include #include int main() { int *p, i, j; /* Add statement here */ for(i=0; i<3; i++) { for(j=0; j<4; j++) { p[i*4+j] = i; printf("%d", p[i*4+j]); } } return 0; }

A. p = (int*) malloc(3, 4);
B. p = (int*) malloc(3*sizeof(int));
C. p = malloc(3*4*sizeof(int));
D. p = (int*) malloc(3*4*sizeof(int));
Answer» E.
29.

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. 8
D. Garbage value
Answer» C. 8
30.

malloc() returns a NULL if it fails to allocate the requested memory.

A. 1
B.
Answer» B.
31.

Assume integer is 2 bytes wide. How many bytes will be allocated for the following code? #include #include #define MAXROW 3 #define MAXCOL 4 int main() { int (*p)[MAXCOL]; p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p)); return 0; }

A. 56 bytes
B. 128 bytes
C. 24 bytes
D. 12 bytes
Answer» D. 12 bytes
32.

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. 10.1
D. Error
Answer» D. Error
33.

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);
34.

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
35.

Point out the correct statement which correctly free the memory pointed to by 's' and 'p' in the following program? #include #include int main() { struct ex { int i; float j; char *s }; struct ex *p; p = (struct ex *)malloc(sizeof(struct ex)); p->s = (char*)malloc(20); return 0; }

A. free(p); , free(p->s);
B. free(p->s); , free(p);
C. free(p->s);
D. free(p);
Answer» C. free(p->s);
36.

If malloc() successfully allocates memory it returns the number of bytes it has allocated.

A. 1
B.
Answer» C.
37.

malloc() allocates memory from the heap and not from the stack.

A. 1
B.
Answer» B.
38.

When we dynamically allocate memory is there any way to free memory during run time?

A. Yes
B. No
Answer» B. No
39.

Can I increase the size of statically allocated array?

A. Yes
B. No
Answer» C.
40.

Assume integer is 2 bytes wide. What will be the output of the following code? #include #include #define MAXROW 3 #define MAXCOL 4 int main() { int (*p)[MAXCOL]; p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p)); printf("%d, %d\n", sizeof(p), sizeof(*p)); return 0; }

A. 2, 8
B. 4, 16
C. 8, 24
D. 16, 32
Answer» B. 4, 16
41.

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.
42.

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
43.

Point out the correct statement will let you access the elements of the array using 'p' in the following program? #include #include int main() { int i, j; int(*p)[3]; p = (int(*)[3])malloc(3*sizeof(*p)); return 0; }

A. for(i=0; i<3; i++) { for(j=0; j<3; j++) printf("%d", p[i+j]); }
B. for(i=0; i<3; i++) printf("%d", p[i]);
C. for(i=0; i<3; i++) { for(j=0; j<3; j++) printf("%d", p[i][j]); }
D. for(j=0; j<3; j++) printf("%d", p[i][j]);
Answer» D. for(j=0; j<3; j++) printf("%d", p[i][j]);
44.

Can I increase the size of dynamically allocated array?

A. Yes
B. No
C. Yes
D. No
Answer» B. No
45.

How will you free the memory allocated by the following program? #include #include #define MAXROW 3 #define MAXCOL 4 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.
46.

Point out the error in the following program. #include #include int main() { int *a[3]; a = (int*) malloc(sizeof(int)*3); 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
47.

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. 1
B.
C. 1
D.
Answer» C. 1