Java Advanced Quiz

Test your advanced knowledge of Java programming

1. Which memory area replaced PermGen in Java 8?
Heap
Stack
Metaspace
Eden Space
Correct Answer: Metaspace
Explanation: Metaspace stores class metadata and replaced PermGen in Java 8.
2. Which collection is thread-safe and optimized for read-heavy operations?
ArrayList
Vector
CopyOnWriteArrayList
LinkedList
Correct Answer: CopyOnWriteArrayList
Explanation: It allows safe concurrent access without explicit synchronization.
3. What happens if wait() is called without owning the monitor?
Thread waits
Deadlock occurs
IllegalMonitorStateException
Thread stops
Correct Answer: IllegalMonitorStateException
Explanation: A thread must own the object's monitor to call wait().
4. Which keyword guarantees variable visibility across threads?
synchronized
static
volatile
final
Correct Answer: volatile
Explanation: volatile ensures changes are visible across threads.
5. Which Stream method transforms elements?
filter()
map()
reduce()
collect()
Correct Answer: map()
Explanation: map() converts each element into another form.
6. Which GC is optimized for low-latency applications?
Serial GC
Parallel GC
G1 GC
Epsilon GC
Correct Answer: G1 GC
Explanation: G1 GC balances throughput with low pause times.
7. Which design pattern ensures only one instance exists?
Factory
Builder
Singleton
Prototype
Correct Answer: Singleton
Explanation: Singleton restricts instantiation to one object.
8. Which interface is a marker interface?
Runnable
Comparable
Serializable
Callable
Correct Answer: Serializable
Explanation: Marker interfaces contain no methods.
9. What does Optional help prevent?
ClassCastException
NullPointerException
IOException
StackOverflowError
Correct Answer: NullPointerException
Explanation: Optional provides a safe container for nullable values.
10. What is the role of ClassLoader?
Compile Java code
Execute bytecode
Load classes at runtime
Manage garbage collection
Correct Answer: Load classes at runtime
Explanation: ClassLoader dynamically loads class definitions into the JVM.
11. Which ExecutorService method stops accepting new tasks?
close()
shutdown()
terminate()
destroy()
Correct Answer: shutdown()
Explanation: shutdown() prevents new tasks but allows running tasks to finish.
12. Which keyword prevents a method from being overridden?
static
abstract
final
private
Correct Answer: final
Explanation: final methods cannot be overridden by subclasses.
13. Which collection does NOT allow duplicate elements?
ArrayList
LinkedList
HashSet
Vector
Correct Answer: HashSet
Explanation: Set implementations do not allow duplicate values.
14. Which exception is thrown when a thread is interrupted?
InterruptedException
IllegalThreadStateException
RuntimeException
ThreadDeath
Correct Answer: InterruptedException
Explanation: Thrown when a thread is waiting or sleeping and gets interrupted.
15. What is the main benefit of immutable objects?
Faster execution
Thread safety
Less memory usage
Easier syntax
Correct Answer: Thread safety
Explanation: Immutable objects cannot be changed after creation.
16. Which interface allows custom object sorting?
Comparable
Serializable
Cloneable
AutoCloseable
Correct Answer: Comparable
Explanation: Comparable defines natural ordering of objects.
17. Which keyword allows a method to be accessed without creating an object?
final
abstract
static
synchronized
Correct Answer: static
Explanation: Static methods belong to the class, not instances.
18. Which Stream method converts elements into a single result?
map()
filter()
reduce()
peek()
Correct Answer: reduce()
Explanation: reduce() aggregates stream elements into a single value.
19. Which Java feature supports multiple inheritance?
Abstract classes
Interfaces
Inner classes
Packages
Correct Answer: Interfaces
Explanation: A class can implement multiple interfaces.
20. What does the JVM use the stack for?
Object storage
Method execution
Class loading
Garbage collection
Correct Answer: Method execution
Explanation: The stack stores method calls and local variables per thread.