Switch case MCQ 10 : Shift Operators in Switch

Problem Statement 10 :

void main()
{
int choice = 1 ;
switch(choice<<1)
{

case 2:
printf("nAllas");
break;

case 4:
printf("nBabo");
break;

case 8:
printf("nHurray");
break;
}
}
Options : Guess the Output
  1. Babo
  2. Error : << is not allowed
  3. Hurray
  4. Allas



How ? Why ?
  1. Integer Requires 2 bytes of memory
  2. Choice = 1 , Binary Representation ( 0000 0000 0000 0001 )
  3. After shifting above binary representation towards left by 1 bit (0000 0000 0000 0010)
  4. Equivalent decimal value of the above binary representation is 2
  5. So case 2 will be executed

Select Switch Case Question :


Bookmark & Share