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. FTP
B. TCP
C. HTTP
D. UDP

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

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

3 Unlike HTTP, a WebSocket connection is:

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

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

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

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

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

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.send()
C. client.write()
D. client.emit()

7 What does Socket.IO provide that native WebSockets do not handle automatically?

A. HTTP requests
B. Automatic reconnection and fallback support
C. JSON parsing
D. TCP connections

8 In Socket.IO, how do you send a message to all connected clients?

A. io.sendToAll(data)
B. socket.broadcast.emit('msg', data)
C. io.emit('msg', data)
D. socket.emit('msg', data)

9 Which Socket.IO method is used to send a message to everyone except the sender?

A. socket.broadcast.emit()
B. io.emit()
C. socket.toAll()
D. socket.send()

10 In Express.js, what is 'middleware'?

A. A front-end framework
B. Functions that have access to the request and response objects
C. A CSS preprocessor
D. A database driver

11 What is the purpose of the 'next' function in Express middleware?

A. To redirect the user
B. To send the response immediately
C. To pass control to the next middleware function
D. To restart the server

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

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

13 What does app.all('/api/*', ...) do?

A. Handles all HTTP methods (GET, POST, etc.) for routes starting with /api/
B. Handles only POST requests for /api/
C. Handles only GET requests for /api/
D. Handles requests from all IP addresses

14 What is the primary purpose of the cookie-parser middleware?

A. To populate req.cookies with an object keyed by the cookie names
B. To eat cookies
C. To encrypt cookies automatically
D. To delete all cookies on the client

15 Where does cookie-session store the session data?

A. In the server's memory
B. On the client-side within the cookie itself
C. In a Redis cache
D. On the server's database

16 Where does express-session store the session data by default?

A. In the URL
B. In server-side memory (MemoryStore)
C. In a text file
D. On the client browser

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. io.emit('roomName', data)
C. io.in('roomName').broadcast(data)
D. socket.send('roomName', data)

19 What argument is required to initialize cookie-parser if you want to use signed cookies?

A. A file path
B. The session ID
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. saveUninitialized
B. resave
C. secret
D. cookie

22 In middleware, what happens if you call next() with an error argument, like next(new Error('Fail'))?

A. It reloads the page
B. It skips to the next non-error middleware
C. The server crashes
D. It skips to the next error-handling middleware

23 What is the signature of a standard Express error-handling middleware function?

A. (err, req, res, next)
B. (err, req, res)
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.files()
B. express.static()
C. express.assets()
D. express.public()

25 Which library is the client-side counterpart to the Node.js 'socket.io' package?

A. socket-client.js
B. websocket.js
C. socket.io-client
D. ws-client

26 In a WebSocket 'message' event, the data received is typically:

A. Always an Array
B. Always an Integer
C. Always a JSON object
D. Always a String or Buffer

27 Which cookie-session option helps prevent Cross-Site Scripting (XSS) attacks by preventing client-side scripts from accessing the cookie?

A. maxAge: 1000
B. secure: true
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.get(middleware)
B. app.post(middleware)
C. app.use(middleware)
D. app.use('/home', middleware)

29 What is the default name of the cookie used by express-session to store the session ID?

A. connect.sid
B. jsessionid
C. express.sid
D. session_id

30 In Socket.IO, what is a 'Namespace'?

A. A database table
B. A type of error
C. A separate communication channel allowing multiplexing over a single connection
D. A variable name

31 How do you access signed cookies in Express if cookie-parser is set up with a secret?

A. req.secureCookies
B. req.cookies
C. req.secretCookies
D. req.signedCookies

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

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

33 When using app.use(bodyParser.json()), what does this middleware do?

A. Validates JSON syntax only
B. Encrypts JSON data
C. Parses incoming requests with JSON payloads
D. Sends JSON responses

34 In Socket.IO, what does the volatile flag do? e.g., socket.volatile.emit(...)

A. Ensures the message is encrypted
B. Sends the message but allows it to be lost if the client is not ready
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 requires a database
B. It is slower
C. It only works with HTTPS
D. It cannot store large amounts of data (limited by cookie size)

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

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

37 How can you limit middleware to a specific HTTP verb (e.g., POST) for a specific path?

A. app.route()
B. app.all()
C. app.post('/path', middleware)
D. app.use()

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

A. Partitioning connected clients into groups
B. Logging errors
C. Authentication
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. An array variable
B. A text file
C. MemoryStore
D. A compatible store like Redis or MongoDB

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

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

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

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

42 In a WebSocket connection, what property indicates the current state (connecting, open, closing, closed)?

A. socket.status
B. socket.readyState
C. socket.connStatus
D. socket.state

43 When initializing express-session, what does the saveUninitialized: false option do?

A. It saves the session without a cookie
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 empty sessions to the store

44 Which function allows multiple middleware functions to be applied to a single route?

A. They cannot be chained
B. Using app.double()
C. Passing them as comma-separated arguments to the route method
D. Creating a loop

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 Boolean
B. A Buffer or ArrayBuffer
C. A JSON string
D. An Object

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 hide the server IP
D. To encrypt the database connection

48 Can middleware modify the req and res objects?

A. Only req can be modified
B. Yes, middleware can add properties or methods to them
C. No, they are read-only
D. Only res can be modified

49 In a Chat Server, why would you store socket.id mapped to a username?

A. To prevent the user from logging out
B. To use less memory
C. To enable private messaging functionality
D. To increase download speed

50 What happens if you attempt to use req.body without installing and using a body-parsing middleware (like express.json())?

A. The server will crash
B. req.body will be undefined
C. It will work automatically
D. It will contain the raw stream