1.
#include “stdio.h”
int main()
{
int i;
for (i = 9; i!=0; i–)
printf(“i = %d”, i–);
return 0;
}

(a) 9 7 5 3 1
(b) 9 8 7 6 5 4 3 2 1
(c) Infinite Loop
(d) 9 7 5 3

Answer
(c)The program goes in an infinite loop because i is never zero when loop condition (i != 0) is checked. i changes like 7 5 3 1 -1 -3 -5 -7 -9 …

2.
#include “stdio.h”
int main()
{
int A = 10, B = 20;
do {
B /= A;
} while(A–);

printf (“%d\n”, B);
return 0;
}
(a) 1
(b) Runtime Error
(c) 0
(d) Compiler Error
Answer

(b) There is a bug in the above program. It goes inside the do-while loop for A = 0 also. So it fails during runtime.

3.
#include “stdio.h”
int main()
{
int i = -8;
while (i <= 8) { if (i >= 0)
break;
else
{
i++;
continue;
}
printf(“BHOPAL”);
}
return 0;
}
(a) 10 times
(b) 8 times
(c) Infinite times
(d) 0 times
Answer

(d) The loop keeps incrementing i while it is smaller than 0. When i becomes 0, the loop breaks. So BHOPAL is not printed at all.

4.
#include “stdio.h”
int main()
{
int i = 3;
while (i–)
{
int i = 20;
i–;
printf(“%d “, i);
}
return 0;
}
(a) Infinite Loop
(b) 19 19 19
(c) 19 18 17
(d) 2 2 2

Answer
(b) Note that the i–- in the statement while(i-–) changes the i of main()
And i== just after declaration statement int i=20; changes local i of while loop.

5.
#include “stdio.h”
int main()
{
int i;
for (i = 1; i != 10; i += 2)
printf(” SCABHOPAl “);
return 0;
}
(a) SCABHOPAl SCABHOPAl SCABHOPAl ……… Infinite
(b) SCABHOPAl SCABHOPAl SCABHOPAl SCABHOPAl
(c) SCABHOPAl SCABHOPAl SCABHOPAl SCABHOPAl SCABHOPAl
(d) SCABHOPAl

Answer
(a) The loop termination condition never becomes true and the loop prints sCABHOPAl infinite times. In general, if a for or while statement uses a loop counter, then it is safer to use a relational operator (such as <) to terminate the loop than using an inequality operator (operator !=).

6.
#include “stdio.h”
int main()
{
int x = 3;
if (x == 2); x = 0;
if (x == 3) x++;
else x += 2;

printf(“x = %d”, x);

return 0;
}
(a) x = 4
(b) x = 2
(c) Compiler Error
(d) x = 0
Answer

(b) Value of x would be 2. Note the semicolon after first if statement. x becomes 0 after the first if statement. So control goes to else part of second if else statement.

7.
#include “stdio.h”

int main()
{
int i = 1, j;
for ( ; ; )
{
if (i)
j = –i;
if (j < 10)
printf(“SCABHOPAL”, j++);
else
break;
}
return 0;
}
(A) Compile Error.
(B) No compile error but it will run into infinite loop printing SCABHOPAL.
(C) No compile error and it’ll print SCABHOPAL 10 times.
(D) No compile error but it’ll print SCABHOPAL 9 times.
Answer

(c) Basically, even though the for loop doesn’t have any of three expressions in parenthesis, the initialization, control and increment has been done in the body of the loop. So j would be initialized to 0 via first if. This if itself would be executed only once due to i–. Next if and else blocks are being used to check the value of j and existing the loop if j becomes 10. Please note that j is getting incremented in printf even though there’s no format specifier in format string. That’s why SCABHOPAL would be printed for j=0 to j=9 i.e. a total of 10 times.

8.
#include
int a();
int main(){
for(a();a();a()) {
printf(“%d “,a());
}
return 0;
}
int a(){
int static n=7;
return n–;
}

(a) 6 4
(b) 5 2
(c) Infinite loop with garbage value
(d) 7 5
Answer

(b)
First iteration:
Loop initial value: a() = 7
Loop condition: a() = 6
Since condition is true so printf function will print a() i.e. 5
Loop incrimination: a() = 4
Second iteration:
Loop condition: a() = 3
Since condition is true so printf function will print a() i.e. 2
Loop incrimination: a() = 1
Third iteration:
Loop condition: a() = 0
Since condition is false so program control will come out of the for loop.

9.
#include “stdio.h”
int main(){
static int a;
for(++a;++a;++a) {
printf(“%d “,a);
if(a==4) break;
}
return 0;
}
(a) 4
(b) Compile error: a is not referenced
(c) 2 4
(d) None of the above.
Answer

(c)Default value of static int variable in c is zero. So, initial value
of variable a = 0
First iteration:
For loop starts value: ++a i.e. a= 0 + 1 = 1
For loop condition: ++a i.e. a = 1 + 1 = 2 i.e. loop condition is
true. Hence printf statement will print 2
Loop incrimination: ++a i.e. a = 2 + 1 =3
Second iteration:
For loop condition: ++a i.e. a = 3 + 1 = 4 i.e. loop condition is
true. Hence printf statement will print 4.
Since is equal to for so if condition is also true. But due to
break keyword program control will come out of the for loop.

10.
#include “stdio.h”
int main(){
for(;;) {
printf(“%d “,67);
}
return 0;
}
(a) Infinite loop
(b) 67
(c) Compilation error: declaration not done
(d) None
Answer

(a)In for loop each part is optional. If nothing is given in
condition then it will take increment condition as Infinite.
Loops in C