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 movie=1;

switch(movie<<2+movie){

default: printf("3 Idiots");

case 4: printf(" Ghajini");

case 5: printf(" Krrish");

case 8: printf(" Race");

}

}

  1. A.

    3 Idiots Ghajini Krrish Race

  2. B.

    Race

  3. C.

    Krrish

  4. D.

    Ghajini Krrish Race

Attempted by 327 students.

Show answer & explanation

Correct answer: B

Expression inside switch: movie << 2 + movie.
Operator precedence: + has higher precedence than <<.
So it becomes movie << (2 + movie) = 1 << (2+1) = 1 << 3 = 8.
So switch(8) matches case 8: and prints " Race". There is no break, but since case 8 is the last case, only "Race" is printed.

Explore the full course: Dsssb Tgt Computer Science Paper 2