MCQs on Introduction to Concurrency | Scala

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.


Introduction to Concurrency in Scala

1. Futures and Promises

  1. What is a Future in Scala used for?
    • A) To represent a computation that will complete at some point in the future
    • B) To represent a fixed value that is computed immediately
    • C) To create a new thread
    • D) To manage errors during computation
  2. How is a Future usually created in Scala?
    • A) Using new Future() constructor
    • B) Using the Future.apply() method
    • C) By extending the Future class
    • D) Using the Promise() constructor
  3. What does a Promise in Scala represent?
    • A) A placeholder for a result that may be available in the future
    • B) A fixed value that is already computed
    • C) An abstraction for a background task
    • D) A signal for canceling a computation
  4. How can a Future in Scala be completed?
    • A) By calling future.complete()
    • B) By setting the value manually
    • C) By the system once the computation finishes
    • D) By invoking promise.set()
  5. What is the relationship between a Future and a Promise in Scala?
    • A) A Future is completed by a Promise
    • B) A Promise is completed by a Future
    • C) A Promise and a Future are independent
    • D) They are both used for asynchronous callbacks
  6. Which method is used to combine multiple Futures in Scala?
    • A) Future.combine()
    • B) Future.zip()
    • C) Future.merge()
    • D) Future.parallel()
  7. What does future.onComplete() do in Scala?
    • A) It specifies a callback to execute when the future completes
    • B) It cancels the future once the computation ends
    • C) It waits for the future to finish
    • D) It repeats the future’s task if it fails
  8. What will happen if a Future fails in Scala?
    • A) The program will terminate immediately
    • B) The Future will retry the operation
    • C) The exception can be handled using recover() or onFailure()
    • D) The computation will continue despite the failure
  9. How can you block and wait for a Future to complete in Scala?
    • A) Using Await.result()
    • B) Using Future.wait()
    • C) Using promise.wait()
    • D) Using Future.await()
  10. What is the default execution context for Futures in Scala?
    • A) global execution context
    • B) parallel execution context
    • C) default execution context
    • D) async execution context

2. Asynchronous Programming Basics

  1. What is the primary advantage of asynchronous programming?
    • A) To prevent the blocking of threads during I/O operations
    • B) To handle errors more effectively
    • C) To simplify concurrent programming
    • D) To improve the performance of single-threaded applications
  2. Which Scala feature is used to execute asynchronous tasks?
    • A) Future
    • B) Thread
    • C) Actor
    • D) Promise
  3. In asynchronous programming, what happens when an operation is executed without waiting for its result?
    • A) It runs synchronously
    • B) It runs in parallel
    • C) It runs concurrently without blocking the main thread
    • D) It blocks the execution of the program
  4. Which of the following is NOT an asynchronous programming operation in Scala?
    • A) map() on a Future
    • B) flatMap() on a Future
    • C) Await.result() on a Future
    • D) future.onSuccess()
  5. What is the for-comprehension used for in Scala’s asynchronous programming?
    • A) To execute a sequence of asynchronous operations sequentially
    • B) To handle exceptions
    • C) To create Futures from a synchronous operation
    • D) To synchronize multiple Futures
  6. How do you manage asynchronous tasks in Scala without blocking the thread?
    • A) By using Futures and Promises
    • B) By creating new threads manually
    • C) By using the Thread.sleep() method
    • D) By using synchronized blocks
  7. What does Future.successful() do in Scala?
    • A) Returns a Future that is immediately completed with a result
    • B) Returns a Future that will always fail
    • C) Starts a new asynchronous computation
    • D) Blocks the thread until the Future completes
  8. In Scala, how can you chain asynchronous operations?
    • A) By using flatMap()
    • B) By using Await.result()
    • C) By using future.onComplete()
    • D) By using Future.zip()
  9. Which method is used to recover from an exception in a Future in Scala?
    • A) recover()
    • B) fallback()
    • C) handle()
    • D) retry()
  10. Which of the following is the correct way to specify an asynchronous computation in Scala?
    • A) val future = Future { /* computation */ }
    • B) val future = new Future { /* computation */ }
    • C) val future = Promise { /* computation */ }
    • D) val future = async { /* computation */ }

3. Handling Concurrency Safely

  1. What does thread safety mean in concurrent programming?
    • A) Only one thread can access a resource at a time
    • B) Any number of threads can safely access a resource
    • C) Threads can access resources freely without any coordination
    • D) It means threads can operate on mutable objects without synchronization
  2. Which Scala feature helps ensure thread safety in concurrent programming?
    • A) synchronized blocks
    • B) volatile keyword
    • C) Atomic operations
    • D) All of the above
  3. What is the purpose of the synchronized keyword in Scala?
    • A) To lock an object to ensure that only one thread can execute a block of code at a time
    • B) To execute code asynchronously
    • C) To ensure that code executes only once
    • D) To create a new thread for each task
  4. Which of the following is a common problem in concurrent programming?
    • A) Deadlock
    • B) Non-blocking I/O
    • C) Synchronization
    • D) Garbage collection
  5. Which of the following methods ensures that only one thread at a time can access a critical section of code?
    • A) synchronized
    • B) lock()
    • C) atomic
    • D) volatile
  6. How can you prevent a race condition in Scala?
    • A) By using immutable objects
    • B) By using synchronized blocks or locks
    • C) By using Thread.sleep()
    • D) By using await()
  7. What is the role of the Thread.sleep() method in concurrency?
    • A) To pause a thread temporarily
    • B) To block all threads
    • C) To synchronize threads
    • D) To kill threads after completion
  8. How can you ensure thread safety when accessing a shared mutable variable in Scala?
    • A) By using locks or synchronized blocks
    • B) By using immutable variables only
    • C) By using Thread.sleep()
    • D) By using volatile variables
  9. What is an important factor to consider when using volatile in Scala?
    • A) It ensures that changes to a variable are immediately visible to all threads
    • B) It guarantees thread safety for all types of variables
    • C) It allows multiple threads to read and write to a variable simultaneously
    • D) It automatically handles synchronization for you
  10. Which of the following strategies can help avoid deadlock in concurrent programs?
    • A) Always acquiring locks in the same order
    • B) Randomly acquiring locks
    • C) Acquiring locks only for short periods
    • D) Using immutable data structures

Answer Key

QnoAnswer (Option with the text)
1A) To represent a computation that will complete at some point in the future
2B) Using the Future.apply() method
3A) A placeholder for a result that may be available in the future
4C) By the system once the computation finishes
5A) A Future is completed by a Promise
6B) Future.zip()
7A) It specifies a callback to execute when the future completes
8C) The exception can be handled using recover() or onFailure()
9A) Using Await.result()
10A) global execution context
11A) To prevent the blocking of threads during I/O operations
12A) Future
13C) It runs concurrently without blocking the main thread
14C) Await.result() on a Future
15A) To execute a sequence of asynchronous operations sequentially
16A) By using Futures and Promises
17A) Returns a Future that is immediately completed with a result
18A) By using flatMap()
19A) recover()
20A) val future = Future { /* computation */ }
21A) Only one thread can access a resource at a time
22D) All of the above
23A) To lock an object to ensure that only one thread can execute a block of code at a time
24A) Deadlock
25A) synchronized
26B) By using synchronized blocks or locks
27A) To pause a thread temporarily
28A) By using locks or synchronized blocks
29A) It ensures that changes to a variable are immediately visible to all threads
30A) Always acquiring locks in the same order

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top