?
Which of the following is not a valid identifier for a Java variable?
Answer & Solution
Correct Answer:
Option
C
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.
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.
Join the Discussion
Login to post a comment or share your explanation.
Login