What is the default value of an object reference declared as an instance variable in Java?

Java MCQ: What is the default value of an object reference declared as an instance variable in Java?

a) 0
b) null
c) undefined
d) An object reference does not have a default value

Answer:

b) null

Explanation:

In Java, the default value of an object reference declared as an instance variable is null. This means that the reference does not point to any object until it is explicitly initialized.

Primitive types like int, boolean, and float have their respective default values (0, false, 0.0f), but object references default to null. Attempting to access or invoke methods on a null reference will result in a NullPointerException.

Understanding default values is important for avoiding common pitfalls like NullPointerException and ensuring that object references are properly initialized before use.

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