c for Looping MCQ


Q1) What is the correct syntax of for loop in C?
  1. for(initalization; condition; incrementoperation)
    {
    //statements
    }

  2. for(declaration; condition; incrementoperation)
    {
    //statements
    }
  3. for(declaration; incrementoperation; condition)
    {
    //statements
    }
  4. for(initalization;condition;incrementoperation;)
    {
    //statements
    }

Answer:- (A).
Explanations :None
Q2) What will be the correct syntax for running two variables in for loop simultaneously?
  1. for (i = 0; i < n; i++)
    for (j = 0; j < n; j += 5)
  2. for (i = 0, j = 0; i < n, j < n; i++, j += 5)
  3. for (i = 0; i < n;i++){ }
    for (j = 0; j < n;j += 5){ }
  4. None of these

Answer:- (B).
Explanations : None.
Q3) What is the output of the given below program?
 #include
int main()
{
   int i = 0;
   for (i)
   {
      printf("Hello");
   }
   return 0;
}
    
  1. Know Program
  2. Know Program is printed infinite times.
  3. Compiled Successfully, No Output
  4. Compile-time error

Answer:- (D).
Explanations : 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
int main()
{
   int i;
   for (i=0; i<3; i++);
   {
      printf("%d", i);
   }
   return 0;
}
  1. 0123
  2. 01234
  3. 4
  4. 3

Answer:- (D).
Explanations : 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
int main()
{
   int i;
   for (i=0; i<=3; i++);
   {
      printf("%d", i);
   }
   return 0;
}
  1. 0123
  2. 01234
  3. 4
  4. 3

Answer:- (C).
Explanations : 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
int main()
{
   int i;
   for (i=5; i>=2; i--);
   {
      printf("%d", i);
   }
   return 0;
}
  1. 54321
  2. 5432
  3. 1
  4. 2

Answer:- (C).
Explanations :None
/. QQ7) Find the output of the given program?
#include
int main()
{
   int i;
   for (;;)
   {
      printf("Know Program");
      break;
   }
   return 0;
}
  1. Know Program
  2. Know Program is printed infinite times.
  3. Compiled Successfully, No Output
  4. Compile-time error

Answer:- (A).
Explanations :NIn 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
int main()
{
   for (;;)
   {
      printf("Know Program");
   }
}
  1. Know Program
  2. Know Program is printed infinite times.
  3. Compiled Successfully, No Output
  4. Compile-time error

Answer:- (B).
Explanations : This syntax of for loop is an example of infinite loop.
Q9) Find the output of the given program?
#include
int main()
{
   int i;
   for (printf("Hello "); printf("Know "); printf("Program "))
   {
      break;
   }
   return 0;
}
  1. Hello
  2. Hello Know
  3. Compiled Successfully, No Output
  4. Compile-time error

Answer:- (B).
Explanations :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
int main()
{
   int i = 500;
   for (; i; i >>=2)
   {
      printf("KnowProgram ");
   }
   return 0;
}
  1. 2
  2. 5
  3. 500
  4. infinite times.

Answer:- (B).
Explanations :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.