Quiz On C 3
Quiz On C 3
a) true
b) false
c) Depends on computer
d) Nothing is printed
Solution: (d) x is initialized with 1 and the “if” statement compares it with 0, thus the “if condition” is
“false” and the nested “if” statements do not get executed. Hence, the program do not prints anything.
Solution: 4 is printed. Since ++ is pre-increment operator. It increments the operator by one and then
assignment is done. Thus the incremented value of i which becomes 2 and added to it and the result is 4.
Solution: (c) ‘a--’ post-increment the value of a. Thus, the if statement is executed as the value of a is
considered as 1 which is true. ‘++a’ pre-increment the value of a. Thus, the decremented value of a (which
is 0) is incremented first and then assigned. So, both the if statements are executed ad correspondingly both
True and False will be printed.
Solution: (d) The correct syntax of for loop is not written, thus the compiler will return error.
Solution: (b) do-while loop is executed at least once even though the condition is false.
9. If multiple conditions are used in a single if statement then the testing of those conditions are done
a) From Right to Left
b) From Left to right
c) Randomly
d) None
int a=10,b=7,c=125;
c /= ++a * --b;
a) a=11, b=7,c=0;
b) a=10, b=6,c=1;
c) a=11, b=6,c=1;
d) a=10, b=7,c=0;
Solution: (c) ++a * --b is computed as (a=a+1)*(b=b-1) (11)*(6)=66
c/=66 c=c/66 c=125/66=1 (as c is integer)
Hence the right answer is a=11, b=6 and c=1
switch(month)
{
case < 30:
printf(“It’s February”);
default:
printf(“It’s not February”);
}
Solution: (c) Compilation error. Because only equality can be checked in case. Not <,>,<=,>= etc.
12. How many times “Hello” will be printed when the following code is executed?
#include<stdio.h>
int main()
{
int i=6;
while(--i>0)
printf("Hello\t");
return 0;
}
WEEK 4 ASSIGNMENT SOLUTION
Solution: 5
We have declared and initialize the variable i = 6, the condition is (--i > 0). Here i is pre-decremented once
by one until it becomes 1. It will be 5, 4, 3, 2 and 1 for each iteration.
13. What will be the output of the following program?
#include <stdio.h>
int main()
{
int x = 1;
switch (x)
{
case 1: printf("Choice is 1 \n");
default: printf("Choice other than 1 \n");
}
return 0;
}
a) Choice is 1
b) Choice other than 1
c) Both (a) and (b)
d) Syntax error
Solution: (c)
Since “break;” statement is not used after print statement, it will execute the default instruction as well.
14. What will be the output?
#include <stdio.h>
int main () {
int a = 10;
do {
printf("%d ", a);
a++;
}while( a >= 20 );
printf("%d ", a);
return 0;
}
a) 10 10
b) 10 11 12 … 20 20
c) 10 11 12 … 20 21
d) 10 11
Solution: (d)
The loop body is executed once due to false conditional checking. Hence, 10 is printed within the loop. While
exiting the loop the value of a is incremented to 11. So, after the loop 11 will be printed.
15. What will be printed when the following code is executed?
#include <stdio.h>
int main ()
{
int i;
WEEK 4 ASSIGNMENT SOLUTION
for(i=0;i<5;++i);
printf("%d ",i);
return 0;
}
a) 012345
b) 01234
c) 5
d) 0
Solution: (c)
“printf” is a separate instruction as it is not included within the loop. There is a semicolon “;” after the for
loop. After the execution of loop printf statement prints the value of i (as updated in the loop) i.e. 5.