Question:

Which of the following is not allowed as first character in valid variable name in Java?

Show Hint

In Java, variable names can start with a {letter, underscore (_), or dollar (\$)}, but {not with a digit}.
  • Underscore (_)
  • Digit
  • Letter
  • Dollar (\$)
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Solution and Explanation

Step 1: Understanding variable naming rules in Java.
In Java programming, variables are used to store data values. Each variable must have a valid name that follows certain rules defined by the Java language. These rules ensure that the compiler can correctly identify variables in a program.
Step 2: Rules for naming variables in Java.
A valid Java variable name must follow these important rules:
1. The variable name must start with a letter (A–Z or a–z), underscore (_), or dollar sign (\$).
2. It cannot begin with a numeric digit.
3. After the first character, the name can include letters, digits, underscores, or dollar symbols.
4. The variable name cannot be a reserved keyword such as int, class, or public.
Step 3: Analysis of the options.
(A) Underscore (_): Allowed as the first character in a variable name.
(B) Digit: Not allowed as the first character in a Java variable name.
(C) Letter: Allowed as the first character of a variable name.
(D) Dollar (\$): Also allowed as the first character in Java identifiers.
Step 4: Conclusion.
Therefore, the option that cannot be used as the first character in a valid Java variable name is a digit.
Final Answer: Digit
Was this answer helpful?
0
0