C 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?
Cross
10
Tick
11
Cross
Infinite
Cross
The program will show compile-time error


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

C

#include <stdio.h>
#define PRINT(i, limit) do 
                        { 
                            if (i++ < limit) 
                            { 
                                printf("GeeksQuizn"); 
                                continue; 
                            } 
                        }while(0);

int main()
{
    int i = 0;
    PRINT(i, 3);
    return 0;
}

How many times GeeksQuiz is printed in the above program ?
 

Tick

1
 

Cross

3
 

Cross

4
 

Cross

Compile-time error
 



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? 

C

#include <stdio.h>

int main() {
    int i = 2;
    switch (i) {
        case 0:
            printf("Geeks");
            break;
        case 1:
            printf("Quiz");
            break;
        default:
            printf("GeeksQuiz");
    }
    return 0;
}
Cross

Geeks

Cross

Quiz

Tick

GeeksQuiz

Cross

Compile-time error



Question 3-Explanation: 

As i=2, so GeeksQuiz is printed.

Question 4

C

#include <stdio.h>

int main() {
    int i = 3;
    switch (i) {
        case 1:
            printf("Geeks");
            break;
        case 1+2:
            printf("Quiz");
            break;
        default:
            printf("GeeksQuiz");
    }
    return 0;
}

What is the output of the above program?

Cross

Geeks

Tick

Quiz

Cross

GeeksQuiz

Cross

Compile-time error



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: 

C

#include <stdio.h>

#define EVEN 0
#define ODD 1

int main() {
    int i = 3;
    switch (i % 2) {
        case EVEN:
            printf("Even");
            break;
        case ODD:
            printf("Odd");
            break;
        default:
            printf("Default");
    }
    return 0;
}
Cross

Even

Tick

Odd

Cross

Default

Cross

Compile-time error



Question 5-Explanation: 

The following C code will print 'odd' as the output of the program.

Question 6

C

#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?

Cross

3

Cross

5

Tick

03

Cross

05



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.
Therefore, '03' gets printed here.

Question 7

C

#include <stdio.h>

int i;

int main() {
    if (i) {
        // Do nothing
    } else {
        printf("Else");
    }
    return 0;
}

What is correct about the above program?

Cross

if block is executed.

Tick

else block is executed.

Cross

It is unpredictable as i is not initialized.

Cross

Error: misplaced else



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. . Therefore, the else block gets executed.

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?
Cross
9 7 5 3 1
Cross
9 8 7 6 5 4 3 2 1
Tick
Infinite Loop
Cross
9 7 5 3


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 9 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 ("%dn", no);
    return 0;
}
Cross
1
Tick
Runtime Error
Cross
0
Cross
Compiler Error


Question 9-Explanation: 
There is a bug in the above program. It goes inside the do-while loop for c = 0. Also as the increment is post increment, so (no/0) will create a divide by 0 error. 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;
}
Cross
5 10 15 20
Cross
7 12 17 22
Tick
16 21
Cross
Compiler Error


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 46 questions to complete.

  • Last Updated : 28 Sep, 2023

Share your thoughts in the comments
Similar Reads