Unit 3 - Practice Quiz

INT222

1 What is the primary communication protocol used by WebSockets?

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

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

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

3 Unlike HTTP, a WebSocket connection is:

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

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

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

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

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

6 Which method is used to send a message from the server to a specific client instance in the 'ws' library?

A. client.emit()
B. client.send()
C. client.write()
D. client.push()

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. socket.broadcast.emit('msg', data)
B. io.emit('msg', data)
C. socket.emit('msg', data)
D. io.sendToAll(data)

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

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

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

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

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

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

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

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

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

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

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

A. To eat cookies
B. To encrypt cookies automatically
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. On the server's database
B. In the server's memory
C. On the client-side within the cookie itself
D. In a Redis cache

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

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

17 Which option allows Socket.IO clients to join a specific channel or 'room'?

A. socket.enter('room1')
B. socket.join('room1')
C. io.join('room1')
D. socket.room('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. socket.send('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. A file path
B. A secret string
C. A database connection
D. The session ID

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

A. req.cookies
B. req.session
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. The server crashes
B. It skips to the next non-error middleware
C. It skips to the next error-handling middleware
D. It reloads the page

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

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

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

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

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

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

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. httpOnly: true
C. signed: true
D. maxAge: 1000

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. connect.sid
C. express.sid
D. jsessionid

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

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

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.secretCookies
D. req.secureCookies

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

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

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

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

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. Sends the message repeatedly until acknowledged
D. Deletes the message immediately after sending

35 What is the main disadvantage of cookie-session compared to express-session?

A. It is slower
B. It cannot store large amounts of data (limited by cookie size)
C. It requires a database
D. It only works with HTTPS

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

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

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.all()
D. app.route()

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

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

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. string
C. object
D. function

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

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

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

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

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

A. It saves empty sessions to the store
B. It prevents saving a session to the store if it is new but not modified
C. It deletes the session immediately
D. It saves the session without a cookie

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

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

45 In Socket.IO, socket.id represents:

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

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

47 What is the purpose of the 'secret' used in session middleware?

A. To encrypt the database connection
B. To sign the session ID cookie to prevent tampering
C. To hide the server IP
D. To authenticate users

48 Can middleware modify the req and res objects?

A. No, they are read-only
B. Yes, middleware can add properties or methods to them
C. Only req can be modified
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 enable private messaging functionality
C. To increase download speed
D. To use less memory

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

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