220k views
5 votes
Checkmark each of the variable names listed below that is a valid Java identifier. CurrentTemp, Day1, HighTemp, Temp, T, 1stDayHighTemp.

a) CurrentTemp, Day1, HighTemp
b) Temp, T, 1stDayHighTemp
c) Day1, Temp, T
d) CurrentTemp, HighTemp, 1stDayHighTemp

User Ntshetty
by
8.1k points

1 Answer

5 votes

Final answer:

Valid Java identifiers must start with a letter, underscore, or dollar sign, and cannot start with a digit. The correct valid identifiers from the list provided are CurrentTemp, Day1, HighTemp, Temp, T. The identifier 1stDayHighTemp is invalid because it begins with a digit.

Step-by-step explanation:

When checking variable names to determine which ones are valid Java identifiers, there are a few rules we need to follow. A valid identifier in Java:

  • Must start with a letter (A-Z or a-z), underscore (_), or dollar sign ($).
  • Cannot start with a digit.
  • Can have any combination of characters, digits, underscores, and dollar signs after the first character.
  • Cannot be a reserved keyword in Java.

Given these rules, let's evaluate the variable names provided:

  • CurrentTemp - valid; starts with a letter and contains no illegal characters.
  • Day1 - valid; starts with a letter and followed by a digit, which is allowed.
  • HighTemp - valid; starts with a letter and contains no illegal characters.
  • Temp - valid; starts with a letter and contains no illegal characters.
  • T - valid; a single letter is a valid identifier.
  • 1stDayHighTemp - invalid; starts with a digit, which is not allowed.

Therefore, the correct answer is option a) CurrentTemp, Day1, HighTemp.

User JonoB
by
6.3k points