C Quiz – 105

Question 1
Choose the best statement with respect to following three program snippets.
<br>
/*Program Snippet 1 with for loop*/<br>
for (i = 0; i < 10; i++)<br>
{<br>
   /*statement1*/<br>
   continue;<br>
   /*statement2*/<br>
}<br>

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

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

Cross
All the loops are equivalent i.e. any of the three can be chosen and they all will perform exactly same.
Cross
continue can't be used with all the three loops in C.
Cross
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.
Tick
None of the above is correct.


Question 1-Explanation: 

First and foremost, continue can be used in any of the 3 loops in C. In case of “for” loop, when continue is hit, the next expression to be executed would be i++ followed by controlling expression (i.e. i < 10). In case of “while” loop, when continue is hit, the next expression to be executed would be controlling expression (i.e. i < 10). In case of “do-while” loop, when continue is hit, the next expression to be executed would be controlling expression (i.e. i < 10). That’s why “while” and “do-while” loops would behave exactly same but not the “for” loop. Just to re-iterate, i++ would be executed in “for” loop when continue is hit.

Question 2
In the context of "break" and "continue" statements in C, pick the best statement.
Cross
“break” can be used in “for”, “while” and “do-while” loop body.
Cross
“continue” can be used in “for”, “while” and “do-while” loop body.
Cross
“break” and “continue” can be used in “for”, “while”, “do-while” loop body and “switch” body.
Tick
“break” and “continue” can be used in “for”, “while” and “do-while” loop body. But only “break” can be used in “switch” body.
Cross
“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 2-Explanation: 
As per C standard, “continue” can be used in loop body only. “break” can be used in loop body and switch body only. That’s why correct answer is D.
Question 3
What would happen when we compile and run this program?
<br>
#include < stdio.h ><br>
int main()<br>
{<br>
  int i; <br>
  goto LOOP;<br>
  for (i = 0 ; i < 10 ; i++)<br>
  {<br>
     printf("GeeksQuiz &#92;n");<br>
     LOOP:<br>
      break;<br>
  }<br>
  return 0;<br>
}<br>

Cross
No compile error and it will print GeeksQuiz 10 times because goto label LOOP wouldn’t come in effect.
Cross
No compile error and it’ll print GeeksQuiz only once because goto label LOOP would come in picture only after entering for loop.
Cross
Compile Error because any goto label isn’t allowed in for loop in C.
Cross
No compile error but behaviour of the program would depend on C compiler due to nondeterministic behaviour of goto statement.
Tick
No compile error and it will not print anything.


Question 3-Explanation: 
goto statement can be used inside a function and its label can point to anywhere in the same function. Here, for loop expressions i.e. i = 0 and i < 10 and i++ wouldn’t be executed at all. Because goto would make the program jump directly inside the for loop. And from there, it’ll execute break statement which would exit the loop. So effectively nothing would be printed.
Question 4
A typical “switch” body looks as follows:
<br>
switch (controlling_expression)<br>
{<br>
  case label1:<br>
    /*label1 statements*/<br>
    break;<br>
  case label2:<br>
    /*label1 statements*/<br>
    break;<br>
  default:<br>
    /*Default statements*/<br>
}<br>

Which of the following statement is not correct statement?
Cross
“switch” body may not have any “case” label at all and it would still compile.
Cross
“switch” body may not have the “default” label and it would still compile.
Tick
“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.
Cross
“switch” body may not have any “break” statement and it would still compile.
Cross
“switch” body can have the “default” label at first i.e. before all the other “case” labels. It would still compile.


Question 4-Explanation: 
In \"switch\" body, two \"case\" can\'t result in same value. Though having only \"case\" or only \"default\" is okay. In fact, \"switch\" body can be empty also.
Question 5
Which of the following is correct with respect to “Jump Statements” in C?
Cross
goto
Cross
continue
Cross
break
Cross
return
Tick
All of the above.


Question 5-Explanation: 
As per C standard, “A jump statement causes an unconditional jump to another place”. So if we see carefully, all “goto”, “continue”, “break” and “return” makes the program control jump unconditionally from one place to another. That’s why correct answer is E.
There are 5 questions to complete.


  • Last Updated : 26 Sep, 2023

Share your thoughts in the comments
Similar Reads