Explore Scala’s concurrency concepts like Futures and Promises, asynchronous programming basics, and safe handling of concurrency. These 30 MCQs will help you master concurrency in Scala efficiently.
new Future() constructorFuture.apply() methodFuture classPromise() constructorfuture.complete()promise.set()Future.combine()Future.zip()Future.merge()Future.parallel()future.onComplete() do in Scala?
recover() or onFailure()Await.result()Future.wait()promise.wait()Future.await()global execution contextparallel execution contextdefault execution contextasync execution contextFutureThreadActorPromisemap() on a FutureflatMap() on a FutureAwait.result() on a Futurefuture.onSuccess()for-comprehension used for in Scala’s asynchronous programming?
Thread.sleep() methodsynchronized blocksFuture.successful() do in Scala?
flatMap()Await.result()future.onComplete()Future.zip()recover()fallback()handle()retry()val future = Future { /* computation */ }val future = new Future { /* computation */ }val future = Promise { /* computation */ }val future = async { /* computation */ }synchronized blocksvolatile keywordAtomic operationssynchronized keyword in Scala?
synchronizedlock()atomicvolatileThread.sleep()await()Thread.sleep() method in concurrency?
synchronized blocksThread.sleep()volatile variablesvolatile in Scala?
| Qno | Answer (Option with the text) |
|---|---|
| 1 | A) To represent a computation that will complete at some point in the future |
| 2 | B) Using the Future.apply() method |
| 3 | A) A placeholder for a result that may be available in the future |
| 4 | C) By the system once the computation finishes |
| 5 | A) A Future is completed by a Promise |
| 6 | B) Future.zip() |
| 7 | A) It specifies a callback to execute when the future completes |
| 8 | C) The exception can be handled using recover() or onFailure() |
| 9 | A) Using Await.result() |
| 10 | A) global execution context |
| 11 | A) To prevent the blocking of threads during I/O operations |
| 12 | A) Future |
| 13 | C) It runs concurrently without blocking the main thread |
| 14 | C) Await.result() on a Future |
| 15 | A) To execute a sequence of asynchronous operations sequentially |
| 16 | A) By using Futures and Promises |
| 17 | A) Returns a Future that is immediately completed with a result |
| 18 | A) By using flatMap() |
| 19 | A) recover() |
| 20 | A) val future = Future { /* computation */ } |
| 21 | A) Only one thread can access a resource at a time |
| 22 | D) All of the above |
| 23 | A) To lock an object to ensure that only one thread can execute a block of code at a time |
| 24 | A) Deadlock |
| 25 | A) synchronized |
| 26 | B) By using synchronized blocks or locks |
| 27 | A) To pause a thread temporarily |
| 28 | A) By using locks or synchronized blocks |
| 29 | A) It ensures that changes to a variable are immediately visible to all threads |
| 30 | A) Always acquiring locks in the same order |