Vidyalelo
Java Programming · Q4

Data Types and Variables

Programming · Java Programming · question 4

Q4

Which of the following is not a valid identifier for a Java variable?

A.
my_var
B.
_myVar
C.
3rdVar
Answer
D.
$var

Answer: Option C

Solution

Answer: Option C
Solution:
The correct answer is Option C: 3rdVar.

In Java, variable identifiers (names) must follow certain rules:

They can only start with a letter (A-Z or a-z), underscore (_), or dollar sign ($).
The subsequent characters can be letters, digits (0-9), underscores, or dollar signs.
They cannot be a Java keyword (reserved word).

Option A (my_var), Option B (_myVar), and Option D ($var) all follow these rules and are valid variable identifiers.

Option C (3rdVar) is not valid because it starts with a digit, which is not allowed for variable names in Java.