C Quiz – 104

Question 1
With respect to following “for” loops in C, pick the best statement Assume that there is a prior declaration of 'i' in all cases
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>
Tick
All the above “for” loops would compile successfully.
Cross
All the above “for” loops would compile successfully. Except (iii), the behaviour of all the other “for” loops depend on compiler implementation.
Cross
Only (iii) would compile successfully.
Cross
Only (iii) and (iv) would compile successfully.
Cross
Only (iii) and (iv) would compile successfully but behaviour of (iv) would depend on compiler implementation.


Question 1-Explanation: 
Basically, all of the “for” loops are valid i.e. . In the above examples, it doesn’t matter what expression has been put in which part of a “for” loop. The execution order of these expressions remain same irrespective of where they have been put i.e. \"1st expression\" followed by \"2nd expression\" followed by \"body of the loop\" followed by \"3rd expression\". But the exact behavior of each of the above \"for\" loop depends on the body of loop as well. In fact the following is also valid and work without any issue in C. for(printf(\"1st\") ; printf(\"2nd\") ; printf(\"3rd\")) { break; }
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
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>
Cross
Only (i) and (v) would compile successfully. Also (v) can be used as infinite loop.
Cross
Only (i) would compile successfully.
Cross
All would compile successfully but behavior of (ii), (iii) and (iv) would depend on compiler.
Tick
All would compile successfully.


Question 2-Explanation: 
In C, any of the 3 expressions of “for” loop can be empty. The exact behavior of the loop depends on the body of the loop as well. Basically, all of the 3 expressions of loop can be put inside the loop body. So as per C language standard, all of the above are valid for loops.
Question 3
What’s going to happen when we compile and run the following C program?
#include < stdio.h ><br>
<br>
int main()<br>
{<br>
  int i = 1, j;<br>
  for ( ; ; )<br>
  { <br>
    if (i)<br>
        j = --i;<br>
    if (j < 10)<br>
       printf("GeeksQuiz", j++);<br>
    else<br>
       break;<br>
  }<br>
  return 0;<br>
}<br>
Cross
Compile Error.
Cross
No compile error but it will run into infinite loop printing GeeksQuiz.
Tick
No compile error and it’ll print GeeksQuiz 10 times.
Cross
No compile error but it’ll print GeeksQuiz 9 times.


Question 3-Explanation: 
Basically, even though the for loop doesn\'t have any of three expressions in parenthesis, the initialization, control and increment has been done in the body of the loop. So j would be initialized to 0 via first if. This if itself would be executed only once due to i--. Next if and else blocks are being used to check the value of j and existing the loop if j becomes 10. Please note that j is getting incremented in printf even though there\'s no format specifier in format string. That\'s why GeeksQuiz would be printed for j=0 to j=9 i.e. a total of 10 times.
Question 4
What’s going to happen when we compile and run the following C program?
#include < stdio.h ><br>
int main()<br>
{<br>
 int j = 0;<br>
 for ( ; j < 10 ; )<br>
 { <br>
   if (j < 10)<br>
     printf("Geeks", j++);<br>
   else<br>
     continue;<br>
   printf(“Quiz”);<br>
 }<br>
 return 0;<br>
}<br>
Cross
Compile Error due to use of continue in for loop.
Cross
No compile error but it will run into infinite loop printing Geeks.
Cross
No compile error and it’ll print GeeksQuiz 10 times followed by Quiz once.
Tick
No compile error and it’ll print GeeksQuiz 10 times.


Question 4-Explanation: 
Here, initialization of j has been done outside for loop. if condition serves as control statement and prints GeeksQuiz 10 times due to two printfs. Please note that continue comes in picture when j becomes 10. At that time, second printf gets skipped and second expression in for is checked and it fails. Due to this, for loop ends.
Question 5
Which of the following statement is correct for switch controlling expression?
Cross
Only int can be used in “switch” control expression.
Tick
Both int and char can be used in “switch” control expression.
Cross
All types i.e. int, char and float can be used in “switch” control expression.
Cross
“switch” control expression can be empty as well.


Question 5-Explanation: 
As per C standard, “The controlling expression of a switch statement shall have integer type.” Since char is prompted to integer in switch control expression, it’s allowed but float isn’t promoted. That’s why B is correct statement.
There are 5 questions to complete.

  • Last Updated : 26 Sep, 2023

Share your thoughts in the comments
Similar Reads