C Decision making & Loops-3
1. How long the following loop runs?
for(x = 1; x = 3; x++)
a) Three times
b) Four times
c) Forever
d) Never
Answer: D
The first statement of a for loop is initialize the loop counter, second is conditional statment and the third one is increment/decrement of the loop counter.
In the given expression the second statement is an assignment statement instead of condition.
So the for loop never execute.
2. A switch statement is used to
a) To use switching variable
b) Switch between function in a programchar
c) Switch from one variable to another variable
d) To choose from multiple possibilities which may arise due to different values of a single variable
Answer: D
No explanation is given for this question.
3. If the following loop is implemented
void main() {
int num = 0;
do {
- - num;
printf(“%d”, num);
num ++;
}
while(num >= 0);
}
a) A run time error will be reported
b) The program will not enter into the loop
c) The loop will run infinitely many times
d) There will be a compilation error reported
Answer: C
No explanation is given for this question.
4. What is the value of ‘grade’ after the switch statement is executed?
marks = 80;
switch(marks) {
case 60:
grade = ‘C’;
break;
case 70:
grade = ‘B’ ;
break;
case 80:
grade = ‘A’;
break;
default:
grade = ‘E’;
a) A
b) B
c) C
d) E
Answer: A
No explanation is given for this question.
5. If switch feature is used, then
a) Default case must be present
b) Default case, if used, should be the last case
c) Default case, if used, can be placed anywhere
d) None of the above
Answer: C
default statement can or cannot be used and if they are used then we can place them anywhere we want.
6. In the context of "break" and "continue" statements in C, pick the best statement.
a) "break" and "continue" can be used in "for", "while", "do-while" loop body and "switch" body
b) "break" and "continue" can be used in "for", "while" and "do-while" loop body. But only "break" can be used in "switch" body
c) "break" and "continue" can be used in "for", "while" and "do-while" loop body. Besides, "continue" and "break" can be used in "switch" and "if-else" body
d) "break" can be used in "for", "while" and "do-while" loop body
Answer: B
As per C standard, continue can be used in loop body only. break can be used in loop body and switch body only.
7. In _______, the bodies of the two loops are merged together to form a single loop provided that they do not make any references to each other.
a) Loop unrolling
b) Strength reduction
c) Loop concatenation
d) Loop jamming
Answer: D
In loop jamming, the bodies of the two loops are merged together to form a single loop provided that they do not make any references to each other.
8. What will be the output of the following piece of code?
#include <stdio.h>
int main() {
int value = 0;
if(value)
printf("well done");
printf("Algbly");
return 0;
}
a) well done AlgblY
b) Algbly
c) complier error
d) None of these
Answer: B
As the value of variable value is zero so, it evaluates to false in the if condition.
9. What will be the output of the following piece of code?
#include <stdio.h>
int main() {
for(i = 0;i < 10; i++);
printf("%d", i);
return 0;
}
a) 10
b) 0123456789
c) Syntax error
d) 0
e) Infinite loop
Answer: A
Due to semicolon(;) at the end of the for loop, after 10 iterations for loop will be terminated and the result will be 10.
10. What will be the value of the digit?
#include <stdio.h>
int main() {
int digit = 0;
for(; digit <= 9; )
digit++;
digit *= 2;
--digit;
return 0;
}
a) -1
b) 17
c) 19
d) 16
Answer: D
First of all for loop have no braces so for loop on have only next line in its body.
for( ; digit <= 9; )
digit++;
After completing for loop digit = 10;
next statement digit *= 2; i.e digit = digit * 2 = 20;
next statement digit--; i.e 20-- => 19
So final value of digit is 19.