Spring WebSocket MCQ

 WebSockets provide a bidirectional communication channel over a single, long-lived connection, making it ideal for real-time web applications. Spring's support for WebSockets makes it easier than ever for developers to incorporate real-time, interactive functionalities in their applications. As WebSockets continues to gain popularity in modern web development, it's essential to grasp its integration with the Spring framework. Let's test your foundational knowledge on Spring WebSocket with this set of multiple-choice questions!

1. Why are WebSockets preferred over traditional HTTP for real-time applications?

a) They are newer than HTTP.
b) They allow bidirectional communication.
c) They use JSON only.
d) They are stateful.

Answer:

b) They allow bidirectional communication.

Explanation:

WebSockets allow bidirectional communication between the client and server over a single long-lived connection, making them ideal for real-time interactions.

2. Which Spring module provides support for WebSocket communication?

a) Spring Data
b) Spring MVC
c) Spring Boot
d) Spring Websocket

Answer:

d) Spring Websocket

Explanation:

The Spring Websocket module provides essential support and features to establish WebSocket communication in Spring applications.

3. Which annotation in Spring is used to denote a WebSocket handler class?

a) @WebSocketController
b) @Controller
c) @RestController
d) @MessageMapping

Answer:

d) @MessageMapping

Explanation:

@MessageMapping is used to map WebSocket messages to specific handler methods within a controller class.

4. Which interface is crucial for handling WebSocket messages in Spring?

a) WebSocketHandler
b) HandlerMapping
c) WebSocketConfigurer
d) MessageConverter

Answer:

a) WebSocketHandler

Explanation:

The WebSocketHandler interface defines methods that handle WebSocket messages.

5. How can you send a message to a specific user using Spring's WebSocket support?

a) messagingTemplate.convertAndSend("/user/{username}/queue/messages", message);
b) messagingTemplate.convertAndSend("/topic/{username}", message);
c) messagingTemplate.sendToUser(username, "/queue/messages", message);
d) messagingTemplate.broadcast("/user/{username}", message);

Answer:

a) messagingTemplate.convertAndSend("/user/{username}/queue/messages", message);

Explanation:

Spring provides a way to send messages to specific users using the "/user/{username}" destination prefix.

6. What does the @SendTo annotation do in the context of WebSockets in Spring?

a) Sends a message to a specific user.
b) Sends a message to all connected clients.
c) Converts the message to JSON.
d) Connects to a WebSocket endpoint.

Answer:

b) Sends a message to all connected clients.

Explanation:

The @SendTo annotation defines the destination to which the response should be sent.

7. Which of the following is NOT an advantage of WebSockets?

a) Bidirectional communication
b) Reduced latency
c) Requires a new protocol to be learned
d) Long-lived connection

Answer:

c) Requires a new protocol to be learned

Explanation:

While WebSockets come with many advantages, needing to learn a new protocol can be considered a challenge, not an advantage.

8. How is a connection closed in WebSockets?

a) By terminating the application.
b) By sending a 'CLOSE' frame.
c) By timeout after inactivity.
d) Connections in WebSockets cannot be closed.

Answer:

b) By sending a 'CLOSE' frame.

Explanation:

In WebSockets, a connection can be gracefully closed by either party sending a 'CLOSE' frame.

9. Which of the following is true about the WebSocketSession in Spring?

a) It's not thread-safe.
b) It supports blocking calls.
c) It can only send text messages.
d) It automatically retries failed messages.

Answer:

a) It's not thread-safe.

Explanation:

WebSocketSession in Spring is not thread-safe, so concurrent writes must be synchronized.

10. Which component in Spring's WebSocket support is responsible for brokering messages between clients?

a) WebSocketSession
b) MessageController
c) MessageBroker
d) HandlerInterceptor

Answer:

c) MessageBroker

Explanation:

The MessageBroker in Spring's WebSocket support is responsible for routing messages between message senders and receivers.

11. How can you enable WebSocket message broker in Spring configuration?

a) @EnableWebSocket
b) @EnableWebSocketMessageBroker
c) @EnableWebSocketHandler
d) @WebSocketConfig

Answer:

b) @EnableWebSocketMessageBroker

Explanation:

The @EnableWebSocketMessageBroker annotation is used to enable WebSocket message brokering in Spring.

12. Which transport protocol does Spring's WebSocket fallback to when WebSockets are not supported?

a) HTTP/2
b) HTTP Long Polling
c) FTP
d) TCP

Answer:

b) HTTP Long Polling

Explanation:

When native WebSockets are not available, Spring's WebSocket can fallback to other techniques, with HTTP long polling being a common fallback mechanism.



Comments