Home »
MCQs
RxPY MCQs (Multiple-Choice Questions)
RxPY, also known as ReactiveX for Python, is a library for composing asynchronous and event-based programs using observable collections and operators. It provides tools such as Observables, Observers, Subjects, operators, and schedulers for building reactive applications.
RxPY MCQs
These RxPY MCQs cover important concepts such as Observables, Observers, Subjects, operators, subscriptions, schedulers, error handling, and combining observable sequences.
List of RxPY MCQs
The following RxPY multiple-choice questions are designed to test your knowledge of ReactiveX programming in Python.
1. What is RxPY?
- A Python library for reactive programming
- A Python web framework
- A machine learning library
- A database management system
Answer: A) A Python library for reactive programming
Explanation:
RxPY is ReactiveX for Python, a library for composing asynchronous and event-based programs using observable collections and operators.
2. What is the main abstraction used to represent a sequence of emitted values in RxPY?
- Observer
- Observable
- Scheduler
- Subject
Answer: B) Observable
Explanation:
An Observable represents a push-based sequence that emits values and notifications to its subscribers.
3. Which package is used by current versions of RxPY?
- reactivex
- rxpython
- rxcore
- pyreactive
Answer: A) reactivex
Explanation:
Current RxPY documentation uses the reactivex package for creating Observables, operators, Subjects, and schedulers.
4. Which statement correctly describes an Observable?
- It only stores static values
- It pushes emitted items to observers
- It is used only for database queries
- It can never signal errors
Answer: B) It pushes emitted items to observers
Explanation:
An Observable produces notifications that can be received by subscribers through callbacks such as on_next, on_error, and on_completed.
5. Which method is used to subscribe to an Observable?
- listen()
- observe()
- subscribe()
- attach()
Answer: C) subscribe()
Explanation:
The subscribe() method attaches an observer or callbacks to an Observable and returns a disposable representing the subscription.
6. Which callback receives each value emitted by an Observable?
- on_next
- on_value
- on_emit
- on_data
Answer: A) on_next
Explanation:
The on_next callback is invoked whenever an Observable emits an item.
7. Which callback is invoked when an Observable completes normally?
- on_finish
- on_completed
- on_done
- on_close
Answer: B) on_completed
Explanation:
The on_completed callback indicates that an Observable has completed its sequence normally.
8. Which callback is used when an Observable terminates because of an error?
- on_exception
- on_fail
- on_error
- on_fault
Answer: C) on_error
Explanation:
The on_error callback receives an exception when an Observable terminates with an error.
9. Which function creates an Observable from an iterable?
- from_iterable()
- iterable()
- from_sequence()
- to_observable()
Answer: A) from_iterable()
Explanation:
The from_iterable() factory creates an Observable that emits the items from an iterable.
10. Which RxPY factory creates an Observable from one or more specified values?
- just()
- values()
- emit()
- single()
Answer: A) just()
Explanation:
The just() factory creates an Observable that emits the specified object or objects.
11. Which factory creates an Observable that emits sequential integers within a specified range?
- sequence()
- range()
- numbers()
- count_range()
Answer: B) range()
Explanation:
The range() factory creates an Observable that emits a sequence of sequential integers.
12. Which factory creates an Observable that emits values at regular time intervals?
- timer_range()
- interval()
- periodic()
- repeat_interval()
Answer: B) interval()
Explanation:
The interval() factory creates an Observable that emits sequential integers separated by a specified time interval.
13. Which operator transforms each item emitted by an Observable?
- map
- filter
- take
- merge
Answer: A) map
Explanation:
The map operator applies a function to each item emitted by the source Observable and produces a new Observable.
14. Which operator selects only items that satisfy a specified condition?
- map
- filter
- reduce
- scan
Answer: B) filter
Explanation:
The filter operator emits only those items for which the supplied predicate returns true.
15. Which operator limits the number of items emitted by an Observable?
- limit
- take
- first_n
- restrict
Answer: B) take
Explanation:
The take operator emits a specified number of items from the source Observable and then completes.
16. What does the flat_map operator do?
- Only filters values
- Maps items to Observables and merges the resulting sequences
- Stops an Observable immediately
- Sorts emitted values
Answer: B) Maps items to Observables and merges the resulting sequences
Explanation:
The flat_map operator projects each source item into an Observable and merges the resulting Observables into a single sequence.
17. Which operator combines multiple Observables by subscribing to them and merging their emissions?
- merge
- zip
- concat_map
- join_latest
Answer: A) merge
Explanation:
The merge operator combines multiple Observable sequences into one Observable, allowing their emissions to be interleaved.
18. Which operator emits the values of one Observable followed by another?
- merge
- concat
- zip
- combine_latest
Answer: B) concat
Explanation:
The concat operator emits the items from Observables sequentially, waiting for one sequence to complete before subscribing to the next.
19. Which operator combines corresponding values from multiple Observables into tuples?
- zip
- merge
- concat
- buffer
Answer: A) zip
Explanation:
The zip operator combines items from multiple Observables according to their corresponding positions.
20. Which operator combines the latest values from multiple Observables whenever one of them emits?
- zip
- concat
- combine_latest
- merge_all
Answer: C) combine_latest
Explanation:
The combine_latest operator combines the latest values from multiple source Observables and emits a new result whenever a source emits after all sources have produced a value.
21. Which operator delays emissions until there has been no new item for a specified period?
- debounce
- delay_subscription
- timeout
- sample
Answer: A) debounce
Explanation:
The debounce operator suppresses items that are followed by another item within the specified time span and emits when the source remains quiet for that period.
22. Which operator ignores source items after a specified condition becomes false?
- skip_while
- take_while
- filter
- take_until
Answer: B) take_while
Explanation:
The take_while operator emits items while a specified condition remains true and stops when the condition becomes false.
23. Which operator skips items while a specified condition remains true?
- take_while
- skip_while
- filter
- skip
Answer: B) skip_while
Explanation:
The skip_while operator discards source items while the supplied condition is true and then allows subsequent items through.
24. Which operator counts the number of items emitted by an Observable?
- length
- count
- size
- items_count
Answer: B) count
Explanation:
The count operator counts the items emitted by the source Observable and emits the resulting count.
25. Which operator calculates the sum of numeric values emitted by an Observable?
- total
- add
- sum
- aggregate_sum
Answer: C) sum
Explanation:
The sum operator calculates the sum of numeric items emitted by an Observable.
26. Which operator applies an accumulator function and emits the final accumulated value?
- scan
- reduce
- aggregate
- fold
Answer: B) reduce
Explanation:
The reduce operator applies an accumulator function to the items in sequence and emits the final accumulated result.
27. Which operator emits accumulated intermediate results for each source item?
- reduce
- scan
- sum
- aggregate
Answer: B) scan
Explanation:
The scan operator applies an accumulator function and emits each intermediate accumulated result.
28. What is a Subject in RxPY?
- An object that is both an Observable and an Observer
- A scheduler only
- A filtering operator
- A type of exception
Answer: A) An object that is both an Observable and an Observer
Explanation:
A Subject can receive notifications and broadcast them to its subscribed observers.
29. Which method sends a value to observers subscribed to a Subject?
- emit()
- next()
- on_next()
- send()
Answer: C) on_next()
Explanation:
The on_next() method of a Subject sends a value to all currently subscribed observers.
30. Which Subject stores the latest value and sends it to a new subscriber?
- ReplaySubject
- BehaviorSubject
- AsyncSubject
- PublishSubject
Answer: B) BehaviorSubject
Explanation:
A BehaviorSubject stores a current value and emits that value to a new subscriber.
31. Which Subject can replay previously emitted values to new subscribers?
- BehaviorSubject
- ReplaySubject
- AsyncSubject
- SingleSubject
Answer: B) ReplaySubject
Explanation:
A ReplaySubject can replay previously emitted notifications to new subscribers, depending on its configured buffering behavior.
32. Which method signals that a Subject has completed?
- complete()
- finish()
- on_completed()
- close()
Answer: C) on_completed()
Explanation:
The on_completed() method notifies subscribers that the Subject has completed its sequence.
33. What does the dispose() method of a subscription do?
- Creates a new Observable
- Unsubscribes and releases subscription resources
- Transforms emitted values
- Restarts an Observable
Answer: B) Unsubscribes and releases subscription resources
Explanation:
The disposable returned by subscribe() can be disposed to unsubscribe the observer and release associated resources.
34. Which method is used to compose multiple operators in RxPY?
- chain()
- pipe()
- compose_only()
- operators()
Answer: B) pipe()
Explanation:
The pipe() method composes multiple operators and applies them from left to right to an Observable.
35. Which syntax correctly imports RxPY operators in the current package?
- import rx.ops as op
- from reactivex import operators as op
- import reactivex.operator as op
- from rxpy import operator
Answer: B) from reactivex import operators as op
Explanation:
Current RxPY documentation commonly uses from reactivex import operators as op to access pipe-based operators.
36. Which scheduler is designed to provide a pool of reusable worker threads?
- ImmediateScheduler
- ThreadPoolScheduler
- CurrentThreadScheduler
- VirtualTimeScheduler
Answer: B) ThreadPoolScheduler
Explanation:
ThreadPoolScheduler provides a pool of worker threads and can be used for scheduling asynchronous work.
37. What is the purpose of a Scheduler in RxPY?
- To store Observable values permanently
- To control where and when work is executed
- To define Python classes
- To convert Observables into lists
Answer: B) To control where and when work is executed
Explanation:
Schedulers provide a mechanism for controlling the execution context and timing of work performed by Observable operations.
38. Which operator specifies the scheduler on which an Observable is subscribed?
- observe_on
- subscribe_on
- schedule_on
- run_on
Answer: B) subscribe_on
Explanation:
The subscribe_on operator specifies the scheduler used for subscribing to the source Observable.
39. Which operator specifies the scheduler on which observer notifications are delivered?
- subscribe_on
- observe_on
- notify_on
- callback_on
Answer: B) observe_on
Explanation:
The observe_on operator changes the scheduler on which downstream observer callbacks receive notifications.
40. Which operator can replace an error-producing Observable with another Observable sequence?
- catch
- replace_error
- recover
- handle_exception
Answer: A) catch
Explanation:
The catch operator can continue an Observable sequence with another Observable when an error occurs.
41. Which operator retries a source Observable when an error occurs?
- retry
- repeat_error
- restart
- recover
Answer: A) retry
Explanation:
The retry operator resubscribes to the source when an error occurs, according to its configured retry behavior.
42. Which operator emits a default value when the source Observable completes without emitting an item?
- default_if_empty
- empty_default
- fallback
- default_value
Answer: A) default_if_empty
Explanation:
The default_if_empty operator emits a specified default value when the source Observable produces no items.
43. Which operator checks whether all emitted items satisfy a condition?
- all
- every_item
- check_all
- validate
Answer: A) all
Explanation:
The all operator determines whether all items emitted by an Observable satisfy a specified predicate.
44. Which operator determines whether an Observable emits a particular value?
- find
- contains
- has_value
- exists
Answer: B) contains
Explanation:
The contains operator determines whether a particular item is emitted by the Observable.
45. Which operator can transform an Observable into a shared connectable Observable?
- publish
- share_only
- multicast_only
- broadcast
Answer: A) publish
Explanation:
The publish operator converts an ordinary Observable into a connectable Observable that can share emissions among subscribers.
46. Which operator makes a Connectable Observable behave like an ordinary Observable by managing subscriptions?
- ref_count
- connect_auto
- share_count
- auto_subscribe
Answer: A) ref_count
Explanation:
The ref_count operator manages connections to a Connectable Observable based on the number of active subscribers.
47. Which operator attaches a timestamp to each emitted item?
- time_stamp
- timestamp
- mark_time
- add_time
Answer: B) timestamp
Explanation:
The timestamp operator attaches timing information to each item emitted by the Observable.
48. Which operator measures the time interval between emitted items?
- time_interval
- interval_time
- measure_time
- elapsed
Answer: A) time_interval
Explanation:
The time_interval operator converts an Observable into one that indicates the elapsed time between consecutive emissions.
49. Which operator raises an error if a specified period passes without an item being emitted?
- timeout
- delay_error
- time_limit
- wait_error
Answer: A) timeout
Explanation:
The timeout operator monitors the source Observable and issues an error notification if the specified period passes without the expected emission.
50. Which statement about RxPY operator chaining is correct?
- Operators can only be used individually
- RxPY supports pipe-based operator composition and fluent method chaining
- Operators cannot create new Observables
- Only scheduler methods can be chained
Answer: B) RxPY supports pipe-based operator composition and fluent method chaining
Explanation:
Current RxPY supports both functional pipe-based syntax using pipe() and fluent method chaining directly on an Observable.
Advertisement
Advertisement