Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. Consider the following C program :
    #include
    int main ()
        {
       int i, j, k = 0;
       j = 2 * 3/ 4 + 2.0/ 5 + 8/ 5;
       k –= – –j;
       for (i = 0; i < 5: i++)
       {
       switch (i + k)
       {
       case 1 :
       case 2 : printf (“\ n%d”, i+k);
       case 3 : printf (“\n%d”, i+k);
       default: printf (“\n%d”, i+k);
       }
      }
     return 0;
    }
    The number of time printf statement is executed is ______.
    1. 10
    2. 5
    3. 21
    4. 100
Correct Option: A

j and k will be evaluated to 2 and – 1 respectively. In for loop:
When i = 0; 1 time printed (– 1)
When i = 1; 1 time printed (0)
When i = 2; 3 times printed (1, 1, 1)
When i = 3; 3 times printed (2, 2, 2)
When i = 4; 2 times printed (3, 3)
∴ on the whole printf is executed 10 times



Your comments will be displayed only after manual approval.