Multiple choice technology programming languages

What is the purpose of the Runtime class?

  1. To provide access to the Java runtime system.

  2. To avoid run time exception.

  3. To provide the capability to execute code no matter whether or not an exception is thrown or caught.

  4. Both 2 & 3

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The Runtime class in Java provides access to the Java runtime environment. It allows applications to interface with the environment in which they are running. Common uses include executing system commands (exec), forcing garbage collection (gc), getting free memory (freeMemory), and terminating the JVM (exit). It does not handle exceptions or provide finally-like behavior.

AI explanation

The Runtime class (java.lang.Runtime) provides access to the Java runtime system — it lets a program interact with the environment it's running in: querying/managing memory (freeMemory, totalMemory, gc), executing external processes (exec), and registering shutdown hooks. It is not designed to avoid runtime exceptions (that's exception handling via try/catch), and while shutdown hooks registered through Runtime can run cleanup code regardless of how the JVM exits, that's a specific feature of Runtime rather than its core defining purpose as stated in option 3 (which frames it more broadly/inaccurately as guaranteed execution akin to finally). 'Both 2 & 3' is therefore also wrong since option 1 is the accurate, direct answer.