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.
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 ________.
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.
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.
Consider a case where you have to model a "Book" in a library system. What properties and methods would you define in the Book class and why?
- Properties: name, genre, pages; Methods: play, pause, stop.
- Properties: title, author, ISBN, publication date; Methods: get and set methods for properties, toString for representation.
- Properties: title, author, ISBN; Methods: checkAvailability, reserve, extendDueDate.
- Properties: title, author, publication date; Methods: borrow, return, calculateFine.
In a Book class for a library system, you would define properties like title, author, ISBN, and publication date as they represent essential book attributes. Get and set methods are used for encapsulation and data integrity, and the toString method helps in displaying the book's details. The other options include irrelevant properties and methods.
Which of the following is a key characteristic of a lambda expression in Java?
- It can capture local variables.
- It can have multiple methods.
- It is a data type.
- It is an anonymous inner class.
A key characteristic of a lambda expression in Java is that it can capture local variables from its enclosing scope. This allows lambda expressions to use values from the surrounding context, making them useful for creating concise and expressive code.
Which method is used to display a stage in JavaFX?
- displayStage()
- openStage()
- primaryStage()
- showStage()
In JavaFX, the show() method is used to display a stage. When you create a JavaFX application, you typically create a Stage object and use its show() method to make it visible. The other options are not valid methods for displaying a stage.
How can CSS be applied to style JavaFX components?
- By attaching external CSS files to the JavaFX application using the addStylesheet() method.
- By calling the applyCSS() method on the entire scene graph.
- By using JavaScript code embedded in the JavaFX application.
- By using the setStyle() method to apply inline CSS directly to individual components.
CSS can be applied to style JavaFX components by attaching external CSS files to the application using the addStylesheet() method. This allows for a separation of concerns between the application logic and its styling. It provides flexibility and maintainability in designing the user interface. Inline CSS can also be used for individual components, but it's not the standard way to style JavaFX applications.
The keyword ________ is used to apply restrictions on class, method, and variable.
- final
- private
- protected
- static
The keyword "private" is used to apply restrictions on class members. It restricts access to only within the same class, making it the most restrictive access modifier in Java.
Which of the following methods can be used to create a stream from a collection?
- filter()
- forEach()
- mapToDouble()
- stream()
You can create a stream from a collection in Java using the stream() method. This method is available on all collections and provides a way to obtain a stream that allows you to perform various stream operations on the elements of the collection. Options 2 to 4 are not used to create streams but for other stream operations.