What is the default value of a local variable in Java?

Java MCQ: What is the default value of a local variable in Java?

a) 0
b) null
c) undefined
d) Local variables do not have a default value

Answer:

d) Local variables do not have a default value

Explanation:

In Java, local variables are those declared within a method, constructor, or block, and they are not automatically initialized with a default value. Unlike instance variables or static variables, which are initialized by the Java compiler with default values (e.g., 0 for integers, false for booleans, null for objects), local variables must be explicitly initialized before they can be used.

Attempting to use an uninitialized local variable will result in a compilation error. This rule encourages developers to explicitly set initial values for local variables, reducing the likelihood of unintended behavior caused by uninitialized data.

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