← All 1Z0-819 Flashcard Decks

Exception Handling & Concurrency Flashcards

9 cards from real 1Z0-819 practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.

Read the first 9 Exception Handling & Concurrency flashcards as text
  1. What is the purpose of exception handling in Java?

    Answer: To handle errors gracefully during runtime

    Exception handling in Java provides a structured way to deal with runtime errors, known as exceptions, that disrupt the normal flow of program execution. Its primary purpose is to prevent programs from crashing abruptly and instead allow them to recover gracefully or terminate in a controlled manner, improving robustness. It does not aim to stop execution immediately, increase complexity, or reduce performance, but rather to manage unexpected situations effectively.

  2. Which block must always follow a try block?

    Answer: finally

    In Java's exception handling mechanism, a `try` block must always be followed by either a `catch` block, a `finally` block, or both. The `finally` block is crucial because the code within it is guaranteed to execute, regardless of whether an exception was thrown or caught in the `try` block. This makes it ideal for cleanup operations like closing resources.

  3. What is a checked exception in Java?

    Answer: Exceptions that must be declared or handled

    A checked exception in Java is an exception that the compiler enforces you to handle. This means that methods throwing checked exceptions must either declare them using the `throws` keyword in their signature or enclose the problematic code within a `try-catch` block. This design ensures that developers explicitly consider and address potential error conditions, making programs more robust.

  4. Which class is the superclass of all exceptions?

    Answer: Throwable

    In Java's exception hierarchy, `java.lang.Throwable` is the root class for all exceptions and errors. Both `java.lang.Exception` and `java.lang.Error` are direct subclasses of `Throwable`. Therefore, any object that can be thrown by the `throw` statement must be an instance of `Throwable` or one of its subclasses.

  5. How is a thread created in Java?

    Answer: By extending Thread or implementing Runnable

    In Java, there are two primary ways to create a thread: by extending the `java.lang.Thread` class or by implementing the `java.lang.Runnable` interface. Both approaches require defining the code to be executed by the thread within a `run()` method. Extending `Thread` is simpler for single-use cases, while implementing `Runnable` is preferred for better code organization and when the class already extends another class.

  6. What does the 'synchronized' keyword ensure?

    Answer: Mutual exclusion of threads in critical sections

    The `synchronized` keyword in Java is used to achieve mutual exclusion, ensuring that only one thread can execute a specific block of code or method at a time. This is critical for protecting shared resources from concurrent access by multiple threads, thereby preventing data corruption and race conditions. It helps maintain data consistency in multi-threaded environments.

  7. Which method is used to start a thread?

    Answer: start()

    To initiate the execution of a newly created thread in Java, you must call its `start()` method. This method performs essential setup, including allocating system resources for the new thread, and then invokes the `run()` method in that separate thread of execution. Directly calling `run()` would execute the code in the current thread, not a new one.

  8. What happens if an exception is not caught?

    Answer: The JVM handles it and may terminate the program

    If an exception is thrown in a Java program and is not caught by any `catch` block in the call stack, it propagates up to the Java Virtual Machine (JVM). The JVM's default exception handler will then print the exception's stack trace to the console and subsequently terminate the program. This uncontrolled termination can lead to data loss or an unstable application state.

  9. What is the purpose of the ExecutorService in Java?

    Answer: To manage and control multiple threads

    The `ExecutorService` in Java's `java.util.concurrent` package provides a high-level API for managing and controlling the execution of tasks in a pool of threads. It abstracts away the complexities of thread creation, lifecycle management, and task scheduling, making it easier to implement concurrent applications. This allows developers to focus on the tasks themselves rather than the underlying thread management.