What is the impact of using PrintWriter in file handling without automatic line flushing?
- It buffers data and may not immediately write it to the file.
- It ensures efficient memory usage and high-speed writing to the file.
- It has no impact on file handling.
- It throws an error if used without automatic line flushing.
PrintWriter in Java buffers data by default, which means it doesn't immediately write to the file. Without automatic line flushing, you must manually flush the buffer (using flush()), or it may not write data until the buffer is full or the program exits. This buffering can improve performance but may lead to unexpected behavior if you forget to flush.
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.
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.
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.
You are developing a payroll system. How would you design a class for storing employee details and ensuring that some sensitive information (like salary) cannot be accessed directly from the object?
- Define private instance variables for employee details, including salary, and provide public get and set methods for accessing and modifying the salary.
- Define public instance variables for employee details and make the salary variable final.
- Define public static variables for employee details, including salary, and use the 'protected' access modifier.
- Use the 'volatile' keyword for sensitive information like salary.
To ensure that sensitive information like salary cannot be accessed directly from the object, you should define private instance variables for employee details and provide public get and set methods for salary. This follows the principle of encapsulation, which restricts direct access to sensitive data. The other options do not provide adequate security for sensitive information.
What is the difference between a synchronized block and a synchronized method?
- Synchronized blocks are used for asynchronous programming.
- Synchronized blocks can be used to synchronize multiple sections of code within the same method.
- Synchronized methods are more efficient than synchronized blocks.
- Synchronized methods can only be used to synchronize the entire method.
The key difference between a synchronized block and a synchronized method is that synchronized blocks can be used to synchronize multiple sections of code within the same method, providing more fine-grained control over synchronization. In contrast, synchronized methods synchronize the entire method. Synchronized methods are not necessarily more efficient than synchronized blocks; their efficiency depends on the specific use case.
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.
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.
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.
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.
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.
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.