Java MCQ: Which keyword is used to create a constant variable in Java?
a) final
b) const
c) static
d) constant
Answer:
a) final
Explanation:
In Java, the final
keyword is used to create a constant variable, meaning that the variable’s value cannot be changed once it has been assigned. This makes the variable immutable, providing a way to define constants in your program.
For example, final int MAX_VALUE = 100;
declares a constant integer with the value of 100. If you try to reassign a value to MAX_VALUE
, the compiler will generate an error.
The use of constants is important in programming for values that should remain unchanged throughout the program’s execution, helping to make the code more readable and maintainable.
Reference links:
https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html