Which Of The Following Is Not A Legal Variable Name

4 min read

Which of the following is not a legal variable name: Understanding the Rules that Govern Naming in Programming

When asking which of the following is not a legal variable name, the answer depends on the naming conventions defined by the programming language you are using. In most modern languages such as C, Java, Python, and JavaScript, a variable name must start with a letter or an underscore, may contain letters, digits, and underscores, and cannot be a reserved keyword. This article breaks down those rules, shows examples of valid and invalid names, and highlights the specific option that violates the naming policy.

Introduction to Variable Naming Conventions

A variable name (also called an identifier) is the label you assign to a storage location that holds a value. The legal variable name criteria are not arbitrary; they are designed to keep code readable, avoid syntax errors, and prevent conflicts with language constructs. The main constraints are:

  1. First character – must be a letter (A‑Z, a‑z) or an underscore (_).
  2. Subsequent characters – may be letters, digits (0‑9), or underscores.
  3. No spaces or punctuation – commas, periods, hyphens, or other symbols are prohibited.
  4. Avoid reserved keywords – words like class, if, while, function, etc., cannot be used as identifiers.
  5. Case sensitivity – myVariable and Myvariable are distinct in most languages.
  6. Length – virtually unlimited, though very long names can reduce readability.

If any of these rules are broken, the identifier is not a legal variable name. The following sections explore each rule in detail and illustrate common pitfalls That's the whole idea..

Common Legal Variable Names

Below are examples of identifiers that satisfy all the rules in most mainstream languages:

  • totalSum
  • _privateVar
  • item123
  • myVariable
  • ΣScore (Unicode allowed in languages like Python and JavaScript)

Notice that each name begins with a letter or underscore, contains only alphanumeric characters and underscores, and does not clash with reserved words.

Identifying Illegal Variable Names

1. Starts with a Digit

2ndPlace, 9Lives, 42 – these are invalid because the first character is a numeric digit Easy to understand, harder to ignore..

2. Contains Spaces or Special Characters

first name, user-name, my var!, price$ – spaces and symbols such as hyphens, dollar signs, or exclamation marks break the identifier syntax Small thing, real impact..

3. Uses Reserved Keywords

class, function, while, await, import – these are language‑specific reserved words; attempting to use them as variable names will cause a compilation or runtime error.

4. Includes Unicode Without Support

While languages like Python 3 and JavaScript permit Unicode identifiers (e.g., 变量), older languages or environments that only support ASCII will reject non‑ASCII characters.

5. Contains a Period or Comma

my.var, total,sum – punctuation marks are parsed as operators or separators, not part of an identifier.

6. Duplicate of an Existing Variable (in the same scope)

Although not a syntax error, reusing a name can lead to logical bugs; however, it is still syntactically legal.

Example Question: Which of the Following Is Not a Legal Variable Name?

Consider the following five options:

  1. total_sum
  2. 2ndPlace
  3. _privateVar
  4. myVariable
  5. price$

Analysis of each option

Option Reason it Is Legal / Illegal
**1.
**3.
5. myVariable Begins with a letter, contains only letters → legal. On top of that,
**4.
2. _privateVar Starts with an underscore, followed by letters → legal (common convention for private members). total_sum**

This is the bit that actually matters in practice No workaround needed..

From the table, both 2ndPlace and price$ violate the naming rules. If the multiple‑choice question expects a single answer, the most obvious violation is 2ndPlace, because it fails the first‑character rule, whereas price$ fails due to an invalid character after the first character. In many exam contexts, the intended answer is 2ndPlace, as it is the only option that begins with a digit, a clear and universal breach.

Language‑Specific Nuances

While the general principles above apply to the majority of programming languages, there are subtle differences:

  • Python: Allows Unicode letters, so 变量 is valid. Still, it still forbids starting with a digit and prohibits spaces.
  • Java: Requires the identifier to be a Java Identifier, which mirrors the ASCII‑based rules; keywords like class are reserved.
  • JavaScript: Similar to Java but also permits Unicode escaper sequences (e
Just Got Posted

New and Noteworthy

Close to Home

Same Topic, More Views

Thank you for reading about Which Of The Following Is Not A Legal Variable Name. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home