0
0
Javaprogramming~20 mins

Object lifecycle in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Java Object Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Java code involving object creation and garbage collection?

Consider the following Java code snippet. What will be printed when it runs?

Java
public class Test {
    public static void main(String[] args) {
        Test obj = new Test();
        obj = null;
        System.gc();
        System.out.println("End of main");
    }
    protected void finalize() throws Throwable {
        System.out.println("Finalize called");
    }
}
AEnd of main
BEnd of main\nFinalize called
CFinalize called\nEnd of main
DNo output
Attempts:
2 left
πŸ’‘ Hint

Garbage collection in Java is not guaranteed to run immediately after System.gc().

❓ Predict Output
intermediate
2:00remaining
What is the output when creating multiple objects and nullifying references?

What will be printed by this Java program?

Java
public class Demo {
    static int count = 0;
    Demo() {
        count++;
    }
    public static void main(String[] args) {
        Demo a = new Demo();
        Demo b = new Demo();
        a = null;
        System.gc();
        System.out.println(count);
    }
}
A2
B1
C0
DCompilation error
Attempts:
2 left
πŸ’‘ Hint

Count increments on each object creation, not on garbage collection.

πŸ”§ Debug
advanced
2:00remaining
Identify the error in this Java object lifecycle code

What error will this code produce when compiled or run?

Java
public class Sample {
    public void finalize() throws Throwable {
        System.out.println("Cleaning up");
    }
    public static void main(String[] args) {
        Sample s = new Sample();
        s = null;
        System.gc();
    }
}
ANo error, prints "Cleaning up"
BCompile-time error: finalize() must throw Throwable
CNo output, program ends silently
DRuntime error: finalize() not found
Attempts:
2 left
πŸ’‘ Hint

Check the signature of the finalize() method in Java.

πŸ“ Syntax
advanced
2:00remaining
Which option correctly overrides the finalize method in Java?

Choose the correct method signature to override finalize() in Java.

Aprivate void finalize() throws Throwable
Bpublic void finalize()
Cvoid finalize() throws Exception
Dprotected void finalize() throws Throwable
Attempts:
2 left
πŸ’‘ Hint

The finalize() method must have a specific signature to override properly.

πŸš€ Application
expert
3:00remaining
How many objects are eligible for garbage collection after this code runs?

Given the following Java code, how many objects are eligible for garbage collection after the last line executes?

Java
public class Node {
    Node next;
    public static void main(String[] args) {
        Node a = new Node();
        Node b = new Node();
        Node c = new Node();
        a.next = b;
        b.next = c;
        c.next = a;
        a = null;
        b = null;
    }
}
A3
B0
C1
D2
Attempts:
2 left
πŸ’‘ Hint

Consider the references and the circular links between objects.