Casting and Range of Variables - AP Computer Science A
- 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.
- What is the default data type for decimal values in Java?
- double. Java infers double for literal decimal values.
- How do you cast an int to a double?
- Implicit cast: No cast needed. Widening conversion happens automatically.
- 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.
- Can you cast boolean to int in Java?
- No, boolean cannot be cast to int. boolean is incompatible with numeric types.
- What is a narrowing conversion?
- Converting a larger data type to a smaller one. Potential data loss when reducing precision.
- Identify the cast needed to convert char to byte.
- Explicit cast: (byte) value. Narrowing from 16-bit to 8-bit integer.
- Find the cast needed to convert long to int.
- Explicit cast: (int) value. Narrowing conversion from 64-bit to 32-bit.
- What are primitive data types in Java?
- byte, short, int, long, float, double, char, boolean. Eight basic data types built into Java.
- What is the range of a float in Java?
- Approximately to . 32-bit IEEE 754 floating-point precision.
- What is the effect of casting int to char?
- Character representation of the int value. Unicode value converted to character symbol.
- What is the range of a long in Java?
- to . Standard 64-bit signed integer range.
- Identify the cast needed to convert double to int.
- Explicit cast: (int) value. Narrowing conversion requires explicit cast operator.
- What is a widening conversion?
- Converting a smaller data type to a larger one. Preserves data when increasing capacity.
- What is the range of a byte in Java?
- to . Standard 8-bit signed integer range.
- Can you implicitly cast int to float?
- Yes, int can be implicitly cast to float. int fits within float's range safely.
- What keyword is used to explicitly cast types?
- Cast operator, e.g., (int) or (float). Parentheses with target type name.
- What is the range of a char in Java?
- to (unsigned). 16-bit Unicode characters from 0 to 65535.
- What is implicit casting?
- Implicit casting is automatic type conversion by the Java compiler. Happens when widening conversions are safe.
- What is the result of casting int to boolean?
- Not allowed; compile-time error. boolean and numeric types are incompatible.
- Which data type is used for precise decimal values?
- double. double provides highest precision for decimals.
- 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.
- How would you cast int to byte?
- Explicit cast: (byte) value. Narrowing conversion from 32-bit to 8-bit.
- Identify the cast needed to convert double to float.
- Explicit cast: (float) value. Narrowing conversion loses precision.
- What is the result of casting char to int?
- The Unicode value of the character. char represents Unicode code points.
- What is casting in Java?
- Casting is converting a variable from one data type to another. Required when changing between incompatible types.
- What is explicit casting?
- Explicit casting is manually converting a data type using a cast operator. Required for narrowing conversions to prevent data loss.
- What is the range of a double in Java?
- Approximately to . 64-bit IEEE 754 floating-point precision.
- What is type promotion in expressions?
- Automatic conversion of types to prevent data loss. Smaller types promoted to avoid overflow.
- Identify the cast needed to convert short to long.
- Implicit cast: No cast needed. Widening from 16-bit to 64-bit integer.
- What happens when casting float to int?
- The fractional part is truncated. Decimal portion removed, not rounded.
- What is the purpose of casting in programming?
- To convert one data type to another. Ensures compatibility between different data types.
- What is the range of a short in Java?
- to . Standard 16-bit signed integer range.
- Which cast should be used to convert float to double?
- Implicit cast: No cast needed. Widening conversion preserves precision.
- Identify the cast needed to convert byte to short.
- Implicit cast: No cast needed. Widening from 8-bit to 16-bit integer.
- Which data type has the largest range: int, short, or byte?
- int. int has 32 bits vs 16 for short, 8 for byte.
- What happens if you cast a double to int?
- Fractional part is truncated. Decimal portion discarded, integer part kept.
- What is the range of an int in Java?
- to . Standard 32-bit signed integer range.
- What happens when you cast a larger type to a smaller type?
- Possible data loss or precision loss. Narrowing conversions truncate or overflow values.