What will be output when you will execute following code?

What will be output when you will execute following c code?

#include<stdio.h>

void main(){

int check=2;

switch(check){

case 1: printf("D.W.Steyn");

case 2: printf(" M.G.Johnson");

case 3: printf(" Brett Lee");

default: printf(" M.Muralidaran");

}

}

  1. A.

    M.G.Johnson

  2. B.

    M.Muralidaran

  3. C.

    M.G.Johnson Brett Lee M.Muralidaran

  4. D.

    None of the above

Attempted by 341 students.

Show answer & explanation

Correct answer: C

Key concept: Switch statements fall through when there are no break statements; execution continues into the following cases.

  • The variable check equals 2, so execution jumps to the case labeled 2.

  • Case 2 executes and prints: M.G.Johnson

  • There is no break, so execution falls through to case 3 and prints: Brett Lee

  • Execution then continues to default and prints: M.Muralidaran

  • All printed together (with the spacing from the printf calls):

Final output: M.G.Johnson Brett Lee M.Muralidaran

Explore the full course: Dsssb Tgt Computer Science Paper 2