C MCQs (121-150)
121. Which of the following functions is used to copy a string in C?
a) copy()
b) strcpy()
c) string_copy()
d) copy_string()
Answer: b) strcpy()
122. What is the result of the following C expression: 4 && 0
?
a) 1
b) 0
c) 4
d) Error
Answer: b) 0
123. Which of the following is the correct syntax to declare a constant pointer in C?
a) int* const ptr;
b) const int* ptr;
c) int const* ptr;
d) const ptr int;
Answer: a) int* const ptr;
124. Which of the following operators is used to compare two values for equality in C?
a) ==
b) =
c) !=
d) ===
Answer: a) ==
125. What is the purpose of the #include
directive in C?
a) It is used to declare functions.
b) It is used to include external libraries or files.
c) It is used to initialize variables.
d) It defines a constant.
Answer: b) It is used to include external libraries or files.
126. What is the correct way to allocate memory for an array of 10 integers dynamically in C?
a) int* arr = (int*) malloc(10 * sizeof(int));
b) int* arr = malloc(10 * sizeof(int));
c) Both a and b
d) int* arr = malloc(10);
Answer: c) Both a and b
127. In C, what is the output of the following code?
#include <stdio.h>
int main() {
int x = 10;
printf("%d", x++ + ++x);
return 0;
}
a) 21
b) 22
c) 20
d) 10
Answer: a) 21
128. Which of the following is used to define a macro in C?
a) #define
b) #macro
c) define
d) macro
Answer: a) #define
129. Which of the following functions is used to read a string from standard input in C?
a) get()
b) gets()
c) fgets()
d) read()
Answer: b) gets()
130. In C, which of the following is the correct way to declare a function that takes two integers and returns a float?
a) float function(int a, int b);
b) function float(int a, int b);
c) float function(int, int);
d) Both a and c
Answer: d) Both a and c
131. What will the following C code output?
#include <stdio.h>
int main() {
char ch = 'A';
printf("%c", ++ch);
return 0;
}
a) A
b) B
c) 66
d) Error
Answer: b) B
132. Which of the following is a valid C array declaration?
a) int arr[5];
b) arr[5] int;
c) int[5] arr;
d) int arr5;
Answer: a) int arr[5];
133. What is the purpose of the return
statement in C?
a) It returns a value to the calling function.
b) It ends the program.
c) It terminates the current loop.
d) It prints a message to the console.
Answer: a) It returns a value to the calling function.
134. Which of the following C data types is used to store a single character?
a) int
b) char
c) float
d) double
Answer: b) char
135. What is the output of the following code?
#include <stdio.h>
int main() {
int x = 5, y = 5;
if (x == y) {
printf("Equal");
} else {
printf("Not Equal");
}
return 0;
}
a) Equal
b) Not Equal
c) Error
d) Undefined
Answer: a) Equal
136. What will be the output of the following code?
#include <stdio.h>
int main() {
int x = 1;
int y = 2;
if (x == 1 && y == 2) {
printf("True");
} else {
printf("False");
}
return 0;
}
a) True
b) False
c) Error
d) Undefined
Answer: a) True
137. Which of the following is a correct statement to create a pointer to a pointer in C?
a) int *ptr;
b) int ptr;
c) ptr int;
d) int ptr;
Answer: a) int **ptr;
138. What is the output of the following code?
#include <stdio.h>
int main() {
int a = 5, b = 2;
printf("%d", a > b ? a : b);
return 0;
}
a) 5
b) 2
c) Error
d) Undefined
Answer: a) 5
139. Which of the following is used to declare a pointer to an array in C?
a) int* arr[];
b) int arr*[];
c) int (arr)[];
d) int[] arr;
Answer: c) int (*arr)[];
140. What is the correct syntax to allocate memory for a 2D array dynamically in C?
a) int** arr = malloc(sizeof(int*) * rows * cols);
b) int* arr = malloc(sizeof(int[rows][cols]));
c) int** arr = malloc(sizeof(int[rows][cols]));
d) Both a and b
Answer: d) Both a and b
141. Which of the following is a valid C loop structure?
a) for, while, do-while
b) for, repeat, until
c) while, do-until, for
d) None of the above
Answer: a) for, while, do-while
142. What does the following code do?
#include <stdio.h>
int main() {
int a = 0;
for (a = 0; a < 5; a++) {
if (a == 3) {
break;
}
printf("%d ", a);
}
return 0;
}
a) Prints 0 1 2 3
b) Prints 0 1 2
c) Prints 3
d) Prints 0 1 2 4
Answer: b) Prints 0 1 2
143. Which of the following is true about a structure in C?
a) Structure members must be of the same data type.
b) A structure can only store numeric data types.
c) A structure allows grouping of different data types.
d) A structure cannot have functions as members.
Answer: c) A structure allows grouping of different data types.
144. What is the correct way to define a function that takes no arguments and returns nothing in C?
a) void func();
b) void func(void);
c) func(void);
d) func();
Answer: b) void func(void);
145. What does the following code do?
#include <stdio.h>
int main() {
int a = 10, b = 5;
printf("%d", a > b ? a : b);
return 0;
}
a) Prints 5
b) Prints 10
c) Prints 15
d) Undefined
Answer: b) Prints 10
146. Which of the following is the correct syntax to declare an array of strings in C?
a) char* arr[] = {“apple”, “banana”, “cherry”};
b) char arr[] = {“apple”, “banana”, “cherry”};
c) char[] arr = {“apple”, “banana”, “cherry”};
d) string arr[] = {“apple”, “banana”, “cherry”};
Answer: a) char* arr[] = {“apple”, “banana”, “cherry”};
147. Which of the following is used to allocate memory for a structure dynamically in C?
a) malloc()
b) calloc()
c) realloc()
d) All of the above
Answer: d) All of the above
148. What is the output of the following code?
#include <stdio.h>
int main() {
int a = 0;
for (a = 0; a < 3; a++) {
if (a == 1) continue;
printf("%d ", a);
}
return 0;
}
a) 0 1 2
b) 0 2
c) 1 2
d) 2 0
Answer: b) 0 2
149. What is the correct syntax to declare a function that takes a float
as an argument and returns an integer?
a) int func(float a);
b) float func(int a);
c) int func(int a);
d) float func(float a);
Answer: a) int func(float a);
150. Which of the following operators is used to perform bitwise AND in C?
a) &
b) &&
c) |
d) ||
Answer: a) &