Interview Questions: Self-Assessment¶
Use this quiz to test your understanding of multithreading concepts. Select an answer to see feedback.
Level 1: Foundational (Should know after Sections 01-06)¶
B — Processes are OS-level with isolated memory. Threads are lightweight, within a process, sharing memory.
B — Daemon threads are for background work. JVM doesn't wait for them. Non-daemon threads are the default; JVM waits for them to finish.
run() instead of start() on a thread?
- [ ] A: Nothing; they're equivalent
- [ ] B: Exception is thrown
- [x] C: Code executes in current thread, not new thread
- [ ] D: Thread is created but not started
C — run() executes inline. start() creates a new thread. Always call start().
volatile sufficient for thread-safe counters?
- [ ] A: Yes, volatile guarantees atomicity
- [x] B: No, volatile ensures visibility but not atomicity
- [ ] C: Yes, but only for simple types
- [ ] D: No, threads can never share counters safely
B — volatile count++ is still NOT atomic. It's three operations: read, add, write. Use AtomicInteger.
B — Race condition: multiple threads, shared mutable data, no synchronization, non-deterministic outcome.
Level 2: Intermediate (After Sections 07-10)¶
B — ConcurrentHashMap divides into segments. Multiple threads can write to different segments simultaneously. synchronizedMap locks entire map.
B — ReentrantLock offers: tryLock(timeout), fairness guarantee, multiple Condition variables. Own synchronization is simpler for basic cases.
execute() and submit() on ExecutorService?
- [ ] A: No difference; they're aliases
- [ ] B: execute() returns Future; submit() returns void
- [x] C: submit() returns Future; execute() returns void
- [ ] D: submit() doesn't throw exceptions
C — execute() returns void. submit() returns Future for result/exception handling. Why use submit() for Callables.
B — If every thread acquires A then B (never B then A), circular wait is impossible.
CopyOnWriteArrayList better than Collections.synchronizedList?
- [ ] A: Always; it's always better
- [x] B: When you have many readers, few writers
- [ ] C: When you have many writers
- [ ] D: When order doesn't matter
B — CopyOnWriteArrayList: reads are lock-free (iterate on snapshot), writes are expensive (copy array). Good for read-heavy.
Level 3: Advanced (After Sections 11-15)¶
B — thenApply runs on same thread that produced the value. thenApplyAsync uses ForkJoinPool (parallelism).
B — Virtual threads: cheap JVM-managed, perfect for I/O. NOT for CPU-bound (still need parallelism from cores).
B — StructuredTaskScope scopes tasks to try-with-resources. Tasks complete before scope exits. Clear resource management.
B — ScopedValue: automatic cleanup, immutable, designed for virtual threads. ThreadLocal: manual cleanup, mutable, legacy.
B — Using sleep() for synchronization is busy-waiting. Use CountDownLatch, Condition, or BlockingQueue instead.
Scoring¶
- 1-5 correct: Review Sections 01-06 (Foundations)
- 6-10 correct: Review Sections 07-10 (Synchronization & Executors)
- 11-15 correct: Strong understanding; focus on practice with Labs
Next Steps¶
- Review missed questions in Interview Q&A
- Complete corresponding Lab exercises
- Study relevant theory sections
- Practice explaining concepts out loud (prepares for real interviews)