Bird
Raised Fist0
Javaprogramming~20 mins

Type casting in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Type Casting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of implicit and explicit casting
What is the output of the following Java code?
Java
public class Main {
    public static void main(String[] args) {
        int a = 130;
        byte b = (byte) a;
        System.out.println(b);
    }
}
A130
B0
C-126
DCompilation error
Attempts:
2 left
💡 Hint
Remember that byte can only hold values from -128 to 127, so casting int to byte may cause overflow.
❓ Predict Output
intermediate
2:00remaining
Casting double to int and output
What is the output of this Java code snippet?
Java
public class Main {
    public static void main(String[] args) {
        double d = 9.99;
        int i = (int) d;
        System.out.println(i);
    }
}
A9
BCompilation error
C9.99
D10
Attempts:
2 left
💡 Hint
Casting from double to int truncates the decimal part.
❓ Predict Output
advanced
2:00remaining
Output of casting with arithmetic expressions
What is the output of this Java program?
Java
public class Main {
    public static void main(String[] args) {
        byte b1 = 40;
        byte b2 = 50;
        byte b3 = (byte) (b1 + b2);
        System.out.println(b3);
    }
}
A100
B-116
CCompilation error
D90
Attempts:
2 left
💡 Hint
Arithmetic operations on bytes are promoted to int, so explicit cast is needed.
❓ Predict Output
advanced
2:00remaining
Casting objects and ClassCastException
What happens when you run this Java code?
Java
public class Main {
    public static void main(String[] args) {
        Object obj = "Hello";
        Integer num = (Integer) obj;
        System.out.println(num);
    }
}
AClassCastException at runtime
Bnull
CCompilation error
DHello
Attempts:
2 left
💡 Hint
Casting an object to an incompatible type causes runtime error.
🧠 Conceptual
expert
3:00remaining
Number of elements after casting and filtering
Consider this Java code snippet. How many elements are in the resulting array after casting and filtering?
Java
import java.util.Arrays;
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        Object[] arr = {1, 2.5, 3, 4.0f, 5L, "6", 7};
        int[] result = Arrays.stream(arr)
            .filter(o -> o instanceof Number)
            .mapToInt(o -> ((Number) o).intValue())
            .filter(i -> i > 3)
            .toArray();
        System.out.println(result.length);
    }
}
A4
B3
C5
D6
Attempts:
2 left
💡 Hint
Count only elements that are Number instances and have intValue() > 3.

Practice

(1/5)
1.

What is type casting in Java?

easy
A. Writing a loop
B. Creating a new variable
C. Declaring a method
D. Changing a value from one data type to another

Solution

  1. Step 1: Understand the meaning of type casting

    Type casting means converting a value from one data type to another, like from int to double or vice versa.
  2. Step 2: Identify the correct description

    Among the options, only changing a value's data type matches the definition of type casting.
  3. Final Answer:

    Changing a value from one data type to another -> Option D
  4. Quick Check:

    Type casting = Changing data type [OK]
Hint: Type casting means changing data types [OK]
Common Mistakes:
  • Confusing type casting with variable declaration
  • Thinking type casting creates new variables
  • Mixing type casting with control structures
2.

Which of the following is the correct syntax for explicit type casting from double to int in Java?

easy
A. int x = (int) 3.14;
B. int x = int(3.14);
C. int x = 3.14 as int;
D. int x = cast int 3.14;

Solution

  1. Step 1: Recall Java explicit casting syntax

    In Java, explicit casting uses parentheses with the target type before the value, like (int) value.
  2. Step 2: Match syntax with options

    Only int x = (int) 3.14; uses the correct syntax: int x = (int) 3.14;. Others are invalid in Java.
  3. Final Answer:

    int x = (int) 3.14; -> Option A
  4. Quick Check:

    Explicit cast uses (type) value [OK]
Hint: Use (type) before value for explicit cast [OK]
Common Mistakes:
  • Using function-like cast syntax (e.g., int(3.14))
  • Using 'as' keyword which Java doesn't support
  • Writing 'cast' keyword which is invalid
3.

What is the output of the following Java code?

double d = 9.99;
int i = (int) d;
System.out.println(i);
medium
A. 9.99
B. 10
C. 9
D. Compilation error

Solution

  1. Step 1: Understand explicit cast from double to int

    Casting a double to int truncates the decimal part, it does not round.
  2. Step 2: Apply casting to value 9.99

    When casting 9.99 to int, the decimal .99 is dropped, resulting in 9.
  3. Final Answer:

    9 -> Option C
  4. Quick Check:

    Double to int cast truncates decimals = 9 [OK]
Hint: Casting double to int drops decimals, no rounding [OK]
Common Mistakes:
  • Assuming casting rounds the number
  • Expecting compilation error due to cast
  • Confusing output with original double value
4.

Identify the error in the following Java code snippet:

byte b = 130;
medium
A. Type mismatch: possible lossy conversion from int to byte
B. No error, code runs fine
C. Syntax error: missing cast operator
D. Runtime exception: value too large

Solution

  1. Step 1: Understand byte range in Java

    Byte type can hold values from -128 to 127 only.
  2. Step 2: Check assigned value 130

    130 is outside byte range, so Java reports a compile-time error about possible lossy conversion.
  3. Final Answer:

    Type mismatch: possible lossy conversion from int to byte -> Option A
  4. Quick Check:

    Byte range exceeded causes compile error [OK]
Hint: Byte values must be between -128 and 127 [OK]
Common Mistakes:
  • Thinking code runs without error
  • Expecting runtime exception instead of compile error
  • Missing need for explicit cast
5.

Given the code below, what will be the output?

int a = 300;
byte b = (byte) a;
System.out.println(b);

Note: byte range is -128 to 127.

hard
A. 44
B. -56
C. 300
D. Compilation error

Solution

  1. Step 1: Understand casting int 300 to byte

    When casting int to byte, Java keeps only the lowest 8 bits of the int value.
  2. Step 2: Calculate byte value from 300

    300 in binary is 00000001 00101100 (16 bits). The lowest 8 bits are 00101100 (decimal 44).
  3. Step 3: Confirm signed byte value

    300 mod 256 = 44. However, since byte is signed, the value wraps around to -56.
  4. Final Answer:

    -56 -> Option B
  5. Quick Check:

    Int to byte cast wraps around signed byte range = -56 [OK]
Hint: Casting int to byte keeps lowest 8 bits and wraps signed range [OK]
Common Mistakes:
  • Assuming output is original int value
  • Confusing signed byte range with unsigned
  • Expecting compilation error without cast
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard