MCQs on Networking and APIs | Ruby

In this guide, we will cover essential concepts and techniques for working with networking and APIs in Ruby, including using Net::HTTP, consuming APIs, and integrating WebSockets into your Ruby applications.


MCQs on Networking and APIs in Ruby

Section 1: Using Net::HTTP and Open-URI (10 Questions)

  1. Which Ruby module is commonly used for making HTTP requests?
    • a) Net::HTTP
    • b) HTTP::Request
    • c) Open::HTTP
    • d) Request::Net
  2. How do you make a basic GET request in Ruby using Net::HTTP?
    • a) Net::HTTP.get("http://example.com")
    • b) HTTP.get("http://example.com")
    • c) get("http://example.com")
    • d) HTTP::Request.get("http://example.com")
  3. Which Ruby class allows you to open and read URLs from a web page?
    • a) Net::URL
    • b) Net::HTTP
    • c) OpenURI
    • d) URI::Open
  4. What method in OpenURI can be used to retrieve the content of a URL?
    • a) open
    • b) get
    • c) fetch
    • d) retrieve
  5. How can you pass parameters in a GET request with Net::HTTP?
    • a) By including them in the URL
    • b) By adding them to the header
    • c) By using a POST request
    • d) By encoding them into the body
  6. Which module is required for using OpenURI in Ruby?
    • a) require 'net/http'
    • b) require 'open-uri'
    • c) require 'uri'
    • d) require 'http'
  7. Which method from Net::HTTP is used for making a POST request?
    • a) Net::HTTP.get
    • b) Net::HTTP.post
    • c) Net::HTTP.request
    • d) Net::HTTP.send
  8. How do you parse the JSON response from an HTTP request in Ruby?
    • a) JSON.parse(response)
    • b) parse_json(response)
    • c) response.parse_json
    • d) parse(response)
  9. Which method in Net::HTTP is used to check the response code from an HTTP request?
    • a) response.status_code
    • b) response.code
    • c) response.result
    • d) response.response_code
  10. Which Ruby method would you use to send data with a POST request in Net::HTTP?
    • a) Net::HTTP.send
    • b) Net::HTTP.set_data
    • c) Net::HTTP.post_form
    • d) Net::HTTP.post

Section 2: Creating and Consuming APIs (10 Questions)

  1. What is the first step in consuming an API in Ruby?
    • a) Parsing the response
    • b) Sending a request to the API
    • c) Handling errors
    • d) Formatting the data
  2. Which Ruby gem is commonly used for building APIs in Ruby on Rails?
    • a) rails-api
    • b) rack-api
    • c) active_api
    • d) action_controller
  3. How can you send authentication credentials with an API request in Ruby?
    • a) By including them in the query string
    • b) By using the Authorization header
    • c) By passing them as a JSON body
    • d) By encoding them in the URL
  4. In Ruby, how do you handle API response errors such as a 404 or 500?
    • a) Raise an exception
    • b) Log the error
    • c) Ignore the error
    • d) Retry the request
  5. What is a common format for sending data in an API request body?
    • a) CSV
    • b) XML
    • c) JSON
    • d) HTML
  6. Which HTTP method is commonly used to retrieve data from an API?
    • a) POST
    • b) GET
    • c) PUT
    • d) DELETE
  7. What is the typical status code for a successful API request in Ruby?
    • a) 200
    • b) 404
    • c) 500
    • d) 302
  8. How do you include headers in an API request in Ruby?
    • a) Net::HTTP.set_headers
    • b) Net::HTTP.add_header
    • c) request['header_name'] = value
    • d) request.headers['header_name'] = value
  9. In Ruby, what is the best way to handle API rate limiting?
    • a) Retry after a set amount of time
    • b) Ignore the rate limit
    • c) Send requests in bulk
    • d) Use more threads
  10. How would you make an API call that requires a JSON payload in Ruby?
    • a) Net::HTTP.post with Content-Type: application/json header
    • b) Net::HTTP.get with JSON parameters
    • c) Net::HTTP.fetch
    • d) Net::HTTP.json_post

Section 3: Websockets in Ruby (10 Questions)

  1. What is the purpose of WebSockets in Ruby?
    • a) To establish a two-way communication between a client and server
    • b) To request HTTP responses from a server
    • c) To send static data to a client
    • d) To allow for multiple server connections
  2. Which Ruby gem is commonly used to implement WebSockets in Ruby applications?
    • a) websocket-ruby
    • b) socket-io
    • c) faye-websocket
    • d) http-websocket
  3. What protocol does WebSocket use for communication?
    • a) TCP/IP
    • b) HTTP/2
    • c) WebSocket Protocol (ws:// or wss://)
    • d) FTP
  4. How can you open a WebSocket connection in Ruby using the faye-websocket gem?
    • a) Faye::WebSocket.new('ws://localhost:8080')
    • b) WebSocket.open('ws://localhost:8080')
    • c) Net::HTTP.websocket('ws://localhost:8080')
    • d) Faye::Socket.connect('ws://localhost:8080')
  5. What event is triggered when a WebSocket client receives a message in Ruby?
    • a) on_message
    • b) message_received
    • c) message
    • d) on_data
  6. How would you send a message over a WebSocket connection in Ruby using the faye-websocket gem?
    • a) socket.send("Hello!")
    • b) socket.send_message("Hello!")
    • c) socket.write("Hello!")
    • d) socket.emit("Hello!")
  7. Which of the following methods is used to close a WebSocket connection in Ruby?
    • a) socket.close
    • b) socket.shutdown
    • c) socket.disconnect
    • d) socket.terminate
  8. How do you handle errors in a WebSocket connection in Ruby?
    • a) By using the on_error event handler
    • b) By throwing exceptions
    • c) By catching them with begin-rescue
    • d) By using on_exception
  9. What is a WebSocket server in Ruby typically used for?
    • a) To send real-time data to clients
    • b) To manage HTTP sessions
    • c) To serve static files
    • d) To encrypt data
  10. What is the benefit of using WebSockets over HTTP for real-time communication?
    • a) WebSockets are more secure
    • b) WebSockets allow full-duplex communication
    • c) WebSockets reduce the server load
    • d) WebSockets can send larger payloads

Answer Key

QnoAnswer
1a) Net::HTTP
2a) Net::HTTP.get("http://example.com")
3c) OpenURI
4a) open
5a) By including them in the URL
6b) require 'open-uri'
7b) Net::HTTP.post
8a) JSON.parse(response)
9b) response.code
10d) Net::HTTP.post
11b) Sending a request to the API
12d) action_controller
13b) By using the Authorization header
14a) Raise an exception
15c) JSON
16b) GET
17a) 200
18d) request.headers['header_name'] = value
19a) Retry after a set amount of time
20a) Net::HTTP.post with Content-Type: application/json header
21a) To establish a two-way communication between a client and server
22c) faye-websocket
23c) WebSocket Protocol (ws:// or wss://)
24a) Faye::WebSocket.new('ws://localhost:8080')
25a) on_message
26a) socket.send("Hello!")
27a) socket.close
28a) By using the on_error event handler
29a) To send real-time data to clients
30b) WebSockets allow full-duplex communication

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