Which class allows multiple threads to work in parallel but blocks them until all threads are finished?

  • CountDownLatch
  • CyclicBarrier
  • Semaphore
  • ThreadGroup
The CyclicBarrier class allows multiple threads to work in parallel but blocks them until all threads have reached a certain point (barrier) in the code. Once all threads have reached the barrier, they can continue executing. It is commonly used for tasks that can be divided into subtasks that need to be completed before the main task can proceed.

The ________ keyword can be used to print multi-dimensional arrays in a deep-to-string manner.

  • deepToString
  • display
  • print
  • toString
In Java, the Arrays.deepToString() method can be used to print multi-dimensional arrays in a deep-to-string manner. This method recursively converts the array into a string representation, including nested arrays, making it useful for debugging and displaying complex data structures.

Which loop structure should be used when the number of iterations is known in advance?

  • do-while loop
  • for loop
  • if statement
  • while loop
The for loop is ideal when you know the number of iterations in advance. It consists of an initialization, condition, and increment/decrement statement. This loop is suitable for iterating over arrays, collections, or any situation where the number of iterations is predetermined.

What is the primary purpose of a constructor in Java?

  • To create objects of the class.
  • To define the class methods.
  • To initialize the class variables.
  • To provide a way to destroy objects of the class.
In Java, a constructor's primary purpose is to initialize the class's instance variables when an object is created. Constructors don't define class methods or create/destroy objects; that's not their primary role.

A ________ block will always execute, whether an exception is thrown or not.

  • catch
  • finally
  • throw
  • try
The finally block is used to define a block of code that always executes, regardless of whether an exception is thrown or not. It's commonly used for cleanup operations such as closing files or releasing resources.

Consider a scenario where you are designing a graphics system that includes different types of shapes (e.g., Circle, Rectangle). How would you decide between using an abstract class and an interface for defining common methods?

  • Use a concrete class and use inheritance to define common methods, as concrete classes can directly provide method implementations.
  • Use an abstract class with a base shape class and common methods, as abstract classes can provide both method implementations and fields.
  • Use an interface to define common methods, as interfaces allow for multiple inheritance and can be implemented by different shape classes.
  • Use both an abstract class and an interface, combining the advantages of both.
In this scenario, using an abstract class is appropriate because you can define common methods with default implementations in the base shape class. This provides code reusability and allows shape classes to inherit these methods. Interfaces can also be used, but they don't provide method implementations, making abstract classes a more suitable choice for this use case.

Which searching algorithm requires the data to be sorted to work effectively?

  • Binary Search
  • Linear Search
  • Merge Sort
  • Quick Sort
Binary Search is an algorithm that requires the data to be sorted in ascending or descending order for effective searching. It uses the divide and conquer method and is not suitable for unsorted data.

To update UI components from a non-JavaFX thread, use ________.

  • Platform.exit()
  • Platform.repaint()
  • Platform.runLater()
  • Platform.update()
To update UI components from a non-JavaFX thread in JavaFX, you should use the Platform.runLater() method. This method allows you to enqueue a Runnable object to be executed on the JavaFX Application Thread, ensuring that UI updates are performed on the correct thread to avoid concurrency issues.

In which scenarios is it recommended to use synchronized blocks instead of synchronized methods?

  • When you don't want to use synchronization at all.
  • When you want to apply synchronization to a specific section of code within a method, providing more fine-grained control.
  • When you want to synchronize an entire method for simplicity.
  • When you want to synchronize multiple methods simultaneously.
Synchronized blocks are recommended when you want to apply synchronization to a specific section of code within a method, allowing more fine-grained control over synchronization. This can help reduce contention and improve performance in scenarios where synchronization is necessary. Synchronized methods are used when you want to synchronize the entire method for simplicity.

What is the output of the following code snippet: int[][] arr = new int[3][2]; System.out.println(arr.length);?

  • 2
  • 3
  • 6
  • The code will result in a compilation error.
In the given code, arr.length returns the number of rows in the 2D array. Here, arr is declared as a 2D array with 3 rows and 2 columns, so it prints 3, which is the number of rows.