Interview questions on type casting
Interview questions on type casting
- What is type casting.?
When the data is converted from one data type to another data type, then it is called type casting. type casting is nothing but changing the type of the data.
- What are the types of casting?
There are two types of casting -
1)Primitive Casting : When the data is casted from primitive type ( int, float, double etc… ) to another primitive type, then it is called as Primitive Casting.
2) Derived Casting : When the data is casted from one derived type to another derived type, then it is called derived casting.
- What is auto widening and explicit narrowing.?
The data is implicitly casted from small sized primitive type to big sized primitive type. This is called auto-widening. i.e The data is automatically casted from short to int, int to long, long to float and float to double.
We have to explicitly cast the data from big sized primitive type to small sized primitive type. i.e you have to explicitly convert the data from double to float, float to long, long to int, int to short and short to byte This is called as explicit narrowing.
- What is auto-up casting and explicit down casting.?
An object of sub class type can be automatically casted to super class type. This is called auto-up casting. An object of super class type should be explicitly casted to sub class type, It is called explicit down casting.
- Can an int primitive type of data implicitly casted to Double derived type.?
Yes, first int is auto-widened to double and then double is auto-boxed to Double.
- What is ClassCastException?
ClassCastException is an exception which occurs at run time when an object of one type can not be casted to another type.
- How can you avoid ClassCastException ?
By using generics, you can avoid ClassCastException.
- What is boxing and unboxing.?
Wrapping of primitive content into corresponding wrapper class object is called boxing. Unwrapping the wrapper class object into corresponding primitive content is called unboxing.
- What is the difference between auto-widening, auto-upcasting and auto-boxing.?
Auto-widening occurs when small sized primitive type is casted to big sized primitive type. Auto-upcasting occurs when sub class type is casted to super class type. Auto-boxing occurs when primitive type is casted to corresponding wrapper class.
Comments
Post a Comment