Difficulty: Easy
Correct Answer: 2s$
Explanation:
Introduction:
Java variable names must follow strict lexical rules. This question tests whether you can distinguish valid identifiers from invalid ones, especially with digits and the dollar sign.
Given Data / Assumptions:
Concept / Approach:
Check each name against the rules: start character validity, presence of illegal characters (space), and general composition. The names 'lot', 'c1t', and 's$2' all start with a letter and contain only permitted characters afterward. The name '2s$' starts with a digit, which violates the starting-character rule.
Step-by-Step Solution:
Verification / Alternative check:
Compile-time test: int 2s$ = 0; // illegalint s$2 = 0; // legal
Why Other Options Are Wrong:
'lot' – letters only; valid.
'c1t' – digit allowed after first character; valid.
's$2' – dollar sign is allowed in identifiers (though discouraged by style guides); valid.
Common Pitfalls:
Thinking the dollar sign is forbidden (it is allowed, but not recommended); assuming digits are allowed at the start; including spaces inside identifiers.
Final Answer:
2s$
Discussion & Comments