Which of the following is a legal identifier in Java?

Enhance your Java programming skills with our Introduction to Java Programming Test. Boost your confidence with our multiple choice questions, each complete with hints and explanations. Prepare for your exam success!

In Java, an identifier is a name used to identify a variable, class, method, or any other user-defined item. To be considered legal, an identifier must follow certain rules:

  1. It can consist of letters (uppercase and lowercase), digits, underscores (_), and dollar signs ($).
  1. It must begin with a letter, underscore, or dollar sign, but cannot begin with a digit.

  2. Identifiers cannot include spaces, punctuation marks, or special characters (with the exception of underscores and dollar signs).

  3. Identifiers cannot be the same as Java reserved keywords.

The choice 'first' adheres to all of these rules. It uses only letters and begins with a letter, making it a valid identifier in Java.

In contrast, the other options violate these identifier rules:

  • 'employee Salary' contains a space, making it illegal.

  • 'Hello!' includes an exclamation mark, which is not permitted in identifiers.

  • '2ndProduct' begins with a digit, which is not allowed as per the identifier rules.

Thus, 'first' stands out as the only legal identifier among the presented choices.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy