1. Which of the following is the correct way to connect MongoDB to a Node.js application?
a) mongoose.connect('mongodb://localhost:27017/mydb')
b) mongodb.connect('mongodb://localhost:27017/mydb')
c) mongoose.connect('mongodb://localhost:27017')
d) mongodb.connect('localhost:27017/mydb')
Answer: a) mongoose.connect('mongodb://localhost:27017/mydb')
Explanation: The correct way to connect to MongoDB using mongoose in a Node.js application is by providing the correct MongoDB URI format.
2. What does dotenv do in a Node.js application?
a) It helps in defining routes for the application.
b) It loads environment variables from a .env file into process.env.
c) It helps in routing API calls.
d) It allows connecting to MongoDB.
Answer: b) It loads environment variables from a .env file into process.env.
Explanation: dotenv is a zero-dependency module used to load environment variables from a .env file into process.env.
3. What is the purpose of bcryptjs in the context of user authentication?
a) To hash and compare passwords.
b) To create JWT tokens.
c) To connect to MongoDB.
d) To encrypt all application data.
Answer: a) To hash and compare passwords.
Explanation: bcryptjs is used for hashing passwords securely and comparing hashed passwords during user authentication.
4. Which method is used to create a new user in MongoDB using Mongoose?
a) User.save()
b) User.create()
c) User.insert()
d) User.add()
Answer: b) User.create()
Explanation: User.create() is used to create and save a new document in the MongoDB collection.
5. In the JWT authentication process, which part of the JWT stores user information?
a) Header
b) Payload
c) Signature
d) Token Expiry
Answer: b) Payload
Explanation: The payload part of a JWT contains user-related information (like user ID) and other claims.
6. What does the cors middleware do in an Express application?
a) It handles file uploads.
b) It manages requests for MongoDB.
c) It allows or restricts resources based on the origin of the request.
d) It stores user sessions.
Answer: c) It allows or restricts resources based on the origin of the request.
Explanation: cors is used to handle Cross-Origin Resource Sharing (CORS) requests, allowing or denying access based on the origin.
7. In React, what is the purpose of useState?
a) To manage server-side state.
b) To create component-level state variables.
c) To manage HTTP requests.
d) To bind data between components.
Answer: b) To create component-level state variables.
Explanation: useState is a React hook used to create and manage local state in functional components.
8. What is the default port for a MongoDB database running locally?
a) 3306
b) 27017
c) 5432
d) 8080
Answer: b) 27017
Explanation: The default port for MongoDB running locally is 27017.
9. In which file do you typically store environment variables for your Node.js application?
a) config.js
b) .env
c) server.js
d) app.js
Answer: b) .env
Explanation: Environment variables are typically stored in the .env file for use in Node.js applications.
10. What is the purpose of JWT in user authentication?
a) To store passwords securely.
b) To prevent cross-site scripting attacks.
c) To transmit user information securely between client and server.
d) To manage database connections.
Answer: c) To transmit user information securely between client and server.
Explanation: JWT (JSON Web Token) is used to transmit user authentication and authorization information securely between client and server.
11. Which React hook is used to perform side effects in functional components?
a) useState
b) useEffect
c) useContext
d) useReducer
Answer: b) useEffect
Explanation: useEffect is used in React to perform side effects like data fetching, subscriptions, or manually changing the DOM.
12. What is the role of MongoDB in the MERN stack?
a) Frontend framework
b) Backend framework
c) Database
d) User authentication
Answer: c) Database
Explanation: MongoDB is the database used in the MERN stack (MongoDB, Express, React, Node.js).
13. How do you pass data between components in React?
a) Using useState
b) Using props
c) Using useEffect
d) Using local storage
Answer: b) Using props
Explanation: In React, data is passed between components using props.
14. Which method is used to compare passwords in the backend (Node.js) with bcryptjs?
a) bcrypt.compare()
b) bcrypt.hash()
c) bcrypt.verify()
d) bcrypt.match()
Answer: a) bcrypt.compare()
Explanation: bcrypt.compare() is used to compare a plain text password with a hashed password.
15. What is mongoose.Schema used for?
a) To connect to the MongoDB database.
b) To define the structure of documents in a MongoDB collection.
c) To query the database.
d) To update documents in MongoDB.
Answer: b) To define the structure of documents in a MongoDB collection.
Explanation: mongoose.Schema is used to define the structure of documents in MongoDB collections.
16. Which of the following is used to secure API routes in Express.js?
a) Middleware
b) Routes
c) Models
d) Controllers
Answer: a) Middleware
Explanation: Middleware is used in Express.js to add functionality such as authentication or authorization to API routes.
17. What HTTP method is typically used to delete data from a server?
a) GET
b) POST
c) DELETE
d) PUT
Answer: c) DELETE
Explanation: The DELETE HTTP method is used to remove data from the server.
18. Which package is used to handle routing in a React application?
a) react-router-dom
b) express
c) mongoose
d) axios
Answer: a) react-router-dom
Explanation: react-router-dom is used to handle routing in React applications.
19. What is the purpose of a .env file in a backend application?
a) To store database credentials and API keys.
b) To define routes for the backend.
c) To list all installed packages.
d) To store user session data.
Answer: a) To store database credentials and API keys.
Explanation: The .env file is used to store sensitive information like API keys, database credentials, and other environment variables.
20. How do you send a POST request from React to a Node.js backend?
a) Using axios.post()
b) Using fetch()
c) Using axios.get()
d) Both a) and b)
Answer: d) Both a) and b)
Explanation: Both axios.post() and fetch() can be used to send a POST request from React to a Node.js backend.
21. What does the mongoose.find() method do?
a) Creates a new document.
b) Fetches documents from the MongoDB collection.
c) Updates an existing document.
d) Deletes a document from the collection.
Answer: b) Fetches documents from the MongoDB collection.
Explanation: mongoose.find() is used to retrieve documents from the MongoDB collection.
22. Which React hook is used to manage the side-effects in components like fetching data?
a) useContext
b) useState
c) useEffect
d) useReducer
Answer: c) useEffect
Explanation: useEffect is used for handling side effects such as data fetching or DOM manipulations.
23. What is the purpose of the mongoose.model() function?
a) To create and compile a model based on a schema.
b) To fetch documents from the database.
c) To connect to the MongoDB database.
d) To update a document in MongoDB.
Answer: a) To create and compile a model based on a schema.
Explanation: mongoose.model() is used to create a model based on a defined schema, which is then used to interact with MongoDB.
24. How do you validate user input on the backend in Express?
a) By using the validate() method.
b) By using express-validator or custom validation functions.
c) By checking the data type.
d) By validating cookies.
Answer: b) By using express-validator or custom validation functions.
Explanation: The express-validator library is commonly used for input validation in Express apps.
25. How does React update the DOM when state changes?
a) It manually updates the DOM.
b) It triggers a re-render of the component and updates the DOM efficiently.
c) It requires a manual refresh to update the DOM.
d) It updates the DOM using traditional jQuery.
Answer: b) It triggers a re-render of the component and updates the DOM efficiently.
Explanation: React uses a virtual DOM to efficiently update the real DOM when state changes, avoiding unnecessary re-renders.
26. Which of the following is used to handle HTTP requests in Node.js?
a) react-router-dom
b) express
c) mongoose
d) axios
Answer: b) express
Explanation: express is a minimal and flexible Node.js web application framework used to handle HTTP requests.
27. What type of database is MongoDB?
a) Relational Database
b) Document-Based NoSQL Database
c) Key-Value Store
d) In-Memory Database
Answer: b) Document-Based NoSQL Database
Explanation: MongoDB is a document-based NoSQL database where data is stored as JSON-like documents.
28. Which HTTP method is used to fetch data from the server in React?
a) POST
b) GET
c) PUT
d) DELETE
Answer: b) GET
Explanation: The GET HTTP method is used to retrieve data from the server.
29. Which package in Node.js is used to parse JSON bodies in requests?
a) express.json()
b) mongoose.json()
c) body-parser.json()
d) axios.json()
Answer: c) body-parser.json()
Explanation: body-parser is a middleware used in Express to parse incoming request bodies, including JSON data.
30. How can a React component fetch data from an API?
a) Using axios.get()
b) Using fetch()
c) Using useEffect and axios or fetch()
d) All of the above
Answer: d) All of the above
Explanation: React can fetch data from APIs using axios.get(), fetch(), or within the useEffect hook.
31. What is the output of console.log(req.body) in Express if the request contains a JSON payload?
a) Undefined
b) An object with the parsed data
c) An empty object
d) An array with the data
Answer: b) An object with the parsed data
Explanation: When using express.json(), the req.body will contain the parsed data from the incoming JSON request body.
32. What is the purpose of the mongoose.Schema.Types.ObjectId?
a) It defines the data type of a field.
b) It is used for storing references to other documents.
c) It creates a new collection.
d) It stores the primary key of a collection.
Answer: b) It is used for storing references to other documents.
Explanation: mongoose.Schema.Types.ObjectId is used to store references to other MongoDB documents (like foreign keys).
33. How do you make an API call from React to a backend server running on localhost:5000?
a) axios.get('http://localhost:5000/api')
b) axios.get('localhost:5000/api')
c) fetch('localhost:5000/api')
d) fetch('http://localhost:5000/api')
Answer: a) axios.get('http://localhost:5000/api')
Explanation: The correct format to make an API call to a local backend server is with the full URL, including http://.
34. In React, how do you pass data to child components?
a) By using props
b) By using state
c) By using the context API
d) By using local storage
Answer: a) By using props
Explanation: In React, data is passed to child components using props.
35. What is the purpose of express.Router() in Express.js?
a) To define middleware functions
b) To define routes in a modular way
c) To connect to the MongoDB database
d) To handle requests from React
Answer: b) To define routes in a modular way
Explanation: express.Router() is used to create modular, mountable route handlers in Express.js.
36. What does the res.status(404) indicate in Express?
a) Success
b) Bad Request
c) Unauthorized
d) Not Found
Answer: d) Not Found
Explanation: res.status(404) is used to indicate that the requested resource was not found on the server.
37. Which of the following is an example of a MongoDB query method?
a) find()
b) updateOne()
c) insertMany()
d) All of the above
Answer: d) All of the above
Explanation: MongoDB provides various query methods like find(), updateOne(), insertMany(), etc., to interact with the database.
38. How do you include a header in an API request in React using Axios?
a) axios.get('/api', { headers: { 'x-auth-token': token } })
b) axios.headers('/api')
c) fetch('/api', { headers: { 'x-auth-token': token } })
d) Both a) and c)
Answer: d) Both a) and c)
Explanation: Both Axios and Fetch allow including headers in API requests by passing a headers object.
39. How do you delete a document in MongoDB using Mongoose?
a) Model.delete()
b) Model.remove()
c) Model.deleteOne()
d) Model.drop()
Answer: c) Model.deleteOne()
Explanation: Model.deleteOne() is used to delete a single document from a MongoDB collection.
40. How does React handle the DOM efficiently?
a) By using Virtual DOM
b) By directly modifying the DOM
c) By using jQuery
d) By storing the DOM in local storage
Answer: a) By using Virtual DOM
Explanation: React uses a Virtual DOM to efficiently compare changes and only update the actual DOM when necessary.
41. What is the purpose of useEffect in React?
a) To manage state
b) To perform side effects like data fetching or subscriptions
c) To handle user input
d) To render the component
Answer: b) To perform side effects like data fetching or subscriptions
Explanation: useEffect is a hook used for handling side effects, such as data fetching and setting up subscriptions.
42. Which method does mongoose provide for inserting a new document into a MongoDB collection?
a) insertOne()
b) create()
c) add()
d) push()
Answer: b) create()
Explanation: create() is used to insert a new document into the MongoDB collection.
43. What type of authentication is JWT used for?
a) Cookie-based authentication
b) Session-based authentication
c) Token-based authentication
d) File-based authentication
Answer: c) Token-based authentication
Explanation: JWT (JSON Web Token) is used for token-based authentication, where the server issues a token to the client for subsequent requests.
44. In a MERN stack application, which technology handles the front end?
a) MongoDB
b) Express.js
c) React.js
d) Node.js
Answer: c) React.js
Explanation: In the MERN stack, React.js handles the frontend of the application.
45. Which of the following allows routing in React?
a) express
b) axios
c) react-router-dom
d) mongoose
Answer: c) react-router-dom
Explanation: react-router-dom is used to manage routing in React applications.
46. What does the res.json() method do in Express?
a) Sends a JSON response to the client.
b) Parses a JSON request from the client.
c) Retrieves data from MongoDB in JSON format.
d) Sends a JSON token for authentication.
Answer: a) Sends a JSON response to the client.
Explanation: res.json() sends a JSON response back to the client.
47. Which HTTP method is typically used for creating resources in REST APIs?
a) GET
b) PUT
c) POST
d) DELETE
Answer: c) POST
Explanation: POST is used to create new resources in a RESTful API.
48. What is the correct syntax to include Axios in a React component?
a) import axios from 'axios'
b) import axios from 'fetch'
c) require('axios')
d) import axios from './axios'
Answer: a) import axios from 'axios'
Explanation: To include Axios in a React component, you use import axios from 'axios'.
49. Which of the following is the correct syntax to create a route in Express?
a) app.route('/path').get()
b) app.get('/path')
c) app.addRoute('/path')
d) app.route('/path')
Answer: b) app.get('/path')
Explanation: In Express, routes are created using methods like app.get(), app.post(), etc.
50. Which function is used to connect to MongoDB in Mongoose?
a) mongoose.connect()
b) mongoose.connectToDatabase()
c) mongoose.start()
d) mongoose.createConnection()
Answer: a) mongoose.connect()
Explanation: mongoose.connect() is used to establish a connection to a MongoDB database.