

















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A practice exam for server-side development using nodejs, express, and mongodb. It includes multiple-choice questions covering various aspects of these technologies, such as javascript engine usage, event loop phases, non-blocking i/o models, promise creation, es module imports, package.json configurations, and core node.js modules. The exam also tests knowledge of express middleware, http status codes, and mongodb commands, operators, and aggregation pipelines. Each question is accompanied by a detailed explanation of the correct answer, making it a valuable resource for exam preparation and skill enhancement. This practice exam is designed to help developers assess their understanding of server-side javascript development and database management.
Typology: Exams
1 / 89
This page cannot be seen from the preview
Don't miss anything!


















































































Question 1. Which JavaScript engine does Node.js use to execute code? A) SpiderMonkey B) V C) Chakra D) JavaScriptCore Answer: B Explanation: Node.js is built on Google’s V8 engine, the same engine that powers Chrome, providing fast execution of JavaScript on the server. Question 2. In the Node.js event loop, which phase handles timer callbacks scheduled by setTimeout and setInterval? A) poll B) timers C) check D) close callbacks Answer: B Explanation: The timers phase processes callbacks for timers whose time has expired. Question 3. What is the primary advantage of Node.js’s non‑blocking I/O model? A) It reduces memory usage by compiling code ahead of time. B) It allows the server to handle many connections concurrently without creating a new thread per request. C) It guarantees order of execution for all asynchronous calls. D) It converts JavaScript to native machine code. Answer: B
Explanation: Non‑blocking I/O lets a single thread manage many simultaneous connections, improving scalability. Question 4. Which of the following correctly creates a Promise that resolves with the value 10 after 1 second? A) new Promise((res, rej) => setTimeout(res(10), 1000)); B) new Promise((res, rej) => setTimeout(() => res(10), 1000)); C) Promise.resolve(() => setTimeout(10, 1000)); D) Promise(10).delay(1000); Answer: B Explanation: The executor receives res and rej; we must pass a function to setTimeout that calls res(10) after 1000 ms. Question 5. Which syntax correctly imports the default export from an ES module named utils.js? A) const utils = require('./utils'); B) import utils from './utils.js'; C) import { utils } from './utils.js'; D) const { default: utils } = require('./utils'); Answer: B Explanation: import utils from './utils.js' imports the default export using ES module syntax. Question 6. In a package.json file, which field lists packages required only for development and testing? A) dependencies B) devDependencies C) peerDependencies
C) A stream object that can be piped. D) It does not return anything; it only triggers a callback. Answer: B Explanation: The synchronous version returns the data directly; the asynchronous version uses a callback. Question 10. Which EventEmitter method registers a listener that will be invoked only once? A) on B) once C) addListener D) prependListener Answer: B Explanation: once registers a listener that automatically removes itself after the first invocation. Question 11. In a readable stream, which event signals that there is no more data to be consumed? A) data B) end C) close D) error Answer: B Explanation: The end event is emitted when the stream has been fully read. Question 12. Which of the following correctly sets up an Express server that listens on port 3000?
A) app.listen(3000, () => console.log('Server started')); B) http.createServer(app).listen(3000); C) express().start(3000); D) app.run(3000); Answer: A Explanation: app.listen is the standard Express method to start listening on a given port. Question 13. In Express, what does req.params.id represent when the route is defined as app.get('/users/:id', ...)? A) The query string value for id. B) The body parameter named id. C) The URL segment that matches :id. D) The HTTP header named id. Answer: C Explanation: req.params holds route parameters extracted from the URL path. Question 14. Which HTTP status code is most appropriate for a successful resource creation? A) 200 OK B) 201 Created C) 202 Accepted D) 204 No Content Answer: B Explanation: 201 Created indicates that a new resource has been successfully created. Question 15. Which Express middleware function parses incoming JSON payloads?
Question 18. In Express error‑handling middleware, how many arguments must the function accept? A) 2 B) 3 C) 4 D) 5 Answer: C Explanation: An error‑handling middleware is defined as function (err, req, res, next) – four arguments. Question 19. Which of the following correctly catches asynchronous errors in an Express route using async/await? A) try { await ... } catch (e) { next(e); } B) await ... .catch(next); C) router.get('/', async (req, res) => { await ... }); (no catch) D) process.on('uncaughtException', ...) Answer: A Explanation: Wrapping the await call in try...catch and passing the error to next forwards it to Express’s error handler. Question 20. MongoDB stores documents in which binary format? A) JSON B) BSON C) XML D) CSV Answer: B
Explanation: BSON (Binary JSON) is the native storage format for MongoDB documents. Question 21. Which MongoDB command inserts multiple documents into a collection in a single operation? A) insertOne B) insertMany C) bulkInsert D) save Answer: B Explanation: insertMany accepts an array of documents and inserts them atomically. Question 22. In MongoDB, what does the _id field represent? A) A user‑defined primary key. B) An automatically generated unique identifier of type ObjectId. C) The index of the document in the collection. D) The timestamp of the last update. Answer: B Explanation: _id is a mandatory unique field; if omitted, MongoDB generates an ObjectId. Question 23. Which query operator selects documents where the field age is greater than 30? A) { age: { $gt: 30 } } B) { age: { $lt: 30 } } C) { age: { $gte: 30 } } D) { age: { $eq: 30 } } Answer: A
D) Hashed index Answer: C Explanation: 2dsphere indexes support spherical geometry queries on GeoJSON data. Question 27. In the aggregation pipeline, which stage filters documents based on a condition? A) $match B) $group C) $project D) $sort Answer: A Explanation: $match works like a query filter, passing only matching documents to the next stage. Question 28. Which aggregation stage reshapes each document by adding, removing, or renaming fields? A) $group B) $project C) $unwind D) $lookup Answer: B Explanation: $project specifies the fields to include/exclude and can compute new fields. Question 29. What does the $lookup aggregation stage enable? A) Joining documents from the same collection. B) Performing a map‑reduce operation.
C) Joining documents from a different collection (left outer join). D) Sorting the pipeline output. Answer: C Explanation: $lookup performs a left outer join with another collection. Question 30. Which of the following is not a built‑in Node.js core module? A) crypto B) http C) express D) dns Answer: C Explanation: express is a third‑party package, not a core Node module. Question 31. What is the output of console.log(__dirname) inside a file located at /app/server/index.js? A) /app/server B) /app/server/index.js C) /app D) The current working directory (process.cwd()) Answer: A Explanation: __dirname gives the directory name of the current module file. Question 32. Which of the following statements about process.nextTick() is true? A) It schedules a callback to run after the current poll phase completes. B) It executes callbacks before the event loop continues to the next phase.
A) Resolves a sequence of path segments into an absolute path. B) Normalizes a path by removing redundant .. and . segments. C) Concatenates path segments using the appropriate platform separator. D) Parses a URL string into its components. Answer: C Explanation: path.join joins segments using the OS‑specific separator, handling trailing slashes. Question 36. Which Express setting disables the X-Powered-By: Express header for security hardening? A) app.set('trust proxy', true) B) app.disable('x-powered-by') C) app.use(helmet()) D) app.set('hidePoweredBy', true) Answer: B Explanation: app.disable('x-powered-by') removes the header that reveals the framework. Question 37. In Express, which method is used to serve static files from a directory named public? A) app.use('/static', express.static('public')) B) app.static('public') C) express.static('public') (without app.use) D) app.use(express.static('public')) Answer: D Explanation: app.use(express.static('public')) mounts the static middleware at the root path.
Question 38. Which HTTP verb is idempotent and typically used to replace an entire resource? A) POST B) GET C) PUT D) PATCH Answer: C Explanation: PUT is idempotent; multiple identical requests produce the same result, commonly used for full replacements. Question 39. Which MongoDB command removes all documents from a collection without dropping the collection itself? A) db.collection.drop() B) db.collection.remove({}) C) db.collection.deleteMany({}) D) db.collection.truncate() Answer: C Explanation: deleteMany({}) deletes every document matching the empty filter while keeping the collection and indexes. Question 40. In the aggregation pipeline, which stage deconstructs an array field from the input documents to output a document for each element? A) $unwind B) $group C) $facet D) $bucket Answer: A
Answer: A Explanation: $inc adds the provided amount to a numeric field. Question 44. What is the default port on which an Express app listens if app.listen() is called without arguments? A) 80 B) 3000 C) 8080 D) No default; it throws an error. Answer: D Explanation: app.listen() requires a port number; omitting it results in a TypeError. Question 45. Which of the following statements about callback hell is correct? A) It occurs when callbacks are nested deeply, making code hard to read and maintain. B) It is solved by using setTimeout with a zero delay. C) It can be avoided by using synchronous code only. D) It is a feature of the V8 engine. Answer: A Explanation: Deeply nested callbacks lead to “callback hell”; promises or async/await provide cleaner alternatives. Question 46. Which method of a Node.js EventEmitter returns the number of listeners for a given event? A) listenerCount(eventName) B) getListeners(eventName) C) count(eventName)
D) listeners(eventName).length Answer: A Explanation: EventEmitter.listenerCount(eventName) directly returns the count. Question 47. Which HTTP header indicates the origin of a cross‑site request and is commonly handled by the cors middleware? A) Access-Control-Allow-Origin B) Origin C) Referer D) X-Requested-With Answer: B Explanation: The Origin header is sent by browsers to indicate where the request originated; cors uses it to set Access-Control-Allow-Origin. Question 48. In MongoDB, which index option ensures that duplicate values are not allowed for the indexed field? A) { unique: true } B) { sparse: true } C) { background: true } D) { expireAfterSeconds: 0 } Answer: A Explanation: Setting unique: true creates a unique index that enforces uniqueness. Question 49. Which of the following correctly chains two promises so that the second runs after the first resolves? A) promise1.then(promise2);
B) process.on('unhandledRejection', handler) C) process.on('rejectionHandled', handler) D) process.on('error', handler) Answer: B Explanation: unhandledRejection fires when a promise is rejected and no .catch handler is attached. Question 53. In MongoDB, which stage of the aggregation pipeline calculates aggregate values such as sum, avg, max, etc., across groups? A) $group B) $sort C) $match D) $limit Answer: A Explanation: $group groups documents and can compute accumulator expressions like $sum. Question 54. Which fs method returns a stream that can be piped to another writable stream? A) fs.readFile B) fs.createReadStream C) fs.read D) fs.openSync Answer: B Explanation: fs.createReadStream creates a readable stream for large files.
Question 55. Which of the following is a correct way to define an environment variable for a Node.js app in a Unix shell? A) set NODE_ENV=production B) export NODE_ENV=production C) NODE_ENV=production node app.js (without export) D) ENV NODE_ENV=production Answer: B Explanation: export makes the variable available to child processes, including Node. Question 56. Which Express method is used to override the default HTTP response status code? A) res.setStatus() B) res.statusCode() C) res.status() D) res.sendStatus() Answer: C Explanation: res.status(code) sets the status for the response chain. Question 57. In MongoDB, what does the $text query operator require to function? A) A text index on the field(s) being searched. B) A hashed index. C) A geospatial index. D) No index; it scans the collection. Answer: A Explanation: $text searches only on fields covered by a text index.