Which of the following is not a valid Java variable name (apply Java identifier rules, no spaces; may include letters, digits, underscore, or dollar sign; must not start with a digit)?

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:

  • Identifiers cannot contain spaces.
  • Allowed characters after the first: letters, digits, underscore, dollar sign.
  • First character cannot be a digit.


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:

Step 1: Inspect starting character of each candidate.Step 2: Verify remaining characters belong to the allowed set.Step 3: Mark any starting with a digit as invalid.


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$

More Questions from Microprocessors

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion