Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the object lifecycle in Java?
The object lifecycle is the series of stages an object goes through: creation, usage, and destruction (garbage collection).
Click to reveal answer
beginner
Which Java keyword is used to create a new object?
The new keyword is used to create a new object in Java.
Click to reveal answer
intermediate
What happens when an object is no longer referenced in Java?
When no references point to an object, it becomes eligible for garbage collection, meaning Java can remove it to free memory.
Click to reveal answer
advanced
What is the role of the finalize() method in the object lifecycle?
The finalize() method is called by the garbage collector before an object is destroyed, but its use is discouraged in modern Java and deprecated since Java 9.
Click to reveal answer
intermediate
Explain the difference between stack and heap in the context of object lifecycle.
The stack stores references and method calls, while the heap stores the actual objects. Objects live in the heap during their lifecycle.
Click to reveal answer
Which stage is NOT part of the Java object lifecycle?
ACompilation
BUsage
CDestruction
DCreation
✗ Incorrect
Compilation is a step before running the program and is not part of the object lifecycle.
What keyword do you use to create a new object in Java?
Amake
Bcreate
Cnew
Dinit
✗ Incorrect
The new keyword creates a new object instance.
When does Java remove an object from memory?
AWhen the object is created
BWhen the object is no longer referenced
CWhen the program starts
DWhen the object is declared
✗ Incorrect
Java removes objects when they have no references and are eligible for garbage collection.
Where are Java objects stored during their lifecycle?
AHeap
BRegister
CStack
DCache
✗ Incorrect
Objects are stored in the heap memory during their lifecycle.
What is the purpose of the finalize() method?
ATo create an object
BTo start the program
CTo initialize variables
DTo clean up before object destruction
✗ Incorrect
The finalize() method is called before garbage collection to clean up resources, but its use is discouraged and deprecated in modern Java.
Describe the stages of the object lifecycle in Java and what happens at each stage.
Think about how an object is made, used, and then removed.
You got /4 concepts.
Explain how Java manages memory for objects during their lifecycle.
Consider where objects live and how Java frees memory.
You got /4 concepts.
Practice
(1/5)
1. Which statement best describes the lifecycle of an object in Java?
easy
A. Objects must be manually deleted to free memory.
B. An object is created automatically without new and never gets removed.
C. Objects live forever once created.
D. An object is created with new and exists as long as it has references.
Solution
Step 1: Understand object creation
In Java, objects are created using the new keyword which allocates memory.
Step 2: Understand object lifetime
An object remains alive as long as there is at least one reference pointing to it. When no references remain, it becomes eligible for garbage collection.
Final Answer:
An object is created with new and exists as long as it has references. -> Option D
Quick Check:
Object lifecycle = created with new and referenced [OK]
Hint: Objects live only while referenced, created with new [OK]
Common Mistakes:
Thinking objects live forever
Believing manual deletion is needed
Assuming objects are created without new
2. Which of the following is the correct way to create a new object of class Car in Java?
easy
A. Car myCar = Car();
B. Car myCar = new Car();
C. new Car myCar();
D. Car myCar = create Car();
Solution
Step 1: Recall Java object creation syntax
In Java, to create an object, use the syntax: ClassName variable = new ClassName();
Step 2: Match options with correct syntax
Car myCar = new Car(); matches the correct syntax. Other options have syntax errors or invalid keywords.
Final Answer:
Car myCar = new Car(); -> Option B
Quick Check:
Use new keyword to create objects [OK]
Hint: Use 'new' keyword with class name and parentheses [OK]
Common Mistakes:
Omitting 'new' keyword
Incorrect order of keywords
Using invalid method-like syntax
3. What will be the output of the following Java code?
class Demo {
public static void main(String[] args) {
Demo obj1 = new Demo();
Demo obj2 = obj1;
obj1 = null;
if (obj2 != null) {
System.out.println("Object is alive");
} else {
System.out.println("Object is gone");
}
}
}
medium
A. Object is alive
B. Object is gone
C. Compilation error
D. Runtime exception
Solution
Step 1: Analyze object references
Initially, obj1 points to a new Demo object. Then obj2 is assigned the same reference as obj1.
Step 2: Check null assignment and condition
obj1 is set to null, but obj2 still references the object. The if condition checks obj2 != null, which is true.
Final Answer:
Object is alive -> Option A
Quick Check:
Object lives while referenced = true [OK]
Hint: Object lives if any reference points to it [OK]
Common Mistakes:
Assuming object is gone when one reference is null
Confusing reference variables with objects
Expecting compilation or runtime errors
4. Identify the error in the following code related to object lifecycle:
public class Test {
public static void main(String[] args) {
String s = new String("hello");
s = null;
System.out.println(s.length());
}
}
medium
A. Compilation error due to null assignment
B. No error, prints length of string
C. NullPointerException at runtime
D. String object is not created
Solution
Step 1: Understand object reference and null assignment
The variable s initially references a String object. Then it is set to null, so it no longer points to any object.
Step 2: Analyze method call on null reference
Calling s.length() when s is null causes a NullPointerException at runtime.
Final Answer:
NullPointerException at runtime -> Option C
Quick Check:
Calling method on null reference causes exception [OK]
Hint: Don't call methods on null references [OK]
Common Mistakes:
Thinking null assignment causes compile error
Expecting output instead of exception
Ignoring null pointer risks
5. Consider the following code snippet:
class Node {
Node next;
int value;
Node(int val) { value = val; }
}
public class Test {
public static void main(String[] args) {
Node a = new Node(1);
Node b = new Node(2);
a.next = b;
b = null;
// Which nodes are eligible for garbage collection here?
}
}
Which nodes are eligible for garbage collection after b = null;?
hard
A. Neither a nor b nodes are eligible
B. Only the node originally referenced by b is eligible
C. Both nodes are eligible
D. Only the node referenced by a is eligible
Solution
Step 1: Analyze references after assignment
Variable a references a Node with value 1. This node's next points to the Node with value 2.
Step 2: Check if nodes are still reachable
Even though b is set to null, the Node with value 2 is still referenced by a.next. So both nodes are still reachable and not eligible for garbage collection.
Final Answer:
Neither a nor b nodes are eligible -> Option A
Quick Check:
Objects reachable via references are not collected [OK]
Hint: Objects reachable from any reference stay alive [OK]