C Quiz – 105

1
Question 1
Choose the best statement with respect to following three program snippets.
/*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);
A
All the loops are equivalent i.e. any of the three can be chosen and they all will perform exactly same.
B
continue can't be used with all the three loops in C.
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.
D
None of the above is correct.
Loops & Control Structure    C Quiz - 105    
Discuss it


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.
A
“break” can be used in “for”, “while” and “do-while” loop body.
B
“continue” can be used in “for”, “while” and “do-while” loop body.
C
“break” and “continue” can be used in “for”, “while”, “do-while” loop body and “switch” body.
D
“break” and “continue” can be used in “for”, “while” and “do-while” loop body. But only “break” can be used in “switch” body.
E
“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.
Loops & Control Structure    C Quiz - 105    
Discuss it


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?
#include "stdio.h"
int main()
{
  int i; 
  goto LOOP;
  for (i = 0 ; i < 10 ; i++)
  {
     printf("GeeksQuiz\n");
     LOOP:
      break;
  }
  return 0;
}
A
No compile error and it will print GeeksQuiz 10 times because goto label LOOP wouldn’t come in effect.
B
No compile error and it’ll print GeeksQuiz only once because goto label LOOP would come in picture only after entering for loop.
C
Compile Error because any goto label isn’t allowed in for loop in C.
D
No compile error but behaviour of the program would depend on C compiler due to nondeterministic behaviour of goto statement.
E
No compile error and it will not print anything.
Loops & Control Structure    C Quiz - 105    
Discuss it


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:
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?
A
“switch” body may not have any “case” label at all and it would still compile.
B
“switch” body may not have the “default” label and it would still compile.
C
“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.
D
“switch” body may not have any “break” statement and it would still compile.
E
“switch” body can have the “default” label at first i.e. before all the other “case” labels. It would still compile.
Loops & Control Structure    C Quiz - 105    
Discuss it


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?
A
goto
B
continue
C
break
D
return
E
All of the above.
Loops & Control Structure    C Quiz - 105    
Discuss it


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.
1

Company Wise Coding Practice    Topic Wise Coding Practice