For Loop MCQ in C

The for loop in C programming language is a pre-test loop, where first of all initialization expression is evaluated then the condition is checked and if the condition is true then only the statements of the for loop are executed.

Q1) What is the correct syntax of for loop in C?

a)

for(initalization; condition; incrementoperation)
{
   //statements
}

b)

for(declaration; condition; incrementoperation)
{
   //statements
}

c)

for(declaration; incrementoperation; condition)
{
   //statements
}

d)

for(initalization; condition; incrementoperation;)
{
   //statements
}
View Answer Answer:- a)

for(initalization; condition; incrementoperation)
{
   //statements
}

Q2) What will be the correct syntax for running two variables in for loop simultaneously?

a)

for (i = 0; i < n; i++)
for (j = 0; j < n; j += 5)

b) for (i = 0, j = 0; i < n, j < n; i++, j += 5)

c)

for (i = 0; i < n;i++){ }
for (j = 0; j < n;j += 5){ }

d) None of these

View Answer Answer:- b)

for (i = 0, j = 0; i < n, j < n; i++, j += 5)

Q3) What is the output of the given below program?

#include<stdio.h>
int main()
{
   int i = 0;
   for (i)
   {
      printf("Hello");
   }
   return 0;
}

a) Know Program
b) Know Program is printed infinite times.
c) Compiled Successfully, No Output
d) Compile-time error

View Answer Answer:- d) Compile-time error

We must use the semicolon if we are not passing all three values in the FOR loop. The valid syntax will be:- for (i;;)

Q4) What is the output of the given below program?

#include<stdio.h>
int main()
{
   int i;
   for (i=0; i<3; i++);
   {
      printf("%d", i);
   }
   return 0;
}

a) 0123
b) 01234
c) 4
d) 3

View Answer Answer:- d) 3

Notice a semicolon is at the end of the for loop. The printf(“%d”, i); is not part of the for loop, it is outside of the loop therefore it will be executed after executing the for loop.

Q5) Find the output of the given program?

#include<stdio.h>
int main()
{
   int i;
   for (i=0; i<=3; i++);
   {
      printf("%d", i);
   }
   return 0;
}

a) 0123
b) 01234
c) 4
d) 3

View Answer Answer:- c) 4

It is similar to the previous program but this time for loop will be executed 4 times, and value of “i” becomes 4.

Q6) What is the output of the given below program?

#include<stdio.h>
int main()
{
   int i;
   for (i=5; i>=2; i--);
   {
      printf("%d", i);
   }
   return 0;
}

a) 54321
b) 5432
c) 1
d) 2

View Answer Answer:- c) 1

Q7) Find the output of the given program?

#include<stdio.h>
int main()
{
   int i;
   for (;;)
   {
      printf("Know Program");
      break;
   }
   return 0;
}

a) Know Program
b) Know Program is printed infinite times.
c) Compiled Successfully, No Output
d) Compile-time error

View Answer Answer:- a) Know Program

In the first iteration, break statement will be executed, which terminates the execution of the loop.

Q8) What is the output of the given below program?

#include<stdio.h>
int main()
{
   for (;;)
   {
      printf("Know Program");
   }
}

a) Know Program
b) Know Program is printed infinite times.
c) Compiled Successfully, No Output
d) Compile-time error

View Answer Answer:- b) Know Program is printed infinite times.

This syntax of for loop is an example of infinite loop.

Q9) Find the output of the given program?

#include<stdio.h>
int main()
{
   int i;
   for (printf("Hello "); printf("Know "); printf("Program "))
   {
      break;
   }
   return 0;
}

a) Hello
b) Hello Know
c) Hello Know Program
c) Compiled Successfully, No Output
d) Compile-time error

View Answer Answer:- b) Hello Know

For loop execution process:- initialization, condition checking, statement execution, update. In initialization “Hello” will be printed, in condition checking “Know” will be printed, in the statement execution break will be executed which terminates the loop, and control came out of the loop.

Q10) How Many times will KnowProgram be printed in the below C program?

#include<stdio.h>
int main()
{
   int i = 500;
   for (; i; i >>=2)
   {
      printf("KnowProgram ");
   }
   return 0;
}

a) 2
b) 5
c) 500
d) infinite times.

View Answer Answer:- b) 5

The expression set >>= 2; means set = set >> 2; that is right shift bits of set by 2 (self assigned form of >> bitwise right shift operator. Therefore on each iteration “i” will be divided by 4.

Q11) Find the output of the given program?

#include<stdio.h>
int main()
{
   int i = 0;
   for (i < 5; i++)
   {
      printf("Know Program");
   }
   return 0;
}

a) Know Program
b) Know Program is printed infinite times.
c) Compiled Successfully, No Output
d) Compile-time error
e) None of These

View Answer Answer:- d) Compile-time error

We must either pass initialization, condition, and update statement or must use semicolons. Here the correct form is:- for (;i < 5; i++)

Q12) Find the output of the given program?

#include<stdio.h>
int main()
{
   int i = 0;
   for (i=1.0; i<5.0; i++)
   {
      printf("%d", i);
   }
   return 0;
}

a) 1234
b) 12345
c) Compiled Successfully, No Output
d) Compile-time error

View Answer Answer:- a) 1234

Q13) What is the output of the given below program?

#include<stdio.h>
int main()
{
   int i = 0;
   for (i=1.3; i<5.5; i++)
   {
      printf("%d", i);
   }
   return 0;
}

a) 1234
b) 12345
c) Compiled Successfully, No Output
d) Compile-time error

View Answer Answer:- b) 12345

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *