wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Quiz - 8 on conditional/ternary operator

Total questions: 15

Worksheet time: 8mins

Name
Class
Date
1.

What is the value of x after the following code is executed?

int x = (5 > 3) ? 10 : 20;

a)
10
b)
20
c)
5
d)
3
2.

What is the value of y after the following code is executed?

int y = (5 < 3) ? 10 : 20;

a)
10
b)
20
c)
5
d)
3
3.

What does the following code print?

int a = 1, b = 2;

printf("%d", (a > b) ? a : b);

a)
1
b)
2
c)
0
d)
-1
4.

What is the value of result after the following code is executed?

int x = 4;

int result = (x % 2 == 0) ? x * 2 : x * 3;

a)
8
b)
12
c)
4
d)
6
5.

What does the following code print?

int a = 3, b = 5;

printf("%d", (a == b) ? 100 : 200);

a)
100
b)
200
c)
3
d)
5
6.

What is the value of z after the following code is executed?

int x = 10, y = 20;

int z = (x > y) ? x : y;

a)
10
b)
20
c)
30
d)
0
7.

What does the following code print?

int a = 7;

printf("%s", (a % 2 == 0) ? "even" : "odd");

a)
even
b)
odd
c)
7
d)
0
8.

What is the value of max after the following code is executed?

int a = -1, b = -2;

int max = (a > b) ? a : b;

a)
-1
b)
-2
c)
1
d)
2
9.

What is the output of the following code?

int num = 5;

printf("%d", (num > 0) ? num : -num);

a)
5
b)
-5
c)
0
d)
1
10.

What is the value of min after the following code is executed?

int a = 9, b = 4;

int min = (a < b) ? a : b;

a)
9
b)
4
c)
5
d)
13
11.

What does the following code print?

int x = 0;

printf("%s", (x != 0) ? "True" : "False");

a)
True
b)
False
c)
0
d)
1
12.

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;

a)
2
b)
-2
c)
8
d)
-8
13.

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));

a)
10
b)
20
c)
30
d)
0
14.

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);

a)
5
b)
10
c)
15
d)
20
15.

What is the result of the following code?

int n = 7;

int result = (n % 2 == 0) ? n / 2 : n * 2;

a)
3
b)
7
c)
14
d)
2