WorksheetsQuiz - 8 on conditional/ternary operator
Total questions: 15
Worksheet time: 8mins
What is the value of x after the following code is executed?
int x = (5 > 3) ? 10 : 20;
What is the value of y after the following code is executed?
int y = (5 < 3) ? 10 : 20;
What does the following code print?
int a = 1, b = 2;
printf("%d", (a > b) ? a : b);
What is the value of result after the following code is executed?
int x = 4;
int result = (x % 2 == 0) ? x * 2 : x * 3;
What does the following code print?
int a = 3, b = 5;
printf("%d", (a == b) ? 100 : 200);
What is the value of z after the following code is executed?
int x = 10, y = 20;
int z = (x > y) ? x : y;
What does the following code print?
int a = 7;
printf("%s", (a % 2 == 0) ? "even" : "odd");
What is the value of max after the following code is executed?
int a = -1, b = -2;
int max = (a > b) ? a : b;
What is the output of the following code?
int num = 5;
printf("%d", (num > 0) ? num : -num);
What is the value of min after the following code is executed?
int a = 9, b = 4;
int min = (a < b) ? a : b;
What does the following code print?
int x = 0;
printf("%s", (x != 0) ? "True" : "False");
What is the value of result after the following code is executed?
int a = 5, b = 3;
int result = (a >= b) ? a - b : b - a;
What is the output of the following code?
int a = 10;
int b = 20;
int c = 30;
printf("%d", (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c));
What is the value of x after the following code is executed?
int a = 5, b = 10;
int x = (a < b) ? (b - a) : (a + b);
What is the result of the following code?
int n = 7;
int result = (n % 2 == 0) ? n / 2 : n * 2;
