Java MCQ: Which of the following is a valid Java variable declaration?
a) int number = 10;
b) float 2value = 5.5;
c) char letter = ‘A’;
d) Both a and c
Answer:
d) Both a and c
Explanation:
In Java, a valid variable declaration must follow specific syntax rules. The variable name must start with a letter, underscore, or dollar sign, and cannot start with a digit. Therefore, int number = 10;
and char letter = 'A';
are valid declarations, while float 2value = 5.5;
is invalid because the variable name starts with a digit.
Variables in Java must also be of a specific data type, such as int
, float
, char
, etc., and the value assigned to the variable must be compatible with the declared type.
Understanding variable declaration rules is fundamental for writing correct and error-free Java programs.
Reference links:
https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html