Sample questions
Question 1Easy
What happens when you call run() directly on a Thread object instead of start()?
- A new thread is created and started in the background
- An exception is thrown because start was not called
- It runs as a normal method call in the calling thread
- The thread runs after main ends
Answer: C. It runs as a normal method call in the calling thread
Explanation
Only start() creates a new thread of execution. Calling run() just executes the body in the current thread.
Step-by-step solution
- start() asks JVM for a thread.
- run() is an ordinary call.
- Thread name would be main.
Why the other options are wrong
- A. No new thread is created by run().
- B. It does not throw.
- D. It runs immediately in the caller.
Tip: start, not run.
Question 2Medium
What does ExecutorService.shutdown() do?
- Stops accepting new tasks and lets submitted tasks finish
- Kills running threads immediately
- Blocks the caller until all tasks are done
- Restarts the pool with fresh worker threads
Answer: A. Stops accepting new tasks and lets submitted tasks finish
Explanation
shutdown is a graceful stop. shutdownNow tries to interrupt running tasks, and awaitTermination is what waits.
Step-by-step solution
- Call
shutdown(). - New
submit calls are rejected. - Queued and running tasks continue.
Why the other options are wrong
- B. That describes
shutdownNow more closely. - C.
shutdown returns at once. - D. A pool is not restarted by shutdown.
Question 3Medium
What is the output?
CompletableFuture.supplyAsync(() -> 5)
.thenApply(x -> x * 2)
.thenCombine(CompletableFuture.supplyAsync(() -> 3), (p, q) -> p + q)
.get();
- 30
- 8
- 15
- 13
Answer: D. 13
Explanation
First stage gives 5, thenApply makes 10, thenCombine adds the second future's 3: 13.
Step-by-step solution
- 5 then doubled to 10.
- Second future yields 3.
- Combine with p + q = 13.
Why the other options are wrong
- A. That would multiply instead of add.
- B. 8 ignores the doubling.
- C. 15 would be 5 times 3.
Question 4Medium
Given a ConcurrentHashMap with key a mapped to 1, what does putIfAbsent("a", 2) return and what is the value of a afterwards?
- It returns null and the value becomes 2
- It returns 1 and the value stays 1
- It returns 2 and the value stays 1
- It returns 1 and the value becomes 2
Answer: B. It returns 1 and the value stays 1
Explanation
putIfAbsent stores only when the key has no mapping. It returns the existing value, which is unchanged.
Step-by-step solution
- Key a is already present.
- No update is made.
- The previous value 1 is returned.
Why the other options are wrong
- A. Null is returned only when the key was absent.
- C. It returns the old value, not the new.
- D. The value is not replaced.
Question 5Medium
What is the difference between synchronized method and ReentrantLock?
- synchronized offers tryLock and fair ordering; ReentrantLock does not
- Only synchronized is reentrant; ReentrantLock deadlocks on re-entry
- They differ only in name and behave identically in all cases
- ReentrantLock offers tryLock, timed waits and fairness; synchronized is simpler and auto-releases
Answer: D. ReentrantLock offers tryLock, timed waits and fairness; synchronized is simpler and auto-releases
Explanation
synchronized releases the monitor on exit automatically. ReentrantLock needs unlock() in finally but adds tryLock, interruptible waits and fair ordering.
Step-by-step solution
- synchronized: simple, block structured.
- ReentrantLock: explicit lock/unlock with more control.
Why the other options are wrong
- A. tryLock exists on ReentrantLock.
- B. Both are reentrant.
- C. They have different features.
Question 6Medium
What does ConcurrentHashMap.putIfAbsent("a", 2) return if "a" already maps to 1?
- 2, and the value is replaced
- 1, and the value is unchanged
- null
- An exception
Answer: B. 1, and the value is unchanged
Explanation
It returns the existing value and leaves it, making check-then-put atomic.
Step-by-step solution
- Key present with 1.
- No change.
- Return previous value 1.
Why the other options are wrong
- A. It does not replace an existing value.
- C. null is returned only when the key was absent.
- D. No exception for an existing key.
Question 7Medium
What does AtomicInteger.getAndIncrement() return when the value is 5?
- 6, and the value becomes 6
- 5, and the value stays 5
- 5, and the value becomes 6
- 6, and the value becomes 7
Answer: C. 5, and the value becomes 6
Explanation
getAndIncrement returns the old value then increments, like i++. incrementAndGet is like ++i.
Step-by-step solution
- Old value 5 returned.
- Counter updated to 6.
Why the other options are wrong
- A. That is incrementAndGet().
- B. The value does change.
- D. It increments once only.
Tip: get-and-inc is i++.
Question 8Hard
Two threads each lock objects A and B, thread 1 in order A then B and thread 2 in order B then A. What is the main risk, and the usual fix?
- Starvation; give one thread a higher priority so it always wins
- A race on A only; make A volatile
- Deadlock; acquire locks in the same global order in both threads
- Livelock; add Thread.sleep to both threads and retry
Answer: C. Deadlock; acquire locks in the same global order in both threads
Explanation
Each thread holds one lock and waits for the other. A consistent lock order breaks the circular wait.
Step-by-step solution
- T1 holds A, wants B.
- T2 holds B, wants A.
- Both wait forever; fix by ordering lock acquisition.
Why the other options are wrong
- A. Priorities do not release a held lock.
- B.
volatile does not provide locking. - D. Sleeping does not remove circular waiting.
Question 9Hard
What is the main guarantee of the volatile keyword on a field?
- Visibility of writes across threads, but not atomic count++
- Operations on it are atomic, including compound ones like count++
- It makes the field constant after the first write
- It makes every access synchronised with an implicit lock
Answer: A. Visibility of writes across threads, but not atomic count++
Explanation
Volatile ensures visibility and ordering. It does not make count++ atomic; use AtomicInteger or synchronisation.
Step-by-step solution
- Thread A writes volatile.
- Thread B sees the new value.
- Increments can still race.
Why the other options are wrong
- B. count++ is read-modify-write and is not atomic with volatile.
- C. Final makes it constant, not volatile.
- D. No lock is taken.
Question 10Hard
Which synchronizer lets one thread wait until N other threads each call countDown()?
- Semaphore
- ReentrantLock
- CountDownLatch
- ThreadLocal
Answer: C. CountDownLatch
Explanation
A CountDownLatch starts with a count; await() blocks until it reaches zero. It cannot be reset.
Step-by-step solution
- Create latch(N).
- Workers call countDown.
- Waiter's await returns at zero.
Why the other options are wrong
- A. A Semaphore limits concurrent permits, not a one-time countdown.
- B. A lock gives exclusive access.
- D. ThreadLocal keeps a per-thread variable.
Practise all 10 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →