When should you prefer using try-with-resources over explicitly closing resources in Java file I/O?
try-with-resources
Only when handling multiple resources simultaneously.
Always, as it ensures resources are closed even if exceptions occur.
Only when reading from files, not when writing to them.
Never, explicit closing provides better control and clarity.
Which of the following classes in the java.io package is used to read data from a file?
java.io
FileWriter
FileInputStream
File
FileOutputStream
Which keyword is used to declare a class that cannot be instantiated but serves as a blueprint for other classes?
interface
final
static
abstract
What distinct advantage does a 'Callable' interface offer over 'Runnable' in Java?
Callable can be used with traditional threads, while Runnable requires an ExecutorService
Callable can throw checked exceptions, while Runnable cannot
Callable is used for tasks that don't return a value, while Runnable is for tasks that do
Callable executes faster than Runnable
Which Java method would you use to find the index of the first occurrence of a specific substring within a string?
indexOf()
substring()
length()
charAt()
When a subclass provides a specific implementation for a method already defined in its superclass, what is this called?
Dynamic dispatch
Method overriding
Method hiding
Method overloading
What is the time complexity of searching for an element in a well-implemented HashMap in the average case?
HashMap
O(n log n)
O(log n)
O(1)
O(n)
What does the 'E' typically represent in a generic type parameter like 'List<E>'?
Element
Enum
Exception
Entry
What type of inner class is defined without a name and is typically used for creating one-off event handlers?
Local class
Anonymous class
Static inner class
Nested class
Which method converts a stream of strings to a single comma-separated string?
collect(Collectors.joining(", "))
reduce(", ", String::concat)
Both A and B
map(String::concat).collect(Collectors.toList())