Loops & Control Structure

Question 1
#include <stdio.h>

int main()
{
    int i = 1024;
    for (; i; i >>= 1)
        printf("GeeksQuiz");
    return 0;
}
How many times will GeeksQuiz be printed in the above program?
A
10
B
11
C
Infinite
D
The program will show compile-time error
Loops & Control Structure    
Discuss it


Question 1 Explanation: 
In for loop, mentioning expression is optional. >>= is a composite operator. It shifts the binary representation of the value by 1 to the right and assigns the resulting value to the same variable. The for loop is executed until value of variable i doesn't drop to 0.
Question 2
#include <stdio.h>
#define PRINT(i, limit) do \
                        { \
                            if (i++ < limit) \
                            { \
                                printf("GeeksQuiz\n"); \
                                continue; \
                            } \
                        }while(0)

int main()
{
    int i = 0;
    PRINT(i, 3);
    return 0;
}
How many times GeeksQuiz is printed in the above program ?
A
1
B
3
C
4
D
Compile-time error
Loops & Control Structure    
Discuss it


Question 2 Explanation: 
If a macro needs to be expanded in multiple lines, it is the best practice to write those lines within do{ }while(0) to avoid macro side effects. After GeeksQuiz is printed once, the control reaches the while statement to check for the condition. Since, the condition is false, the loop gets terminated.
Question 3
What is the output of the below program?
#include <stdio.h>
int main()
{
    int i = 0;
    switch (i)
    {
        case '0': printf("Geeks");
                break;
        case '1': printf("Quiz");
                break;
        default: printf("GeeksQuiz");
    }
    return 0;
} 
A
Geeks
B
Quiz
C
GeeksQuiz
D
Compile-time error
Loops & Control Structure    
Discuss it


Question 3 Explanation: 
At first look, the output of the program seems to be Geeks. But, the cases are labeled with characters which gets converted to their ascii values 48(for 0) and 49(for 1). None of the cases is labeled with value 0. So, the control goes to the default block and GeeksQuiz is printed.
Question 4
#include <stdio.h>
int main()
{
    int i = 3;
    switch (i)
    {
        case 0+1: printf("Geeks");
                break;
        case 1+2: printf("Quiz");
                break;
        default: printf("GeeksQuiz");
    }
    return 0;
}
What is the output of the above program?
A
Geeks
B
Quiz
C
GeeksQuiz
D
Compile-time error
Loops & Control Structure    
Discuss it


Question 4 Explanation: 
Expression gets evaluated in cases. The control goes to the second case block after evaluating 1+2 = 3 and Quiz is printed.
Question 5
Predict the output of the below program:
#include <stdio.h>
#define EVEN 0
#define ODD 1
int main()
{
    int i = 3;
    switch (i & 1)
    {
        case EVEN: printf("Even");
                break;
        case ODD: printf("Odd");
                break;
        default: printf("Default");
    }
    return 0;
}
A
Even
B
Odd
C
Default
D
Compile-time error
Loops & Control Structure    
Discuss it


Question 5 Explanation: 
The expression i & 1 returns 1 if the rightmost bit is set and returns 0 if the rightmost bit is not set. As all odd integers have their rightmost bit set, the control goes to the block labeled ODD.
Question 6
#include <stdio.h>
int main()
{
    int i;
    if (printf("0"))
        i = 3;
    else
        i = 5;
    printf("%d", i);
    return 0;
} 
Predict the output of above program?
A
3
B
5
C
03
D
05
Loops & Control Structure    
Discuss it


Question 6 Explanation: 
The control first goes to the if statement where 0 is printed. The printf("0") returns the number of characters being printed i.e. 1. The block under if statement gets executed and i is initialized with 3.
Question 7
#include <stdio.h>
int i;
int main()
{
    if (i);
    else
        printf("Ëlse");
    return 0;
}
What is correct about the above program?
A
if block is executed.
B
else block is executed.
C
It is unpredictable as i is not initialized.
D
Error: misplaced else
Loops & Control Structure    
Discuss it


Question 7 Explanation: 
Since i is defined globally, it is initialized with default value 0. The Else block is executed as the expression within if evaluates to FALSE. Please note that the empty block is equivalent to a semi-colon(;). So the statements if (i); and if (i) {} are equivalent.
Question 8
#include<stdio.h>
int main()
{
   int n;
   for (n = 9; n!=0; n--)
     printf("n = %d", n--);
   return 0;
}
What is the output?
A
9 7 5 3 1
B
9 8 7 6 5 4 3 2 1
C
Infinite Loop
D
9 7 5 3
Loops & Control Structure    
Discuss it


Question 8 Explanation: 
The program goes in an infinite loop because n is never zero when loop condition (n != 0) is checked. n changes like 7 5 3 1 -1 -3 -5 -7 -9 ...
Question 9
Output?
#include <stdio.h>
int main()
{
    int c = 5, no = 10;
    do {
        no /= c;
    } while(c--);
 
    printf ("%d\n", no);
    return 0;
}
A
1
B
Runtime Error
C
0
D
Compiler Error
Loops & Control Structure    
Discuss it


Question 9 Explanation: 
There is a bug in the above program. It goes inside the do-while loop for c = 0 also. So it fails during runtime.
Question 10
# include <stdio.h>
int main()
{
   int i = 0;
   for (i=0; i<20; i++)
   {
     switch(i)
     {
       case 0:
         i += 5;
       case 1:
         i += 2;
       case 5:
         i += 5;
       default:
         i += 4;
         break;
     }
     printf("%d  ", i);
   }
   return 0;
}
A
5 10 15 20
B
7 12 17 22
C
16 21
D
Compiler Error
Loops & Control Structure    
Discuss it


Question 10 Explanation: 
Initially i = 0. Since case 0 is true i becomes 5, and since there is no break statement till last statement of switch block, i becomes 16. Now in next iteration no case is true, so execution goes to default and i becomes 21. In C, if one case is true switch block is executed until it finds break statement. If no break statement is present all cases are executed after the true case. If you want to know why switch is implemented like this, well this implementation is useful for situations like below.
 switch (c)
 {
    case 'a':
    case 'e':
    case 'i' :
    case 'o':
    case 'u':
      printf(" Vowel character");
      break;
    default :
      printf("Not a Vowel character");; break;
  }
There are 34 questions to complete.
 

Company Wise Coding Practice    Topic Wise Coding Practice