QuizzesType Casting — Concept Check
intermediate
java

Type Casting — Concept Check

Questions on Type Casting. Understand implicit widening, explicit narrowing casts for primitives, and safe object upcasting and downcasting with instanceof guards.

8 minutes 8 questions Pass at 70%
Time Limit
8 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

8 questions on Type Casting — Concept Check. Work through them here, then start the quiz to answer them and see your score.

  1. 1.In Java, what happens when you cast a long value of 300 to a byte?

    • AThe value becomes -44 due to sign change.
    • BA runtime error occurs due to overflow.
    • CThe value becomes 300, as byte can store it.
    • DThe value becomes 44 due to overflow and truncation.
  2. 2.You have a variable 'double pi = 3.14159;' and you cast it to an int. What is the result?

    • A4, because casting rounds the value.
    • B3, because casting truncates the decimal part.
    • C3.14159, as double cannot be cast to int.
    • DAn error occurs because double cannot be cast to int.
  3. 3.A method receives an Animal reference pointing to a Dog object. What happens if you call fetch()?

    • AA compile error occurs, as Animal does not have fetch().
    • BAn implicit cast allows fetch() to be called.
    • CA runtime error occurs, as Animal cannot call Dog methods.
    • DThe fetch() method executes, as Dog has it.
  4. 4.What is the result of 'int a = 5, b = 2; System.out.println(a / b);'?

    • A0, because integer division rounds down.
    • B2.5, because division should return a float.
    • CAn error, as division requires floating-point numbers.
    • D2, because integer division truncates the decimal.
  5. 5.Given 'Animal a = new Dog();', what is true about 'a instanceof Dog'?

    • AIt returns false, as 'a' is an Animal reference.
    • BIt returns true, as 'a' is a Dog instance.
    • CIt returns true only if cast to Dog first.
    • DIt causes a compile error, as instanceof is not allowed here.
  6. 6.Why might 'float f = 9999999999999999L;' not store the exact value?

    • AFloat can't represent all 64-bit integers, losing precision.
    • BLong values are truncated to fit in float.
    • CFloat can store any long value exactly.
    • DAn error occurs due to incompatible types.
  7. 7.What happens if you try to store an Integer in a String[] array?

    • AThe Integer is stored as a String.
    • BAn implicit cast occurs to convert Integer to String.
    • CThe array resizes to accommodate the Integer.
    • DArrayStoreException is thrown at runtime.
  8. 8.What is the result of 'byte a = 10, b = 20; byte result = (byte)(a + b);'?

    • AAn error, as byte cannot store the sum of two bytes.
    • B30, as byte arithmetic does not promote to int.
    • C30, as the sum is explicitly cast back to byte.
    • D0, due to overflow in byte addition.