Home »
Aptitude Questions and Answers »
C Aptitude Questions and Answers
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");
}
}
- Hello
- OK
- Hello
OK
- Error
Correct Answer - 3
Hello
OK
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");
}
- Case-2
- Message
- Message
Case-2
- Case-2
Case-3
Exit from switch
Correct Answer - 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");
}
- Case-2
- Error: case expression not constant.
- Message
Case-2
- Case-2
Case-3
Exit from switch
Correct Answer - 2
Error: case expression not constant.
We can not use any variable in case expression. ("case a:" is invalid").
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;
}
}
- 2 nd
- 22 nd
- Error
- 2 nd
22 nd
Correct Answer - 3
Syntax Error
We can not use || operator between case statements, case 2:||case 22: invalid
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;
}
}
- One
- Two
- Else
- Error
Correct Answer - 2
TWO
case 1L: means case 1: (1L stands for 1 as long integer)
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);
}
- 0
- 1
- 2
- Error
Correct Answer - 1
0
when j=0, program's execution will jump to default:, then staticVar will be -1.
In next step j=1, program's exection will jump to case 1:, then staticVar will be 0.
due to break, exit from switch ...
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;
}
}
- One...
- Two...
- Other...
- Error
Correct Answer - 4
ERROR: switch expression of type 'double' is illegal.
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;
}
}
- Case NULL
- Case ZERO
- Case DEFAULT
- Error
Correct Answer - 4
Error: case value '0' already used.
Macro constant NULL has be defined as 0 in stdio.h
ASCII value of character constant 0, and duplicate case value not allowed.
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;
}
- 0IHelp 1IHelp 2IHelp 3IHelp 4IHelp
- 0IHelp 2IHelp 4IHelp
- IHelp
- Error
Correct Answer - 1
0IHelp 1IHelp 2IHelp 3IHelp 4IHelp
lbl label is declared within the for() body just after the if else statement and lbl label is executed after
if or else body.
10) What will be the output of following program ?
#include <stdio.h>
#define TRUE 1
int main()
{
switch(TRUE)
{
printf("Hello");
}
}
- Hello
- ERROR
- No Output
- Garbage Value
Correct Answer - 3
No Output
There is no ERROR in this program, but "Hello" will not print. Because switch statement contains value 1 (TRUE) and there is no case with value 1 even there is no default case, so program’s execution will not jump into switch body.
11) Can we use string value/variable in switch test condition?
- Yes
- No
Correct Answer - 2
No
No, switch statement works with only integral type of variables/literals like integer, short, character.
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?
- Yes
- No
Correct Answer - 2
No
No, continue statement is works with looping statements only.
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!!!");
}
}
- Error
- Oops No choice here!!!
- It's Eight.Oops No choice here!!!
- It's Eight.
Correct Answer - 4
It's Eight.
Here, x=y+1 will be 8 – because x is an integer variable so final value that will return through this expression is 8.