1.
int k=5;
int d = –k + ‘A’ / k++;
System.out.println(d);
–k: pre-decrement, k becomes 4.
‘A’ is 65 in ASCII.
‘A’ / k++: 65 / 4 = 16 (integer division), then k becomes 5 due to post-increment.
d = 4 + 16 = 20.
Output: 20
2.
int k = 7;
int d = (‘D’ – k++) + –k * 2;
System.out.println(d);
‘D’ is 68.
‘D’ – k++: 68 – 7 = 61, then k becomes 8.
–k: pre-decrement, k becomes 7.
–k * 2: 7 * 2 = 14.
d = 61 + 14 = 75.
Output: 75
3.
int k = 3;
int d = ‘E’ / (k++ + 1) + –k;
System.out.println(d);
‘E’ is 69.
k++ + 1: 3 + 1 = 4, then k becomes 4.
‘E’ / 4: 69 / 4 = 17 (integer division).
–k: pre-decrement, k becomes 3.
d = 17 + 3 = 20.
Output: 20
4.
int k = 5;
int d = –k + ‘B’ / k++ + ++k – ‘A’;
System.out.println(d);
–k: k becomes 4.
‘B’ is 66.
‘B’ / k++: 66 / 4 = 16, then k becomes 5.
++k: pre-increment, k becomes 6.
‘A’ is 65.
d = 4 + 16 + 6 – 65 = -39.
Output: -39
5.
int a = 3;
int b = ++a * a– % 4 + ‘C’ / a++ – –a;
System.out.println(b);
++a: a becomes 4.
++a * a–: 4 * 4 = 16, then a becomes 3.
16 % 4 = 0.
‘C’ is 67.
‘C’ / a++: 67 / 3 = 22, then a becomes 4.
–a: a becomes 3.
b = 0 + 22 – 3 = 19.
Output: 19
6.
int x = 7;
int y = ++x + x– – ‘A’ % x++ + x / –x * 2;
System.out.println(y);
++x: x becomes 8.
++x + x–: 8 + 8 = 16, then x becomes 7.
‘A’ is 65.
‘A’ % x++: 65 % 7 = 2, then x becomes 8.
–x: x becomes 7.
x / –x: 7 / 6 = 1 (integer division), x becomes 6.
1 * 2 = 2.
y = 16 – 2 + 2 = 16.
Output: 16
7.
int a = 4;
int b = a++ + –a * 3 % 2 + ‘B’ / a– – ++a * 2;
System.out.println(b);
a++: a becomes 5 (but post-increment, so original value 4 is used).
–a: a becomes 4.
–a * 3 % 2: 4 * 3 = 12, 12 % 2 = 0.
‘B’ is 66.
‘B’ / a–: 66 / 4 = 16, then a becomes 3.
++a: a becomes 4.
++a * 2: 4 * 2 = 8.
b = 4 + 0 + 16 – 8 = 12.
Output: 12
8.
int a=20;
int s = (Math.round(19.7) == a) ? a * 10 : a % 10;
System.out.println(s);
Math.round(19.7) is 20.
20 == 20 is true, so s = a * 10 = 200.
Output: 200
9.
int a = 22;
int s = (Math.round(21.6) == a) ? ((a++ + 3) % 4) * 2 : ((–a / 3) + a % 5);
System.out.println(s);
Math.round(21.6) is 22.
22 == 22 is true, so:
a++ + 3: 22 + 3 = 25, then a becomes 23.
25 % 4 = 1.
1 * 2 = 2.
s = 2.
Output: 2
10.
int a = 7;
int s = (Math.max(5, 7) == a) ? a++ % 3 + 4 : –a + 1;
System.out.println(s);
Math.max(5, 7) is 7.
7 == 7 is true, so:
a++ % 3: 7 % 3 = 1, then a becomes 8.
1 + 4 = 5.
s = 5.
Output: 5
11.
int a = -10;
int s = (Math.abs(a) == 10) ? Math.min(5, 8) * 2 : a % 3 + 6;
System.out.println(s);
Math.abs(-10) is 10.
10 == 10 is true, so:
Math.min(5, 8) is 5.
5 * 2 = 10.
s = 10.
Output: 10
12.
int a = 25;
int s = (Math.sqrt(625) == a) ? a / 5 + 1 : a % 4 * 2;
System.out.println(s);
Math.sqrt(625) is 25.0.
25.0 == 25 is true, so:
a / 5 + 1: 25 / 5 + 1 = 5 + 1 = 6.
s = 6.
Output: 6
13.
int a = 11;
int s = (Math.floor(10.9) == a) ? ++a % 5 * 2 : a– / 2 + 4;
System.out.println(s);
Math.floor(10.9) is 10.0.
10.0 == 11 is false, so:
a– / 2 + 4: 11 / 2 + 4 = 5 + 4 = 9, then a becomes 10.
s = 9.
Output: 9
14.
int a = -3;
int s = (Math.abs(a) == 3) ? (int)Math.pow(2, 3) : Math.max(4, 6) + a;
System.out.println(s);
Math.abs(-3) is 3.
3 == 3 is true, so:
(int)Math.pow(2, 3) is 8.
s = 8.
Output: 8
15.
int x = 9;
int y = 4;
if (x % y > 1)
System.out.println(x / y + y % x);
else
System.out.println(x * y – y);
x % y: 9 % 4 = 1.
1 > 1 is false, so else part:
x * y – y: 9 * 4 – 4 = 36 – 4 = 32.
Output: 32
16.
int a = 15, b = 7;
if (++a % 2 == 0)
System.out.println(a + b–);
else
System.out.println(–b * a);
++a: a becomes 16.
16 % 2 is 0, so if part:
a + b–: 16 + 7 = 23, then b becomes 6.
Output: 23
17.
int m = 12;
double n = 13.5;
if (Math.floor(n) == m)
System.out.println((int)(Math.pow(m, 2)));
else
System.out.println((int)Math.sqrt(m * 10));
Math.floor(13.5) is 13.0.
13.0 == 12 is false, so else part:
m * 10 = 120.
Math.sqrt(120) ≈ 10.95, cast to int is 10.
Output: 10
18.
int p = 20;
int q = 10;
if (Math.max(p, q) == q)
System.out.println(p % q + 5);
else
System.out.println(p / q + 5);
Math.max(20, 10) is 20.
20 == 10 is false, so else part:
p / q + 5: 20 / 10 + 5 = 2 + 5 = 7.
Output: 7
19.
int a = 10;
int b = 5;
if (Math.abs(a – b * 3) > 5)
System.out.println((int)Math.ceil(10.3));
else
System.out.println((int)Math.floor(9.8));
b * 3 = 15.
a – 15 = -5.
Math.abs(-5) = 5.
5 > 5 is false, so else part:
Math.floor(9.8) is 9.0, cast to int is 9.
Output: 9
20.
int x = 8;
int y = 3;
if ((x++ % 3 == 2) && (–y == 2))
System.out.println(x + y);
else
System.out.println(x * y);
x++ % 3: 8 % 3 = 2, then x becomes 9.
–y: y becomes 2.
(2 == 2) && (2 == 2) is true, so:
x + y: 9 + 2 = 11.
Output: 11
21.
int a = 18;
if (Math.sqrt(a) > 4)
System.out.println((int)Math.pow(2, 3));
else
System.out.println((int)Math.floor(7.9));
Math.sqrt(18) = 4.24.
4.24 > 4 is true, so:
Math.pow(2, 3) is 8.
Output: 8
22.
int x = 5;
if (Math.pow(x, 2) > 20)
System.out.println(x– * 2);
else
System.out.println(++x * 2);
Math.pow(5, 2) = 25.
25 > 20 is true, so:
x– * 2: 5 * 2 = 10, then x becomes 4.
Output: 10
23.
int p = 7;
if ((++p % 3 == 0) || (Math.abs(-14 + p) > 10)) {
p += Math.max(2, p % 5);
} else {
p = Math.min(p, 6);
}
System.out.println(“Result = ” + p);
++p: p becomes 8.
8 % 3 = 2 ≠ 0, so first condition false.
Math.abs(-14 + 8) = 6 ≤ 10, so second condition false.
Else part:
Math.min(8, 6) is 6.
p = 6.
Output: Result = 6
24.
int a = 15;
double b = Math.pow(a, 0.5);
int x = (int)b;
int result = (x++ == Math.floor(b)) ? ++x * 2 : x– + 5;
if (result % 2 == 0) {
result += Math.min(a, x);
} else {
result -= Math.max(a, x);
}
System.out.println(result);
Math.pow(15, 0.5) = 3.872.
x = (int)3.872 = 3.
Math.floor(b) is 3.0.
x++ == 3.0: 3 == 3.0 is true, then x becomes 4.
++x * 2: x becomes 5, 5 * 2 = 10.
result = 10.
10 % 2 is 0, so:
Math.min(15, 5) is 5.
result += 5: 10 + 5 = 15.
Output: 15
25.
int a = 12;
int b = 7;
int c = Math.round((float)Math.sqrt(a * b));
int res = ((a++ % 2 == 0) ? ++b : –c);
res += (Math.ceil(a / 2.0) == 7) ? 5 : -3;
System.out.println(res);
a * b = 12 * 7 = 84.
Math.sqrt(84) ≈ 9.165.
Math.round(9.165) = 9.
a++ % 2: 12 % 2 = 0, then a becomes 13.
0 == 0 is true, so ++b: b becomes 8.
res = 8.
a / 2.0 = 13 / 2.0 = 6.5.
Math.ceil(6.5) = 7.0.
7.0 == 7 is true, so res += 5: 8 + 5 = 13.
Output: 13
26.
int a = 5, b = 4;
int x = (int)Math.pow(a++, 2);
int y = (int)Math.sqrt(++b * 10);
int result = (x > y) ? x – y : y – x;
result += ((result % 2 == 0) ? 1 : 2);
System.out.println(result);
Math.pow(a++, 2): a is 5, then becomes 6. 5^2 = 25.
x = 25.
++b: b becomes 5.
Math.sqrt(5 * 10) = Math.sqrt(50) ≈ 7.07, cast to int is 7.
y = 7.
25 > 7 is true, so result = 25 – 7 = 18.
18 % 2 is 0, so result += 1: 18 + 1 = 19.
Output: 19
27.
int a = 5, b = 3;
int x = (int)Math.pow(++a, b–);
double y = Math.sqrt(x) + Math.ceil(4.2);
int z = (int)(y % 7 + a– * b);
z = (z > 20) ? (int)Math.round(y + 2) : (int)Math.floor(y – 1);
System.out.println(z);
++a: a becomes 6.
b–: b is 3, then becomes 2.
Math.pow(6, 3) = 216.
x = 216.
Math.sqrt(216) ≈ 14.6969.
Math.ceil(4.2) = 5.0.
y = 14.6969 + 5.0 = 19.6969.
a–: a is 6, then becomes 5.
a– * b: 6 * 2 = 12.
y % 7: 19.6969 % 7 ≈ 5.6969.
z = (int)(5.6969 + 12) = (int)17.6969 = 17.
17 > 20 is false, so:
Math.floor(y – 1) = Math.floor(18.6969) = 18.0, cast to int is 18.
z = 18.
Output: 18
28.
int a = 10;
int b = 3;
int c = (a++ > 9) ? (b++ * 2) : (b– + 4);
int d = (int)Math.ceil(Math.pow(c, 0.5)) + –a;
d = (d % 2 == 0) ? d + Math.min(a, b) : d – Math.max(a, b);
System.out.println(d);
a++ > 9: 10 > 9 is true, then a becomes 11.
b++ * 2: 3 * 2 = 6, then b becomes 4.
c = 6.
Math.pow(6, 0.5) ≈ 2.449.
Math.ceil(2.449) = 3.0.
–a: a becomes 10.
d = 3 + 10 = 13.
13 % 2 is 1, so:
Math.max(10, 4) is 10.
d -= 10: 13 – 10 = 3.
Output: 3
29.
int x = 14, y = 5;
int r = (x– > 10) ? (int)Math.pow(–y, 2) : (int)Math.sqrt(++x);
r += (r % 3 == 0) ? Math.min(x, y) : Math.max(x, y);
r = (r > 20) ? r – 3 : r + 3;
System.out.println(r);
x– > 10: 14 > 10 is true, then x becomes 13.
–y: y becomes 4.
Math.pow(4, 2) = 16.
r = 16.
r % 3: 16 % 3 = 1 ≠ 0, so:
Math.max(13, 4) is 13.
r += 13: 16 + 13 = 29.
29 > 20 is true, so r -= 3: 29 – 3 = 26.
Output: 26
30.
int a = 9;
int b = 4;
int r = ((a % 2 == 1) && (b++ > 3)) ? a– + b : ++a – b–;
r *= (Math.floorMod(a, 3) == 0) ? 2 : 3;
System.out.println(r);
a % 2: 9 % 2 = 1, so first condition true.
b++ > 3: 4 > 3 is true, then b becomes 5.
a– + b: 9 + 5 = 14, then a becomes 8.
r = 14.
Math.floorMod(8, 3) = 2 (since 8 % 3 = 2).
2 == 0 is false, so r *= 3: 14 * 3 = 42.
Output: 42