Type Casting in Java Interview Questions and Answers for internship

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

    • Answer: Type casting, also known as type conversion, is the process of converting a variable from one data type to another. In Java, this is necessary because Java is a strongly-typed language, meaning variables must have a specific data type.
  2. What are the two types of type casting in Java?

    • Answer: There are two types: Implicit type casting (automatic) and Explicit type casting (manual).
  3. Explain implicit type casting with an example.

    • Answer: Implicit casting happens automatically when a smaller data type is assigned to a larger data type. For example, assigning an `int` to a `long` or a `float` to a `double`. int x = 10; long y = x;
  4. Explain explicit type casting with an example.

    • Answer: Explicit casting requires manual intervention using casting operators (e.g., `(int)`, `(double)`). It's used when converting a larger data type to a smaller one, which might result in data loss. For example: double d = 10.5; int i = (int) d; (i will be 10, the decimal part is truncated)
  5. What happens when you cast a `double` to an `int`?

    • Answer: The fractional part of the `double` is truncated (removed), not rounded. The integer part is retained.
  6. What is narrowing type casting?

    • Answer: Narrowing is explicit casting from a larger data type to a smaller one (e.g., `double` to `int`, `long` to `int`). It can lead to data loss.
  7. What is widening type casting?

    • Answer: Widening is implicit casting from a smaller data type to a larger one (e.g., `int` to `long`, `float` to `double`). No data loss occurs.
  8. Can you cast a `String` to an `int` directly?

    • Answer: No, you cannot directly cast a `String` to an `int`. You need to use methods like `Integer.parseInt()` to convert the String representation of an integer into an actual `int` value. Failure to parse a non-numeric string will throw a `NumberFormatException`.
  9. How do you cast an `int` to a `String`?

    • Answer: You can use the `String.valueOf()` method or the `+ ""` operator (concatenation with an empty string). Example: `String s = String.valueOf(10);` or `String s = 10 + "";`
  10. What is the difference between casting and converting data types?

    • Answer: In Java, the terms are often used interchangeably. However, "casting" usually refers to the explicit conversion of types using the cast operator, while "converting" might encompass broader techniques, including using methods like `Integer.parseInt()` or `Double.parseDouble()`.
  11. What are some common exceptions related to type casting?

    • Answer: `ClassCastException` (occurs when attempting to cast an object to a class it's not an instance of), and `NumberFormatException` (occurs when trying to parse a non-numeric String to a numeric type).
  12. How does Java handle potential data loss during narrowing type casting?

    • Answer: Java performs truncation. The extra bits are simply discarded. It does not round the value.
  13. Give an example of a `ClassCastException` and how to avoid it.

    • Answer: Trying to cast an `Object` to a `String` when the `Object` is actually an `Integer`. Avoid it by using the `instanceof` operator to check the object type before casting: if (myObject instanceof String) { String str = (String) myObject; }
  14. Explain the concept of upcasting and downcasting.

    • Answer: Upcasting is implicit casting from a subclass to a superclass. Downcasting is explicit casting from a superclass to a subclass. Downcasting requires caution to avoid `ClassCastException`.

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