Concurrency Questions with Answers

10 practice questions on Concurrency (Advanced Java) with answers and step-by-step solutions. Try 10 samples below, then practise all 10 free in the AptiNest app.

Sample questions

Question 1Easy

What happens when you call run() directly on a Thread object instead of start()?

  1. A new thread is created and started in the background
  2. An exception is thrown because start was not called
  3. It runs as a normal method call in the calling thread
  4. 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

  1. start() asks JVM for a thread.
  2. run() is an ordinary call.
  3. 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?

  1. Stops accepting new tasks and lets submitted tasks finish
  2. Kills running threads immediately
  3. Blocks the caller until all tasks are done
  4. 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

  1. Call shutdown().
  2. New submit calls are rejected.
  3. 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();
  1. 30
  2. 8
  3. 15
  4. 13

Answer: D. 13

Explanation

First stage gives 5, thenApply makes 10, thenCombine adds the second future's 3: 13.

Step-by-step solution

  1. 5 then doubled to 10.
  2. Second future yields 3.
  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?

  1. It returns null and the value becomes 2
  2. It returns 1 and the value stays 1
  3. It returns 2 and the value stays 1
  4. 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

  1. Key a is already present.
  2. No update is made.
  3. 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?

  1. synchronized offers tryLock and fair ordering; ReentrantLock does not
  2. Only synchronized is reentrant; ReentrantLock deadlocks on re-entry
  3. They differ only in name and behave identically in all cases
  4. 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

  1. synchronized: simple, block structured.
  2. 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?

  1. 2, and the value is replaced
  2. 1, and the value is unchanged
  3. null
  4. 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

  1. Key present with 1.
  2. No change.
  3. 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?

  1. 6, and the value becomes 6
  2. 5, and the value stays 5
  3. 5, and the value becomes 6
  4. 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

  1. Old value 5 returned.
  2. 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?

  1. Starvation; give one thread a higher priority so it always wins
  2. A race on A only; make A volatile
  3. Deadlock; acquire locks in the same global order in both threads
  4. 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

  1. T1 holds A, wants B.
  2. T2 holds B, wants A.
  3. 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?

  1. Visibility of writes across threads, but not atomic count++
  2. Operations on it are atomic, including compound ones like count++
  3. It makes the field constant after the first write
  4. 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

  1. Thread A writes volatile.
  2. Thread B sees the new value.
  3. 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()?

  1. Semaphore
  2. ReentrantLock
  3. CountDownLatch
  4. 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

  1. Create latch(N).
  2. Workers call countDown.
  3. 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 →

Related topics

See all Advanced Java topics · All topics