7.5 – Quiz on for iteration statement

by subbu on October 7, 2013

Quiz-1:
1) What would be the output of following program?

#include<stdio.h>
int main()
{
int i;
for(i=5;i;i--)
   printf("\t%d",i);
return 0;
}
Show Answer
Output:
5        4        3        2        1

The value of i is printed as long as i is not a zero (true). Terminated when i is zero

2) What would be the output of following program?

#include<stdio.h>
int main()
{
char ch;
for(ch=1;ch<=255;ch++)
  printf("\t%d",ch);
return 0;
}
Show Answer
Output:
1  2  3….. 127  -128  -127 …… 0  1  2 3….

It is an infinite loop. As the range of char type is -128 to 127, ch reaches to 127, goes to the negative side as the result of overflow and come back to 0 1 2… ch can never be more than 255.

3) What would be the output of following program?

#include<stdio.h>
int main()
{
int x;
for(x=1;x<=5;)
  printf("\t%d",++x);
return 0;
}
Show Answer
Output:
2        3        4        5        6

4) What would be the output of following program?

#include<stdio.h>
int main()
{
int x;
for(x=1;x<=5;);
   printf("\t%d",++x);
return 0;
}
Show Answer
Output:
Infinite loop

Here the loop is terminated with a semicolon and the counter x is not changing. Hence the condition will never be false.

5) What would be the output of following program?

#include<stdio.h>
int main()
{
short x;
for(x=1;x++;)
  printf("\t%d",x);
return 0;
}
Show Answer
Output:
1  2  3…32767  -32768  -32767  ……-1  0

As the range of short is -32768 to 32767, loop terminates only when x reaches to 0


Quiz-2
1) What would be the output of following program?

#include<stdio.h>
int main()
{
short x;
for(x=1;x<=32767;x++)
  printf("\t%d",x);
return 0;
}
Show Answer
Output:
Infinite loop

When x reaches to 32767, increment of which by 1 result -32768. Hence the condition will never be false

2) What would be the output of following program?

#include<stdio.h>
int main()
{
int x;
for(x=1;x<=5;x=+1)
   printf("\t%d",x);
return 0;
}
Show Answer
Output:
1  1   1  ………

Here x=+1 resetting the value of x to 1 for every iteration. Hence the condition will never be false.

3) What would be the output of following program?

#include<stdio.h>
int main()
{
int x;
for(x=5;x++<=8;printf("\t%d",x));
return 0;
}
Show Answer
Output:
6        7        8        9

4) What would be the output of following program?

#include<stdio.h>
int main()
{
int x;
for(x=97;x<=100;x++)
  printf("\n%c\t%d",x,x);
return 0;
}
Show Answer
Output:
a        97
b        98
c        99
d        100

Here ASCII values and equivalent alphabets are printed

5) What would be the output of following program?

#include<stdio.h>
int main()
{
char ch;
for(ch='A';ch<='D';ch++)
   printf("\n%c\t%d",ch,ch);
return 0;
}
Show Answer
Output:
A       65
B       66
C       67
D       68

Even a char type variables can be used as a loop driver variables, actually ASCII values of characters participate in increment/decrement and comparison.


Quiz-3

1) What would be the output of following program?

#include<stdio.h>
int main()
{
int i,j;
for(i=1,j=5;i<j;i++,j--)
   printf("\n%d\t%d",i,j);
return 0;
}
Show Answer
Output:
1        5
2        4

Condition would be false when both i and j becomes 3

2) What would be the output of following program?

#include<stdio.h>
int main()
{
float x;
for(x=0.1;x<=1.0;x+=0.2)
  printf("\t%.1f",x);
return 0;
}
Show Answer
Output:
0.1     0.3     0.5     0.7     0.9

3) What would be the output of following program?

#include<stdio.h>
int main()
{
int i=2;
for(i<=5;--i;i=1)
  printf("Hello world");
return 0;
}
Show Answer
Output:
Hello world

4) What would be the output of following program?

#include<stdio.h>
int main()
{
float x=12.765876;
int i;
for(i=1;i<=6;i++)
  printf("\n%-*.*f",10,i,x);
return 0;
}
Show Answer
Output:
12.8
12.77
12.766
12.7659
12.76588
12.765876

%w.pf is used to format the floating point numbers in printf() statements. %*.*f can be used to customize formatting by supplying arguments. – symbol is used to left justify the number

Previous post:

Next post: