0
0
Javaprogramming~5 mins

Type casting in Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is type casting in Java?
Type casting is converting a variable from one data type to another. It helps when you want to treat data as a different type.
Click to reveal answer
beginner
What is the difference between implicit and explicit type casting?
Implicit casting (widening) happens automatically when converting a smaller type to a bigger type (e.g., int to long). Explicit casting (narrowing) requires you to write the cast manually (e.g., double to int).
Click to reveal answer
beginner
How do you explicitly cast a double to an int in Java?
Use parentheses with the target type before the value: <br> int x = (int) 9.7; <br> This cuts off the decimal part.
Click to reveal answer
intermediate
What happens if you cast a large long value to an int?
If the long value is too big for int, the value wraps around and you get a different number. This is called overflow.
Click to reveal answer
intermediate
Can you cast between unrelated object types in Java?
No. You can only cast between compatible types in the same class hierarchy. Otherwise, you get a ClassCastException at runtime.
Click to reveal answer
Which of these is an example of implicit type casting?
Adouble to int
BString to int
Cint to long
Dlong to byte
What is the result of casting 9.99 (double) to int?
AError
B10
C9.99
D9
Which cast requires explicit syntax in Java?
Adouble to int
Bfloat to double
Cint to long
Dbyte to short
What exception occurs if you cast incompatible object types?
AClassCastException
BIllegalArgumentException
CArrayIndexOutOfBoundsException
DNullPointerException
What happens if a large long value is cast to int?
AValue remains the same
BValue is truncated to fit int size
CCompilation error
DValue is rounded
Explain the difference between implicit and explicit type casting in Java.
Think about when Java does the conversion for you and when you must tell it to convert.
You got /6 concepts.
    Describe what happens when you cast a double value to an int in Java.
    Focus on how the number changes and what you must write in code.
    You got /4 concepts.