MCQs on Asynchronous Programming | Dart

Learn about Asynchronous Programming in Dart through these 30 MCQs, covering Future and async-await, Streams and their usage, and Handling asynchronous errors. Enhance your understanding of asynchronous tasks in Dart with these well-structured questions.


Future and async-await

  1. What is the purpose of async in Dart?
    a) It converts a function into a synchronous one
    b) It makes a function execute asynchronously
    c) It marks a function as an entry point
    d) It handles errors in a function
  2. What does the await keyword do in Dart?
    a) Waits for the function to complete synchronously
    b) Pauses the execution of an asynchronous function until the result is returned
    c) Makes the function execute faster
    d) Defines the return type of a function
  3. Which of the following is correct for defining an asynchronous function in Dart?
    a) void async main()
    b) Future async main()
    c) Future<void> main()
    d) async Future main()
  4. What type does an async function return by default in Dart?
    a) void
    b) Future<void>
    c) Future<String>
    d) Future<int>
  5. How do you handle the result of an asynchronous function in Dart?
    a) Using await
    b) Using then()
    c) Both await and then()
    d) Using async-await only
  6. Which of the following is a valid usage of the async and await keywords?
    a) await fetchData()
    b) async await fetchData()
    c) await async fetchData()
    d) async await fetchData()
  7. How does Dart handle the execution order when using async-await?
    a) Executes synchronously
    b) Executes asynchronously but waits for the result before proceeding
    c) Executes asynchronously in parallel
    d) It doesn’t wait for the result
  8. What happens if you forget to use await in an asynchronous function?
    a) The code will throw an error
    b) The function will execute synchronously
    c) The result will be ignored, and the program continues
    d) Dart automatically adds await
  9. What will be the return type of a function that is marked as async?
    a) int
    b) Future
    c) String
    d) null
  10. How can you make a non-blocking call in Dart?
    a) Using Future.delayed()
    b) By using async
    c) By using await
    d) Using setTimeout()

Streams and Their Usage

  1. What is a Stream in Dart?
    a) A sequence of synchronous events
    b) A sequence of asynchronous events
    c) A synchronous function that returns multiple values
    d) A function that returns an integer
  2. How do you listen to a stream in Dart?
    a) stream.listen()
    b) await stream.listen()
    c) stream.addListener()
    d) stream.subscribe()
  3. What does stream.listen() do?
    a) Subscribes to a stream and listens for events
    b) Executes a function when the stream ends
    c) Returns a stream
    d) Pauses the stream
  4. How can you create a stream in Dart?
    a) Stream.fromIterable()
    b) Stream.create()
    c) Stream()
    d) Stream.of()
  5. Which method is used to add an event to a stream in Dart?
    a) stream.add()
    b) stream.addEvent()
    c) stream.emit()
    d) stream.push()
  6. What is the default behavior of a Stream in Dart?
    a) Streams are synchronous
    b) Streams are always closed
    c) Streams are asynchronous and may emit multiple events
    d) Streams run only once
  7. What is the StreamController used for?
    a) To process stream events
    b) To manage and add events to a stream
    c) To close the stream
    d) To transform stream data
  8. Which of the following is a type of Stream in Dart?
    a) SingleStream
    b) SingleSubscriptionStream
    c) InfiniteStream
    d) AsyncStream
  9. How do you close a stream in Dart?
    a) stream.close()
    b) stream.cancel()
    c) stream.end()
    d) streamController.close()
  10. What happens if you try to listen to a closed stream in Dart?
    a) An error occurs
    b) The stream restarts automatically
    c) The listener is ignored
    d) The stream keeps emitting events

Handling Asynchronous Errors

  1. How are errors handled in Dart’s asynchronous code?
    a) By using try-catch blocks
    b) By using throw statements
    c) By using await only
    d) By using final variables
  2. How do you handle errors with Future in Dart?
    a) By using catchError()
    b) By using try-catch in async functions
    c) Both catchError() and try-catch
    d) Dart automatically handles errors
  3. What is the catchError() method used for in Dart?
    a) To execute a function when the stream completes
    b) To catch errors in a Future or stream
    c) To catch all exceptions in synchronous code
    d) To emit events from a stream
  4. What does the onError property of a Stream do in Dart?
    a) It signals when a stream is finished
    b) It handles errors in a stream
    c) It cancels a stream
    d) It converts stream errors into warnings
  5. How can you handle errors in a synchronous code block in Dart?
    a) Using async
    b) Using try-catch
    c) Using throw
    d) Using finally
  6. What is the output if a Future throws an error and you don’t handle it?
    a) The Future completes with an exception
    b) The program crashes
    c) The error is caught automatically
    d) It retries the operation
  7. What is the best practice for error handling in asynchronous functions in Dart?
    a) Ignore all errors
    b) Use try-catch blocks around await calls
    c) Use catchError() for all streams
    d) Always return null on errors
  8. How do you propagate an error from a Future in Dart?
    a) Using throw
    b) Using rethrow
    c) Using catchError()
    d) Using returnError()
  9. How do you handle errors in Stream in Dart?
    a) Using throwError()
    b) By returning an error stream
    c) Using onError handler
    d) Using catchStreamError()
  10. Which of the following is the correct way to catch errors in Dart’s asynchronous code?
    a) Future.error(error)
    b) try-catch
    c) catchError()
    d) All of the above

Answer Key

QnoAnswer
1b) It makes a function execute asynchronously
2b) Pauses the execution of an asynchronous function until the result is returned
3c) Future<void> main()
4b) Future<void>
5c) Both await and then()
6a) await fetchData()
7b) Executes asynchronously but waits for the result before proceeding
8c) The result will be ignored, and the program continues
9b) Future
10b) By using async
11b) A sequence of asynchronous events
12a) stream.listen()
13a) Subscribes to a stream and listens for events
14a) Stream.fromIterable()
15a) stream.add()
16c) Streams are asynchronous and may emit multiple events
17b) To manage and add events to a stream
18b) SingleSubscriptionStream
19d) streamController.close()
20a) An error occurs
21a) By using try-catch blocks
22c) Both catchError() and try-catch
23b) To catch errors in a Future or stream
24b) It handles errors in a stream
25b) Using try-catch
26a) The Future completes with an exception
27b) Use try-catch blocks around await calls
28b) Using rethrow
29c) Using onError handler
30d) All of the above

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