Which interface is used to create a type-safe heterogeneous container in Java?
Iterable
Iterator
Collection
Comparable
Which method converts a stream of strings to a single comma-separated string?
collect(Collectors.joining(", "))
reduce(", ", String::concat)
map(String::concat).collect(Collectors.toList())
Both A and B
What happens when a Stream encounters an exception during execution?
The exception is caught and wrapped in a RuntimeException
The exception is ignored, and the stream continues processing
The stream processing pauses, and the user is prompted to handle the exception
The stream processing stops, and the exception is thrown
Which of these is NOT a valid retention policy for a custom annotation in Java?
SOURCE
DEPLOYMENT
CLASS
RUNTIME
What is the difference between StringBuilder and StringBuffer in Java?
StringBuilder
StringBuffer
StringBuilder is thread-safe, StringBuffer is not thread-safe.
StringBuilder is mutable, StringBuffer is immutable.
StringBuilder is not thread-safe, StringBuffer is thread-safe.
There is no difference, they are aliases for the same class.
What is the primary purpose of an interface in Java?
To enforce specific method implementations
To define a template for object creation
To create multiple inheritance of classes
To define a contract for classes to adhere to
Which of the following is true about the 'peek' operation?
It's an intermediate operation primarily used for debugging
It's a terminal operation that triggers stream processing
It's an intermediate operation that modifies the stream elements
It's a terminal operation that returns a new Stream
What does the @Retention(RetentionPolicy.SOURCE) declaration signify for a custom annotation?
@Retention(RetentionPolicy.SOURCE)
The annotation is stored in the class file but is not available at runtime.
The annotation is available at runtime via reflection.
The annotation is discarded by the compiler and not included in the class file.
The annotation is only applicable to source code and has no impact on compiled code.
Which annotation is used to suppress compiler warnings in Java?
@IgnoreWarnings
@NoWarnings
@DisableWarnings
@SuppressWarnings
When should you prefer using try-with-resources over explicitly closing resources in Java file I/O?
try-with-resources
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.
Only when handling multiple resources simultaneously.