Learn React Js MCQs

Prepare React Js MCQs (Multiple choice Questions) for exam and job interviews.

What does the `useState` hook return?

1) A value and a setter function

2) A state variable and a ref

3) A state variable and a callback function

4) A prop and a function

Answer : Option 1

How do you perform routing in React?

1) Using `useState` hook

2) Using `React Router` library

3) Using `window.location`

4) Using `fetch` API

Answer : Option 2

What is the purpose of `useContext` hook?

1) To manage component state

2) To handle side effects

3) To consume context in functional components

4) To create a new context

Answer : Option 3

How do you make an API call in React?

1) Using `window.fetch`

2) Using `XMLHttpRequest`

3) Using `useEffect` with fetch or axios

4) Using `window.location`

Answer : Option 3

What is the significance of the `defaultValue` prop in React?

1) To set the initial value of a controlled input

2) To set the initial value of an uncontrolled input

3) To create a default state

4) To set default props

Answer : Option 2

How do you optimize performance in React?

1) Using `componentWillMount`

2) Using `componentDidMount`

3) Using `React.memo` or `useMemo`

4) Using `shouldComponentUpdate`

Answer : Option 3

What is the purpose of `useReducer` hook?

1) To handle side effects

2) To manage complex state logic

3) To fetch data from an API

4) To manage component lifecycle

Answer : Option 2

How do you create a class component in React?

1) Using `React.createClass`

2) Using `function` keyword

3) Using `class` keyword

4) Using `React.Component`

Answer : Option 3

What does the `ReactDOM.render` method do?

1) Renders a React element into a DOM node

2) Creates a virtual DOM

3) Returns a JSX element

4) Adds a component to the component tree

Answer : Option 1

How do you handle errors in React components?

1) Using `try-catch` blocks

2) Using `componentDidCatch` lifecycle method or `ErrorBoundary`

3) Using `useEffect` hook

4) Using `componentDidMount`

Answer : Option 2