Exercises

Memory Management Across Python, Ruby, Java, and C

Explore how Python, Ruby, Java, and C manage memory. This quiz examines stack and heap storage, manual allocation, garbage collection, object reachability, reference counting, weak references, memory leaks, and common hazards such as dangling pointers. Questions compare managed-language behavior with C's explicit memory management and include practical memory diagrams.

Answer the questions below and check the explanation for each answer.

0/13 answered

  1. 1

    In the diagram, a local C pointer p is stored in a stack frame and points to a dynamically allocated heap block. What does free(p) release?

    Question 1
  2. 2

    When does an ordinary Java object become eligible for garbage collection?

  3. 3

    In CPython, which event most directly allows an ordinary acyclic object to be deallocated promptly?

  4. 4

    The illustration represents a block just returned by C's malloc. What may the program safely assume about its initial contents?

    Question 4
  5. 5

    Which statement best describes garbage collection in Ruby?

  6. 6

    In the Java reachability diagram, B is referenced only by C, C is referenced only by B, and neither has a path from a GC root. What is their status?

    Question 6
  7. 7

    What is a use-after-free error in C?

  8. 8

    Which Java feature is intended for deterministic cleanup of files, sockets, and similar AutoCloseable resources?

  9. 9

    The diagram shows two Python lists that reference each other, with no external references remaining. How can CPython normally reclaim them?

    Question 9
  10. 10

    How does calloc differ from malloc in standard C?

  11. 11

    What is the main memory-management property of a Python weak reference?

  12. 12

    A C program requests a nonzero size with realloc and the call returns NULL. According to the diagrammed safe pattern, what is true of the original allocation?

    Question 12
  13. 13

    How can a memory leak occur in Java, Python, or Ruby even though these languages use garbage collection?

Video Courses

Courses related to this topic

See all video courses

Download the App now to have access to + 5000 free courses, exercises, certificates and lots of content without paying anything!

  • 100% free online courses from start to finish

    Thousands of online courses in video, ebooks and audiobooks.

  • More than 60 thousand free exercises

    To test your knowledge during online courses

  • Valid free Digital Certificate with QR Code

    Generated directly from your cell phone's photo gallery and sent to your email

Cursa app on the ebook screen, the video course screen and the course exercises screen, plus the course completion certificate