Advanced Java Quiz: Challenge Your OOP and Concurrency Knowledge
Ready to push your Java skills beyond the basics? This advanced Java quiz focuses on object-oriented design principles, concurrency, and related advanced topics you’ll encounter in real-world projects and interviews. Below is a brief guide to the quiz structure, sample questions with explanations, and study tips to help you master these areas.
Quiz structure
- Total questions: 15
- Format: mix of multiple choice and short-answer
- Topics covered: advanced OOP (inheritance, polymorphism, SOLID principles, design patterns), concurrency (threads, synchronization, java.util.concurrent), memory model, and performance considerations.
- Difficulty: medium–hard
Sample questions (with concise explanations)
- Question: What will be printed by this code?
class A { void m() { System.out.println(“A”); } }class B extends A { void m() { System.out.println(“B”); } }A a = new B(); a.m();
Answer: B
Explanation: Method dispatch is dynamic; the runtime type B determines which overridden method runs.
-
Question: Which SOLID principle is primarily concerned with classes having only one reason to change? Answer: Single Responsibility Principle (SRP)
Explanation: SRP states a class should have one responsibility to reduce coupling and improve maintainability. -
Question: What’s the difference between synchronized and ReentrantLock? Answer: ReentrantLock offers advanced features (tryLock, lockInterruptibly, fairness policies) and can be more flexible; synchronized is simpler and less error-prone for basic mutual exclusion.
Explanation: Use ReentrantLock when you need timed waits, interruptible locks, or explicit fairness. -
Question: How does volatile differ from synchronized? Answer: volatile guarantees visibility of writes to other threads but does not provide atomicity for compound actions; synchronized provides both mutual exclusion and visibility.
Explanation: Use volatile for simple flags; use synchronized or locks for compound operations. -
Question: What happens if you call wait() without owning the object’s monitor? Answer: Throws IllegalMonitorStateException.
Explanation: wait() must be called from within a synchronized block that locks the same object. -
Question: Describe a race condition and give a short Java example. Answer: A race condition occurs when two or more threads access shared data and try to change it concurrently without proper synchronization. Example: incrementing a shared int without synchronization leads to lost updates.
Explanation: Fix with synchronization or atomic variables (AtomicInteger). -
Question: Which collection classes are thread-safe in java.util.concurrent and suitable for high concurrency? Answer: ConcurrentHashMap, CopyOnWriteArrayList (for read-heavy), ConcurrentLinkedQueue, BlockingQueue implementations like ArrayBlockingQueue or LinkedBlockingQueue.
Explanation: These classes minimize locking and scale better than synchronized collections. -
Question: What is a deadlock? How can you avoid it? Answer: Deadlock is when two or more threads are blocked forever, each waiting for locks held by others. Avoid by ordering lock acquisition, using tryLock with timeouts, reducing lock scope, or using higher-level concurrency utilities.
Explanation: Designing lock ordering and preferring nonblocking algorithms helps. -
Question: Explain the happens-before relationship in the Java Memory Model. Answer: Happens-before defines visibility and ordering guarantees: if action A happens-before B, then effects of A are visible to B. Synchronization actions (locks, volatile writes, thread start/join) establish happens-before edges.
Explanation: Understanding this prevents subtle concurrency bugs. -
Question: When would you use CompletableFuture over Future? Answer: Use CompletableFuture for non-blocking, composable asynchronous workflows, chaining, and combining multiple asynchronous tasks without blocking threads.
Explanation: CompletableFuture offers many functional-style methods (thenApply, thenCompose, allOf).
Mini quiz (5 quick questions — answer mentally)
- Can a Java interface have private methods? (Yes/No)
- Does final on a reference prevent modification of the referenced object’s fields? (Yes/No)
- Is read() on InputStream thread-safe by default? (Yes/No)
- Does ConcurrentHashMap allow null keys or values? (Yes/No)
- Will starting a thread twice (thread.start() called twice) succeed? (Yes/No)
Answers: 1. Yes (since Java 9), 2. No, 3. No, 4. No, 5. No (IllegalThreadStateException)
Study tips and resources
- Practice writing small concurrent programs: producers/consumers, thread pools, lock-free structures.
- Read the Java Language Specification sections on the Java Memory Model and synchronization.
- Implement and review common design patterns (Factory, Strategy, Decorator, Singleton) and their thread-safety implications.
- Use java.util.concurrent utilities instead of low-level wait/notify when possible.
- Timeboxed exercises: debug one concurrency bug per week using thread dumps and logging.
Final challenge
Implement a thread-safe LRU cache using ConcurrentHashMap and a concurrent eviction policy (hint: use a combination of map + concurrent linked list or LinkedHashMap with external synchronization, or consider using Caffeine library in production).
Good luck — take the quiz, review explanations, and retake with tougher variations to deepen your mastery.
Leave a Reply