0
0
Javaprogramming~20 mins

Type promotion in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Type Promotion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
    }
}
A30.0
B30
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Remember that byte is promoted to int before arithmetic.
Predict Output
intermediate
2: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);
    }
}
A66
BB
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
char is promoted to int before addition.
Predict Output
advanced
2: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);
    }
}
A5
BCompilation error
CRuntime error
D15
Attempts:
2 left
💡 Hint
Compound assignment operators include implicit casting.
Predict Output
advanced
2: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);
    }
}
ACompilation error
B15
CRuntime error
D5
Attempts:
2 left
💡 Hint
Check the type of b + 10 expression and assignment compatibility.
🧠 Conceptual
expert
3: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;
    }
}
Along
Bfloat
Cdouble
Dint
Attempts:
2 left
💡 Hint
Remember the order of type promotion in mixed expressions: from smaller to larger types.