MEAN Trivia 3 — Questions and Answers
Question 1: In Mongoose, what method is used to find a single document by its _id field?
- findOne()
- findById() (Correct answer)
- findByPk()
- getById()
Correct answer: findById()
Mongoose's findById() is a shorthand for findOne({ _id: id }) and is the idiomatic way to fetch by ID.
Question 2: Which Angular module must be imported to use reactive forms?
- FormsModule
- HttpClientModule
- ReactiveFormsModule (Correct answer)
- CommonModule
Correct answer: ReactiveFormsModule
ReactiveFormsModule provides FormGroup, FormControl, and FormBuilder for reactive form handling.
Question 3: What does `npm init -y` do when setting up a new Node.js project?
- Installs all dependencies
- Creates package.json with default values (Correct answer)
- Starts the Node server
- Links global packages
Correct answer: Creates package.json with default values
`npm init -y` creates a package.json file with default values without prompting the user.
Question 4: In Express.js, what does `res.json()` do differently from `res.send()`?
- It sets the status code to 201
- It automatically sets Content-Type to application/json (Correct answer)
- It compresses the response
- It enables CORS headers
Correct answer: It automatically sets Content-Type to application/json
res.json() automatically sets the Content-Type header to application/json and JSON-stringifies the object.
Question 5: Which MongoDB operator is used to match documents where a field value is in a given array?
- $contains
- $in (Correct answer)
- $has
- $within
Correct answer: $in
The $in operator selects documents where the field value equals any value in the specified array.
Question 6: What is the Angular CLI command to generate a new service?
- ng new service
- ng create service
- ng generate service (Correct answer)
- ng make service
Correct answer: ng generate service
`ng generate service <name>` (or `ng g s <name>`) creates a new injectable service class.
Question 7: Which Node.js built-in module is used to work with file and directory paths?
- fs
- url
- path (Correct answer)
- os
Correct answer: path
The `path` module provides utilities for working with file and directory paths in a cross-platform way.
In Mongoose, what method is used to find a single document by its _id field?