C Loops & Control Structure Read Courses C Loops & Control Structure Please wait while the activity loads. If this activity does not load, try refreshing your browser. Also, this page requires javascript. Please visit using a browser with javascript enabled. If loading fails, click here to try again Question 1 #include <stdio.h> int main() { int i = 1024; for (; i; i >>= 1) printf("GeeksQuiz"); return 0; } How many times will GeeksQuiz be printed in the above program?1011InfiniteThe program will show compile-time errorC Loops & Control Structure Discuss itQuestion 1-Explanation: In for loop, mentioning expression is optional. >>= is a composite operator. It shifts the binary representation of the value by 1 to the right and assigns the resulting value to the same variable. The for loop is executed until value of variable i doesn't drop to 0.Question 2 C #include <stdio.h> #define PRINT(i, limit) do { if (i++ < limit) { printf("GeeksQuizn"); continue; } }while(0); int main() { int i = 0; PRINT(i, 3); return 0; } How many times GeeksQuiz is printed in the above program ? 1 3 4 Compile-time error C Loops & Control Structure Discuss itQuestion 2-Explanation: If a macro needs to be expanded in multiple lines, it is the best practice to write those lines within do{ }while(0) to avoid macro side effects. After GeeksQuiz is printed once, the control reaches the while statement to check for the condition. Since, the condition is false, the loop gets terminated. Question 3 What is the output of the below program? C #include <stdio.h> int main() { int i = 2; switch (i) { case 0: printf("Geeks"); break; case 1: printf("Quiz"); break; default: printf("GeeksQuiz"); } return 0; } Geeks Quiz GeeksQuiz Compile-time error C Loops & Control Structure Discuss itQuestion 3-Explanation: As i=2, so GeeksQuiz is printed. Question 4 C #include <stdio.h> int main() { int i = 3; switch (i) { case 1: printf("Geeks"); break; case 1+2: printf("Quiz"); break; default: printf("GeeksQuiz"); } return 0; } What is the output of the above program? Geeks Quiz GeeksQuiz Compile-time error C Loops & Control Structure Discuss itQuestion 4-Explanation: Expression gets evaluated in cases. The control goes to the second case block after evaluating 1+2 = 3 and Quiz is printed. Question 5 Predict the output of the below program: C #include <stdio.h> #define EVEN 0 #define ODD 1 int main() { int i = 3; switch (i % 2) { case EVEN: printf("Even"); break; case ODD: printf("Odd"); break; default: printf("Default"); } return 0; } Even Odd Default Compile-time error C Loops & Control Structure Discuss itQuestion 5-Explanation: The following C code will print 'odd' as the output of the program. Question 6 C #include <stdio.h> int main() { int i; if (printf("0")) i = 3; else i = 5; printf("%d", i); return 0; } Predict the output of above program? 3 5 03 05 C Loops & Control Structure Discuss itQuestion 6-Explanation: The control first goes to the if statement where 0 is printed. The printf("0") returns the number of characters being printed i.e. 1. The block under if statement gets executed and i is initialized with 3.Therefore, '03' gets printed here. Question 7 C #include <stdio.h> int i; int main() { if (i) { // Do nothing } else { printf("Else"); } return 0; } What is correct about the above program? if block is executed. else block is executed. It is unpredictable as i is not initialized. Error: misplaced else C Loops & Control Structure Discuss itQuestion 7-Explanation: Since i is defined globally, it is initialized with default value 0. The Else block is executed as the expression within if evaluates to FALSE. . Therefore, the else block gets executed. Question 8 #include<stdio.h> int main() { int n; for (n = 9; n!=0; n--) printf("n = %d", n--); return 0; } What is the output?9 7 5 3 19 8 7 6 5 4 3 2 1Infinite Loop9 7 5 3C Loops & Control Structure Discuss itQuestion 8-Explanation: The program goes in an infinite loop because n is never zero when loop condition (n != 0) is checked. n changes like 9 7 5 3 1 -1 -3 -5 -7 -9 ...Question 9Output? #include <stdio.h> int main() { int c = 5, no = 10; do { no /= c; } while(c--); printf ("%dn", no); return 0; } 1Runtime Error0Compiler ErrorC Loops & Control Structure Discuss itQuestion 9-Explanation: There is a bug in the above program. It goes inside the do-while loop for c = 0. Also as the increment is post increment, so (no/0) will create a divide by 0 error. So it fails during runtime.Question 10 # include <stdio.h> int main() { int i = 0; for (i=0; i<20; i++) { switch(i) { case 0: i += 5; case 1: i += 2; case 5: i += 5; default: i += 4; break; } printf("%d ", i); } return 0; } 5 10 15 207 12 17 2216 21Compiler ErrorC Loops & Control Structure Discuss itQuestion 10-Explanation: Initially i = 0. Since case 0 is true i becomes 5, and since there is no break statement till last statement of switch block, i becomes 16. Now in next iteration no case is true, so execution goes to default and i becomes 21. In C, if one case is true switch block is executed until it finds break statement. If no break statement is present all cases are executed after the true case. If you want to know why switch is implemented like this, well this implementation is useful for situations like below. switch (c) { case 'a': case 'e': case 'i' : case 'o': case 'u': printf(" Vowel character"); break; default : printf("Not a Vowel character");; break; } 12345 There are 46 questions to complete. You have completed questions question Your accuracy is Correct Wrong Partial-Credit You have not finished your quiz. If you leave this page, your progress will be lost. Correct Answer You Selected Not Attempted Final Score on Quiz Attempted Questions Correct Attempted Questions Wrong Questions Not Attempted Total Questions on Quiz Question Details Results Date Score Hint Time allowed minutes seconds Time used Answer Choice(s) Selected Question Text Need more practice! Keep trying! Not bad! Good work! Perfect! Last Updated : 28 Sep, 2023 Share your thoughts in the comments Add Your Comment Please Login to comment...