Professional Documents
Culture Documents
2. Program 2
#include<stdio.h>
void main()
{
int movie = 1;
switch (movie << (2 + movie))
{
default:
printf(" Traffic");
case 4:
printf(" Sultan");
case 5:
printf(" Dangal");
case 8:
printf(" Bahubali");
}
}
Output:
Bahubali
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. The value of expression “movie << (2 + movie)" is 8.
3. Program 3
filter_none
#include<stdio.h>
#define L 10
void main()
{
auto a = 10;
switch (a, a*2)
{
case L:
printf("ABC");
break;
case L*2:
printf("XYZ");
break;
case L*3:
printf("PQR");
break;
default:
printf("MNO");
case L*4:
printf("www");
break;
}
}
Output:
XYZ
Explanation: In C, comma is also operator with least precedence. So if
x = (a, b);
Then x = b
Note: Case expression can be macro constant.
4. Program 4
#include<stdio.h>
void main()
{
switch(2)
{
case 1L:
printf("No");
case 2L:
printf("%s","GEEKS");
goto Love;
case 3L:
printf("Please");
case 4L:Love:
printf("FOR");
}
}
Output:
GEEKSFOR
Explanation: It is possible to write label of goto statement in the case of switch case
statement.
A. 5 7 6
B. 5 6 7
C. 6 6 6
D. 5 7 7
Answer: Option B
Explanation
The if statement condition is (i > j == k) becomes (5 > 6 == 7) its looks like
(expression1 == expression2), expression1 becomes 0 and expression2 is
a non-zero value so expression2 returns 1. Finally the condition is False
(0 == 1) so else block gets executed.
A. Compilation Error
B. Runtime Error
C. Hai
D. No Hai
Option: C
Explanation
The condition if(i == (1, 2)) is if(expression1 == (expression2,
expression3)). The expression3 is evaluated last so the if block gets
executed.
#include<stdio.h>
int main()
{
if(printf("0"))
printf("inside if block");
else
printf("inside else block");
return 0;
}
A. inside if block ✘
B. inside else block
C. 0inside else block
D. 0inside if block ✔
A. 6 5
B. 5 5✘
C. 5 4✔
D. 6 4
Option: C
Explanation
The if(!printf("")) is in a format if(!expression). The expression(printf("")
returns 0 because it prints nothing) and the condition becomes true
because of ! operator. So the if block gets executed.
A. i = 1
B. i = -1
C. i = 0
D. j = 1✔
Option: D
Explanation
The if(i-- == j) will be if(1 == 0). It uses post decrement operator so, first
the value i is used then it decrementd by one. i.e) if(1 == 0) condition is
false so it executes the else block
A. Compilation Error ✔
B. Runtime Error
C. Equal
D. Not Equal
Option: A
Explanation
Compilation Error: Misplaced else
else block needs to follow directly after the statement of if block. In the
above program, the if statement ends with semicolon so there is no block
of statements under if block. The printf statement after if is normal one. So
else block is misplaced in this program.
A. 50
B. 51 ✔
C. 26 ✘
D. 27
Option: B
Explanation
Sorry. No Explanation.
A. Compilation Error
B. No
C. yes, Get in ✔
D. None of above
Option: C
Explanation
The expression inside if returns true. So the if block gets executed.
A. Bye
B. Hai ✔
C. Compilation Error
D. None
Option: B
Explanation
The sizeof(0) will returns size of integer, So the condtion is true.
What will be the output of the C program?
#include<stdio.h>
int main()
{
int i = 1;
switch(i)
{
case i:
printf("case 1 executed");
break;
case i + 1;
printf("case 2 executed");
break;
default:
printf("default block executed");
break;
}
return 0;
}
A. Compilation Error
B. case 1 executed ✘
C. case 2 executed
D. default block executed
Option: A
Explanation
Compilation Error: Constant expression required. The case statement
accepts only a constant expression.
#include <stdio.h>
int main()
{
int x;
x = 5 > 8 ? 10 : 1 != 2 < 5 ? 20 : 30;
printf("Value of x:%d", x);
return 0;
}
Output:
Value of x:30