How does thread priority impact the order in which threads are executed?

  • Thread priority has no impact on the order of thread execution.
  • Thread priority is randomly assigned, so there is no fixed order.
  • Threads with higher priority are always executed before threads with lower priority.
  • Threads with lower priority are always executed before threads with higher priority.
Thread priority in Java can be set using the setPriority() method and can range from 1 to 10, where 1 is the lowest and 10 is the highest priority. Threads with higher priority are given preference, but it's not guaranteed that they will always execute first, as it depends on the thread scheduler and other factors.

Why might a programmer choose to use package-private (default) access level over private for a method within a class?

  • Package-private methods allow access to subclasses, promoting code reusability within the same package.
  • Package-private methods are accessible from outside the class, making them more versatile.
  • Package-private methods are more restrictive than private methods and provide better encapsulation.
  • Private methods cannot be used within a class.
Programmers might choose package-private access level for a method when they want to allow subclasses to access the method for code reuse but restrict access from outside the package. It strikes a balance between encapsulation and reusability, making it a useful choice in certain situations.

When using a single-thread executor, what happens if a submitted task throws an exception?

  • The exception is caught, logged, and the executor continues to execute other tasks.
  • The exception is ignored, and the executor continues to execute other tasks.
  • The exception is propagated up the call stack to the caller of the submit() method.
  • The thread running the task is terminated, and the executor replaces it with a new thread.
In a single-thread executor, when a submitted task throws an exception, it is caught and logged, but the executor continues to execute other tasks. The executor does not terminate due to an exception in a single task; it maintains the single thread for execution.

In Java, if a class implements an interface and does not provide implementations for all its methods, it must be declared as ________.

  • Abstract Class
  • Concrete Class
  • Final Class
  • Static Class
When a class in Java implements an interface but doesn't provide implementations for all the interface methods, it must be declared as an abstract class. This is because an abstract class can have unimplemented methods, while concrete classes need to provide implementations for all interface methods they inherit.

The keyword ________ is used to skip the rest of the current loop iteration.

  • break
  • continue
  • exit
  • return
In Java, the continue keyword is used to skip the rest of the current loop iteration and move to the next iteration. It is often used in loops like for and while when certain conditions are met, and you want to skip the current iteration and continue with the next one. The other options do not serve this purpose.

The relational operators are often used inside the ______ statement to produce boolean value.

  • for
  • if
  • switch
  • while
Relational operators (e.g., <, >, ==) are commonly used inside the "if" statement to create conditions that produce boolean (true/false) values based on comparisons.

What will happen if the DriverManager is unable to connect to the database using the provided URL?

  • A compilation error will occur
  • A runtime exception will be thrown
  • An SQLException will be thrown
  • It will silently retry to connect in the background
When the DriverManager in JDBC fails to connect to the database using the provided URL, it throws an SQLException. This exception should be handled in your code to gracefully manage the failure and take appropriate actions, such as logging the error or providing a user-friendly message.

Which of the following character stream classes should be used to read characters, arrays, and lines efficiently?

  • BufferedReader
  • CharArrayReader
  • FileReader
  • InputStreamReader
The BufferedReader class is used to read characters, arrays, and lines efficiently from a character input stream. It provides buffering, which makes reading more efficient. FileReader is used to read characters from files, and InputStreamReader is used for byte-to-character conversion. CharArrayReader is used to read from character arrays.

What is the result of Double.POSITIVE_INFINITY == Double.POSITIVE_INFINITY in Java?

  • It depends on the context of the comparison.
  • It returns false.
  • It returns true.
  • It throws an ArithmeticException.
In Java, comparing Double.POSITIVE_INFINITY to itself using the == operator returns true. This behavior is consistent with the IEEE 754 standard, which defines the behavior of floating-point numbers. Double.POSITIVE_INFINITY is considered equal to itself. Options 2 and 3 are incorrect because they don't accurately represent the result of this comparison. Option 4 is incorrect because the comparison of infinity values is well-defined in Java.

How does the linear search algorithm find the target value in its input?

  • It divides the array into two halves and checks each half separately
  • It jumps to a random location and checks if the element is present there
  • It starts from the first element and compares each element one by one
  • It uses a mathematical formula to calculate the position of the target element
The linear search algorithm finds the target value by starting from the first element of the array and comparing each element one by one until it either finds a match or reaches the end of the array. It is a straightforward and sequential search method, which means it has a worst-case time complexity of O(n), where 'n' is the number of elements in the array.