Unit 3: Socket Services in Node.js & Creating middlewares - Practice Quiz

INT222 — Advanced Web Development 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 What is the primary communication protocol used by WebSockets?

A. UDP
B. HTTP
C. TCP
D. FTP

2 Which URL scheme is used for an unencrypted WebSocket connection?

A. wss://
B. http://
C. ftp://
D. ws://

3 Unlike HTTP, a WebSocket connection is:

A. Full-duplex and persistent
B. Unreliable
C. Stateless
D. One-way only

4 Which HTTP header is essential for the WebSocket handshake process?

A. Authorization
B. Upgrade
C. Content-Type
D. Accept

5 In a basic Node.js 'ws' server, which event is triggered when a client connects?

A. init
B. connection
C. open
D. connect

6 Which 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()

7 What 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

8 In 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)

9 Which 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()

10 In 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

11 What 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

12 Which Express method is used to mount middleware functions at a specific path?

A. app.middleware()
B. app.use()
C. app.mount()
D. app.bind()

13 What 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

14 What 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

15 Where 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

16 Where 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

17 Which 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')

18 In Socket.IO, how do you send a message only to clients in a specific room?

A. io.to('roomName').emit('event', data)
B. socket.send('roomName', data)
C. io.emit('roomName', data)
D. io.in('roomName').broadcast(data)

19 What 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

20 When using express-session, which property is added to the request object?

A. req.session
B. req.cookies
C. req.storage
D. req.cache

21 Which 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

22 In 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

23 What 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)

24 To 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()

25 Which 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

26 In 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

27 Which 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

28 If 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)

29 What 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

30 In 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

31 How 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

32 Which method removes a specific session in express-session?

A. req.session.remove()
B. req.session.destroy()
C. req.session.clear()
D. req.session.delete()

33 When 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

34 In 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

35 What 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

36 Which event is fired in Socket.IO when a client disconnects?

A. halt
B. disconnect
C. end
D. stop

37 How 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()

38 What is a 'room' in Socket.IO primarily used for?

A. Logging errors
B. Authentication
C. Partitioning connected clients into groups
D. Storing data permanently

39 If 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

40 What is the output of typeof req.session inside a route handler when using express-session?

A. undefined
B. function
C. string
D. object

41 Which middleware would you use to log details of every incoming request (method, URL, etc.)?

A. cookie-parser
B. express-session
C. helmet
D. morgan

42 In 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

43 When 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

44 Which 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

45 In Socket.IO, socket.id represents:

A. The port number
B. A unique identifier for the socket session
C. The IP address
D. The user's database ID

46 If 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

47 What 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

48 Can 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

49 In 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

50 What 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