Please Turn On Java Script To Access This Site.

C Institute | Questions


Previous Topic : if else - 2Next Topic : switch statement - 2

This section focuses on C interview questions, tricky questions, and puzzles, and viva questions based on “switch case default statement”. One shall practice these questions and puzzles to improve their C programming skills needed for various interviews (campus interviews, walk-in interviews, viva exams and company interviews), placements, entrance exams and other competitive exams. These programming questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate, engineering student, bca student, mca student, mcs students or an experienced IT professional. It will improve their understanding of C Language. Here is a listing of C questions and puzzles on “switch case default statement” along with answers and/or explanations.

Q1: What will be printed?
#include <stdio.h> 
int main() 
{  int x=1; 
    switch(x+2) 
    { 
    case 1: 
        printf("1 "); 
    case 2: 
        printf("2 "); 
    case 3: 
        printf("3 "); 
    default: 
        printf("4 "); 
    } 
    return 0; 
} 
a)  1 2 3 4
b)  2 3 4
c)  3 4
d)  34
Correct Answer : c
Explanation : In switch,the expression x+2 where x is 1 and after addition the expression results 3. So it will execute statements of case 3, and default too, because there is not break after case 3. How switch works


Q2: What will be the output of following program ?
#include <stdio.h>
void main()
{   int a=20;
    switch(a){
        case 30-10:
            printf("Hi");
        default:
            printf(" Handsome");
    }
}
a)  HiHandsome
b)  Hi
c)  Handsome
d)  Hi Handsome
Correct Answer : d


Q3: What will be the output of following program ?
#include <stdio.h>
void main()
{   int x=20;
    switch(x)
    {   printf("Hi ");
        default:
            printf("Hello ");
        case 20:
            printf("Bye");
        case 30:
            printf(" World");
    }
}
a)  Hi Bye World
b)  Bye
c)  Bye World
d)  Error
Correct Answer : c
Explanation : Any statement outside case or default is simply ignored. so Bye World is printed. It happens because value of x is 20 so case 20 and case 30 will be executed, there is no break after case 20.


Q4: What will be the output?
#include<stdio.h> 
void main() 
{   int movie=1; 
    switch (movie << (2 + movie)) 
    { 
    default: 
        printf("Thackrey"); 
    case 11: 
        printf("Manikarnika"); 
    case 5: 
        printf("APM"); 
    case 8: 
        printf("URI"); 
    } 
} 
a)  Thackrey Manikarnika APM URI
b)  Thackrey
c)  URI
d)  Error
Correct Answer : c
Explanation : The 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. The value of expression “movie << (2 + movie)" is 8, so case 8 will be executed.


Q5: What will be printed?
#include<stdio.h>
int main()
{       switch(2/3)
	{	case 1:
		printf("0 ");		
		case 2:
		printf("2 ");
		break;
		default:
		printf("Not Zero");
	}
}
a)  0
b)  2
c)  Not Zero
d)  0 2
Correct Answer : c
Explanation : 2/3 results in 0, so defaul will be executed, because there is no case having value 0.


Q6: What will be the output of the C program?

#include<stdio.h>
#define N 1.5
int main()
{	int i = 6.5;
	switch(i)
	{
		case N + 5:
		printf("Switch ON the fan");
		break;
		case N * 4:
		printf("Switch ON the Air Cooler");
		break;
		default:
		printf("Save Energy");
	}
return 0;
}
a)  Switch ON the fan
b)  Switch ON the Air Cooler
c)  Save Energy
d)  Syntax Error
Correct Answer : d
Explanation : The values used in switch and case expression must be an integer.


Q7: What will be printed?
#include<stdio.h>
int main(){
int kd = 0;
if (kd++, kd--, ++kd)
	switch(kd)
	{       case 1:
		printf("Ding");
		break;
		case 2:
		printf("Dong");
		break;
		default:
		printf("Ding Dong");
		break;
	}
   return 0;
}
a)  Ding
b)  Dong
c)  Ding Dong
d)  Error
Correct Answer : a
Explanation : The final value of kd is 1, so if condition is true and case 1 will be executed.


Previous Topic : if else - 2Next Topic : switch statement - 2