mcq on looping statement in C language,while loop mcq,dowhile loop mcq, for loop mcq  and more

Q5.What happens if the condition in a while loop is always true?
A. The loop will run indefinitely
B. The loop will run only once
C. The loop will never execute
D. The program will crash
Answer: A. The loop will run indefinitely


Q6.What is the purpose of a while loop in C?
A. To exit the program immediately
B. To iterate over a collection of elements
C. To execute a block of code a specific number of times
D. To check a condition earlier or before executing the loop
Answer: D. To check a condition earlier or before executing the loop


Q7.Which of the following is an an infinite loop in C?
A.while(i != 0) {}
B.while(i > 0) {}
C.while(i < 10) {}
D.while(1) {}
Answer: D. while(1) {}


Q8.Find out the output of the following code?
int p = 5;
while(p > 2) {
printf("%d ", p);
p--;
}
A. 5 4 3
B. 4 3 2 1
C. 5 4 3 2
D. 1 2 3 4 5
Answer: A. 5 4 3


Q9.Find out the value of x after the program execution?
int x = 5;
while(x > 0) {
x -= 5;
}
A. 5
B. 15
C. -5
D. 0
Answer: D. 0


Q10.Which keyword is used to skip the current iteration in a loop in C?
A. break;
B. continue;
C. pass;
D. skip;
Answer: B.continue;


Q11.What is the initializer in a while loop in C?
A.To initialize variables before the loop starts
B.To specify the number of iterations
C.To increment or decrement the loop variable
D.To set the condition for exiting the loop
Answer: A. To initialize variables before the loop starts


Q12.Which is the syntax for a do-while loop in C?
A. while(condition) do { }
B. do while(condition) { }
C. do { } while(condition);
D. do(condition) { } while;
Answer: C. do { } while(condition);


Q13.Trace out the output of the following code?
int p = 10;
while(p> 0) {
printf("%d ", p);
p -= 2;
}
A. 10 8 6 4 2
B. 10 7 4 3
C. 10 8 7 6
D. 10 9 8 7 5 4 2 0
Answer: A. 10 8 6 4 2


Q14.What is the purpose of the increment (++)/decrement (--) statement in a while loop in C?
A. To set the exit condition of the loop
B. To check the condition before executing the loop
C. To initialize variables before the loop starts
D. To change or modify the value of loop variable with each iteration or execution
Answer: D. To change or modify the value of loop variable with each iteration or execution


Q15.Find out the value of variable tot after the code is successfully executed in C?
int tot = 0; int i = 1;
while(i <= 10) {
tot += i;
i++;
}
A. 36
B. 55
C. 49
D. 28
Answer: A. 55


Q16.Which keyword is used to exit a loop in C?
A. break;
B. return;
C. exit;
D. continue;
Answer: A. break;


Q17.what is the importance of condition in while loop?
A. To exit the program immediately
B. To increment or decrement the loop variable
C. To decide whether to execute the loop or not
D. To set the initial value of variables
Answer: C. To decide whether to execute the loop or not


Q18.In a do-while loop, the condition is checked:
A. at the same time as the loop body is executed
B. before the loop body is executed
C. after the loop body is executed
D. none of the above
Answer: C. after the loop body is executed


Q19.What will take place if the condition in a do-while loop is not ever true?
A. The loop will execute indefinitely
B. The loop will execute at least once
C. The program will crash
D. The loop will never execute
Answer: B. The loop will execute at least once


Q20.Which of the following is not necessary in a do-while loop?
A. initialization
B. loop body
C. increment
D. condition
Answer: A. initialization


Q21.How many times will the following do-while loop execute?
int i = 0;
do {
i++;
} while (i < 0);
A. 0
B. 1
C. 4
D. 5
Answer: B. 1


Q22.Which of the following statement is executed at least once?
A. while loop
B. if statement
C. do-while loop
D. for loop
Answer: C. do-while loop


Q23.What does the following code snippet do?
int p = 9;
do {
printf("%d", p);
p--;
} while (p> 5);
A. Prints numbers from 9 to 5
B. Prints numbers from 1 to 9
C. Prints numbers from 9 to 1
D. Prints numbers from 5 to 9
Answer:A. Prints numbers from 9 to 5


Q24.Which of the following statement is true about the do-while loop in c?
A. It can only execute once
B. It does not have a condition
C. It is a pre-test loop
D. It is a post-test loop
Answer: D. It is a post-test loop


Q25.What is the output of the following code snippet?
int x = 20;
do {
printf("%d", x);
i -= 2;
} while (i < 15);
A. 20 14 116
B. 20 18 16
C. 18 16 15
D. 20
Answer: B. 20 18 16


Q26.What is the highest difference between a do-while loop and a while loop ?
A. There is no difference between them
B. In a do-while loop, the condition is checked before the loop body is executed
C. In a do-while loop, the loop body is executed at least once
D. In a while loop, the loop body is executed at least once
Answer: C. In a do-while loop, the loop body is executed at least once


Q27.Which of the following is an essential component of a do-while loop?
A. body of the loop
B. condition
C. initialization
D. all of the above
Answer: D. all of the above


Q28.In which state would you wish to prefer using a do-while loop over a while loop?
A. When the loop needs to run indefinitely
B. When the loop body should not be executed at all
C. When the loop body needs to be executed at least once
D. When there are multiple conditions to be checked
Answer: C. When the loop body needs to be executed at least once


Q29.What will happen when the condition in a do-while loop is always remains true?
A. The loop will execute once
B. The program will crash
C. The loop will never execute
D. The loop will execute indefinitely
Answer: D. The loop will execute indefinitely


Q30.how variable can be incrementing in a while , do- while loop?
A. i += 1
B. i++
C. i = i++
D. all of the above
Answer: D


Q31.How many times will the following loop executes ?
for(int i = 20; i >= 10; i -= 2) {
// Some code
}
A) 10 times
B) 6 times
C) 5 times
D) Infinitely
Answer: B) 6 times


Q32.Which of the following is the correct nested for loop in c?
A) for(i = 0; i < n; i++) { for(j = 0; j < m) { // code } }
B) for(i = 0; i < n; i++) { for(j = 0; j < m; j++) { // code }
C) for(i = 0; i < n) { for(j = 0; j < m; j++) { // code } }
D) for(i = 0; i < n; i++) { for(j = 0; j < m; j++) { // code } }
Answer: D) for(i = 0; i < n; i++) { for(j = 0; j < m; j++) { // code } }


Q33.What will be the output of the following loop?
for(int p = 0; p < 1; p++) {
for(int p = 0; p < 3; p++) {
printf("%d %d ", p, q);
}
}
A) 0 1 1 2 2 3
B) 0 0 0 0 1 1
C) 0 0 0 1 0 2
D) 0 0 1 1 2 2
Answer: C) 0 0 0 1 0 2


Q34.What will be the output of the following loop?
for(int x = 5; x > 0; x -= 1) {
printf("%d ", x);
}
A) 5 4 3 2 1
B) 4 2
C) 5 3
D) 5 3 1
Answer: A) 5 4 3 2 1


Q35.How do you create an infinite loop in C?
A) do { // code } while(1)
B) for(;;) { // code }
C) while(1) { // code }
D) All of the above
Answer: D) All of the above


Q36.What will be the output of the following loop?
int j= 0;
while(j < 5) {
printf("%d ", j*2);
j += 1;
}
A) 2 4 6 8 2
B) 0 2 6 8
C) 0 2 4 6 8 10
D) 4 6 8 10
Answer: B) 0 2 6 8


Q37.What will be the output of the following loop?
for(int x = 0; x < 4; x++) {
for(int y = 0; y < 2; y++) {
if(y == 1) {
break;
} printf("%d %d ", x, y);
}
}
A) 0 0
B) 0 0 0 1 1 0 1 1 2 0 3 0
C) 0 0 1 0 2 0 3 0
D) 0 0 1 1 2 0 3 0
Answer: D) 0 0 1 0 2 0 3 0


Previous Topic:-->>Conditional Statements MCQ. || Next topic:-->>Arrays MCQ.