C Case Control Structures Set 1

Ques: 1 What will be output when you will execute following C code?
#include<stdio.h>
int main()
{
     int x=1;
   	switch(x<<2+x)
{
    	default:printf("Williamson");
	case 8: printf("Jhones");        
	case 4: printf(" Brady");
	break;
    	case 16: printf("Break");
	break;
        
     } 
}
A. Williamson
B. Jhones
C. Jhones  Brady
D. Break
E. Nothing gets printed
Description
Answer: C
X<<2+1 evaluates to 1<<3 which is equal to 8.
Case 8 matches Jhones gets printed but since there is no break Brady also gets printed.

Ques: 2 What will be the output of the following program?
#include<stdio.h>
#define A 10
void main(){
     int feel =10;
     switch(feel++,feel*2){
        case A:  printf("joy");
                  break;
	case A|2:printf("angry");
                  break;
	case A*4:printf("excited");
		  break;
	case A*2:printf("sad");
                  break;
        default: printf("happy");
                  break;
     }  
}
A. excited
B. sad
C. angry
D. happy
Description
Answer: D
In c expressions separated by comma operator gets evaluated from left to right
 but right most value is assigned.
int x=(2,8) here 8 gets assigned to x.
here feel++ increases feel to 11 and feel*2 makes it 22 hence switch(22).
Note that feel increases by 1 due 
to postfix operator since comma operator 
is a sequence point in c.
Also note that Case expression can be macro constant.

Ques: 3 What will be the output of the following program?
#include<stdio.h>
void main(){
     int const X=1;
     switch(){
        
        case X<<3:printf("brown");
                  break;
        case X>>1:printf("jill");
                  break;
	case X:   printf("allen");
                  break;
	default:  printf("jack");
     } 
}

A. brown
B. Error
C. allen
D. jack
Description
Answer: B

Ques: 4 What will be the output of the following program?
#include<stdio.h>
int main()
{
    int a = 20, arr[] = {10, 20, 30};
    switch (a)
    {
        case arr[0]: printf("hurry ");
        case arr[1]: printf("think ");
        case arr[2]: printf("ponder");
    }
    return 0;
}

A. Think
B. Think ponder
C. Error
D. hurry
Description
Answer: C
The case labels must be constant inside switch block. Thats why the compile-time error: case label does not reduce to an integer constant is flashed.

Ques: 5 What will be the output of the following program?
#include<stdio.h>
enum color{
    black=5,
    blue=-2,
    yellow,
    red
};
int main(){
     enum color a=0;
     switch(a){
        case black:  printf("blue");
                         break;
        case blue:  printf("Red");
                         break;
        case yellow:printf("yellow");
                         break;
        case red:  printf("default");
     }  
	return 0;
}
A. yellow
B. Error
C. default
D. red
Description
Answer: C

Default value of enum constant 
yellow = -2 +1 = -1
And default value of enum constant 
red = -1 + 1 = 0

Note: Case expression can be enum constant.


Ques: 6 What will be the output of the following program?
#include<stdio.h>
int main(){
     switch(*(1+"ST""WC"+1)){
        case 'U':printf("Google");
                  break;
        case 'S':printf("Yahoo");
                  break;
        case 'W':printf("Bing");
                  break;
        case 'C':printf("Ask");
     }
     return 0;
   
}
A. Google
B. Bing
C. Ask
D. Nothing gets printed
Description
Answer: B
Consider on the expression:
*(1+"ST""WC"+1)
= *(2+"STWC")
=*("WC") 
= 'W'.
Note: Case expression can be character constant.

Ques: 7 What will be the output of the following program?
#include<stdio.h>
void main(){
    char *str="novel"
    str++;
    switch(str){
        case "ovel":printf("America");
                break;
        case "n": printf("Brazil");
                break;
        case "v":  printf("India");
                break;
        case "E":  printf("Spain");
     }  
}

A. America
B. India
C. Error
D. Spain
Description
Answer: C

Case expression cannot be string constant.


Ques: 8 What will be the output of the following program?
#include<stdio.h>
void main(){
    switch(5||2|1){
        case 3&2:printf("sound and fury");
              break;
case 5>=5:printf("we the living");
 break;

        case -~11:printf("darkness at noon");
               break;

        case 6-3<<2:printf("anthem");
               break;
         } 
}


A. We the living 
B. error
C. anthem
D. Sound and fury
Description
Answer: B
Consider on the expression:
5||2|1
=5|| (2|1) //Bitwise or has higher precedence
=5||3
=1
Now, value of each case expression:
3&2 = 2
-~11 = -(-12) =12
6-3<<2 = 3 <<2 = 12
5>=5 = 1
case -~11 and case 6-3<<2 have same constant expression i.e. case 12
In c duplicate case is not possible.

Ques: 9 What will be the output of the following program?
#include<stdio.h>
void main(){
    switch(4)
	{
        case 4.0f:printf("intelligent");
               break;
        case 5.0: printf("good");
               break;
        case 4:printf("average");
               break;
        default:  printf("bad");
        } 
}

A. Intelligent gets printed
B. automatically gets type casted into integer value and average gets printed.
C. error
D. nothing gets printed.
Description
Answer: C
[Error] case label does not reduce to an integer constant.

Ques: 10 What will be the output of the following program?
#include<stdio.h>
void main(){
    switch(3*4/6*5/12)
    {      
    	case 1: printf("hi");
   	     break;
        case NULL: printf("hello");
             break;
        case '\0':printf("you are hungry");
             break;

     	default: printf("get some food");
    }
}
A. Hi
B. [Error] NULL cannot be used in case label
C. You are hungry
D. [Error] duplicate case value


Description
Answer: D
Macro constant NULL has be defined as 0 in stdio.h
ASCII value of character constant '\0' is 0
As we duplicate case is not possible in c.

Ques: 11 What will be the output of the following program?
#include<stdio.h>
void main(){
    unsigned char c=256;
    switch(c){
             printf("Start\t");
		 case 256:
			printf("swift\t");
			break;
   		 case NULL:
			printf("accord");
			break;
    		 default:  
			printf("santro\t");
        		printf("end");
    }
    
}
A. Start swift
B. Start swift end
C. swift
D. accord
Description
Answer: D
256 is beyond the range of unsigned char.
So its value becomes zero(next cycle).
Also statements outside case does not get executed.



Ques: 12 What will be the output of the following program?
#include<stdio.h>
void main(){
     int b=5;
     b=b>=6;
     switch(2){
        case 0:int b=8;
        case 1:int b=10;
        case 2:++b;
        case 3:printf("%d",b);
     }
}
A. 8
B. 10
C. Compilation error
D. 11
Description
Answer: C
We can not declare any variable in any case of switch case statement

Ques: 13 What will be the output of the following program?
#include<stdio.h>
void main(){
    int a=4;
     int b=5;
     switch(b){
        case<4: printf("less than %d",a);   
		break;
     	case >4: printf("more than %d",a);
		break;
	default:
		printf("default");
        
     }
}
A. less than 4
B. More than 4
C. Compilation error
D. default
Description
Answer: C
Comparison cannot be done in case labels.

Ques: 14 What will be the output of the following program?
#include<stdio.h>
int main(){
     int b=5;
     switch(b){
	
	default:printf("default");

        case 5 :printf("you need to jump "); 
		goto jump;  
		break;
     	  jump :printf("label");
		break;
        
     }
     }
A. Error misplaced default
B. Error goto cannot be used in switch
C. Both A and B
D. you need to jump label
Description
Answer: D
It is possible to write label of goto statement in the case of switch case statement.


comments powered by Disqus