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 ______ block can be used to define customized serialization logic.
- finalization
- synchronized
- try-catch
- writeObject
In Java, the writeObject block is used to define customized serialization logic for an object. It allows you to control how an object is serialized when it's written to an output stream. The other options are not used for custom serialization logic.
To avoid an infinite loop, the condition within the ________ loop must eventually be false.
- infinite
- inner
- nested
- outer
To prevent an infinite loop in Java, the condition within the outer loop must eventually evaluate to false. An infinite loop occurs when the loop condition is always true, and the loop keeps executing indefinitely. The other options are not directly related to the prevention of infinite loops.
The ________ block can be used to handle different types of exceptions in a single block.
- catch
- finally
- throw
- try
The try block is used to enclose a section of code that may throw exceptions. You can have multiple catch blocks after a single try block to handle different types of exceptions, making it possible to handle various exceptions in a unified way.
Which of the following methods returns the number of rows affected by the DML operation?
- getRowCount()
- getUpdateCount()
- getResultCount()
- getAffectedRowCount()
The correct method to retrieve the number of rows affected by a DML (Data Manipulation Language) operation in JDBC is getUpdateCount(). This method returns an integer representing the number of rows affected by the last executed query or update statement. Options a, c, and d are not standard JDBC methods for retrieving the row count affected by DML operations.
The ______ interface is implemented by classes that want to handle events of a specific type, with event type and event source defined as generic parameters.
- ActionListener
- EventHandler
- EventListener
- EventSource
In Java, the EventHandler interface is used for handling events of a specific type. It allows you to define the event type and event source as generic parameters, making it a versatile choice for handling various types of events in a type-safe manner. Classes that implement this interface can respond to events of the specified type.
Which method converts a given string to a sequence of characters?
- charAt()
- split()
- toCharArray()
- toString()
The toCharArray() method in Java's String class converts a given string into an array of characters, providing a sequence of characters. The other methods do not perform this specific task.
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.
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.
You are developing a real-time gaming application where certain operations need to be repeated at regular time intervals. Which looping mechanism and timing control statements would you use to achieve this without blocking the user interface?
- do-while loop and Thread.yield()
- for loop and System.nanoTime()
- forEach loop and System.currentTimeMillis()
- while loop and Thread.sleep()
In a real-time gaming application, you would typically use a while loop in combination with the Thread.sleep() method to introduce a delay between iterations without blocking the UI. The other options may not be suitable for achieving this specific timing control.