React JS 100 MCQ. In these 100 Multiple Choice Questions (MCQs), we are going to cover topics such as React basics, JSX, components, Hooks, Redux, performance optimization, and API handling.
| React JS 100 MCQ | MCQ Range |
| Introduction to React | MCQ 1–4 |
| JSX (JavaScript XML) | MCQ 5–7 |
| Components | MCQ 8–12 |
| Events in React | MCQ 13–20 |
| State and Lifecycle | MCQ 21–25 |
| React Hooks | MCQ 26–30 |
| Context API | MCQ 31–34 |
| Forms in React | MCQ 35–40 |
| React Router | MCQ 41–50 |
| Introduction to Redux | MCQ 51–55 |
| Redux with React | MCQ 56–60 |
| Error Boundaries in React | MCQ 61–64 |
| Handling Errors in Async Code | MCQ 65–66 |
| Debugging React Applications | MCQ 67–70 |
| React Performance Techniques | MCQ 71–80 |
| Fetching Data | MCQ 81–85 |
| RESTful APIs | MCQ 86–90 |
| Unit Testing React Components | MCQ 91–95 |
| Styling Components in React | MCQ 96–100 |

Question 1: What is React primarily used for?
Show Explanation
Correct Answer: B. React is primarily used to create dynamic and interactive user interfaces.
Question 2: How is React different from other JavaScript frameworks like Angular and Vue?
Show Explanation
Correct Answer: B. React uses a virtual DOM to enhance performance compared to the real DOM.
Question 3: What does the virtual DOM in React do?
Show Explanation
Correct Answer: B. The virtual DOM is a lightweight version of the real DOM that updates asynchronously.
Question 4: Which of the following best describes JSX?
Show Explanation
Correct Answer: B. JSX is a syntax extension that allows developers to write HTML within JavaScript.
Question 5: What does JSX stand for?
Show Explanation
Correct Answer: B. JSX stands for JavaScript XML and is a syntax extension for writing HTML-like code inside JavaScript.
Question 6: How can you embed JavaScript expressions in JSX?
Show Explanation
Correct Answer: D. JavaScript expressions in JSX are embedded using single curly braces { }.
Question 7: Which of the following is an advantage of using JSX?
Show Explanation
Correct Answer: C. JSX allows developers to write HTML elements directly inside JavaScript.
Question 8: What is a functional component in React?
Show Explanation
Correct Answer: B. A functional component is a JavaScript function that returns HTML elements using JSX.
Question 9: What is the primary difference between functional and class components in React?
Show Explanation
Correct Answer: B. Class components can use lifecycle methods like componentDidMount, while functional components cannot unless using Hooks.
Question 10: What are props in React used for?
Show Explanation
Correct Answer: C. Props are used to pass data from one component to another in React.
Question 11: Which method is required in a React class component?
Show Explanation
Correct Answer: A. The render method is required in a React class component as it returns the HTML for the component.
Question 12: What is the main purpose of state in a React component?
Show Explanation
Correct Answer: B. State in React is used to manage dynamic data that affects how a component renders.
Question 13: How does React handle events?
Show Explanation
Correct Answer: B. React uses a synthetic event system that wraps around the browser’s native event system, providing cross-browser compatibility.
Question 14: Which of the following is an example of event binding in React?
Show Explanation
Correct Answer: A. Event binding in React class components can be done by using onClick={this.handleClick.bind(this)} to bind the method’s ‘this’ context.
Question 15: How can you prevent the default behavior of an event in React?
Show Explanation
Correct Answer: B. You can prevent the default behavior of an event in React by calling event.preventDefault() inside the event handler.
Question 16: What is event delegation in React?
Show Explanation
Correct Answer: B. Event delegation in React refers to handling events on the parent component rather than individual child elements.
Question 17: Which of the following is the correct way to handle an event in React?
Show Explanation
Correct Answer: D. The correct syntax for handling events in React is onClick={this.handleClick}.
Question 18: How does React handle form inputs using events like onChange?
Show Explanation
Correct Answer: B. React handles form inputs using its synthetic event system.
Question 19: Which of the following is a correct example of using the onChange event in React?
Show Explanation
Correct Answer: C. The correct syntax for using the onChange event in React is onChange={this.handleChange}.
Question 20: How can you bind an event handler to ensure it has the correct ‘this’ context in class components?
Show Explanation
Correct Answer: A. You can bind an event handler to the correct ‘this’ context in React class components using this.methodName.bind(this) in the constructor.
Question 21: What is the purpose of the state in React?
Show Explanation
Correct Answer: A. State in React is used to store dynamic data that can change over time and affect the rendering of the component.
Question 22: Which lifecycle method is called when a component is first rendered to the DOM?
Show Explanation
Correct Answer: A. The componentDidMount lifecycle method is called once after the component is rendered to the DOM.
Question 23: How do functional components manage state in React?
Show Explanation
Correct Answer: A. Functional components use the useState hook to manage state in React.
Question 24: What is the role of the componentWillUnmount lifecycle method?
Show Explanation
Correct Answer: B. componentWillUnmount is used to clean up resources like subscriptions or event listeners before the component is removed from the DOM.
Question 25: What is the correct syntax for updating state in a React class component?
Show Explanation
Correct Answer: B. In a class component, you update state using this.setState({ value: newValue }).
Question 26: What is the useState hook used for in React?
Show Explanation
Correct Answer: B. The useState hook is used to initialize and update state in functional components.
Question 27: What does the useEffect hook allow you to do?
Show Explanation
Correct Answer: A. The useEffect hook allows you to handle side effects like data fetching, subscriptions, and timers.
Question 28: Which of the following is a correct example of using the useState hook?
Show Explanation
Correct Answer: B. The correct syntax for using the useState hook is const {value, setValue} = useState(0);
Question 29: What does the useContext hook allow you to do?
Show Explanation
Correct Answer: B. The useContext hook allows you to consume context values without passing them through every level of a component tree (prop drilling).
Question 30: How does the useReducer hook differ from the useState hook?
Show Explanation
Correct Answer: A. The useReducer hook is used for managing more complex state logic than the useState hook.
Question 31: What is the main purpose of the Context API in React?
Show Explanation
Correct Answer: B. The Context API allows you to share global data across multiple components without needing to pass props down through every level.
Question 32: How do you create a Context in React?
Show Explanation
Correct Answer: B. You create a context in React using React.createContext().
Question 33: What is the main difference between Context API and Redux?
Show Explanation
Correct Answer: B. Context API is built-in to React, while Redux is an external library used for complex state management.
Question 34: How do you consume values from a Context in functional components?
Show Explanation
Correct Answer: B. You consume values from a Context in functional components using the useContext hook.
Question 35: What is a controlled component in React?
Show Explanation
Correct Answer: C. A controlled component is one where form data is managed by React using state, rather than the DOM.
Question 36: How do you handle form submission in React?
Show Explanation
Correct Answer: C. You handle form submission in React by using event.preventDefault() to prevent the default browser form submission behavior.
Question 37: What is the key difference between controlled and uncontrolled components in React?
Show Explanation
Correct Answer: B. Uncontrolled components manage form data through the DOM, while controlled components manage it through React state.
Question 38: Which of the following is an example of a controlled component?
Show Explanation
Correct Answer: B. A controlled component is one where the input’s value is managed by React state, using value and onChange attributes.
Question 39: How do you handle validation in forms in React?
Show Explanation
Correct Answer: A. Validation in forms is typically handled by writing logic inside the onSubmit handler to validate the form inputs before submission.
Question 40: Which of the following is an advantage of using controlled components in forms?
Show Explanation
Correct Answer: B. Controlled components make it easier to handle form validation and control user input, as the form data is managed by React state.
Question 41: What is React Router used for in a React application?
Show Explanation
Correct Answer: B. React Router is used to handle navigation and routing between different components in a React application.
Question 42: How do you set up routing in a React application?
Show Explanation
Correct Answer: A. You set up routing in React by using the BrowserRouter, Route, and Switch components from React Router.
Question 43: What is the purpose of the Switch component in React Router?
Show Explanation
Correct Answer: B. The Switch component renders the first child Route that matches the current location, ensuring that only one route is rendered at a time.
Question 44: What is a common use case for route parameters in React Router?
Show Explanation
Correct Answer: A. Route parameters allow you to pass dynamic values through the URL, which can be used to render specific data for a particular route.
Question 45: How do you define a route with a parameter in React Router?
Show Explanation
Correct Answer: A. A route with a parameter in React Router is defined using a colon before the parameter name, such as path=”/user/”.
Question 46: How do you access route parameters in a component?
Show Explanation
Correct Answer: A. Route parameters in React Router can be accessed through the props object using props.match.params.
Question 47: What is the purpose of the Link component in React Router?
Show Explanation
Correct Answer: B. The Link component in React Router is used to create navigation links that allow users to navigate between routes without reloading the page.
Question 48: How can you redirect users programmatically in React Router?
Show Explanation
Correct Answer: B. You can redirect users programmatically in React Router by using the history object and calling history.push().
Question 49: What is the purpose of the useHistory hook in React Router?
Show Explanation
Correct Answer: B. The useHistory hook gives access to the history object, which can be used for programmatic navigation within a React component.
Question 50: How can you handle nested routes in React Router?
Show Explanation
Correct Answer: B. Nested routes are handled by placing child Route components inside a parent Route component, allowing for more complex navigation structures.
Question 51: What is the main purpose of Redux in a React application?
Show Explanation
Correct Answer: B. Redux is used to manage global state across the entire React application, making state accessible from any component.
Question 52: Which of the following is a core concept of Redux?
Show Explanation
Correct Answer: C. The core concepts of Redux are Actions, Reducers, and Store.
Question 53: What is an action in Redux?
Show Explanation
Correct Answer: B. An action in Redux is a plain JavaScript object that describes a change or event in the application state.
Question 54: What is the purpose of a reducer in Redux?
Show Explanation
Correct Answer: C. A reducer specifies how the application’s state changes in response to an action in Redux.
Question 55: How is the Redux store created in a React application?
Show Explanation
Correct Answer: A. The Redux store is created by calling createStore and passing in the root reducer.
Question 56: How do you connect a React component to the Redux store?
Show Explanation
Correct Answer: B. React components are connected to the Redux store by using the connect function from react-redux.
Question 57: What does the mapStateToProps function do in Redux?
Show Explanation
Correct Answer: B. The mapStateToProps function maps the state from the Redux store to the props of the connected component.
Question 58: What is the purpose of the mapDispatchToProps function in Redux?
Show Explanation
Correct Answer: B. The mapDispatchToProps function maps dispatchable actions to the props of the component.
Question 59: Which of the following is a common use case for Redux middleware?
Show Explanation
Correct Answer: B. Redux middleware is commonly used to handle asynchronous actions such as API calls in the application.
Question 60: What does Redux Thunk allow you to do?
Show Explanation
Correct Answer: A. Redux Thunk allows action creators to return functions instead of plain action objects, enabling asynchronous actions like API calls.
Question 61: What are error boundaries in React used for?
Show Explanation
Correct Answer: B. Error boundaries are React components that catch JavaScript errors in their child component tree during rendering, preventing the application from crashing.
Question 62: Which lifecycle method is used to catch errors in an error boundary?
Show Explanation
Correct Answer: A. The componentDidCatch lifecycle method is used in error boundaries to catch errors that occur in the component tree.
Question 63: Can error boundaries catch errors in event handlers?
Show Explanation
Correct Answer: B. Error boundaries do not catch errors in event handlers, which need to be handled with try-catch blocks inside the event handler.
Question 64: What happens if an error boundary catches an error?
Show Explanation
Correct Answer: B. When an error boundary catches an error, it displays a fallback UI, preventing the entire application from crashing.
Question 65: What is the best way to handle errors in async code in React?
Show Explanation
Correct Answer: B. Async code errors in React should be handled by wrapping the code in a try-catch block.
Question 66: How can you handle errors in Promises in React?
Show Explanation
Correct Answer: B. Errors in Promises can be handled by adding a .catch() block at the end of the Promise chain.
Question 67: Which of the following tools is most commonly used for debugging React applications?
Show Explanation
Correct Answer: C. React Developer Tools is a browser extension used specifically for inspecting and debugging React applications.
Question 68: How can you inspect the state and props of a React component during development?
Show Explanation
Correct Answer: B. You can inspect the state and props of a React component using React Developer Tools, which shows the component tree and the current values of props and state.
Question 69: What does the profiler feature in React Developer Tools do?
Show Explanation
Correct Answer: B. The profiler feature in React Developer Tools helps identify performance bottlenecks by recording renders and updates of components.
Question 70: How can you view performance optimizations or re-renders in React?
Show Explanation
Correct Answer: B. You can view performance optimizations or re-renders in React using the performance tab in React Developer Tools, which shows how components are being rendered.
Question 71: What is the purpose of memoization in React?
Show Explanation
Correct Answer: B. Memoization in React is used to prevent unnecessary re-renders by caching the results of function calls and reusing them when the inputs haven’t changed.
Question 72: Which hook in React can be used to memoize values?
Show Explanation
Correct Answer: C. The useMemo hook is used to memoize values, ensuring that expensive calculations are only performed when necessary.
Question 73: What is the purpose of the useCallback hook?
Show Explanation
Correct Answer: A. The useCallback hook memoizes function definitions so that they are not recreated on every render, which can improve performance.
Question 74: How can React.memo help optimize component performance?
Show Explanation
Correct Answer: A. React.memo is a higher-order component that prevents a component from re-rendering unless its props have changed, optimizing performance.
Question 75: What is the purpose of code splitting in React?
Show Explanation
Correct Answer: B. Code splitting is a performance optimization technique that allows React to load only the necessary JavaScript files when needed, improving load times.
Question 76: Which of the following methods can be used for lazy loading components in React?
Show Explanation
Correct Answer: C. React.lazy is used to lazy load components in React, meaning they are only loaded when needed.
Question 77: How does the useMemo hook improve performance in React?
Show Explanation
Correct Answer: A. The useMemo hook memoizes values, ensuring that expensive recalculations are only performed when necessary, improving performance.
Question 78: What is the purpose of the shouldComponentUpdate lifecycle method in class components?
Show Explanation
Correct Answer: B. shouldComponentUpdate allows React to decide whether to re-render a component based on changes to its props or state, which can improve performance by avoiding unnecessary renders.
Question 79: What is the role of the React Profiler?
Show Explanation
Correct Answer: A. The React Profiler tracks and visualizes the performance of components, helping developers understand rendering times and identify performance bottlenecks.
Question 80: Which of the following is a way to fix memory leaks in React applications?
Show Explanation
Correct Answer: A. Memory leaks can be fixed by clearing intervals and timeouts in the componentWillUnmount lifecycle method to ensure resources are properly cleaned up when a component is unmounted.
Question 81: Which method is commonly used in React to fetch data from an API?
Show Explanation
Correct Answer: B. The fetch method and Axios are commonly used in React to fetch data from APIs.
Question 82: Where is the best place to fetch data in a React component?
Show Explanation
Correct Answer: B. Data fetching is typically done inside a useEffect hook in functional components to ensure it happens after the component mounts.
Question 83: How can you handle asynchronous data fetching in React?
Show Explanation
Correct Answer: B. Asynchronous data fetching in React is commonly handled using Promises or async/await inside the useEffect hook.
Question 84: What is the purpose of handling loading states when fetching data in React?
Show Explanation
Correct Answer: B. Handling loading states allows you to display a loading indicator while data is being fetched, improving the user experience.
Question 85: How do you handle errors when fetching data from an API in React?
Show Explanation
Correct Answer: A. Errors during data fetching are handled using try-catch blocks in async/await functions or .catch() in Promises.
Question 86: Which HTTP methods are commonly used for CRUD operations in RESTful APIs?
Show Explanation
Correct Answer: A. The common HTTP methods for CRUD operations in RESTful APIs are GET, POST, PUT, and DELETE.
Question 87: How can you perform a GET request in React using Axios?
Show Explanation
Correct Answer: A. A GET request can be performed using Axios with the axios.get(‘/url’) method.
Question 88: What is the purpose of using async/await in API calls in React?
Show Explanation
Correct Answer: B. Async/await makes asynchronous code easier to write and read by allowing the use of synchronous-looking syntax for asynchronous operations.
Question 89: What is the main advantage of using Axios over the fetch API in React?
Show Explanation
Correct Answer: B. Axios provides automatic JSON handling, simplified syntax, and additional features like request/response interceptors, making it more user-friendly than fetch.
Question 90: How do you perform a POST request in React using the fetch API?
Show Explanation
Correct Answer: A. A POST request using the fetch API is done with fetch(‘/url’, { method: ‘POST’, body: JSON.stringify(data) }).
Question 91: What is the purpose of unit testing in React?
Show Explanation
Correct Answer: B. Unit testing in React involves testing individual components in isolation to ensure they work as expected.
Question 92: Which library is commonly used for testing React components?
Show Explanation
Correct Answer: C. Jest and React Testing Library are commonly used for testing React components.
Question 93: What is snapshot testing in React?
Show Explanation
Correct Answer: A. Snapshot testing compares the rendered output of a component to a previously stored snapshot to detect unexpected changes.
Question 94: How can you mock API calls in unit tests for React components?
Show Explanation
Correct Answer: B. Mocking API calls in unit tests is typically done using Jest or other testing libraries to simulate API responses.
Question 95: What is the primary advantage of using Jest for testing React applications?
Show Explanation
Correct Answer: B. Jest provides built-in support for mocking, snapshot testing, and easy configuration for React applications, making it a popular choice for testing React components.
Question 96: What is CSS-in-JS in React?
Show Explanation
Correct Answer: B. CSS-in-JS refers to writing styles directly in JavaScript files using libraries like styled-components or Emotion.
Question 97: Which of the following is a benefit of using CSS-in-JS in React?
Show Explanation
Correct Answer: B. CSS-in-JS allows for scoped styles within components, which helps avoid class name collisions.
Question 98: What is the difference between inline styles and external stylesheets in React?
Show Explanation
Correct Answer: A. Inline styles are written directly within the component’s JSX, while external stylesheets are stored in separate files and linked to the component.
Question 99: What are CSS modules in React?
Show Explanation
Correct Answer: B. CSS modules allow developers to write locally scoped CSS classes that only apply to the component where they are used.
Question 100: Which of the following is a best practice when styling components in React?
Show Explanation
Correct Answer: C. It is a best practice to use scoped or modular styles to prevent style conflicts across components and ensure maintainability.
Read all important Computer Science MCQs here.