C Switch Case Statements - Aptitude Questions & Answers

C programming switch, case Aptitude Questions and Answers: In this section you will find C Aptitude Questions and Answers on switch and case statements.



List of C programming Switch Case Aptitude Questions and Answers

1) What will be the output of following program ?
#include <stdio.h>
void main()
{  
	int a=10;
	switch(a){
		case 5+5:
			printf("Hello\n");
		default:
			printf("OK\n");
	}
}       
  1. Hello
  2. OK
  3. Hello
    OK
  4. Error

2) What will be the output of following program ?
#include <stdio.h>
void main()
{  
	int a=2;
	switch(a)
	{
		printf("Message\n");
		default:
			printf("Default\n");
		case 2:
			printf("Case-2\n");
		case 3:
			printf("Case-3\n");
	}
	printf("Exit from switch\n");
}       
  1. Case-2
  2. Message
  3. Message
    Case-2
  4. Case-2
    Case-3
    Exit from switch

3) What will be the output of following program ?
#include <stdio.h>
void main()
{
	int a=2;
	int b=a;

	switch(b)
	{
		case a:
			printf("Case-a\n");		break;
		case 3:
			printf("Case-3\n");		break;
		default:
			printf("No option\n");	break;
	}
	printf("Exit from switch");
}      
  1. Case-2
  2. Error: case expression not constant.
  3. Message
    Case-2
  4. Case-2
    Case-3
    Exit from switch

4) What will be the output of following program ?
#include <stdio.h>
void main()
{
	short day=2;
	switch(day)
	{
		case 2: || case 22: 
			printf("%d nd",day);
		break;
		default:
			printf("%d th",day);
		break;	
	}
}      
  1. 2 nd
  2. 22 nd
  3. Error
  4. 2 nd
    22 nd

5) What will be the output of following program ?
#include <stdio.h>
void main()
{
	short a=2;
	switch(a)
	{
		case 1L:
			printf("One\n"); 
			break;
		case 2L:
			printf("Two\n"); 
			break;
		default:
			printf("Else\n");
			break;
	}
}     
  1. One
  2. Two
  3. Else
  4. Error

6) What will be the output of following program ?
#include <stdio.h>
void main(){
     static int staticVar;
     int j;
     for(j=0;j<=5;j+=2)
     switch(j){
        case 1: 
			staticVar++;
			break;
        case 2: 
			staticVar+=2;
        case 4: 
			staticVar%=2;
			j=-1;
			continue;
        default: 
			--staticVar;
			continue;
     }
     printf("%d",staticVar); 
}
    
  1. 0
  2. 1
  3. 2
  4. Error

7) What will be the output of following program ?
#include <stdio.h>
void main()
{
	int a=2;
	switch(a/2*1.5)
	{
		case 1:
			printf("One...");
			break;
		case 2:
			printf("Two...");
			break;
		default:
			printf("Other...");
			break;
	}
}    
  1. One...
  2. Two...
  3. Other...
  4. Error

8) What will be the output of following program ?
#include <stdio.h>
void main(){
	int a=1;
    switch(a/2)
	{
        case NULL:
			printf("Case NULL\n");
            break;
        case 0: 
			printf("Case ZERO\n");
            break;
        default: 
			printf("DEFAULT\n");
			break;
    } 
}
    
  1. Case NULL
  2. Case ZERO
  3. Case DEFAULT
  4. Error

9) What will be the output of following program ?
#include <stdio.h>
int main()
{
	int i;
	for(i=0; i< 5; i++)
	{
		if(i*i > 30 )
			goto lbl;
		else
			printf("%d",i);
	lbl:
		printf("IHelp ");

	}
	return 0;
}
    
  1. 0IHelp 1IHelp 2IHelp 3IHelp 4IHelp
  2. 0IHelp 2IHelp 4IHelp
  3. IHelp
  4. Error

10) What will be the output of following program ?
#include <stdio.h>
#define TRUE 1
int main()
{
	switch(TRUE)
	{	
		printf("Hello");
	}
}
    
  1. Hello
  2. ERROR
  3. No Output
  4. Garbage Value

11) Can we use string value/variable in switch test condition?
  1. Yes
  2. No

12) Can we use string value/variable in switch test condition?
switch(test_expression)
{
	case VALUE1:
	case VALUE2:
	case VALUE3:
		/*statements block 1*/
		break;
	case VALUE4:
	case VALUE5:
		/*statements block 2*/
		break;
}
    
Can we use continue instead of break to move program’s execution at the starting of switch?
  1. Yes
  2. No

13) Can we use string value/variable in switch test condition?
#include <stdio.h>
int main()
{
	int x;
	float y=7.0;
	switch(x=y+1)
	{
		case 8: printf("It's Eight."); break;
		default: printf("Oops No choice here!!!");
	}
}
    
  1. Error
  2. Oops No choice here!!!
  3. It's Eight.Oops No choice here!!!
  4. It's Eight.