Type Casting in Java Interview Questions and Answers for freshers

100 Java Type Casting Interview Questions for Freshers
  1. What is type casting in Java?

    • Answer: Type casting, also known as type conversion, is the process of converting a variable of one data type into another data type. It's essential for handling different data types in Java's strictly-typed system. There are two main types: implicit and explicit.
  2. Explain implicit type casting with an example.

    • Answer: Implicit type casting, or widening conversion, happens automatically when a smaller data type is assigned to a larger data type. No explicit code is needed. For example: int x = 10; double y = x; Here, the integer x is automatically converted to a double y without any explicit cast.
  3. Explain explicit type casting with an example.

    • Answer: Explicit type casting, or narrowing conversion, requires a manual cast using parentheses. It's necessary when converting a larger data type to a smaller one, potentially leading to data loss. Example: double z = 10.5; int a = (int) z; Here, the double z is explicitly cast to an integer a, truncating the decimal part.
  4. What are the possible data loss scenarios in type casting?

    • Answer: Data loss can occur during narrowing conversions. For example, converting a double to an int truncates the fractional part. Converting a larger integer to a smaller integer (like a long to an int) can lead to overflow if the value exceeds the smaller type's range.
  5. What happens if you try to cast a String to an int without proper parsing?

    • Answer: You'll get a compile-time error. Direct casting from String to a numeric type like int is not allowed. You need to use methods like Integer.parseInt() or similar to parse the String into an integer first.
  6. How do you cast a String to an integer in Java?

    • Answer: Use Integer.parseInt("yourString"). This method attempts to convert the String to an int. It throws a NumberFormatException if the String cannot be parsed.
  7. What is the difference between casting and converting?

    • Answer: In Java, the terms are often used interchangeably, especially in the context of primitive data types. However, a subtle difference is that "converting" might encompass broader data transformations (like converting a String to a Date), while "casting" is more specifically about changing the type of a variable.
  8. Can you cast an object of a subclass to its superclass?

    • Answer: Yes, this is called upcasting and is generally safe. It's implicitly allowed because a subclass "is-a" superclass. The subclass object will retain its original type, but the reference will be treated as if it's of the superclass type.
  9. Can you cast an object of a superclass to its subclass?

    • Answer: Yes, but this is called downcasting and requires an explicit cast. It's potentially unsafe and can lead to a ClassCastException at runtime if the object isn't actually an instance of the subclass.

Thank you for reading our blog post on 'Type Casting in Java Interview Questions and Answers for freshers'.We hope you found it informative and useful.Stay tuned for more insightful content!