MEAN Stack Development 5 — Questions and Answers
Question 1: In Angular, what is the difference between `Observable` and `Promise`?
- Promises support multiple values; Observables support only one
- Observables are lazy and can emit multiple values over time; Promises resolve once (Correct answer)
- Observables are synchronous; Promises are asynchronous
- There is no difference — they are interchangeable
Correct answer: Observables are lazy and can emit multiple values over time; Promises resolve once
Observables from RxJS are lazy streams that can emit multiple values and be cancelled, whereas Promises eagerly resolve to a single value.
Question 2: What is the purpose of MongoDB indexes, and what is the trade-off of adding too many?
- Indexes compress data to save disk space; too many reduce storage efficiency
- Indexes speed up query performance; too many slow down write operations (Correct answer)
- Indexes enforce data validation; too many increase memory usage only
- Indexes shard data across nodes; too many cause replication lag
Correct answer: Indexes speed up query performance; too many slow down write operations
Indexes dramatically speed up read queries by allowing MongoDB to avoid full collection scans, but each index must be updated on every write, increasing write overhead.
Question 3: Which Angular CLI command generates a new service called `data`?
- ng create service data
- ng generate service data (Correct answer)
- ng new service data
- ng add service data
Correct answer: ng generate service data
`ng generate service data` (or `ng g s data`) creates a new service file with the @Injectable decorator pre-configured.
Question 4: In Express.js, how do you access JSON data sent in a POST request body?
- req.params.body
- req.query
- req.body (after using express.json() middleware) (Correct answer)
- req.data
Correct answer: req.body (after using express.json() middleware)
After applying the express.json() middleware, parsed JSON from the request body is available on req.body.
Question 5: What does the Mongoose `populate()` method do?
- Inserts seed data into a collection
- Replaces ObjectId references with the actual referenced documents (Correct answer)
- Creates a full-text search index
- Validates schema constraints before saving
Correct answer: Replaces ObjectId references with the actual referenced documents
populate() automatically replaces ObjectId reference fields with the full document data from the referenced collection, similar to a JOIN.
Question 6: Which Node.js global object provides information about the current process, such as environment variables?
- global
- window
- process (Correct answer)
- runtime
Correct answer: process
The `process` global object provides access to environment variables via process.env, command-line arguments via process.argv, and process lifecycle methods.
Question 7: In the MEAN stack, what is the typical role of an Angular route guard (`CanActivate`)?
- To preload data before a component renders
- To animate route transitions
- To prevent unauthorized users from accessing certain routes (Correct answer)
- To cache API responses for offline use
Correct answer: To prevent unauthorized users from accessing certain routes
CanActivate guards intercept navigation to a route and return true or false based on logic such as checking if a user is authenticated.
In Angular, what is the difference between `Observable` and `Promise`?