Home »
Aptitude Que. & Ans. »
C Aptitude Que. & Ans.
C Looping (for, while, do while) - Aptitude Questions & Answers
C programming Looping Aptitude Questions and Answers: In this section you will find C Aptitude Questions and Answers on various looping statements like while, do dhile, for nested looping etc.
1) What will be the output of following program ?
#include < stdio.h >
void main()
{ unsigned char var=0;
for(var=0;var<=255;var++)
{
printf("%d ",var);
}
}
- 0 1 2 ... infinite times
- 0 1 2 ... 255
Correct Answer - 1
0 1 2 ... 255
The range of unsigned char is 0 to 255 and when the value of var will cross over 255, value will be 0 and again same process will happen.
2) What will be the output of following program ?
#include <stdio.h>
void main()
{
char cnt=0;
for(;cnt++;printf("%d",cnt)) ;
printf("%d",cnt);
}
- 0 1 2 ... infinite times
- 0 1 2 ... 127
- 0
- 1
Correct Answer - 4
1
Before entering into the for loop the CHECK CONDITION is "evaluated". Here it evaluates to 0 (false) and comes out of the loop, and i is incremented (note the semicolon after the for loop).
3) What will be the output of following program ?
#include <stdio.h>
void main()
{
int i=1;
while (i<=5)
{
printf("%d",i);
if (i==5)
goto print;
i++;
}
}
fun()
{
print:
printf("includehelp.com");
}
- Error
- 12345includehelp.com
- 1234includehelp.com
- 1includehelp.com 2includehelp.com 3includehelp.com 4includehelp.com 5includehelp.com
Correct Answer - 1
Compiler error: Undefined label ‘print’ in function main.
Labels have functions scope, in other words the scope of the labels is limited to functions. The label ‘print’ is available in function fun() Hence it is not visible in function main.
4) What will be the output of following program ?
#include < stdio.h >
int main()
{
int tally=0;
for(;;)
{
if(tally==10)
break;
printf("%d ",++tally);
}
return 0;
}
- 0 1 2 3 4 5 6 7 8 9 10
- 0 1 2 3 ... infinite times
- 1 2 3 4 5 6 7 8 9 10
- 1 2 3 4 5 6 7 8 9
Correct Answer - 3
1 2 3 4 5 6 7 8 9 10
for(; ;) it is possible in c, there is no need to place condition with in the for(), you can
place condition with in the body of the loop.
5) What will be the output of following program ?
#include <stdio.h>
void main()
{
int tally;
for(tally=0;tally<10;++tally)
{
printf("#");
if(tally>6)
continue;
printf("%d",tally);
}
}
- #0#1#2#3#4#5#6###
- #0#1#2#3#4#5#6#7#8#9#10
- #0#1#2#3#4#5##7#8#9#10
- #0#1#2#3#4#5#
Correct Answer - 1
#0#1#2#3#4#5#6###
6) What will be the output of following program ?
#include <stdio.h>
void main()
{
int i,j,charVal='A';
for(i=5;i>=1;i--)
{
for(j=0;j< i;j++)
printf("%c ",(charVal+j));
printf("\n");
}
}
-
A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
-
A B C D
A B C D
A B C D
A B C D
-
A B C D
A B C
A B
A
-
A B C D E
A B C D
A B C
A B
A
Correct Answer - 4
A B C D E
A B C D
A B C
A B
A
7) What will be the output of following program ?
#include <stdio.h>
void main()
{
int cnt=1;
do
{
printf("%d,",cnt);
cnt+=1;
}while(cnt>=10);
printf("\nAfter loop cnt=%d",cnt);
printf("\n");
}
- After loop cnt=1
- 1,
After loop cnt=2
- After loop cnt=2
Correct Answer - 2
1,
After loop cnt=2
do while is an exit controlled loop, here loop body executed first, then condition will be checked.
8) What will be the output of following program ?
#include <stdio.h>
void main()
{
int cnt=1;
while(cnt>=10)
{
printf("%d,",cnt);
cnt+=1;
}
printf("\nAfter loop cnt=%d",cnt);
printf("\n");
}
- After loop cnt=1
- 1,
After loop cnt=2
- After loop cnt=2
Correct Answer - 1
1,
After loop cnt=1
Here condition will be checked first.
9) What will be the output of following program ?
#include <stdio.h>
#define TRUE 1
int main()
{
int loop=10;
while(printf("Hello ") && loop--);
}
- Hello
- Hello Hello Hello Hello ... (Infinite times)
- Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello (10 times)
- Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello (11 times)
Correct Answer - 4
Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello (11 times)
10) What will be the output of following program ?
#include <stdio.h>
int main()
{
int i=1;
while(i<=10 && 1++)
printf("Hello");
}
- Error: lvalue required as increment operand
- HelloHello ... 10 times
- HelloHello ... 11 times
- Hello
Correct Answer - 1
Error: lvalue required as increment operand.