175k views
4 votes
Which of the following would be a legal Java identifier?

A) i
B) class
C) idon'tlikeclass
D) i-like-class

1 Answer

3 votes

A legal Java identifier must start with a letter, underscore, or dollar sign, and can only contain letters, digits, underscores, or dollar signs, without any spaces or special characters. Among the given options, only 'i' is a valid identifier. 'class' is a keyword, and the others contain invalid characters.

In Java, legal identifiers must start with a letter, underscore, or dollar sign and can be followed by any combination of letters, digits, underscores, or dollar signs. They cannot be Java keywords. Following these rules, the options provided are evaluated:

  • A) i is a valid identifier, as it starts with a letter.
  • B) class is not valid because it is a Java keyword.
  • C) idon'tlikeclass is not valid because it contains an apostrophe, which is not allowed.
  • D) i-like-class is not valid as it contains hyphens, which are not allowed in identifiers.

Therefore, the correct answer is A) i.

So, when choosing a legal identifier for Java, ensure it adheres to the proper format and is not a reserved keyword.

User Mike Bockus
by
8.8k points