Which of the following is not a valid Java identifier?

Java MCQ: Which of the following is not a valid Java identifier?

a) myVariable
b) $myVariable
c) _myVariable
d) 1myVariable

Answer:

d) 1myVariable

Explanation:

In Java, an identifier is a name given to a variable, method, class, or any other user-defined item. Identifiers must follow certain rules: they can start with a letter, underscore (_), or dollar sign ($), but not with a digit.

The identifier 1myVariable is invalid because it starts with a digit, which is not allowed. Valid identifiers include myVariable, $myVariable, and _myVariable.

Choosing meaningful and valid identifiers is crucial for writing clear and maintainable Java code. It helps in avoiding errors and improving code readability.

Reference links:

https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top