Which mechanism is best suited for coordinating communication between threads where one thread produces data and another consumes it?
Using 'volatile' variables
Directly sharing objects between threads
Employing a 'BlockingQueue'
Using a 'Semaphore'
Which of these is NOT a valid retention policy for a custom annotation in Java?
CLASS
DEPLOYMENT
SOURCE
RUNTIME
What is a key advantage of using inner classes in Java?
Reduced memory footprint of the program
Increased code execution speed
Enhanced support for multithreading
Improved code organization and encapsulation
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(1)
O(n)
O(log n)
What happens when a generic class is instantiated with a primitive type in Java?
A runtime exception is thrown.
A compile-time error occurs.
The primitive type is autoboxed to its wrapper class.
It works as expected.
What does the File.createNewFile() method do if a file with the same name already exists in the specified location?
File.createNewFile()
It overwrites the existing file with an empty file.
It throws a FileNotFoundException.
FileNotFoundException
It returns false without creating a new file.
false
It appends a number to the file name to create a unique file.
The 'flatMap' operation on a stream is used for:
Transforming each element into a single new element
Flattening a stream of collections into a single stream
Filtering elements based on a predicate
Reducing the stream to a single summary value
Can a method be both generic and static in Java?
Yes, there are no restrictions.
Yes, but the type parameter should be declared after the 'static' keyword.
No, generic methods cannot be static.
Yes, but the type parameter should be declared before the method name.
What does the @Retention(RetentionPolicy.SOURCE) declaration signify for a custom annotation?
@Retention(RetentionPolicy.SOURCE)
The annotation is available at runtime via reflection.
The annotation is only applicable to source code and has no impact on compiled code.
The annotation is discarded by the compiler and not included in the class file.
The annotation is stored in the class file but is not available at runtime.
What is the time complexity of retrieving an element from a HashSet in Java, assuming the hash function distributes elements evenly?