C Quiz – 104

1
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)
for (i < 10; i++ ; i = 0) // (ii)
for (i = 0; i < 10 ; i++) // (iii)
for (i = 0; i++ ; i < 10) // (iv)
for (i++; i = 0 ; i < 10) // (v)
for (i++; i < 0 ; i = 10) // (vi)
A
All the above “for” loops would compile successfully.
B
All the above “for” loops would compile successfully. Except (iii), the behaviour of all the other “for” loops depend on compiler implementation.
C
Only (iii) would compile successfully.
D
Only (iii) and (iv) would compile successfully.
E
Only (iii) and (iv) would compile successfully but behaviour of (iv) would depend on compiler implementation.
Loops & Control Structure    C Quiz - 104    
Discuss it


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)
for ( ; i < 10 ; i++) // (ii)
for (i = 0;  ; i++) // (iii)
for (i = 0; i < 10 ; ) // (iv)
for ( ; ; ) // (v)
A
Only (i) and (v) would compile successfully. Also (v) can be used as infinite loop.
B
Only (i) would compile successfully.
C
All would compile successfully but behavior of (ii), (iii) and (iv) would depend on compiler.
D
All would compile successfully.
Loops & Control Structure    C Quiz - 104    
Discuss it


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"

int main()
{
  int i = 1, j;
  for ( ; ; )
  { 
    if (i)
        j = --i;
    if (j < 10)
       printf("GeeksQuiz", j++);
    else
       break;
  }
  return 0;
}
A
Compile Error.
B
No compile error but it will run into infinite loop printing GeeksQuiz.
C
No compile error and it’ll print GeeksQuiz 10 times.
D
No compile error but it’ll print GeeksQuiz 9 times.
Loops & Control Structure    C Quiz - 104    
Discuss it


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"
int main()
{
 int j = 0;
 for ( ; j < 10 ; )
 { 
   if (j < 10)
     printf("Geeks", j++);
   else
     continue;
   printf(“Quiz”);
 }
 return 0;
}
A
Compile Error due to use of continue in for loop.
B
No compile error but it will run into infinite loop printing Geeks.
C
No compile error and it’ll print GeeksQuiz 10 times followed by Quiz once.
D
No compile error and it’ll print GeeksQuiz 10 times.
Loops & Control Structure    C Quiz - 104    
Discuss it


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?
A
Only int can be used in “switch” control expression.
B
Both int and char can be used in “switch” control expression.
C
All types i.e. int, char and float can be used in “switch” control expression.
D
“switch” control expression can be empty as well.
Loops & Control Structure    C Quiz - 104    
Discuss it


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

Company Wise Coding Practice    Topic Wise Coding Practice