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

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

a) true
b) false
c) 0
d) null

Answer:

b) false

Explanation:

In Java, when a boolean variable is declared as an instance variable (a member of a class) and is not explicitly initialized, it is automatically assigned a default value. For boolean variables, this default value is false.

This behavior is part of Java’s memory management, where the language ensures that all instance variables are initialized to a default value if not explicitly set by the programmer.

This helps prevent the use of uninitialized variables, which can lead to undefined behavior or errors. However, it is always a good practice to explicitly initialize variables 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

Leave a Comment

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

Scroll to Top