GATE | GATE-CS-2015 (Set 3) | Question 65

• Difficulty Level : Medium
• Last Updated : 28 Jun, 2021

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 times printf statement is executed is __________.

(A) 8
(B) 9
(C) 10
(D) 11

Explanation: The following statement makes j = 2

j = 2 * 3 / 4 + 2.0 / 5 + 8 / 5;

The following statement makes k = -1.

k  -= --j;

There is one important thing to note in switch is, there is no break. Let count of printf statements be ‘count’

For i = 0, the value of i+k becomes -1, default block
is executed, count = 1.
For i = 1, the value of i+k becomes 0, default block
is executed, count = 2.
For i = 2, the value of i+k becomes 1, all blocks are
executed as there is no break, count = 5
For i = 3, the value of i+k becomes 2, three blocks
after case 1: are executed, count = 8
For i = 4, the value of i+k becomes 3, two blocks
are executed, count = 10
My Personal Notes arrow_drop_up