Here is a listing of C Objective Questions on “Conditional Expressions” along with answers, explanations and/or solutions:
1. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int x = 2, y = 0;
-
int z = (y++) ? y == 1 && x : 0;
-
printf("%dn", z);
-
return 0;
-
}
a) 0
b) 1
c) Undefined behaviour
d) Compile time error
Answer: a
Clarification: None.
2. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int x = 1;
-
int y = x == 1 ? getchar(): 2;
-
printf("%dn", y);
-
}
a) Compile time error
b) Whatever character getchar function returns
c) Ascii value of character getchar function returns
d) 2
Answer: c
Clarification: None.
3. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int x = 1;
-
short int i = 2;
-
float f = 3;
-
if (sizeof((x == 2) ? f : i) == sizeof(float))
-
printf("floatn");
-
else if (sizeof((x == 2) ? f : i) == sizeof(short int))
-
printf("short intn");
-
}
a) float
b) short int
c) Undefined behaviour
d) Compile time error
Answer: a
Clarification: None.
4. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int a = 2;
-
int b = 0;
-
int y = (b == 0) ? a :(a > b) ? (b = 1): a;
-
printf("%dn", y);
-
}
a) Compile time error
b) 1
c) 2
d) Undefined behaviour
Answer: c
Clarification: None.
5. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int y = 1, x = 0;
-
int l = (y++, x++) ? y : x;
-
printf("%dn", l);
-
}
a) 1
b) 2
c) Compile time error
d) Undefined behaviour
Answer: a
Clarification: None.
6. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
int k = 8;
-
int m = 7;
-
int z = k < m ? k++ : m++;
-
printf("%d", z);
-
}
a) 7
b) 8
c) Run time error
d) 15
Answer: a
Clarification: None.
7. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
int k = 8;
-
int m = 7;
-
int z = k < m ? k = m : m++;
-
printf("%d", z);
-
}
a) Run time error
b) 7
c) 8
d) Depends on compiler
Answer: b
Clarification: None.
8. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
1 < 2 ? return 1 : return 2;
-
}
a) returns 1
b) returns 2
c) Varies
d) Compile time error
Answer: d
Clarification: None.