Unit 3 - Practice Quiz

INT222 50 Questions
0 Correct 0 Wrong 50 Left
0/50

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. ftp://
B. http://
C. ws://
D. wss://

3 Unlike HTTP, a WebSocket connection is:

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

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

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

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

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

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

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

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

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

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

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 database driver
D. A CSS preprocessor

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

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

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

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

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

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

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

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

15 Where does cookie-session store the session data?

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

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

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

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

A. io.join('room1')
B. socket.room('room1')
C. socket.join('room1')
D. socket.enter('room1')

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

A. io.emit('roomName', data)
B. socket.send('roomName', data)
C. io.to('roomName').emit('event', 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 secret string
B. The session ID
C. A database connection
D. A file path

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

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

21 Which express-session option forces the session to be saved back to the store, even if it wasn't modified?

A. secret
B. cookie
C. resave
D. saveUninitialized

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

A. It skips to the next non-error middleware
B. The server crashes
C. It reloads the page
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.public()
B. express.static()
C. express.assets()
D. express.files()

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

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

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

A. Always a String or Buffer
B. Always an Integer
C. Always an Array
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. signed: true
B. httpOnly: true
C. secure: 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.get(middleware)
C. app.use(middleware)
D. app.post(middleware)

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

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

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

A. A separate communication channel allowing multiplexing over a single connection
B. A type of error
C. A variable name
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.secureCookies
C. req.signedCookies
D. req.secretCookies

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

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

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

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

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

A. Ensures the message is encrypted
B. Deletes the message immediately after sending
C. Sends the message repeatedly until acknowledged
D. Sends the message but allows it to be lost if the client is not ready

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

A. It prevents saving a session to the store if it is new but not modified
B. It saves empty sessions to the store
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. Passing them as comma-separated arguments to the route method
B. Using app.double()
C. Creating a loop
D. They cannot be chained

45 In Socket.IO, socket.id represents:

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

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 JSON string
B. A Boolean
C. A Buffer or ArrayBuffer
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. Only res can be modified
B. Yes, middleware can add properties or methods to them
C. No, they are read-only
D. Only req 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 increase download speed
C. To enable private messaging functionality
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. req.body will be undefined
B. It will work automatically
C. The server will crash
D. It will contain the raw stream