Java Questions 41 - 50  «Prev  Next»

Java SE 25 Interview Questions: Assignment, Scope, and Boxing

Review signed byte values, compound assignment, checked reference casts, variable scope, and character-to-wrapper conversions.

  1. Are Java byte values signed?

    Answer: Yes. Byte is an 8-bit signed two's-complement integral type with values from -128 through 127. A bit pattern such as all eight bits set represents -1 as a byte.

    Utilities such as Byte.toUnsignedInt can interpret that pattern as an unsigned value from 0 through 255 in an int. They do not introduce an unsigned byte primitive.

  2. What implicit conversion does compound assignment provide?

    Answer: A compound operation such as b += amount converts the operation's result back to the left variable's type as specified by the compound-assignment rules. For a byte, it behaves numerically like b = (byte) (b + amount).

    The left-hand expression is evaluated only once, which matters for array indices or other side effects. This conversion can hide overflow or information loss; it does not validate a numeric range.

  3. How do arithmetic compound assignment operators behave?

    Answer: They combine an operation with assignment while evaluating the left-hand operand once. Arithmetic still follows the relevant promotion, division, and overflow rules before conversion back to the variable's type.

    public class CompoundAndBoxing {
        public static void main(String[] args) {
            byte count = 120;
            count += 10;
            System.out.println(count);
            int[] values = {10, 20};
            int index = 0;
            values[index++] += 5;
            System.out.println(index + " " + values[0]);
            char letter = 'N';
            Integer integer = (int) letter;
            Double decimal = (double) letter;
            System.out.println(integer + " " + decimal);
        }
    }

    This prints -126, then 1 15, then 78 78.0. Integer division by zero still throws ArithmeticException; compound assignment is not an exception to arithmetic rules.

  4. What is the assignment rule for superclass and subclass references?

    Answer: A subclass reference can be widened to a compatible superclass type. Narrowing a superclass-typed expression normally requires a cast or a successful pattern test.

    A checked cast of a non-null value succeeds only when the actual object is compatible with the target type. It cannot turn a parent object into a child object. Casting null to a reference type simply yields null.

  5. What is ClassCastException?

    Answer: It is an unchecked exception raised when a checked reference cast finds an incompatible non-null object. For example, an Object variable holding an Integer cannot successfully be cast to String.

    Some impossible casts are rejected at compile time instead. Use accurate generic types and pattern tests to avoid unsafe assumptions; catching ClassCastException is usually less clear than designing the type boundary correctly.

  6. Which variable categories have different scope rules?

    Answer: Fields, parameters, local variables, and pattern variables have defined scopes. A variable declared in a nested block is a local variable, not a separate primitive category called a block variable.

    Scope describes where a declaration can be referred to by name. It differs from access control and runtime lifetime. Static and instance fields differ in ownership, while pattern-variable scope can depend on control flow.

  7. What commonly causes variable-scope errors?

    Answer: Using a local variable outside its declaring scope is a common cause. A variable declared inside an if block is not available by that name after the block ends.

    Another issue is reading a local that is in scope but not definitely assigned on every relevant path. These are separate checks. Declare variables in the smallest useful scope and initialize them along paths that establish a meaningful value.

  8. Can a char be assigned directly to Integer, Long, Float, or Double?

    Answer: Not through arbitrary widening followed by boxing in an ordinary assignment. A char normally boxes to Character. To obtain another numeric wrapper, first convert to the corresponding primitive, as in Integer value = (int) 'N';.

    Similarly use (long), (float), or (double) before boxing to Long, Float, or Double. The executable example above demonstrates Integer and Double results. These are numeric code-unit values, not parsing of the character as a decimal digit.

  9. What are local variables sometimes called?

    Answer: Older texts may call them automatic, temporary, method, or stack variables. Local variable is the precise language term and includes locals in constructors and blocks as well as methods.

    They must be definitely assigned before reading. Calling them stack variables should not be interpreted as a guarantee about physical storage after JVM optimization.

  10. What is the scope of an instance field?

    Answer: A member field's declaration is in scope throughout the class body, including nested declarations, subject to rules such as shadowing and forward-reference restrictions. Access from other code also depends on accessibility and inheritance.

    Each object has its own instance-field variable. An instance receiver is needed to access it from a static method. This ownership and receiver requirement is different from the field declaration's lexical scope.

References: JLS: conversions, JLS: scope and access, and JLS: compound assignment.

SEMrush Software