Express lecture
20 Multiple-Choice Questions (MCQs) with Answers
1. What is the purpose of [Link]() in Express?
a. To handle HTTP POST requests
b. To handle HTTP GET requests
c. To define middleware functions
d. To redirect routes
Answer: b
2. What method is used to send a JSON response in Express?
a. [Link]()
b. [Link]()
c. [Link]()
d. [Link]()
Answer: b
3. What does [Link] represent?
a. Query parameters in the URL
b. Dynamic route parameters
c. Data from a POST request
d. Middleware properties
Answer: b
4. Which HTTP method is used for updating data?
a. GET
b. POST
c. PUT
d. DELETE
Answer: c
5. What is the default port number for an Express app?
a. 3000
b. 8080
c. 5000
d. 80
Answer: a
6. How do you apply middleware globally in Express?
a. [Link]()
b. [Link]()
c. [Link]()
d. [Link]()
Answer: b
7. What does [Link]('/home') do?
a. Ends the response process
b. Redirects the user to /home
c. Renders the /home view
d. Sends a 404 response
Answer: b
8. How do you access query parameters in Express?
a. [Link]
b. [Link]
c. [Link]
d. [Link]
Answer: a
9. What is the purpose of next() in middleware?
a. Ends the response cycle
b. Passes control to the next middleware
c. Logs the request
d. Redirects the user
Answer: b
10. What will this route match? [Link]('/user/:id', ...)
a. /user?id=5
b. /user/5
c. /user?id=5&name=John
d. /user
Answer: b
11. Which of the following is a valid response method in Express?
a. [Link]()
b. [Link]()
c. [Link]()
d. [Link]()
Answer: b
12. What type of data does [Link] hold?
a. Query parameters
b. JSON or form data from POST requests
c. Headers sent by the client
d. Dynamic route parameters
Answer: b
13. Which function is used to start the Express server?
a. [Link]()
b. [Link]()
c. [Link]()
d. [Link]()
Answer: a
14. What is the output of [Link]({ name: 'John' })?
a. Sends a text response
b. Sends a JSON response
c. Sends an HTML response
d. Sends an error
Answer: b
15. What does [Link]() do?
a. Matches any HTTP method for a given path
b. Applies middleware to all routes
c. Ends the response process
d. Matches only GET requests
Answer: a
16. What happens if you don’t call next() in middleware?
a. The request continues normally
b. The request hangs indefinitely
c. The server crashes
d. A 404 error is sent
Answer: b
17. What is the purpose of [Link](404)?
a. To end the response cycle
b. To set the HTTP status code to 404
c. To redirect the user
d. To send JSON data
Answer: b
18. Which method handles dynamic routing?
a. [Link]()
b. [Link]
c. [Link]()
d. [Link]()
Answer: c
19. How do you handle 404 errors in Express?
a. [Link]()
b. [Link]()
c. Using a catch-all middleware
d. Redirecting to /error
Answer: c
20. Which of these is not a valid HTTP method in Express?
a. GET
b. PATCH
c. OPTIONS
d. QUERY
Answer: d
Questions with Coding Requirements
1. Write a route to handle a GET request to /welcome and send the response
"Welcome to Express!".
javascript
CopyEdit
[Link]('/welcome', (req, res) => {
[Link]('Welcome to Express!');
});
2. Write a dynamic route that extracts a userId from the URL /user/:id and sends it
back in the response.
javascript
CopyEdit
[Link]('/user/:id', (req, res) => {
const userId = [Link];
[Link](`User ID: ${userId}`);
});
3. Create middleware that logs the requested URL and then passes control to the next
middleware.
javascript
CopyEdit
[Link]((req, res, next) => {
[Link](`Request URL: ${[Link]}`);
next();
});
4. Write a route to handle a POST request to /submit and respond with "Data
received".
javascript
CopyEdit
[Link]('/submit', (req, res) => {
[Link]('Data received');
});
5. Create a middleware that checks if a query parameter auth=true is present. If not,
respond with "Unauthorized".
javascript
CopyEdit
[Link]((req, res, next) => {
if ([Link] !== 'true') {
[Link](401).send('Unauthorized');
} else {
next();
}
});