Casting and Range of Variables - AP Computer Science A

  1. What is overflow in the context of variable casting?
    Overflow is exceeding the maximum range of a data type. Value wraps around when exceeding maximum.
  2. What is the default data type for decimal values in Java?
    double. Java infers double for literal decimal values.
  3. How do you cast an int to a double?
    Implicit cast: No cast needed. Widening conversion happens automatically.
  4. What is underflow in the context of variable casting?
    Underflow is exceeding the minimum range of a data type. Value wraps around when below minimum.
  5. Can you cast boolean to int in Java?
    No, boolean cannot be cast to int. boolean is incompatible with numeric types.
  6. What is a narrowing conversion?
    Converting a larger data type to a smaller one. Potential data loss when reducing precision.
  7. Identify the cast needed to convert char to byte.
    Explicit cast: (byte) value. Narrowing from 16-bit to 8-bit integer.
  8. Find the cast needed to convert long to int.
    Explicit cast: (int) value. Narrowing conversion from 64-bit to 32-bit.
  9. What are primitive data types in Java?
    byte, short, int, long, float, double, char, boolean. Eight basic data types built into Java.
  10. What is the range of a float in Java?
    Approximately 3.4×10−383.4 \times 10^{-38} to 3.4×10383.4 \times 10^{38}. 32-bit IEEE 754 floating-point precision.
  11. What is the effect of casting int to char?
    Character representation of the int value. Unicode value converted to character symbol.
  12. What is the range of a long in Java?
    −263-2^{63} to 263−12^{63}-1. Standard 64-bit signed integer range.
  13. Identify the cast needed to convert double to int.
    Explicit cast: (int) value. Narrowing conversion requires explicit cast operator.
  14. What is a widening conversion?
    Converting a smaller data type to a larger one. Preserves data when increasing capacity.
  15. What is the range of a byte in Java?
    −27-2^{7} to 27−12^{7}-1. Standard 8-bit signed integer range.
  16. Can you implicitly cast int to float?
    Yes, int can be implicitly cast to float. int fits within float's range safely.
  17. What keyword is used to explicitly cast types?
    Cast operator, e.g., (int) or (float). Parentheses with target type name.
  18. What is the range of a char in Java?
    00 to 6553565535 (unsigned). 16-bit Unicode characters from 0 to 65535.
  19. What is implicit casting?
    Implicit casting is automatic type conversion by the Java compiler. Happens when widening conversions are safe.
  20. What is the result of casting int to boolean?
    Not allowed; compile-time error. boolean and numeric types are incompatible.
  21. Which data type is used for precise decimal values?
    double. double provides highest precision for decimals.
  22. What is a potential issue when casting long to int?
    Loss of data if the value exceeds int range. Values outside int range cause overflow.
  23. How would you cast int to byte?
    Explicit cast: (byte) value. Narrowing conversion from 32-bit to 8-bit.
  24. Identify the cast needed to convert double to float.
    Explicit cast: (float) value. Narrowing conversion loses precision.
  25. What is the result of casting char to int?
    The Unicode value of the character. char represents Unicode code points.
  26. What is casting in Java?
    Casting is converting a variable from one data type to another. Required when changing between incompatible types.
  27. What is explicit casting?
    Explicit casting is manually converting a data type using a cast operator. Required for narrowing conversions to prevent data loss.
  28. What is the range of a double in Java?
    Approximately 1.7×10−3081.7 \times 10^{-308} to 1.7×103081.7 \times 10^{308}. 64-bit IEEE 754 floating-point precision.
  29. What is type promotion in expressions?
    Automatic conversion of types to prevent data loss. Smaller types promoted to avoid overflow.
  30. Identify the cast needed to convert short to long.
    Implicit cast: No cast needed. Widening from 16-bit to 64-bit integer.
  31. What happens when casting float to int?
    The fractional part is truncated. Decimal portion removed, not rounded.
  32. What is the purpose of casting in programming?
    To convert one data type to another. Ensures compatibility between different data types.
  33. What is the range of a short in Java?
    −215-2^{15} to 215−12^{15}-1. Standard 16-bit signed integer range.
  34. Which cast should be used to convert float to double?
    Implicit cast: No cast needed. Widening conversion preserves precision.
  35. Identify the cast needed to convert byte to short.
    Implicit cast: No cast needed. Widening from 8-bit to 16-bit integer.
  36. Which data type has the largest range: int, short, or byte?
    int. int has 32 bits vs 16 for short, 8 for byte.
  37. What happens if you cast a double to int?
    Fractional part is truncated. Decimal portion discarded, integer part kept.
  38. What is the range of an int in Java?
    −231-2^{31} to 231−12^{31}-1. Standard 32-bit signed integer range.
  39. What happens when you cast a larger type to a smaller type?
    Possible data loss or precision loss. Narrowing conversions truncate or overflow values.

Study these flashcards