0
0
Javaprogramming~15 mins

Garbage collection overview in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
Garbage Collection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 conceptual
intermediate
2:00remaining
What is the primary purpose of garbage collection in Java?
Choose the best description of what garbage collection does in Java.
APrevents the creation of new objects to save memory.
BManually deletes objects when the programmer calls a delete method.
CAutomatically frees memory by removing objects that are no longer reachable.
DOptimizes CPU usage by pausing all running threads.
Attempts:
2 left
query result
intermediate
2:00remaining
What output does this code produce regarding object finalization?
Consider the following Java code snippet. What will be printed when the program runs?
Java
class Test {
  protected void finalize() {
    System.out.println("Finalized");
  }
  public static void main(String[] args) {
    Test t = new Test();
    t = null;
    System.gc();
    System.out.println("End of main");
  }
}
AEnd of main
B
Finalized
End of main
C
End of main
Finalized
DNo output
Attempts:
2 left
📝 syntax
advanced
2:00remaining
Identify the syntax error in this garbage collection related code snippet
Which option contains a syntax error preventing compilation?
Java
public class GCExample {
  public static void main(String[] args) {
    Object obj = new Object();
    obj = null;
    System.gc();
  }
}
Afinalize() { System.out.println("Cleaning up"); }
Bprotected void finalize() { System.out.println("Cleaning up"); }
Cvoid finalize() { System.out.println("Cleaning up"); }
Dpublic void finalize() { System.out.println("Cleaning up"); }
Attempts:
2 left
optimization
advanced
2:00remaining
Which approach helps reduce garbage collection overhead in Java?
Select the best practice to minimize the impact of garbage collection on application performance.
ACreate many short-lived objects frequently to keep memory usage high.
BAvoid using local variables to reduce memory allocation.
CCall System.gc() explicitly after every object creation.
DReuse objects when possible instead of creating new ones repeatedly.
Attempts:
2 left
🔧 debug
expert
3:00remaining
Why does this Java program cause a memory leak despite garbage collection?
Examine the code below and select the reason it causes a memory leak.
Java
import java.util.ArrayList;
public class LeakExample {
  static ArrayList<Object> list = new ArrayList<>();
  public static void main(String[] args) {
    while(true) {
      Object obj = new Object();
      list.add(obj);
    }
  }
}
AThe infinite loop causes the program to crash before garbage collection can run.
BObjects are never dereferenced because they are stored in a static list, preventing garbage collection.
CSystem.gc() is not called, so garbage collection never happens.
DThe Object class does not support garbage collection.
Attempts:
2 left