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
- Babo
- Error : << is not allowed
- Hurray
- Allas
How ? Why ?
- Integer Requires 2 bytes of memory
- Choice = 1 , Binary Representation ( 0000 0000 0000 0001 )
- After shifting above binary representation towards left by 1 bit (0000 0000 0000 0010)
- Equivalent decimal value of the above binary representation is 2
- So case 2 will be executed
Select Switch Case Question :
Bookmark & Share