Multiple Choice Questions and Answers for – Conditional Expressions in C Language

https://www.computersprofessor.com/2017/09/multiple-choice-questions-and-answers_25.html
1. What is the output of this C code?
#include
int main()
{
int x = 2, y = 0;
int z = (y++) ? y == 1 && x : 0;
printf("%d\n", z);
return 0;
}
a) 0
b) 1
c) Undefined behaviour
d) Compile time error
b) 1
c) Undefined behaviour
d) Compile time error
Answer:a
2. What is the output of this C code?
#include
int main()
{
int x = 1;
int y = x == 1 ? getchar(): 2;
printf("%d\n", y);
}
a) Compile time error
b) Whatever character getchar function returns
c) Ascii value of character getchar function returns
d) 2
b) Whatever character getchar function returns
c) Ascii value of character getchar function returns
d) 2
Answer:c
3. What is the output of this C code?
#include
int main()
{
int x = 1;
short int i = 2;
float f = 3;
if (sizeof((x == 2) ? f : i) == sizeof(float))
printf("float\n");
else if (sizeof((x == 2) ? f : i) == sizeof(short int))
printf("short int\n");
}
a) float
b) short int
c) Undefined behaviour
d) Compile time error
b) short int
c) Undefined behaviour
d) Compile time error
Answer:a
4. What is the output of this C code?
#include
int main()
{
int a = 2;
int b = 0;
int y = (b == 0) ? a :(a > b) ? (b = 1): a;
printf("%d\n", y);
}
a) Compile time error
b) 1
c) 2
d) Undefined behaviour
b) 1
c) 2
d) Undefined behaviour
Answer:c
5. What is the output of this C code?
#include
int main()
{
int y = 1, x = 0;
int l = (y++, x++) ? y : x;
printf("%d\n", l);
}
a) 1
b) 2
c) Compile time error
d) Undefined behaviour
b) 2
c) Compile time error
d) Undefined behaviour
Answer:a
6. What is the output of this 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) None of the mentioned
b) 8
c) Run time error
d) None of the mentioned
Answer:a
7. Comment on the output of this 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
b) 7
c) 8
d) Depends on compiler
Answer:b
8. The code snippet below produces
#include
void main()
{
1 < 2 ? return 1 : return 2;
}
a) returns 1
b) returns 2
c) Varies
d) Compile time error
b) returns 2
c) Varies
d) Compile time error
Answer:d
9. The output of the code below is
#include
void main()
{
int k = 8;
int m = 7;
k < m ? k++ : m = k;
printf("%d", k);
}
a) 7
b) 8
c) Compile time error
d) Run time error
b) 8
c) Compile time error
d) Run time error
Answer:c
10. The output of the code below is
#include
void main()
{
int k = 8;
int m = 7;
k < m ? k = k + 1 : m = m + 1;
printf("%d", k);
}
a) Compile time error
b) 9
c) 8
d) Run time error
b) 9
c) 8
d) Run time error
Answer:a
11. For initialization a = 2, c = 1 the value of a and c after this code will be
c = (c) ? a = 0 : 2;
a) a = 0, c = 0;
b) a = 2, c = 2;
c) a = 2, c = 2;
d) a = 1, c = 2;
c = (c) ? a = 0 : 2;
a) a = 0, c = 0;
b) a = 2, c = 2;
c) a = 2, c = 2;
d) a = 1, c = 2;
Answer:a
12. What will be the data type of the expression (a < 50) ? var1 : var2; provided a = int, var1 = double, var2 = float
a) int
b) float
c) double
d) Cannot be determined
Answer:c
13. Which expression has to be present in the following? exp1 ? exp2 : exp3;
a) exp1
b) exp2
c) exp3
d) All of the mentioned
Answer:d
14. Value of c after the following expression (initializations a = 1, b = 2, c = 1): c += (-c) ? a : b;
a) Syntax Error
b) c = 1
c) c = 2
d) c = 3
Answer:c
15. Comment on the following expression? c = (n) ? a : b; can be rewritten as
a) if (!n)c = b; else c = a;
b) if (n <= 0)c = b; else c = a;
c) if (n > 0)c = a;
else c = b;
d) All of the mentioned
else c = b;
d) All of the mentioned
Answer:a