Comprehensive Interview Question Bank!
Comprehensive Interview Question Bank!
Questions)
1. React Lifecycle
Q1: What are React lifecycle methods?
A: React lifecycle methods are methods that get called at different stages of a component’s
existence, such as when a component is created, updated, or unmounted. Examples include
componentDidMount, componentDidUpdate, and componentWillUnmount.
Q4: What is Link in React Router, and how is it different from a regular <a> tag?
A: The Link component from React Router is used to navigate between routes without
refreshing the page. Unlike the regular <a> tag, which reloads the entire page, Link performs
client-side routing and updates the view without a full page reload.
Q6: What is the Switch component in React Router, and how does it work?
A: Switch is a component that renders the first child <Route> or <Redirect> that matches
the location. It ensures that only one route is rendered at a time.
3. State Management
Q1: What is the useState hook in React?
A: The useState hook allows you to add state to functional components in React. It returns an
array with two elements: the current state value and a function to update that state.
Q2: What is the difference between local state and global state?
A: Local state is specific to a single component and is managed with useState or class
component state, while global state is shared across multiple components, typically managed
with context APIs like React Context or libraries like Redux.
Q3: What is the useReducer hook, and how is it different from useState?
A: useReducer is a hook used for managing more complex state logic. It is an alternative to
useState and is often used when there are multiple state transitions or complex updates. It
returns the current state and a dispatch function to trigger updates.
4. Forms in React
Q1: How do you handle form inputs in React?
A: Form inputs in React are controlled using the useState hook for functional components or
state in class components. The value of the input is bound to the state, and the onChange
event is used to update the state as the user types.
Q2: What is the difference between controlled and uncontrolled components in React
forms?
A: Controlled components are those where form data is handled by the React component state,
whereas uncontrolled components use refs to directly access the DOM elements to get form
values.
5. React Authentication
Q1: How do you handle authentication in React?
A: Authentication in React can be handled by using token-based systems like JWT (JSON Web
Tokens). After logging in, the user receives a token, which is stored (usually in localStorage or
sessionStorage) and is included in subsequent requests to protected routes.
Q2: What is a higher-order component (HOC) and how is it used for authentication?
A: A higher-order component (HOC) is a function that takes a component and returns a new
component. It can be used for authentication by wrapping protected components, checking if the
user is authenticated before rendering the component.
Q5: How do you implement social login (e.g., Google, Facebook) in React?
A: Social login can be implemented in React using OAuth providers like Google or Facebook.
You can use libraries like react-oauth/google or implement OAuth flows manually by
interacting with the provider’s API.
2. Querying
Q1: What is a query in MongoDB, and how do you perform a basic query?
A: A query in MongoDB retrieves data from a collection. You can perform a basic query using
the find() method.
Q3: How do you query for documents with a logical operator in MongoDB?
A: MongoDB allows logical operators like $and, $or, $not.
Example:
try {
await User.find();
} catch (error) {
console.error(error);
}
4. Indexing in MongoDB
Q1: What is indexing in MongoDB, and why is it important?
A: Indexing in MongoDB helps to improve the performance of search queries by reducing the
number of documents MongoDB scans. It is essential for optimizing read operations.
Example: db.collection.getIndexes();
2. File System
Q1: How do you read a file asynchronously in Node.js?
A: You can use the fs.readFile() method to read a file asynchronously. Example:
const fs = require('fs');
fs.readFile('file.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
if (fs.existsSync('file.txt')) {
console.log('File exists');
}
3. Middleware
Q1: What is middleware in Node.js?
A: Middleware functions in Node.js are functions that have access to the request object (req),
response object (res), and the next middleware function in the application’s request-response
cycle.
Example: app.use(express.json());
4. Single-threaded Application
Q1: Is Node.js single-threaded, and how does it handle concurrency?
A: Yes, Node.js is single-threaded, but it uses non-blocking I/O and an event-driven
architecture to handle multiple requests concurrently via an event loop.
Q4: How do you send a status code along with a response in Express.js?
A: You can send a status code using the res.status() method. Example:
Q5: How do you handle POST requests and access request body data in Express.js?
A: To handle POST requests and access body data, use app.post() and middleware like
express.json() or express.urlencoded(). Example:
app.use(express.json());
app.post('/submit', (req, res) => {
const data = req.body;
res.send(`Received: ${JSON.stringify(data)}`);
});
2. Error Handling
Q1: How do you handle errors in Express.js?
A: In Express.js, errors can be handled using a middleware function with four arguments: err,
req, res, and next. Example:
4. Scaffolding
Q1: What is scaffolding in Express.js?
A: Scaffolding in Express.js refers to the process of generating the basic structure of an
Express app, often using a tool like express-generator.
npm start
Q5: What are the benefits of using scaffolding tools like express-generator?
A: Scaffolding tools like express-generator provide a standard folder structure, boilerplate
code, and essential configurations, which help speed up the development process and ensure a
clean organization of the project.