Home »
Aptitude Questions and Answers »
C Aptitude Questions and Answers
C for loop Aptitude Questions and Answers
This section contains Aptitude Questions and Answers on C language For Loops (with multiple answers) with explanation.
Submitted by Ashish Varshney, on February 17, 2018
List of C programming for loops Aptitude Questions and Answers
1) How many times this loop will execute?
int main()
{
inti;
for(i=0;i<10;i++)
{
printf("%d\n",i++);
i=++i;
}
return 0;
}
- 10
- 5
- 4
- None of these
Correct Answer - 3
4
In this code, i initialize by ‘0’ and terminate condition is ‘i less than 10’ and there is 3 increment conditions is used, first in for loop, second in print statement, third in the body of the forloop. For single iteration, there is 3 increment operation perform. That’s way loop execute 4 times for 0,3,6,9.
2) How many times this loop will execute?
const MAX=10;
int main()
{
inti=1;
for(;i<MAX;i=i/i)
{
printf("%d\n",i);
}
return 0;
}
- 10
- 5
- 4
- Infinite
Correct Answer - 4
Infinite
In this code, for loop never achieve the termination condition, because iteration condition reset the variable of the loop(i) to an initial value that is ‘1’.
3) How many times this loop will execute?
int main()
{
int max = 5;
inti = 0;
for(;;)
{
i++;
if(i> max)
break;
printf("i = %d\n",i);
}
return 0;
}
- 2
- 5
- 4
- None of these
Correct Answer - 2
5
In this code,Loop can run infinitely if condition is set to TRUE always or no condition is specified, i.e. for(;;)but in this condition, we specify terminating in if statement and by break statement we exit the for loop.
4) What will be the output?
int main()
{
int max = 5;
int c = 0;
for(; c <max;c++);
{
printf("%d ",c);
}
printf("END\n");
return 0;
}
- 1 2 3 4 END
- 1 2 3 4 5 END
- 5 END
- Error
Correct Answer - 3
5 END
In this code, semicolon at end of the for loop signifies that there is no body exists for this loop. So, for variable initialized with ‘0’ and terminate with ‘5’.
5) What will be the output?
int main()
{
inti=5;
for (; 0; i--)
{
printf("%d\n",i);
}
return 0;
}
- 54321
- No output
- Compile time error
- Run time error
Correct Answer - 2
No output
Variables ‘i’ is initialized before for loop to ‘5’, condition is FALSE always as ‘0’ is provided that causes NOT to execute loop statement, iteration is decrement of counter variable ‘i’.
6) How many times loop will be executed?
#include <stdio.h>
int main()
{
int i,k;
for (i=0, k=0; (i< 5 && k < 3); i++, k++)
{
;
}
printf("%d\n",i);
return 0;
}
- 5
- 3
- 4
- No output
Correct Answer - 2
3
Variables ‘i’ and ‘k’ are initialized to 0; condition is to execute loop when ‘i’ is lesser than ‘5’and ‘k’ is lesser than ‘ 3’; iteration is increment of counter variables ‘i’ and ‘k’.