Swagger is a widely-used tool for designing, building, and documenting APIs. These Swagger MCQ questions and answers cover topics like defining API specifications, understanding the OpenAPI structure, writing endpoints and methods, and handling parameters, responses, and request bodies. Test your knowledge to master API design and documentation with Swagger.
Defining API Specifications
Structure of OpenAPI Specification (Paths, Definitions, and Components)
What does the “paths” section in OpenAPI specify? a) Authentication methods b) HTTP methods and endpoints c) Error codes and their descriptions d) Database schemas
Which part of the OpenAPI specification defines reusable objects? a) Paths b) Components c) Definitions d) Tags
What is the purpose of the “definitions” section in Swagger? a) To define endpoint paths b) To store reusable schemas and objects c) To specify HTTP status codes d) To document API versions
What format is used for writing OpenAPI specifications? a) JSON or YAML b) XML c) Plain text d) Markdown
In OpenAPI, where would you define reusable parameters or responses? a) Paths b) Components c) Info section d) Servers
Writing API Endpoints and Methods
What HTTP method is used for retrieving data from an API? a) POST b) GET c) PUT d) DELETE
Which HTTP method is typically used to create a resource? a) GET b) POST c) PATCH d) OPTIONS
In Swagger, how do you specify an operation for a particular endpoint? a) Add the HTTP method under the endpoint in the paths section b) Use the “info” section c) Define it in the components section d) Add it under the “servers” section
How do you document multiple operations for the same endpoint in OpenAPI? a) Use a different paths section for each operation b) Use separate HTTP methods under the same endpoint c) Write them in the servers section d) It is not possible
What is a common practice for naming API endpoints? a) Use action-based verbs and plural nouns (e.g., /getUsers) b) Use resources and plural nouns (e.g., /users) c) Use single-word paths (e.g., /list) d) Use random alphanumeric strings
Parameters, Responses, and Request Bodies
What type of parameter is appended to the endpoint URL? a) Path parameter b) Query parameter c) Header parameter d) Request body parameter
How are path parameters represented in OpenAPI? a) With {} brackets b) Using [] brackets c) As a JSON object d) Using query strings
What section in Swagger defines expected API responses? a) Paths b) Responses c) Components d) Servers
How do you specify a required parameter in OpenAPI? a) By adding "required": true b) By placing it in the “components” section c) By marking it with an asterisk (*) d) By writing it in uppercase
What does a 201 HTTP status code indicate? a) Success but no content b) The resource was successfully created c) Client-side error d) Server-side error
Where do you document the schema of a request body in OpenAPI? a) Under the “responses” section b) Under the “requestBody” section c) In the “paths” definition d) In the “info” section
What is the purpose of the “description” field in a response? a) To specify the API version b) To explain the meaning or purpose of the response c) To list associated endpoints d) To provide authorization details
What type of response format is typically used in APIs? a) JSON b) CSV c) Plain text d) YAML
How do you specify multiple response types (e.g., JSON, XML) in OpenAPI? a) Use the “content” object in the responses section b) Add them to the “info” section c) Include them in the “servers” section d) Write them as comments
What does a 400 status code usually indicate in an API response? a) Successful request b) Bad request (client error) c) Resource not found d) Server error
Advanced Concepts in Swagger
What is a Swagger “tag” used for? a) Grouping related operations b) Defining reusable schemas c) Creating endpoints d) Specifying request methods
How do you define multiple servers for an API in Swagger? a) Using the “servers” section b) Listing them under the “components” section c) Adding them to the “responses” section d) Including them in the “info” section
Which HTTP header parameter is commonly used for authentication? a) Authorization b) Content-Type c) Accept d) User-Agent
What is an advantage of using reusable components in Swagger? a) Simplifies debugging b) Reduces redundancy in API specifications c) Speeds up API responses d) Increases schema complexity
How are nested JSON objects represented in Swagger schemas? a) As a string field b) Using $ref for component definitions c) As plain text descriptions d) They cannot be represented
What is the purpose of the “info” section in OpenAPI? a) To define authentication methods b) To provide metadata about the API c) To document request parameters d) To store reusable objects
What does the “servers” section define in OpenAPI? a) API endpoints b) Base URLs for the API c) Database connections d) Test cases
Which keyword is used to define data types in OpenAPI? a) format b) type c) schema d) example
What is the role of an API description in Swagger? a) To explain the entire API purpose and usage b) To group endpoints c) To specify authentication methods d) To log errors
What does the example field in OpenAPI do? a) Provides an example value for documentation and testing b) Validates the schema c) Overrides the parameter name d) Specifies the response time
Answer Key
Qno
Answer (Option with text)
1
b) HTTP methods and endpoints
2
b) Components
3
b) To store reusable schemas and objects
4
a) JSON or YAML
5
b) Components
6
b) GET
7
b) POST
8
a) Add the HTTP method under the endpoint in the paths section
9
b) Use separate HTTP methods under the same endpoint
10
b) Use resources and plural nouns (e.g., /users)
11
b) Query parameter
12
a) With {} brackets
13
b) Responses
14
a) By adding "required": true
15
b) The resource was successfully created
16
b) Under the “requestBody” section
17
b) To explain the meaning or purpose of the response
18
a) JSON
19
a) Use the “content” object in the responses section
20
b) Bad request (client error)
21
a) Grouping related operations
22
a) Using the “servers” section
23
a) Authorization
24
b) Reduces redundancy in API specifications
25
b) Using $ref for component definitions
26
b) To provide metadata about the API
27
b) Base URLs for the API
28
b) type
29
a) To explain the entire API purpose and usage
30
a) Provides an example value for documentation and testing