Recall & Review
beginner
What is an instance variable in Java?
An instance variable is a variable declared inside a class but outside any method, constructor, or block. Each object of the class has its own copy of the instance variable.Click to reveal answer
intermediate
Where are instance variables stored in memory?
Instance variables are stored in the heap memory as part of the object they belong to.
Click to reveal answer
beginner
How do instance variables differ from static variables?
Instance variables belong to objects and each object has its own copy. Static variables belong to the class itself and are shared among all objects.Click to reveal answer
beginner
Can instance variables have default values if not initialized?
Yes, instance variables have default values based on their type (e.g., 0 for int, null for objects) if not explicitly initialized.
Click to reveal answer
beginner
Example: Identify the instance variable in this code snippet:<br><pre>class Car {<br> String color;<br> void setColor(String c) {<br> color = c;<br> }<br>}</pre>The instance variable is <code>color</code>. It is declared inside the class but outside any method, so each Car object has its own color.Click to reveal answer
What does each object of a class have regarding instance variables?
✗ Incorrect
Each object has its own copy of instance variables, so changes in one object do not affect others.
Where are instance variables declared in Java?
✗ Incorrect
Instance variables are declared inside the class but outside any method or constructor.
What is the default value of an uninitialized instance variable of type int?
✗ Incorrect
Instance variables of type int default to 0 if not initialized.
Which memory area stores instance variables?
✗ Incorrect
Instance variables are stored in the heap as part of the object.
How do instance variables differ from static variables?
✗ Incorrect
Instance variables belong to each object separately; static variables belong to the class and are shared.
Explain what instance variables are and how they behave in Java objects.
Think about how each object keeps its own data separate.
You got /4 concepts.
Describe the difference between instance variables and static variables in Java.
Consider who owns the variable: the object or the class.
You got /4 concepts.