The ________ method of ExecutorService attempts to stop all actively executing tasks and halts the processing of waiting tasks.
- pause()
- shutdown()
- stop()
- terminate()
In Java, the shutdown() method of ExecutorService attempts to stop all actively executing tasks and halts the processing of waiting tasks. It's a graceful way to shut down an executor, allowing it to finish executing tasks before terminating. It is essential to manage thread pools effectively in concurrent applications.
Which of the following statements are true regarding the intern() method of the String class?
- Calling intern() on a String can reduce memory usage by ensuring only one copy exists in the string pool.
- The intern() method adds the String to the string pool.
- The intern() method is only available in Java 9 and later.
- The intern() method returns a new String object.
The intern() method of the String class is used to add the String to the string pool if it's not already there and returns a reference to that String. This can reduce memory usage by ensuring only one copy of a particular string exists in the string pool, which is useful for memory optimization. The intern() method has been available since early versions of Java, not just in Java 9 and later.
Consider a scenario where a very large number of string concatenation operations are being performed in a single-threaded application. Which class would be appropriate to use for string manipulation, and why?
- String
- StringBuffer
- StringBuilder
- StringJoiner
In a single-threaded application with frequent string concatenation, StringBuilder is the most suitable choice. It's efficient because it doesn't create new objects when you modify the string, which can save memory and reduce overhead. StringBuffer is also thread-safe but slightly slower due to synchronization. String creates a new string each time you modify it, and StringJoiner is used for joining strings, not efficient for concatenation.
The operator ______ is invalid in Java.
- $
- %
- +
- -
In Java, the dollar sign ($) is not a valid operator. It's used in variable names and identifiers but not as an operator. The other options (+, -, %) are valid arithmetic operators in Java.
In Java 8 and above, the ________ method can be used to perform a certain action for each element of a collection.
- applyActionToElement() Method
- forEach() Method
- iterate() Method
- processElement() Method
In Java 8 and above, the "forEach()" method is used to perform a specified action for each element of a collection. It provides a concise way to iterate through elements in a collection and apply a given action to each element. The other options do not represent the correct method for this purpose.
How does intrinsic locking in synchronized methods/blocks ensure thread safety?
- It allows all threads to execute synchronized code simultaneously.
- It doesn't affect thread safety.
- It prevents all threads from executing synchronized code simultaneously.
- It relies on hardware-specific instructions.
Intrinsic locking in synchronized methods/blocks ensures thread safety by preventing multiple threads from executing synchronized code simultaneously. When a thread enters a synchronized block, it acquires the lock associated with the synchronized object, preventing other threads from entering the same synchronized block until the lock is released. This ensures that only one thread can execute the synchronized code at a time, preventing data races and ensuring thread safety.
Imagine developing a JavaFX application where UI responsiveness is critical. How might you ensure that long-running tasks (like database operations) do not freeze the UI?
- Disable the UI during long-running tasks and re-enable it after the task completes.
- Increase the JavaFX UI thread priority to give more resources to UI updates during long-running tasks.
- Use Java's Thread.sleep() method to pause the UI updates temporarily while the task runs.
- Use JavaFX Task and Platform.runLater() to run long tasks on background threads and update the UI on the JavaFX application thread.
In JavaFX, long-running tasks like database operations should be executed on background threads to avoid freezing the UI. The recommended approach is to use the Task class and Platform.runLater() to safely update the UI from background threads. The other options are not suitable for ensuring UI responsiveness during long tasks.
What is the primary difference between StringBuilder and StringBuffer classes in Java?
- StringBuffer is not synchronized, making it faster but not thread-safe.
- StringBuilder has more methods for manipulating strings.
- StringBuilder is immutable, while StringBuffer is mutable.
- StringBuilder is synchronized, making it thread-safe but potentially slower.
The primary difference is that StringBuilder is not synchronized, making it faster but not thread-safe, while StringBuffer is synchronized, making it thread-safe but potentially slower. Immutable means unchangeable, which is not true for either class.
Which access modifier allows a member to be accessed from within its own class only?
- default (no modifier)
- private
- protected
- public
In Java, the private access modifier restricts access to the member to within the same class only. It is used to encapsulate the implementation details and hide them from external classes. The other options allow varying degrees of access to the member from outside the class.
Which interface or class should a class use or extend to create a new thread in Java?
- Executor
- Runnable
- Thread
- java.lang
In Java, to create a new thread, a class should implement the Runnable interface. The Runnable interface defines a single abstract method, run(), which should be overridden to provide the code that the new thread will execute. The other options are not used for directly creating a new thread.