Exercise: Questions on Loops & Control Structure


Questions on Loops & Control Structure : Question 1 :
 #include <stdio.h>
 
 int main()
 {
     int i = 1024;
     for (; i; i >>= 1)
         printf("PROcoder");
     return 0;
 }
 
How many times will PROcoder be printed in the above program?

10
11
Infinite
The program will show compile-time error
Show Answer
Questions on Loops & Control Structure : Question 2 :
 #include <stdio.h>
 #define PRINT(i, limit) do \
                         { \
                             if (i++ < limit) \
                             { \
                                 printf("PROcoder\n"); \
                                 continue; \
                             } \
                         }while(0)
 
 int main()
 {
     int i = 0;
     PRINT(i, 3);
     return 0;
 }
 
How many times PROcoder is printed in the above program ?

1
3
4
Compile-time error
Show Answer
Questions on Loops & Control Structure : Question 3 :
What is the output of the below program?
 #include <stdio.h>
 int main()
 {
     int i = 0;
     switch (i)
     {
         case '0': printf("PRO");
                 break;
         case '1': printf("coder");
                 break;
         default: printf("PROcoder");
     }
     return 0;
 } 
 

PRO
coder
PROcoder
Compile-time error
Show Answer
Questions on Loops & Control Structure : Question 4 :
 #include <stdio.h>
 int main()
 {
     int i = 3;
     switch (i)
     {
         case 0+1: printf("PRO");
                 break;
         case 1+2: printf("coder");
                 break;
         default: printf("PROcoder");
     }
     return 0;
 }
 
What is the output of the above program?

PRO
coder
PROcoder
Compile-time error
Show Answer
Questions on Loops & Control Structure : 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;
}

Even
Odd
Default
Compile-time error
Show Answer
Questions on Loops & Control Structure : 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?

3
5
03
05
Show Answer
Questions on Loops & Control Structure : Question 7 :
#include <stdio.h>
int i;
int main()
{
    if (i);
    else
        printf("Ëlse");
    return 0;
}
What is correct about the above program?

if block is executed.
else block is executed.
It is unpredictable as i is not initialized.
Error: misplaced else
Show Answer
Questions on Loops & Control Structure : 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?

9 7 5 3 1
9 8 7 6 5 4 3 2 1
Infinite Loop
9 7 5 3
Show Answer
Questions on Loops & Control Structure : Question 9 :
Output?
#include <stdio.h>
int main()
{
    int c = 5, no = 10;
    do {
        no /= c;
    } while(c--);
 
    printf ("%d\n", no);
    return 0;
}

1
Runtime Error
0
Compiler Error
Show Answer
Questions on Loops & Control Structure : 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;
}

5 10 15 20
7 12 17 22
16 21
Compiler Error
Show Answer