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
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
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
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()
What type does an async function return by default in Dart? a) void b) Future<void> c) Future<String> d) Future<int>
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
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()
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
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
What will be the return type of a function that is marked as async? a) int b) Future c) String d) null
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
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
How do you listen to a stream in Dart? a) stream.listen() b) await stream.listen() c) stream.addListener() d) stream.subscribe()
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
How can you create a stream in Dart? a) Stream.fromIterable() b) Stream.create() c) Stream() d) Stream.of()
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()
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
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
Which of the following is a type of Stream in Dart? a) SingleStream b) SingleSubscriptionStream c) InfiniteStream d) AsyncStream
How do you close a stream in Dart? a) stream.close() b) stream.cancel() c) stream.end() d) streamController.close()
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
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
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
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
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
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
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
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
How do you propagate an error from a Future in Dart? a) Using throw b) Using rethrow c) Using catchError() d) Using returnError()
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()
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
Qno
Answer
1
b) It makes a function execute asynchronously
2
b) Pauses the execution of an asynchronous function until the result is returned
3
c) Future<void> main()
4
b) Future<void>
5
c) Both await and then()
6
a) await fetchData()
7
b) Executes asynchronously but waits for the result before proceeding
8
c) The result will be ignored, and the program continues
9
b) Future
10
b) By using async
11
b) A sequence of asynchronous events
12
a) stream.listen()
13
a) Subscribes to a stream and listens for events
14
a) Stream.fromIterable()
15
a) stream.add()
16
c) Streams are asynchronous and may emit multiple events