Please Turn On Java Script To Access This Site.

C Institute | Questions


Previous Topic : for loop - 1Next Topic : while loop - 1

This section focuses on C interview questions, tricky questions, and puzzles, and viva questions based on “for loop statement”. One shall practice these questions and puzzles to improve their C programming skills needed for various interviews (campus interviews, walk-in interviews, viva exams and company interviews), placements, entrance exams and other competitive exams. These programming questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate, engineering student, bca student, mca student, mcs students or an experienced IT professional. It will improve their understanding of C Language. Here is a listing of C questions and puzzles on “for loop statement” along with answers and/or explanations.

Q1: What will be the result of compiling and executing the following c code?
#include <stdio.h>
int main()
{   int n,c;
    for(n=5,c=10;c--; ) 
    {     n/=c;
    } 
    printf ("%dn",n);
    return 0;
}
a)  1
b)  Runtime error
c)  0
d)  Compile time error
Correct Answer : b
Explanation : when the value of c becomes 0, then n/0 is a runtime error. In c if an int value is divided by 0 then this is a runtime error.


Q2: What will be the output when we compile and run the below given code?
# include <stdio.h>
int main()
{  int i = 0;
   for (i=0; i<20; i++)
   { switch(i)
     {case 0:
         i += 5;
       case 1:
         i += 2;
       case 5:
         i += 5;
       default:
         i += 4;
         break;
     }
     printf("%d  ", i);
   }
   return 0;
}
a)  7 12 17 22
b)  16 21
c)  5 10 15 20
d)  4 8 12 16 20
Correct Answer : b
Explanation : The initial value of i is 0. Condition of loop is true, case 0 matches with value of i, but there is no break till end so all case from 0 to default are executed and i becomes 16, so after switch 16 is printed, then i is incremented by 1 in inc/dec part of for loop. Again condition of for loop is true, but no case matches the valueof i, so default is executed, i becomes 21, so 21 is printed outside switch. The i becomes 22 in inc/dec part of for loop, here condition of for loop becomes false.
Learn for loop
Learn switch case


Q3: What will be the output of the following c code?
#include<stdio.h>
int main()
{	int a=3;
	for(;a--;)
	{	printf("%d ",a);
	}
	return 0;
}
a)  3 2 1 0
b)  2 1 0
c)  2 1 0 -1
d)  Syntax Error
Correct Answer : b
Explanation : In for loop the initialization and increment/decrement parts can be left blank. So there is not syntax error.
Learn increment decrement operators in depth


Q4: With respect to following for loops in C, pick the best statement Assume that there is a prior declaration of variable i  in all cases
for (i < 10; i = 0 ; i++) // (i)
for (i < 10; i++ ; i = 0) // (ii)
for (i = 0; i < 10 ; i++) // (iii)
for (i = 0; i++ ; i < 10) // (iv)
for (i++; i = 0 ; i < 10) // (v)
for (i++; i < 0 ; i = 10) // (vi)
a)  All the above for loops would compile successfully. Except (iii), the behaviour of all the other forl oops depend on compiler implementation.
b)  All the above for loops would compile successfully.
c)  Only (iii) and (iv) would compile successfully but behaviour of (iv) would depend on compiler implementation.
d)  Only (iii) and (iv) would compile successfully
Correct Answer : b
Explanation : All of the for loops are valid In the above examples, it doesn’t matter what expression has been put in which part of a for loop. The execution order of these expressions remain same irrespective of where they have been put i.e. 1st expression followed by 2nd expression followed by body of the loop followed by 3rd expression.


Previous Topic : for loop - 1Next Topic : while loop - 1