Loops & Control Structure
Question 1
WRONG
|
#include <stdio.h> int main() { int i = 1024; for (; i; i >>= 1) printf ( "GeeksQuiz" ); return 0; } |
10
| |
11
| |
Infinite
| |
The program will show compile-time error
|
Discuss it
Question 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
WRONG
|
#include <stdio.h> #define PRINT(i, limit) do \ { \ if (i++ < limit) \ { \ printf ( "GeeksQuiz\n" ); \ continue ; \ } \ } while (0) int main() { int i = 0; PRINT(i, 3); return 0; } |
1
| |
3
| |
4
| |
Compile-time error
|
Discuss it
Question 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
CORRECT
|
What is the output of the below program?
#include <stdio.h> int main() { int i = 0; switch (i) { case '0' : printf ( "Geeks" ); break ; case '1' : printf ( "Quiz" ); break ; default : printf ( "GeeksQuiz" ); } return 0; } |
Geeks
| |
Quiz
| |
GeeksQuiz
| |
Compile-time error
|
Discuss it
Question 3 Explanation:
At first look, the output of the program seems to be Geeks. But, the cases are labeled with characters which gets converted to their ascii values 48(for 0) and 49(for 1). None of the cases is labeled with value 0. So, the control goes to the default block and GeeksQuiz is printed.
Question 4
CORRECT
|
#include <stdio.h> int main() { int i = 3; switch (i) { case 0+1: printf ( "Geeks" ); break ; case 1+2: printf ( "Quiz" ); break ; default : printf ( "GeeksQuiz" ); } return 0; } |
Geeks
| |
Quiz
| |
GeeksQuiz
| |
Compile-time error
|
Discuss it
Question 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
CORRECT
|
Predict the output of the below program:
#include <stdio.h> #define EVEN 0 #define ODD 1 int main() { int i = 3; switch (i & 1) { case EVEN: printf ( "Even" ); break ; case ODD: printf ( "Odd" ); break ; default : printf ( "Default" ); } return 0; } |
Even
| |
Odd
| |
Default
| |
Compile-time error
|
Discuss it
Question 5 Explanation:
The expression i & 1 returns 1 if the rightmost bit is set and returns 0 if the rightmost bit is not set. As all odd integers have their rightmost bit set, the control goes to the block labeled ODD.
Question 6
WRONG
|
#include <stdio.h> int main() { int i; if ( printf ( "0" )) i = 3; else i = 5; printf ( "%d" , i); return 0; } |
3
| |
5
| |
03
| |
05
|
Discuss it
Question 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.
Question 7
CORRECT
|
#include <stdio.h> int i; int main() { if (i); else printf ( "Ëlse" ); return 0; } |
if block is executed.
| |
else block is executed.
| |
It is unpredictable as i is not initialized.
| |
Error: misplaced else
|
Discuss it
Question 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. Please note that the empty block is equivalent to a semi-colon(;). So the statements if (i); and if (i) {} are equivalent.
Question 8
CORRECT
|
#include<stdio.h> int main() { int n; for (n = 9; n!=0; n--) printf ( "n = %d" , n--); return 0; } |
9 7 5 3 1
| |
9 8 7 6 5 4 3 2 1
| |
Infinite Loop
| |
9 7 5 3
|
Discuss it
Question 8 Explanation:
The program goes in an infinite loop because n is never zero when loop condition (n != 0) is checked. n changes like 7 5 3 1 -1 -3 -5 -7 -9 ...
Question 9
*****WRONG
|
Output?
#include <stdio.h> int main() { int c = 5, no = 10; do { no /= c; } while (c--); printf ( "%d\n" , no); return 0; } |
1
| |
Runtime Error
| |
0
| |
Compiler Error
|
Discuss it
Question 9 Explanation:
There is a bug in the above program. It goes inside the do-while loop for c = 0 also. So it fails during runtime.
Question 10
CORRECT
|
# 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 20
| |
7 12 17 22
| |
16 21
| |
Compiler Error
|
Discuss it
Question 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; }
Question 11
WRONG
|
Output of following C program?
#include<stdio.h> int main() { int i = 0; for ( printf ( "1st\n" ); i < 2 && printf ( "2nd\n" ); ++i && printf ( "3rd\n" )) { printf ( "*\n" ); } return 0; } |
1st
2nd * 3rd 2nd * | |
1st
2nd * 3rd 2nd * 3rd | |
1st
2nd 3rd * 2nd 3rd | |
1st
2nd 3rd * 1st 2nd 3rd |
Discuss it
Question 11 Explanation:
It is just one by one execution of statements in for loop. a) The initial statement is executed only once. b) The second condition is printed before '*' is printed. The second statement also has short circuiting logical && operator which prints the second part only if 'i' is smaller than 2 b) The third statement is printed after '*' is printed. This also has short circuiting logical && operator which prints the second part only if '++i' is not zero.
No comments:
Post a Comment