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.

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.

What is the impact of using a SocketChannel in non-blocking mode over traditional blocking I/O socket communication?

  • a. SocketChannel offers better performance with lower CPU usage.
  • b. Non-blocking SocketChannel can handle only one connection at a time.
  • c. Blocking I/O sockets are more suitable for high-throughput applications.
  • d. Non-blocking SocketChannel improves data integrity.
Using SocketChannel in non-blocking mode (option a) can lead to improved performance with lower CPU usage compared to traditional blocking I/O sockets. Non-blocking SocketChannels can handle multiple connections concurrently. Option c is incorrect because non-blocking SocketChannels are often favored for high-throughput scenarios. Option d is not accurate as data integrity is not directly related to blocking or non-blocking mode.

A ________ is a result-bearing computation that can be canceled and can compute the result asynchronously provided by ExecutorService.

  • Callable
  • ExecutorService
  • Runnable
  • Thread
A Callable in Java is a result-bearing computation that can be canceled and can compute the result asynchronously. It is typically used with ExecutorService to perform tasks that return values or throw exceptions.

How can you securely serialize and deserialize objects to protect sensitive information during the process?

  • Define custom serialization and deserialization logic with encryption and authentication mechanisms
  • Employ access modifiers such as private and protected for fields and methods
  • Ensure that classes are marked as final to prevent inheritance
  • Use encryption techniques
To securely serialize and deserialize objects to protect sensitive information, you should use encryption techniques. Encrypting the data before serialization and decrypting it after deserialization ensures that the data remains confidential. While access modifiers and marking classes as final provide some level of security, they don't directly address the need for encryption. Custom logic with encryption and authentication is a comprehensive approach to security.

Consider a scenario where you want to invert the sign of a numeric value only if a particular boolean condition is true. How can unary operators be utilized to achieve this without using an if statement?

  • int invertedValue = (condition) ? -numericValue : numericValue;
  • int invertedValue = (condition) ? numericValue : -numericValue;
  • int invertedValue = -numericValue;
  • int invertedValue = numericValue;
You can use the conditional (ternary) operator ? : to achieve this. If the condition is true, it negates the numericValue by using the unary minus operator. If false, it leaves the numericValue unchanged. Option 1 demonstrates this technique.

What is the role of the ObjectOutputStream class in serialization?

  • It handles user input for serialization
  • It performs encryption on serialized data
  • It reads objects from a stream
  • It serializes objects into a byte stream
The ObjectOutputStream class in Java is used to serialize objects into a byte stream. It's responsible for writing the state of an object to the output stream. Conversely, ObjectInputStream is used for reading serialized objects from a stream. It is an essential part of Java's serialization mechanism.

What will be the outcome if you try to execute a DML (Data Manipulation Language) operation using executeQuery() method?

  • The DML operation will execute successfully, and the result set will be returned.
  • A SQLException will be thrown, as executeQuery() is meant for querying data, not for DML operations.
  • The DML operation will execute, but no result set will be returned.
  • An UnsupportedOperationException will be thrown, indicating that executeQuery() cannot be used for DML operations.
When you try to execute a DML operation (such as INSERT, UPDATE, DELETE) using the executeQuery() method, option b is correct. It will throw a SQLException because executeQuery() is meant for querying data and returns a ResultSet, which is not applicable to DML operations. Options a and c are incorrect because they suggest that the DML operation can proceed, which is not the case. Option d is also incorrect; it does not represent the actual behavior of executeQuery().

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.

Can an interface contain static methods in Java?

  • No
  • Only if it doesn't have any abstract methods.
  • Only if it doesn't have any default methods.
  • Yes
Yes, Java allows interfaces to contain static methods, introduced in Java 8. Static methods in interfaces can be called without creating an instance of the interface and are often used for utility functions or factory methods.

Deadlocks involving synchronized methods or blocks can potentially be resolved by ________.

  • using fewer threads
  • using more synchronization
  • using more threads
  • using thread priority
Deadlocks occur when two or more threads are blocked, each waiting for a resource that the other holds. To resolve deadlocks involving synchronized methods or blocks, one approach is to use fewer threads or to minimize the usage of synchronized sections to reduce the chances of threads waiting indefinitely, thereby avoiding ________.

Which of the following reference data types is used for storing a single character?

  • Character
  • Integer
  • String
  • char
The reference data type Character is used for storing a single character in Java. It is different from the primitive data type char, which also stores a single character but is not a reference data type. String is used to store sequences of characters, and Integer is for integer values.