The ______ interface in Java represents the result of an asynchronous computation.
- Callable
- Executor
- Future
- Runnable
In Java, the Future interface represents the result of an asynchronous computation. It allows you to retrieve the result or handle exceptions once the computation is complete. A Callable is used to perform a task and return a result, and a Runnable is used to represent a task that can be executed asynchronously, but neither of them directly represents the result of the computation. The Executor interface is used to execute tasks, not represent results.
The @FunctionalInterface annotation is used to indicate that an interface is to be used with ________.
- Annotations
- Generics
- Lambdas
- Threads
The @FunctionalInterface annotation is used to indicate that an interface is intended to be used with lambda expressions. It serves as a marker for functional interfaces, making it clear that they are designed to be used in lambda expressions.
Which method can be used to temporarily pause the execution of a thread for a specified time?
- pause()
- sleep()
- stop()
- yield()
The sleep() method in Java is used to temporarily pause the execution of a thread for a specified amount of time. It's a way to introduce delays in a program and is often used for synchronization or timing purposes. The other options are not used for pausing threads in this manner.
What is the purpose of the finally block in Java exception handling?
- To always execute code
- To catch exceptions
- To handle checked exceptions
- To throw exceptions
The finally block in Java exception handling is used to ensure that a certain block of code is executed regardless of whether an exception is thrown or not. It is typically used to perform cleanup actions, such as closing resources.
Which keyword is used in Java to test a condition?
- check
- if
- test
- verify
In Java, the if keyword is used to test a condition. It allows you to execute a block of code only if the condition specified inside the parentheses evaluates to true. The if statement is fundamental for implementing conditional logic in Java programs.
In memory, the rows of a two-dimensional array in Java can be __________.
- none of the above
- stored as linked lists
- stored in random order
- stored sequentially
In Java, the rows of a two-dimensional array are stored sequentially in memory. This means that the elements of each row are stored one after the other in memory, making it more efficient for accessing elements in a row. This sequential storage pattern is different from languages like C, where a 2D array is essentially an array of pointers to individual rows.
What symbol is used in the syntax of a Lambda expression in Java?
- ->
- ::
- =>
- {}
In Java, the syntax of a lambda expression uses the -> symbol. It separates the lambda parameters from the lambda body and is a distinctive feature of lambda expressions, making them easy to identify in code.
What is the maximum number of interfaces a Java class can implement?
- 1
- 2 or more
- 3 or more
- Unlimited
A Java class can implement an unlimited number of interfaces. This allows for multiple inheritance of method signatures, where the class must provide implementations for all the methods declared in the interfaces it implements.
Deadlock occurs when two or more threads are blocked forever, each waiting for the other to ________.
- acquire
- complete
- join
- release
Deadlock in multithreading occurs when two or more threads are waiting for resources acquired by each other. They cannot proceed as they are both holding resources the other needs, resulting in a deadlock situation.
The method ________ is used to remove all the mappings from a Map.
- clear()
- eraseAll()
- removeAll()
- removeMappings()
In Java, the clear() method is used to remove all the mappings from a Map. It does not remove the map itself but makes it empty, removing all key-value pairs. The other options do not perform this specific function.
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.
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.