switch case questions in c language | CTechnotips

Technical questions in c


C programming technical questions and answers with explanation


1.
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(" Mohammad Asif");
        default: printf(" M.Muralidaran");
     }
}
Choose all that apply:

(A) M.G.Johnson
(B) M.Muralidaran
(C)
M.G.Johnson Mohammad Asif M.Muralidaran

(D) Compilation error
(E) None of the above
Output : (C)

Explanation:

If we will not use break keyword in each case the program control will come in each case after the case witch satisfy the switch condition.

2.
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");
     } 
}
Choose all that apply:
(A) 3 Idiots Ghajini Krrish Race
(B) Race
(C) Krrish
(D) Ghajini Krrish Race
(E) Compilation error
Output : (B)

Explanation:

We can write case statement in any order including the default case. That default case may be first case, last case or in between the any case in the switch case statement.

3.
What will be output when you will execute following c code?
#include<stdio.h>
#define L 10
void main(){
     auto money=10;
     switch(money,money*2){
        case L:  printf("Willian");
                  break;
        case L*2:printf("Warren");
                  break;
        case L*3:printf("Carlos");
                  break;
        default: printf("Lawrence");
        case L*4:printf("Inqvar");
                  break;
     }  
}
Choose all that apply:

(A) Willian
(B) Warren
(C) Lawrence Inqvar
(D) Compilation error: Misplaced default
(E) None of the above
Output : (B)

Explanation:

In c comma is also operator which enjoy least precedence. So if
x = (a , b);
Then x = b
Note: Case expression can be macro constant.

4.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
     int const X=0;
     switch(5/4/3){
        case X:  printf("Clinton");
                  break;
        case X+1:printf("Gandhi");
                  break;
        case X+2:printf("Gates");
                  break;
        default: printf("Brown");
     } 
}
Choose all that apply:

(A) Clinton
(B) Gandhi
(C) Gates
(D) Brown
(E) Compilation error
Output : (E)

Explanation:

Case expression cannot be constant variable.

5.
What will be output when you will execute following c code?
#include<stdio.h>
enum actor{
    SeanPenn=5,
    AlPacino=-2,
    GaryOldman,
    EdNorton
};
void main(){
     enum actor a=0;
     switch(a){
        case SeanPenn:  printf("Kevin Spacey");
                         break;
        case AlPacino:  printf("Paul Giamatti");
                         break;
        case GaryOldman:printf("Donald Shuterland");
                         break;
        case EdNorton:  printf("Johnny Depp");
     }  
}
Choose all that apply:

(A) Kevin Spacey
(B) Paul Giamatti
(C) Donald Shuterland
(D) Johnny Depp
(E) Compilation error
Output : (D)

Explanation:

Default value of enum constant 
GaryOldman = -2 +1 = -1
And default value of enum constant 
EdNorton = -1 + 1 = 0
Note: Case expression can be enum constant.

6.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
     switch(*(1+"AB" "CD"+1)){
        case 'A':printf("Pulp Fiction");
                  break;
        case 'B':printf("12 Angry Man");
                  break;
        case 'C':printf("Casabance");
                  break;
        case 'D':printf("Blood Diamond");
     }
    
}
Choose all that apply:

(A) Pulp Fiction
(B) 12 Angry Man
(C) Casabance
(D) Blood Diamond
(E) Compilation error
Output : (C)

Explanation:

Consider on the expression:
*(1+"AB" "CD"+1)
= *(2+"AB" "CD")
= *(2+"ABCD")
=*("CD")
='C'
Note: Case expression can be character constant.

7.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
    char *str="ONE"
    str++;
    switch(str){
        case "ONE":printf("Brazil");
                break;
        case "NE": printf("Toy story");
                break;
        case "N":  printf("His Girl Friday");
                break;
        case "E":  printf("Casino Royale");
     }  
}
Choose all that apply:

(A) Brazil
(B) Toy story
(C) His Girl Friday
(D) Casino Royale
(E) Compilation error
Output : (E)

Explanation:

Case expression cannot be string constant.

8.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
    switch(5||2|1){
        case 3&2:printf("Anatomy of a Murder");
              break;
        case -~11:printf("Planet of Apes");
               break;
        case 6-3<<2:printf("The conversation");
               break;
    case 5>=5:printf("Shaun of the Dead");
     } 
}
Choose all that apply:

(A) Anatomy of a Murder
(B) Planet of Apes
(C) The conversation
(D) Shaun of the Dead
(E) Compilation error
Output : (E)

Explanation:

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.

9.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
    switch(6){
        case 6.0f:printf("Sangakkara");
               break;
        case 6.0: printf("Sehwag");
               break;
        case 6.0L:printf("Steyn");
               break;
        default:  printf("Smith");
    } 
}
Choose all that apply:

(A) Sangakkara
(B) Sehwag
(C) Steyn
(D) Smith
(E) Compilation error
Output : (E)

Explanation:

Case expression must be integral constant expression. If it is not integer then it is automatically type casted into integer value.
so. (int)6.0f = 6
(int)6.0 = 6
(int)6.0L = 6
In c duplicate case is not possible.

10.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
    switch(0X0){
        case NULL:printf("Thierry Henry");
             break;
        case '\0':printf("Steven Gerrend");
             break;
        case 0: printf("Kaka");
             break;
        default: printf("Michael Ballack");
    }
    
}
Choose all that apply:

(A) Thierry Henry
(B) Steven Gerrend
(C) Kaka
(D) Michael Ballack
(E) Compilation error
Output : (E)

Explanation:

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.

11.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
     switch(5/2*6+3.0){
        case 3:printf("David Beckham");
             break;
        case 15:printf("Ronaldinho");
             break;
        case 0:printf("Lionel Messi");
             break;
        default:printf("Ronaldo");
     }  
}
Choose all that apply:

(A) David Beckham
(B) Ronaldinho
(C) Lionel Messi
(D) Ronaldo
(E) Compilation error
Output : (E)

Explanation:

Consider on the expression:
5/2*6+3.0
=2*6+3.0
=12 + 3.0
=15.0
In c switch expression must return an integer value. It cannot be float, double or long double

12.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
    unsigned char c=280;
    switch(c){
        printf("Start\t");
        case 280:printf("David Beckham\t");
        case 24: printf("Ronaldinho\t");
        default:  printf("Ronaldo\t");
        printf("End");
    }
    
}
Choose all that apply:
(A)
Start David Beckham Ronaldinho Ronaldo End

(B) Start David Beckham Ronaldinho Ronaldo
(C) Start Ronaldinho Ronaldo End
(D)
Ronaldinho Ronaldo End

(E) Compilation error
Output : (D)

Explanation:

280 is beyond the range of unsigned char. Its corresponding cyclic value is: 24
In c switch case statement program control always move from the case which satisfy the switch condition and end with either break keyword, terminating} or any null character which will come first.

13.
What will be output when you will execute following c code?
#include<stdio.h>
#define TRUE 1
void main(){
    switch(TRUE){
        printf("ctechnotips.blogspot.com");
     }  
}
Choose all that apply:

(A) ctechnotips.blogspot.com
(B) It will print nothing
(C) Runtime error
(D) Compilation error
(E) None of the above

Output : (B)

Explanation:

In c it is possible a switch case statement without any case but it is meaning less.

14.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
     static int i;
     int j=1;
     int arr[5]={1,2,3,4};
     switch(arr[j]){
        case 1: i++;break;
        case 2: i+=2;j=3;continue;
        case 3: i%=2;j=4;continue;
        default: --i;
     }
     printf("%d",i);  
}
Choose all that apply:

(A) 0
(B) 1
(C) 2
(D) Compilation error
(E) None of the above
Output : (D)

Explanation:

We cannot use continue keyword in switch case. It is part loop.

15.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
     static int i;
     int j;
     for(j=0;j<=5;j+=2)
     switch(j){
        case 1: i++;break;
        case 2: i+=2;
        case 4: i%=2;j=-1;continue;
        default: --i;continue;
     }
     printf("%d",i); 
}
Choose all that apply:

(A) 0
(B) 1
(C) 2
(D)
Compilation error

(E) None of the above
Output : (A)

Explanation:

In first iteration of for loop:
j = 0
So, control will come to default,
i = -1
Due to continue keyword program control will move to beginning of for loop
In second iteration of for loop:
j =2
So, control will come to case 2,
i+=2
i = i+2 = -1 +2 =1
Then come to case 4,
i%=2
i = i%2 = 1%2 = 1
j= -1
Due to continue keyword program control will move to beginning of for loop
In third iteration of for loop:
j = -1 +2 =1
So, control will come to case 1
i = 2
In the fourth iteration of for loop:
j = 1+ 2 =3
So, control will come to default,
so i = 1
In the fifth iteration of for loop:
j = 3 + 2 =5
So, control will come to default,
so i = 0
In the sixth iteration of for loop:
j = 5 + 2 =7
Since loop condition is false. So control will come out of the for loop. 

16.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
     int x=3;
     while(1){
        switch(x){
             case 5/2: x+=x+++x;
             case 3%4: x+=x---x;continue;
             case 3>=3: x+=!!!x;break;
             case 5&&0:x+=~~~x;continue;
             default: x+=-x--;
        }
        break;
     }
     printf("%d",x);   
}
Choose all that apply:

(A) 3
(B) -1
(C) 5
(D) Compilation error
(E) None of the above
Output : (B)

Explanation:

Think yourself.

17.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
     char *str="ctechnotips.blogspot.com";
     int a=2;
     switch('A'){
        case 97:
             switch(97){
                 default: str+=1;
             }
        case 65:
             switch(97){
                 case 'A':str+=2;
                     case 'a':str+=4;
         }
        default:
         for(;a;a--)
             str+=8;
     }
     printf("%s",str); 
}
Choose all that apply:
(A)
ctechnotips.blogspot.com

(B) blogspot.com
(C) .com
(D)
Compilation error

(E) None of the above
Output : (C)

Explanation:

ASCII value of the character constant 'A' is 65 and 'a' is 97
Nesting of switch case is possible in c.
For loop condition in default case of switch become false when variable "a" become 0 (zero) value.    

18.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
     switch(2){
        case 1L:printf("No");
        case 2L:printf("%s","I");
             goto Love;
        case 3L:printf("Please");
        case 4L:Love:printf("Hi");
     } 
}
Choose all that apply:

(A) I
(B) IPleaseHi
(C) IHi
(D) Compilation error
(E) None of the above
Output : (C)

Explanation:

It is possible to write label of goto statement in the case of switch case statement.

19.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
     int a=5;
     a=a>=4;
     switch(2){
        case 0:int a=8;
        case 1:int a=10;
        case 2:++a;
        case 3:printf("%d",a);
     }
}
Choose all that apply:

(A) 8
(B) 11
(C) 10
(D) Compilation error
(E) None of the above
Output : (E)

Explanation:

We can not declare any variable in any case of switch case statement.
 
 20.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
     int a=3,b=2;
     a=a==b==0;
     switch(1){
        a=a+10;
     }
     sizeof(a++);
     printf("%d",a); 
}
Choose all that apply:

(A) 10
(B) 11
(C)
12

(D)
1

(E) Compilation error

Output : (D)

Explanation:

Consider on the expression:
a=a==b==0;
a=(a==b)==0; //Since associate is right to left
a =(3==2)==0
a=0==0
a=1
switch case will not affect the value of variable a.
Also sizeof operator doesn't affect the value of the any variable

0 comments:

Post a Comment