C Loops & Control Structure

Last Updated :
Discuss
Comments

Question 1

Consider the following program fragment i=6720; j=4; while (i%j)==0 { i=i/j; j=j+1; } On termination j will have the value

  • 4

  • 8

  • 9

  • 6720

Question 2

With respect to following “for” loops in C, pick the best statement Assume that there is a prior declaration of 'i' in all cases
C
for (i < 10; i = 0 ; i++) // (i)<br>
for (i < 10; i++ ; i = 0) // (ii)<br>
for (i = 0; i < 10 ; i++) // (iii)<br>
for (i = 0; i++ ; i < 10) // (iv)<br>
for (i++; i = 0 ; i < 10) // (v)<br>
for (i++; i < 0 ; i = 10) // (vi)<br>
  • All the above “for” loops would compile successfully.
  • All the above “for” loops would compile successfully. Except (iii), the behaviour of all the other “for” loops depend on compiler implementation.
  • Only (iii) would compile successfully.
  • Only (iii) and (iv) would compile successfully.
  • Only (iii) and (iv) would compile successfully but behaviour of (iv) would depend on compiler implementation.

Question 3

With respect to following “for” loops in C, pick the best statement. Assume that there is a prior declaration of 'i' in all cases
C
for (i = 0; i < 10 ; i++) // (i)<br>
for ( ; i < 10 ; i++) // (ii)<br>
for (i = 0;  ; i++) // (iii)<br>
for (i = 0; i < 10 ; ) // (iv)<br>
for ( ; ; ) // (v)<br>
  • Only (i) and (v) would compile successfully. Also (v) can be used as infinite loop.
  • Only (i) would compile successfully.
  • All would compile successfully but behavior of (ii), (iii) and (iv) would depend on compiler.
  • All would compile successfully.

Question 4

What’s going to happen when we compile and run the following C program?


C
#include < stdio.h >
int main()
{
  int i = 1, j;
  for ( ; ; )
  { 
    if (i)
        j = --i;
    if (j < 10)
       printf("GeeksQuiz", j++);
    else
       break;
  }
  return 0;
}
  • Compile Error.

  • No compile error but it will run into infinite loop printing GeeksQuiz.

  • No compile error and it’ll print GeeksQuiz 10 times.

  • No compile error but it’ll print GeeksQuiz 9 times.

Question 5

What’s going to happen when we compile and run the following C program?


C
#include < stdio.h >
int main()
{
 int j = 0;
 for ( ; j < 10 ; )
 { 
   if (j < 10)
     printf("Geeks", j++);
   else
     continue;
   printf(Quiz);
 }
 return 0;
}
  • Compile Error due to use of continue in for loop.

  • No compile error but it will run into infinite loop printing Geeks.

  • No compile error and it’ll print GeeksQuiz 10 times followed by Quiz once.

  • No compile error and it’ll print GeeksQuiz 10 times.

Question 6

Which of the following statement is correct for switch controlling expression?

  • Only int can be used in “switch” control expression.
  • Both int and char can be used in “switch” control expression.
  • All types i.e. int, char and float can be used in “switch” control expression.
  • “switch” control expression can be empty as well.

Question 7

Choose the best statement with respect to following three program snippets.


C
/*Program Snippet 1 with for loop*/
for (i = 0; i < 10; i++)
{
   /*statement1*/
   continue;
   /*statement2*/
}

/*Program Snippet 2 with while loop*/
i = 0;
while (i < 10)
{
   /*statement1*/
   continue;
   /*statement2*/
   i++;
}

/*Program Snippet 3 with do-while loop*/
i = 0;
do
{
   /*statement1*/
   continue;
   /*statement2*/
   i++;
}while (i < 10);


  • All the loops are equivalent i.e. any of the three can be chosen and they all will perform exactly same.

  • continue can't be used with all the three loops in C.

  • After hitting the continue; statement in all the loops, the next expression to be executed would be controlling expression (i.e. i < 10) in all the 3 loops.

  • None of the above is correct.

Question 8

In the context of "break" and "continue" statements in C, pick the best statement.

  • “break” can be used in “for”, “while” and “do-while” loop body.
  • “continue” can be used in “for”, “while” and “do-while” loop body.
  • “break” and “continue” can be used in “for”, “while”, “do-while” loop body and “switch” body.
  • “break” and “continue” can be used in “for”, “while” and “do-while” loop body. But only “break” can be used in “switch” body.
  • “break” and “continue” can be used in “for”, “while” and “do-while” loop body. Besides, “continue” and “break” can be used in “switch” and “if-else” body.

Question 9

What would happen when we compile and run this program?


C
#include <stdio.h>
int main()
{
  int i; 
  goto LOOP;
  for (i = 0 ; i < 10 ; i++)
  {
     printf("GeeksQuiz &#92;n");
     LOOP:
      break;
  }
  return 0;
}


  • No compile error and it will print GeeksQuiz 10 times because goto label LOOP wouldn’t come in effect.

  • No compile error and it’ll print GeeksQuiz only once because goto label LOOP would come in picture only after entering for loop.

  • Compile Error because any goto label isn’t allowed in for loop in C.

  • No compile error but behaviour of the program would depend on C compiler due to nondeterministic behaviour of goto statement.

  • No compile error and it will not print anything.

Question 10

A typical “switch” body looks as follows:


C
switch (controlling_expression)
{
  case label1:
    /*label1 statements*/
    break;
  case label2:
    /*label1 statements*/
    break;
  default:
    /*Default statements*/
}


Which of the following statement is not correct statement?

  • “switch” body may not have any “case” label at all and it would still compile.

  • “switch” body may not have the “default” label and it would still compile.

  • “switch” body may contain more than one “case” labels where the label value of these “case” is same and it would still compile. If “switch” controlling expression results in this “case” label value, the “case” which is placed first would be executed.

  • “switch” body may not have any “break” statement and it would still compile.

  • “switch” body can have the “default” label at first i.e. before all the other “case” labels. It would still compile.

Tags:

There are 45 questions to complete.

Take a part in the ongoing discussion