Answer: Option C
Explanation:
The switch/case statement in the c language is defined by the language specification to use an int value, so you can not use a float value.
switch( expression )
{
case constant-expression1: statements 1;
case constant-expression2: statements 2;
case constant-expression3: statements3 ;
...
...
default : statements 4;
}
The value of the 'expression' in a switch-case statement must be an integer, char, short, long. Float and double are not allowed.
Find Output of Program
1. |
What will be the output of the program?
#include<stdio.h>
int main()
{
int i=0;
for(; i<=5; i++);
printf("%d", i);
return 0;
}
|
A. |
0, 1, 2, 3, 4, 5 | B. |
5 |
C. |
1, 2, 3, 4 | D. |
6 |
Answer: Option D
Explanation:
Step 1: int i = 0; here variable i is an integer type and initialized to '0'.
Step 2: for(; i<=5; i++); variable i=0 is already assigned in previous step. The semi-colon at the end of this for loop tells, "there is no more statement is inside the loop".
Loop 1: here i=0, the condition in for(; 0<=5; i++) loop satisfies and then i is incremented by '1'(one)
Loop 2: here i=1, the condition in for(; 1<=5; i++) loop satisfies and then i is incremented by '1'(one)
Loop 3: here i=2, the condition in for(; 2<=5; i++) loop satisfies and then i is incremented by '1'(one)
Loop 4: here i=3, the condition in for(; 3<=5; i++) loop satisfies and then i is increemented by '1'(one)
Loop 5: here i=4, the condition in for(; 4<=5; i++) loop satisfies and then i is incremented by '1'(one)
Loop 6: here i=5, the condition in for(; 5<=5; i++) loop satisfies and then i is incremented by '1'(one)
Loop 7: here i=6, the condition in for(; 6<=5; i++) loop fails and then i is not incremented.
Step 3: printf("%d", i); here the value of i is 6. Hence the output is '6'.
|
2. |
What will be the output of the program?
#include<stdio.h>
int main()
{
char str[]="C-program";
int a = 5;
printf(a >10?"Ps\n":"%s\n", str);
return 0;
}
|
A. |
C-program | B. |
Ps |
C. |
Error | D. |
None of above |
Answer: Option A
Explanation:
Step 1: char str[]="C-program"; here variable str contains "C-program".
Step 2: int a = 5; here variable a contains "5".
Step 3: printf(a >10?"Ps\n":"%s\n", str); this statement can be written as
if(a > 10)
{
printf("Ps\n");
}
else
{
printf("%s\n", str);
}
Here we are checking a > 10 means 5 > 10. Hence this condition will be failed. So it prints variable str.
Hence the output is "C-program".
|
3. |
What will be the output of the program?
#include<stdio.h>
int main()
{
int a = 500, b = 100, c;
if(!a >= 400)
b = 300;
c = 200;
printf("b = %d c = %d\n", b, c);
return 0;
}
|
A. |
b = 300 c = 200 | B. |
b = 100 c = garbage |
C. |
b = 300 c = garbage | D. |
b = 100 c = 200 |
Answer: Option D
Explanation:
Initially variables a = 500, b = 100 and c is not assigned.
Step 1: if(!a >= 400)
Step 2: if(!500 >= 400)
Step 3: if(0 >= 400)
Step 4: if(FALSE) Hence the if condition is failed.
Step 5: So, variable c is assigned to a value '200'.
Step 6: printf("b = %d c = %d\n", b, c); It prints value of b and c.
Hence the output is "b = 100 c = 200"
| |
4. |
What will be the output of the program?
#include<stdio.h>
int main()
{
unsigned int i = 65535;
while(i++ != 0)
printf("%d",++i);
printf("\n");
return 0;
}
|
A. |
Infinite loop |
B. |
0 1 2 ... 65535 |
C. |
0 1 2 ... 32767 - 32766 -32765 -1 0 |
D. |
No output |
Answer: Option A
Explanation:
Here unsigned int size is 2 bytes. It varies from 0,1,2,3, ... to 65535.
Step 1:unsigned int i = 65535;
Step 2:
Loop 1: while(i++ != 0) this statement becomes while(65535 != 0). Hence the while(TRUE) condition is satisfied. Then the printf("%d", ++i); prints '1'(variable 'i' is already increemented by '1' in while statement and now increemented by '1' in printf statement)
Loop 2: while(i++ != 0) this statement becomes while(1 != 0). Hence the while(TRUE) condition is satisfied. Then the printf("%d", ++i); prints '3'(variable 'i' is already increemented by '1' in while statement and now increemented by '1' in printf statement)
....
....
The while loop will never stops executing, because variable i will never become '0'(zero). Hence it is an 'Infinite loop'.
|
5. |
What will be the output of the program?
#include<stdio.h>
int main()
{
int x = 3;
float y = 3.0;
if(x == y)
printf("x and y are equal");
else
printf("x and y are not equal");
return 0;
}
|
A. |
x and y are equal | B. |
x and y are not equal |
C. |
Unpredictable | D. |
No output | |
|
|
No comments:
Post a Comment