Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
You're offline
Unit 3 MCQsSocket Services in Node.js & Creating middlewares
What is the primary communication protocol used by WebSockets?
Not quite. Try another option.
Answer: TCP
WebSockets provide full-duplex communication channels over a single TCP connection.
Question 2 of 50
Which URL scheme is used for an unencrypted WebSocket connection?
Not quite. Try another option.
Answer: ws://
The 'ws://' scheme is used for unencrypted WebSocket connections, while 'wss://' is used for encrypted connections.
Question 3 of 50
Unlike HTTP, a WebSocket connection is:
Not quite. Try another option.
Answer: Full-duplex and persistent
WebSockets allow data to flow in both directions (full-duplex) and remain open until closed (persistent), unlike the request-response model of HTTP.
Question 4 of 50
Which HTTP header is essential for the WebSocket handshake process?
Not quite. Try another option.
Answer: Upgrade
The 'Upgrade' header is sent by the client to request that the server switch the connection protocol from HTTP to WebSocket.
Question 5 of 50
In a basic Node.js 'ws' server, which event is triggered when a client connects?
Not quite. Try another option.
Answer: connection
The 'connection' event is emitted by the WebSocket server when a new handshake is successfully completed.
Question 6 of 50
Which method is used to send a message from the server to a specific client instance in the 'ws' library?
Not quite. Try another option.
Answer: client.send()
In the standard 'ws' library, the .send() method is used to transmit data to the connected client.
Question 7 of 50
What does Socket.IO provide that native WebSockets do not handle automatically?
Not quite. Try another option.
Answer: Automatic reconnection and fallback support
Socket.IO provides features like automatic reconnection, fallbacks to long-polling if WebSockets aren't supported, and broadcasting.
Question 8 of 50
In Socket.IO, how do you send a message to all connected clients?
Not quite. Try another option.
Answer: io.emit('msg', data)
io.emit() sends a message to all connected clients. socket.emit() sends it only to the specific socket, and socket.broadcast.emit() sends it to everyone except the sender.
Question 9 of 50
Which Socket.IO method is used to send a message to everyone except the sender?
Not quite. Try another option.
Answer: socket.broadcast.emit()
The 'broadcast' flag causes the message to be sent to all clients other than the socket that initiated the event.
Question 10 of 50
In Express.js, what is 'middleware'?
Not quite. Try another option.
Answer: Functions that have access to the request and response objects
Middleware functions access the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle.
Question 11 of 50
What is the purpose of the 'next' function in Express middleware?
Not quite. Try another option.
Answer: To pass control to the next middleware function
If the current middleware does not end the request-response cycle, it must call next() to pass control to the next middleware function, otherwise the request hangs.
Question 12 of 50
Which Express method is used to mount middleware functions at a specific path?
Not quite. Try another option.
Answer: app.use()
app.use() is the standard method to mount middleware functions. If a path is specified, the middleware applies to requests starting with that path.
Question 13 of 50
What does app.all('/api/*', ...) do?
Not quite. Try another option.
Answer: Handles all HTTP methods (GET, POST, etc.) for routes starting with /api/
app.all() is a special routing method used to load middleware functions at a path for all HTTP request methods.
Question 14 of 50
What is the primary purpose of the cookie-parser middleware?
Not quite. Try another option.
Answer: To populate req.cookies with an object keyed by the cookie names
cookie-parser parses the Cookie header and populates req.cookies with an object keyed by the cookie names.
Question 15 of 50
Where does cookie-session store the session data?
Not quite. Try another option.
Answer: On the client-side within the cookie itself
Unlike express-session, cookie-session stores the entire session data directly in the cookie on the client (usually serialized and signed).
Question 16 of 50
Where does express-session store the session data by default?
Not quite. Try another option.
Answer: In server-side memory (MemoryStore)
By default, express-session uses MemoryStore to store session data on the server, sending only a Session ID to the client.
Question 17 of 50
Which option allows Socket.IO clients to join a specific channel or 'room'?
Not quite. Try another option.
Answer: socket.join('room1')
The socket.join() method is used to subscribe a specific socket to a given channel/room.
Question 18 of 50
In Socket.IO, how do you send a message only to clients in a specific room?
Not quite. Try another option.
Answer: io.to('roomName').emit('event', data)
io.to('roomName').emit() (or io.in) targets a specific room and sends the message to all sockets joined to that room.
Question 19 of 50
What argument is required to initialize cookie-parser if you want to use signed cookies?
Not quite. Try another option.
Answer: A secret string
To sign cookies (verifying their integrity), cookie-parser requires a secret string as the first argument (e.g., cookieParser('mySecret')).
Question 20 of 50
When using express-session, which property is added to the request object?
Not quite. Try another option.
Answer: req.session
express-session middleware adds a session object to req, allowing you to get or set session variables (e.g., req.session.user).
Question 21 of 50
Which express-session option forces the session to be saved back to the store, even if it wasn't modified?
Not quite. Try another option.
Answer: resave
The 'resave' option forces the session to be saved back to the session store, even if the session was never modified during the request.
Question 22 of 50
In middleware, what happens if you call next() with an error argument, like next(new Error('Fail'))?
Not quite. Try another option.
Answer: It skips to the next error-handling middleware
Calling next() with an argument is interpreted as an error, and Express will skip remaining non-error middleware to find an error-handling middleware.
Question 23 of 50
What is the signature of a standard Express error-handling middleware function?
Not quite. Try another option.
Answer: (err, req, res, next)
Error-handling middleware must provide four arguments to identify it as an error handler: (err, req, res, next).
Question 24 of 50
To serve static files such as images or CSS files in Express, which built-in middleware is used?
Not quite. Try another option.
Answer: express.static()
express.static(root, [options]) is the built-in middleware function used to serve static files.
Question 25 of 50
Which library is the client-side counterpart to the Node.js 'socket.io' package?
Not quite. Try another option.
Answer: socket.io-client
The 'socket.io-client' library is used in the browser to connect to a Socket.IO server.
Question 26 of 50
In a WebSocket 'message' event, the data received is typically:
Not quite. Try another option.
Answer: Always a String or Buffer
Raw WebSockets transmit data as strings or binary buffers. If you send JSON, you must parse the string manually on the receiving end.
Question 27 of 50
Which cookie-session option helps prevent Cross-Site Scripting (XSS) attacks by preventing client-side scripts from accessing the cookie?
Not quite. Try another option.
Answer: httpOnly: true
The httpOnly flag ensures the cookie is sent only over HTTP(S), not accessible via client-side JavaScript (document.cookie).
Question 28 of 50
If you want middleware to run for every request to the application, how should you mount it?
Not quite. Try another option.
Answer: app.use(middleware)
Calling app.use() without a path argument defaults to '/', meaning the middleware is executed for every request.
Question 29 of 50
What is the default name of the cookie used by express-session to store the session ID?
Not quite. Try another option.
Answer: connect.sid
The default name for the session ID cookie in express-session is 'connect.sid'.
Question 30 of 50
In Socket.IO, what is a 'Namespace'?
Not quite. Try another option.
Answer: A separate communication channel allowing multiplexing over a single connection
Namespaces allow you to split the logic of your application over a single shared connection (e.g., /admin, /chat).
Question 31 of 50
How do you access signed cookies in Express if cookie-parser is set up with a secret?
Not quite. Try another option.
Answer: req.signedCookies
When a secret is provided to cookie-parser, signed cookies are available in the req.signedCookies object.
Question 32 of 50
Which method removes a specific session in express-session?
Not quite. Try another option.
Answer: req.session.destroy()
req.session.destroy(callback) is used to destroy the session, removing it from the store.
Question 33 of 50
When using app.use(bodyParser.json()), what does this middleware do?
Not quite. Try another option.
Answer: Parses incoming requests with JSON payloads
It parses incoming request bodies in a middleware before your handlers, available under the req.body property.
Question 34 of 50
In Socket.IO, what does the volatile flag do? e.g., socket.volatile.emit(...)
Not quite. Try another option.
Answer: Sends the message but allows it to be lost if the client is not ready
Volatile messages may be dropped if the underlying transport is not ready/open; useful for high-frequency data like mouse movements where missing one packet is acceptable.
Question 35 of 50
What is the main disadvantage of cookie-session compared to express-session?
Not quite. Try another option.
Answer: It cannot store large amounts of data (limited by cookie size)
Cookies are limited in size (typically 4KB). cookie-session stores all data in the cookie, whereas express-session only stores an ID.
Question 36 of 50
Which event is fired in Socket.IO when a client disconnects?
Not quite. Try another option.
Answer: disconnect
The 'disconnect' event is fired upon disconnection.
Question 37 of 50
How can you limit middleware to a specific HTTP verb (e.g., POST) for a specific path?
Not quite. Try another option.
Answer: app.post('/path', middleware)
Using app.post() applies the middleware/handler only to HTTP POST requests for that specific path.
Question 38 of 50
What is a 'room' in Socket.IO primarily used for?
Not quite. Try another option.
Answer: Partitioning connected clients into groups
Rooms are arbitrary channels that sockets can join and leave, allowing you to broadcast data to a subset of clients.
Question 39 of 50
If you want to persist login state across server restarts effectively, which session store should you use with express-session?
Not quite. Try another option.
Answer: A compatible store like Redis or MongoDB
MemoryStore leaks memory and clears sessions on restart. For production/persistence, a database store like Redis or MongoDB is required.
Question 40 of 50
What is the output of typeof req.session inside a route handler when using express-session?
Not quite. Try another option.
Answer: object
req.session is an object where you can read/write properties.
Question 41 of 50
Which middleware would you use to log details of every incoming request (method, URL, etc.)?
Not quite. Try another option.
Answer: morgan
Morgan is a popular HTTP request logger middleware for Node.js.
Question 42 of 50
In a WebSocket connection, what property indicates the current state (connecting, open, closing, closed)?
Not quite. Try another option.
Answer: socket.readyState
The readyState property returns the current state of the WebSocket connection (0: CONNECTING, 1: OPEN, 2: CLOSING, 3: CLOSED).
Question 43 of 50
When initializing express-session, what does the saveUninitialized: false option do?
Not quite. Try another option.
Answer: It prevents saving a session to the store if it is new but not modified
This is useful for implementing login sessions, reducing storage usage, or complying with laws that require permission before setting cookies.
Question 44 of 50
Which function allows multiple middleware functions to be applied to a single route?
Not quite. Try another option.
Answer: Passing them as comma-separated arguments to the route method
Nearly through. Did unit 3 of INT222 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 3 of INT222 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill;
the rest comes out of a student's own pocket: the domain, the storage, and keeping
the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason.
to try again.