Spring WebFlux MCQ

With the increasing need for building reactive and scalable applications, Spring introduced WebFlux - a reactive programming framework for building non-blocking, asynchronous applications. Whether you're a newbie exploring the reactive programming paradigm or a seasoned developer wanting to validate your knowledge, this MCQ guide on Spring WebFlux is tailor-made for you. So, without further ado, let's test your reactive mettle!

1. What is the primary motivation behind Spring WebFlux?

a) Object-relational mapping
b) MVC pattern
c) Reactive programming
d) Synchronous processing

Answer:

c) Reactive programming

Explanation:

Spring WebFlux is designed to support reactive programming, allowing for building non-blocking and asynchronous applications.

2. Which of the following is a core reactive type in Spring WebFlux?

a) List
b) Set
c) Mono
d) HashMap

Answer:

c) Mono

Explanation:

Mono is a core reactive type in WebFlux, representing a single value or no value.

3. Which reactive type represents a stream of multiple items in WebFlux?

a) Mono
b) Duo
c) Flux
d) ReactiveStream

Answer:

c) Flux

Explanation:

Flux is a reactive type in WebFlux that represents a stream of 0 or more items.

4. Which annotation is used to define a reactive REST controller in Spring WebFlux?

a) @Controller
b) @RestController
c) @ReactiveController
d) @RestControllerFlux

Answer:

b) @RestController

Explanation:

The @RestController annotation is used to define both traditional and reactive REST controllers in Spring.

5. Which of the following is NOT a core component of Spring WebFlux?

A) Mono
B) Flux
C) Callable
D) RouterFunction

Answer:

C) Callable

Explanation:

Callable is associated with Spring MVC for handling asynchronous requests. In Spring WebFlux, Mono represents a single value (0 or 1) and Flux represents a sequence of values (0 to N).

6. What is a Mono in the context of Spring WebFlux?

a) A data structure holding 0 or 1 element.
b) A data structure holding multiple elements.
c) A data processing operation.
d) A type of database.

Answer:

a) A data structure holding 0 or 1 element.

Explanation:

Mono is a Publisher that can emit 0 or 1 element, representing a single result or an empty result.

7. What is a Flux in the context of Spring WebFlux?

a) A data structure holding 0 to N elements.
b) A data processing operation.
c) A type of database.
d) A network protocol.

Answer:

a) A data structure holding 0 to N elements.

Explanation:

Flux is a Publisher that can emit 0 to N elements, potentially emitting elements indefinitely.

8. In Spring WebFlux, which component is responsible for handling incoming HTTP requests in a non-blocking manner?

a) WebHandler
b) ReactiveHandler
c) FluxHandler
d) MonoHandler

Answer:

a) WebHandler

Explanation:

The WebHandler interface handles incoming HTTP requests in a non-blocking way in Spring WebFlux.

9. Which of the following methods is used to handle errors in a Subscriber?

a) onSubscribe()
b) onNext()
c) onError()
d) onComplete()

Answer:

c) onError().

Explanation:

The onError() method is used to handle the next steps whenever an error is encountered.

10. How can you execute a long-running task in a separate thread using WebFlux?

a) publishOn()
b) subscribeOn()
c) block()
d) asyncRun()

Answer:

b) subscribeOn()

Explanation:

The subscribeOn() method allows the execution of a task on a different thread in WebFlux.

11. Which interface in WebFlux allows you to produce values dynamically?

a) Producer
b) Emitter
c) FluxSink
d) MonoSink

Answer:

c) FluxSink

Explanation:

The FluxSink interface allows you to produce values dynamically and pass them to a Flux.

12. How do you retrieve a single value from a Mono?

a) get()
b) retrieve()
c) block()
d) single()

Answer:

c) block()

Explanation:

The block() method can be used on a Mono to retrieve its value, blocking until it's available.

13. In WebFlux, which component is responsible for routing and handling web requests?

a) RouterFunction
b) ReactiveRouter
c) WebRouter
d) FluxRouter

Answer:

a) RouterFunction

Explanation:

RouterFunction is responsible for routing and handling web requests in a reactive manner.

14. Which one of these is not a part of Spring WebFlux's reactive web client?

a) WebClient
b) WebTestClient
c) RestTemplate
d) ClientResponse

Answer:

c) RestTemplate

Explanation:

RestTemplate is a synchronous client and not part of WebFlux. WebClient is the reactive web client in WebFlux.

15. Which of these is a testing utility provided by Spring WebFlux for testing reactive code?

a) TestFlux
b) WebTest
c) StepVerifier
d) WebVerifier

Answer:

c) StepVerifier

Explanation:

StepVerifier is a utility provided by Spring WebFlux for testing reactive streams.

16. In Spring WebFlux, which annotation is used to specify a method parameter should be bound to a URI template variable?

a) @RequestParam
b) @PathVariable
c) @WebVariable
d) @URIParam

Answer:

b) @PathVariable

Explanation:

The @PathVariable annotation is used to specify that a method parameter should be bound to a URI template variable.

17. Which Spring WebFlux annotation assists in handling exceptions in a reactive manner?

a) @ExceptionHandler
b) @ReactiveHandler
c) @ErrorController
d) @ResponseEntityExceptionHandler

Answer:

a) @ExceptionHandler

Explanation:

The @ExceptionHandler annotation assists in handling exceptions in a reactive manner within controller methods.



Comments