Challenge - 5 Problems
Type Promotion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of arithmetic with byte and int
What is the output of this Java code snippet?
Java
public class Main { public static void main(String[] args) { byte b = 10; int i = 20; var result = b + i; System.out.println(result); } }
Attempts:
2 left
💡 Hint
Remember that byte is promoted to int before arithmetic.
✗ Incorrect
In Java, when a byte is used in arithmetic with an int, the byte is promoted to int. So b + i is int + int, resulting in int 30.
❓ Predict Output
intermediate2:00remaining
Output of char and int addition
What is the output of this Java code snippet?
Java
public class Main { public static void main(String[] args) { char c = 'A'; int i = 1; int result = c + i; System.out.println(result); } }
Attempts:
2 left
💡 Hint
char is promoted to int before addition.
✗ Incorrect
The char 'A' has Unicode value 65. Adding 1 results in 66, which is printed as an int.
❓ Predict Output
advanced2:00remaining
Output of compound assignment with byte
What is the output of this Java code snippet?
Java
public class Main { public static void main(String[] args) { byte b = 5; b += 10; System.out.println(b); } }
Attempts:
2 left
💡 Hint
Compound assignment operators include implicit casting.
✗ Incorrect
The compound assignment operator '+=' automatically casts the result back to byte, so no error occurs and b becomes 15.
❓ Predict Output
advanced2:00remaining
Output of simple assignment with byte and int addition
What is the output or error of this Java code snippet?
Java
public class Main { public static void main(String[] args) { byte b = 5; b = b + 10; System.out.println(b); } }
Attempts:
2 left
💡 Hint
Check the type of b + 10 expression and assignment compatibility.
✗ Incorrect
b + 10 promotes b to int, so the expression is int. Assigning int to byte without cast causes compilation error.
🧠 Conceptual
expert3:00remaining
Type promotion in mixed arithmetic expressions
Given the following Java code, what is the type of the variable 'result' after evaluation?
Java
public class Main { public static void main(String[] args) { byte b = 1; short s = 2; char c = 3; int i = 4; long l = 5L; float f = 6.0f; double d = 7.0; var result = b + s * c - i / l + f - d; } }
Attempts:
2 left
💡 Hint
Remember the order of type promotion in mixed expressions: from smaller to larger types.
✗ Incorrect
In mixed arithmetic, all operands are promoted to the largest type involved. Here, double is the largest type, so result is double.