How would you handle a situation where a task submitted to ExecutorService is stuck or running for too long?

  • There is no way to handle a stuck task in Java; it must be manually terminated by killing the JVM process.
  • You can cancel the task using the cancel method of the Future object returned when submitting the task.
  • You can increase the task timeout setting to give it more time to complete.
  • You can use the ExecutorService.shutdownNow() method to forcefully terminate all running tasks and shut down the service.
When a task is stuck or running for too long, you can handle it by canceling the task using the cancel method of the Future object returned when submitting the task. This allows graceful termination of the task without affecting the entire application. Other options, like forcefully shutting down the ExecutorService or modifying the task's timeout settings, may have unintended consequences.

________ is an interface in JDBC, which can be used to move a cursor in the result set in both directions.

  • PreparedStatement
  • ResultSet
  • ResultSetMetaData
  • Statement
The ResultSet interface in JDBC is used to retrieve data from a database query result. It allows you to move a cursor in the result set in both directions, forward and backward, and retrieve data from the current row. This is especially useful when you need to navigate and process the result set efficiently.

Which index of a multi-dimensional array represents the row index in Java?

  • First index
  • It depends on the array's dimensions.
  • Second index
  • Third index
In Java, for a multi-dimensional array, the first index represents the row index. For a 2D array, it represents the row number, and for a 3D array, it represents the depth along the first dimension. The other indices represent the columns (2nd index), and if applicable, additional dimensions.

Which class allows you to read lines of text from a file?

  • BufferedReader
  • FileReader
  • InputStreamReader
  • Scanner
The BufferedReader class is commonly used for reading lines of text from a file in Java. It provides methods like readLine() that allow you to read a complete line of text at a time. While other classes like FileReader and Scanner can also be used for reading files, BufferedReader is preferred for its efficiency and ease of use when reading lines of text.

When would you prefer byte streams over character streams while performing I/O operations in Java?

  • When dealing with binary data and non-text files, such as images or audio.
  • When working with XML or HTML files that contain text and tags.
  • When you need to perform text-based operations, like reading and writing characters.
  • When you want to handle file I/O in a platform-independent manner.
Byte streams are suitable for reading and writing binary data, while character streams are used for handling text data. Byte streams are ideal for handling non-text files, like images or audio, where individual bytes matter. Character streams are used for text files and automatically handle character encoding.

A ________ is used to create a client socket in Java.

  • DatagramSocket
  • InetAddress
  • ServerSocket
  • Socket
In Java, to create a client socket, you use the Socket class. It allows you to establish a connection to a remote server and communicate with it. The other options represent different types of socket classes in Java but are not used for creating client sockets.

The ________ method of DatagramSocket class is used to send a packet to a server.

  • send()
  • sendPacket()
  • sendToServer()
  • transmitPacket()
In Java, the send() method of the DatagramSocket class is used to send a packet to a server. It is a crucial method for working with UDP (User Datagram Protocol) sockets.

What will be the output of the following code snippet: public int add(int a, long b) { return a + b; } and public long add(int a, int b) { return a + b; }?

  • Compilation Error: Ambiguous method call.
  • Compilation Error: Duplicate method add with the same parameter types.
  • Compilation Error: Mismatched return types.
  • The code will run without errors, and the output will be the sum of a and b.
The code will result in a compilation error because both methods have the same name and the same parameter types (int and long). Java does not allow you to overload methods based solely on the return type. Overloaded methods must have different parameter lists. Overloading based on return types would lead to ambiguity.

How does the FileWriter class handle character encoding?

  • It always uses the system's default character encoding.
  • It automatically detects and sets the appropriate character encoding.
  • It doesn't support character encoding; it writes bytes as is.
  • It prompts the user to choose the encoding when creating an instance.
The FileWriter class in Java uses the system's default character encoding for writing text to a file. It doesn't automatically detect or set character encoding. To specify a different encoding, you should use constructors like FileWriter(String fileName, Charset charset) to explicitly set the encoding.

What will be the initial capacity of a HashSet when no size is defined during its creation?

  • 0
  • 10
  • 16
  • It will result in an error
When you create a HashSet in Java without specifying its initial capacity, it defaults to an initial capacity of 16. The capacity dynamically increases as needed when elements are added to the HashSet.