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.

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 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.

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.

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.

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.