Difficulty: Easy
Correct Answer: Compilation fails
Explanation:
Introduction / Context:
In Java, every field declared in an interface is implicitly public, static, and final. Attempting to change such a field at runtime violates the final constraint and should be caught at compile time. This question verifies familiarity with these interface rules and constant behavior.
Given Data / Assumptions:
Concept / Approach:
Because Count.counter is a compile-time constant (final), it cannot be incremented. The statement ++counter attempts to assign a new value to a final variable, which is illegal. The compiler flags the increment as an error.
Step-by-Step Solution:
Verification / Alternative check:
A correct approach would use a local variable, e.g., short c = counter; then modify c within the loop while comparing against a fixed boundary.
Why Other Options Are Wrong:
Common Pitfalls:
Forgetting the implicit modifiers on interface fields; assuming counter is an instance field that can be changed.
Final Answer:
Compilation fails
Discussion & Comments