1What is the primary communication protocol used by WebSockets?
A.UDP
B.HTTP
C.TCP
D.FTP
Correct Answer: TCP
Explanation:
WebSockets provide full-duplex communication channels over a single TCP connection.
Incorrect! Try again.
2Which URL scheme is used for an unencrypted WebSocket connection?
A.wss://
B.http://
C.ftp://
D.ws://
Correct Answer: ws://
Explanation:
The 'ws://' scheme is used for unencrypted WebSocket connections, while 'wss://' is used for encrypted connections.
Incorrect! Try again.
3Unlike HTTP, a WebSocket connection is:
A.Full-duplex and persistent
B.Unreliable
C.Stateless
D.One-way only
Correct Answer: Full-duplex and persistent
Explanation:
WebSockets allow data to flow in both directions (full-duplex) and remain open until closed (persistent), unlike the request-response model of HTTP.
Incorrect! Try again.
4Which HTTP header is essential for the WebSocket handshake process?
A.Authorization
B.Upgrade
C.Content-Type
D.Accept
Correct Answer: Upgrade
Explanation:
The 'Upgrade' header is sent by the client to request that the server switch the connection protocol from HTTP to WebSocket.
Incorrect! Try again.
5In a basic Node.js 'ws' server, which event is triggered when a client connects?
A.init
B.connection
C.open
D.connect
Correct Answer: connection
Explanation:
The 'connection' event is emitted by the WebSocket server when a new handshake is successfully completed.
Incorrect! Try again.
6Which method is used to send a message from the server to a specific client instance in the 'ws' library?
A.client.push()
B.client.emit()
C.client.write()
D.client.send()
Correct Answer: client.send()
Explanation:
In the standard 'ws' library, the .send() method is used to transmit data to the connected client.
Incorrect! Try again.
7What does Socket.IO provide that native WebSockets do not handle automatically?
A.TCP connections
B.Automatic reconnection and fallback support
C.HTTP requests
D.JSON parsing
Correct Answer: Automatic reconnection and fallback support
Explanation:
Socket.IO provides features like automatic reconnection, fallbacks to long-polling if WebSockets aren't supported, and broadcasting.
Incorrect! Try again.
8In Socket.IO, how do you send a message to all connected clients?
A.io.sendToAll(data)
B.socket.emit('msg', data)
C.io.emit('msg', data)
D.socket.broadcast.emit('msg', data)
Correct Answer: io.emit('msg', data)
Explanation:
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.
Incorrect! Try again.
9Which Socket.IO method is used to send a message to everyone except the sender?
A.io.emit()
B.socket.toAll()
C.socket.broadcast.emit()
D.socket.send()
Correct Answer: socket.broadcast.emit()
Explanation:
The 'broadcast' flag causes the message to be sent to all clients other than the socket that initiated the event.
Incorrect! Try again.
10In Express.js, what is 'middleware'?
A.A CSS preprocessor
B.A database driver
C.A front-end framework
D.Functions that have access to the request and response objects
Correct Answer: Functions that have access to the request and response objects
Explanation:
Middleware functions access the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle.
Incorrect! Try again.
11What is the purpose of the 'next' function in Express middleware?
A.To pass control to the next middleware function
B.To restart the server
C.To send the response immediately
D.To redirect the user
Correct Answer: To pass control to the next middleware function
Explanation:
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.
Incorrect! Try again.
12Which Express method is used to mount middleware functions at a specific path?
A.app.middleware()
B.app.use()
C.app.mount()
D.app.bind()
Correct Answer: app.use()
Explanation:
app.use() is the standard method to mount middleware functions. If a path is specified, the middleware applies to requests starting with that path.
Incorrect! Try again.
13What does app.all('/api/*', ...) do?
A.Handles only GET requests for /api/
B.Handles all HTTP methods (GET, POST, etc.) for routes starting with /api/
C.Handles only POST requests for /api/
D.Handles requests from all IP addresses
Correct Answer: Handles all HTTP methods (GET, POST, etc.) for routes starting with /api/
Explanation:
app.all() is a special routing method used to load middleware functions at a path for all HTTP request methods.
Incorrect! Try again.
14What is the primary purpose of the cookie-parser middleware?
A.To encrypt cookies automatically
B.To eat cookies
C.To populate req.cookies with an object keyed by the cookie names
D.To delete all cookies on the client
Correct Answer: To populate req.cookies with an object keyed by the cookie names
Explanation:
cookie-parser parses the Cookie header and populates req.cookies with an object keyed by the cookie names.
Incorrect! Try again.
15Where does cookie-session store the session data?
A.In a Redis cache
B.In the server's memory
C.On the client-side within the cookie itself
D.On the server's database
Correct Answer: On the client-side within the cookie itself
Explanation:
Unlike express-session, cookie-session stores the entire session data directly in the cookie on the client (usually serialized and signed).
Incorrect! Try again.
16Where does express-session store the session data by default?
A.On the client browser
B.In server-side memory (MemoryStore)
C.In a text file
D.In the URL
Correct Answer: In server-side memory (MemoryStore)
Explanation:
By default, express-session uses MemoryStore to store session data on the server, sending only a Session ID to the client.
Incorrect! Try again.
17Which option allows Socket.IO clients to join a specific channel or 'room'?
A.io.join('room1')
B.socket.room('room1')
C.socket.enter('room1')
D.socket.join('room1')
Correct Answer: socket.join('room1')
Explanation:
The socket.join() method is used to subscribe a specific socket to a given channel/room.
Incorrect! Try again.
18In Socket.IO, how do you send a message only to clients in a specific room?
io.to('roomName').emit() (or io.in) targets a specific room and sends the message to all sockets joined to that room.
Incorrect! Try again.
19What argument is required to initialize cookie-parser if you want to use signed cookies?
A.The session ID
B.A file path
C.A database connection
D.A secret string
Correct Answer: A secret string
Explanation:
To sign cookies (verifying their integrity), cookie-parser requires a secret string as the first argument (e.g., cookieParser('mySecret')).
Incorrect! Try again.
20When using express-session, which property is added to the request object?
A.req.session
B.req.cookies
C.req.storage
D.req.cache
Correct Answer: req.session
Explanation:
express-session middleware adds a session object to req, allowing you to get or set session variables (e.g., req.session.user).
Incorrect! Try again.
21Which express-session option forces the session to be saved back to the store, even if it wasn't modified?
A.resave
B.saveUninitialized
C.cookie
D.secret
Correct Answer: resave
Explanation:
The 'resave' option forces the session to be saved back to the session store, even if the session was never modified during the request.
Incorrect! Try again.
22In middleware, what happens if you call next() with an error argument, like next(new Error('Fail'))?
A.The server crashes
B.It skips to the next error-handling middleware
C.It skips to the next non-error middleware
D.It reloads the page
Correct Answer: It skips to the next error-handling middleware
Explanation:
Calling next() with an argument is interpreted as an error, and Express will skip remaining non-error middleware to find an error-handling middleware.
Incorrect! Try again.
23What is the signature of a standard Express error-handling middleware function?
A.(err, req, res)
B.(err, req, res, next)
C.(req, res, next)
D.(req, res)
Correct Answer: (err, req, res, next)
Explanation:
Error-handling middleware must provide four arguments to identify it as an error handler: (err, req, res, next).
Incorrect! Try again.
24To serve static files such as images or CSS files in Express, which built-in middleware is used?
A.express.assets()
B.express.files()
C.express.static()
D.express.public()
Correct Answer: express.static()
Explanation:
express.static(root, [options]) is the built-in middleware function used to serve static files.
Incorrect! Try again.
25Which library is the client-side counterpart to the Node.js 'socket.io' package?
A.socket-client.js
B.socket.io-client
C.websocket.js
D.ws-client
Correct Answer: socket.io-client
Explanation:
The 'socket.io-client' library is used in the browser to connect to a Socket.IO server.
Incorrect! Try again.
26In a WebSocket 'message' event, the data received is typically:
A.Always an Integer
B.Always an Array
C.Always a String or Buffer
D.Always a JSON object
Correct Answer: Always a String or Buffer
Explanation:
Raw WebSockets transmit data as strings or binary buffers. If you send JSON, you must parse the string manually on the receiving end.
Incorrect! Try again.
27Which cookie-session option helps prevent Cross-Site Scripting (XSS) attacks by preventing client-side scripts from accessing the cookie?
A.secure: true
B.maxAge: 1000
C.httpOnly: true
D.signed: true
Correct Answer: httpOnly: true
Explanation:
The httpOnly flag ensures the cookie is sent only over HTTP(S), not accessible via client-side JavaScript (document.cookie).
Incorrect! Try again.
28If you want middleware to run for every request to the application, how should you mount it?
A.app.use('/home', middleware)
B.app.use(middleware)
C.app.get(middleware)
D.app.post(middleware)
Correct Answer: app.use(middleware)
Explanation:
Calling app.use() without a path argument defaults to '/', meaning the middleware is executed for every request.
Incorrect! Try again.
29What is the default name of the cookie used by express-session to store the session ID?
A.session_id
B.jsessionid
C.connect.sid
D.express.sid
Correct Answer: connect.sid
Explanation:
The default name for the session ID cookie in express-session is 'connect.sid'.
Incorrect! Try again.
30In Socket.IO, what is a 'Namespace'?
A.A type of error
B.A separate communication channel allowing multiplexing over a single connection
C.A database table
D.A variable name
Correct Answer: A separate communication channel allowing multiplexing over a single connection
Explanation:
Namespaces allow you to split the logic of your application over a single shared connection (e.g., /admin, /chat).
Incorrect! Try again.
31How do you access signed cookies in Express if cookie-parser is set up with a secret?
A.req.cookies
B.req.signedCookies
C.req.secureCookies
D.req.secretCookies
Correct Answer: req.signedCookies
Explanation:
When a secret is provided to cookie-parser, signed cookies are available in the req.signedCookies object.
Incorrect! Try again.
32Which method removes a specific session in express-session?
A.req.session.remove()
B.req.session.destroy()
C.req.session.clear()
D.req.session.delete()
Correct Answer: req.session.destroy()
Explanation:
req.session.destroy(callback) is used to destroy the session, removing it from the store.
Incorrect! Try again.
33When using app.use(bodyParser.json()), what does this middleware do?
A.Encrypts JSON data
B.Parses incoming requests with JSON payloads
C.Sends JSON responses
D.Validates JSON syntax only
Correct Answer: Parses incoming requests with JSON payloads
Explanation:
It parses incoming request bodies in a middleware before your handlers, available under the req.body property.
Incorrect! Try again.
34In Socket.IO, what does the volatile flag do? e.g., socket.volatile.emit(...)
A.Sends the message but allows it to be lost if the client is not ready
B.Ensures the message is encrypted
C.Deletes the message immediately after sending
D.Sends the message repeatedly until acknowledged
Correct Answer: Sends the message but allows it to be lost if the client is not ready
Explanation:
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.
Incorrect! Try again.
35What is the main disadvantage of cookie-session compared to express-session?
A.It cannot store large amounts of data (limited by cookie size)
B.It is slower
C.It only works with HTTPS
D.It requires a database
Correct Answer: It cannot store large amounts of data (limited by cookie size)
Explanation:
Cookies are limited in size (typically 4KB). cookie-session stores all data in the cookie, whereas express-session only stores an ID.
Incorrect! Try again.
36Which event is fired in Socket.IO when a client disconnects?
A.halt
B.disconnect
C.end
D.stop
Correct Answer: disconnect
Explanation:
The 'disconnect' event is fired upon disconnection.
Incorrect! Try again.
37How can you limit middleware to a specific HTTP verb (e.g., POST) for a specific path?
A.app.use()
B.app.post('/path', middleware)
C.app.route()
D.app.all()
Correct Answer: app.post('/path', middleware)
Explanation:
Using app.post() applies the middleware/handler only to HTTP POST requests for that specific path.
Incorrect! Try again.
38What is a 'room' in Socket.IO primarily used for?
A.Logging errors
B.Authentication
C.Partitioning connected clients into groups
D.Storing data permanently
Correct Answer: Partitioning connected clients into groups
Explanation:
Rooms are arbitrary channels that sockets can join and leave, allowing you to broadcast data to a subset of clients.
Incorrect! Try again.
39If you want to persist login state across server restarts effectively, which session store should you use with express-session?
A.MemoryStore
B.A compatible store like Redis or MongoDB
C.A text file
D.An array variable
Correct Answer: A compatible store like Redis or MongoDB
Explanation:
MemoryStore leaks memory and clears sessions on restart. For production/persistence, a database store like Redis or MongoDB is required.
Incorrect! Try again.
40What is the output of typeof req.session inside a route handler when using express-session?
A.undefined
B.function
C.string
D.object
Correct Answer: object
Explanation:
req.session is an object where you can read/write properties.
Incorrect! Try again.
41Which middleware would you use to log details of every incoming request (method, URL, etc.)?
A.cookie-parser
B.express-session
C.helmet
D.morgan
Correct Answer: morgan
Explanation:
Morgan is a popular HTTP request logger middleware for Node.js.
Incorrect! Try again.
42In a WebSocket connection, what property indicates the current state (connecting, open, closing, closed)?
A.socket.state
B.socket.connStatus
C.socket.readyState
D.socket.status
Correct Answer: socket.readyState
Explanation:
The readyState property returns the current state of the WebSocket connection (0: CONNECTING, 1: OPEN, 2: CLOSING, 3: CLOSED).
Incorrect! Try again.
43When initializing express-session, what does the saveUninitialized: false option do?
A.It saves empty sessions to the store
B.It deletes the session immediately
C.It prevents saving a session to the store if it is new but not modified
D.It saves the session without a cookie
Correct Answer: It prevents saving a session to the store if it is new but not modified
Explanation:
This is useful for implementing login sessions, reducing storage usage, or complying with laws that require permission before setting cookies.
Incorrect! Try again.
44Which function allows multiple middleware functions to be applied to a single route?
A.Using app.double()
B.They cannot be chained
C.Creating a loop
D.Passing them as comma-separated arguments to the route method
Correct Answer: Passing them as comma-separated arguments to the route method
Correct Answer: A unique identifier for the socket session
Explanation:
Each new connection is assigned a random, unique 20-character identifier.
Incorrect! Try again.
46If you wish to send binary data (like an image) via ws library, what type of data should you pass to .send()?
A.A Buffer or ArrayBuffer
B.An Object
C.A JSON string
D.A Boolean
Correct Answer: A Buffer or ArrayBuffer
Explanation:
WebSockets support binary data transmission using Buffers or ArrayBuffers.
Incorrect! Try again.
47What is the purpose of the 'secret' used in session middleware?
A.To authenticate users
B.To sign the session ID cookie to prevent tampering
C.To encrypt the database connection
D.To hide the server IP
Correct Answer: To sign the session ID cookie to prevent tampering
Explanation:
The secret is used to compute a hash (signature) of the session ID cookie. If the client modifies the cookie, the signature won't match.
Incorrect! Try again.
48Can middleware modify the req and res objects?
A.No, they are read-only
B.Only res can be modified
C.Only req can be modified
D.Yes, middleware can add properties or methods to them
Correct Answer: Yes, middleware can add properties or methods to them
Explanation:
It is common practice for middleware to attach data to req (like req.user or req.session) for use in subsequent handlers.
Incorrect! Try again.
49In a Chat Server, why would you store socket.id mapped to a username?
A.To increase download speed
B.To use less memory
C.To enable private messaging functionality
D.To prevent the user from logging out
Correct Answer: To enable private messaging functionality
Explanation:
To send a private message, the server needs to know which specific socket ID corresponds to a specific username.
Incorrect! Try again.
50What happens if you attempt to use req.body without installing and using a body-parsing middleware (like express.json())?
A.It will contain the raw stream
B.The server will crash
C.req.body will be undefined
D.It will work automatically
Correct Answer: req.body will be undefined
Explanation:
Express does not parse the request body by default; req.body is undefined without middleware like express.json() or express.urlencoded().
Incorrect! Try again.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. 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.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it.
What it pays for →