Learn React Js MCQs

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

How do you create a ref in a class component?

1) Using `this.state`

2) Using `React.createRef`

3) Using `useRef`

4) Using `React.forwardRef`

Answer : Option 2

What is the purpose of the `useMemo` hook?

1) To manage state

2) Creates a ref

3) To memoize a value to prevent unnecessary recalculations

4) To handle side effects

Answer : Option 3

How do you use the `useEffect` hook to run only once?

1) By not passing any dependencies

2) By passing an empty array as the second argument

3) By using `useMemo` instead

4) By passing `null` as the second argument

Answer : Option 2

What does `React.Fragment` do?

1) Handles routing

2) Creates a new context

3) Allows grouping of multiple elements without adding extra nodes to the DOM

4) Manages state

Answer : Option 3

How do you update a component’s state in a class component?

1) Using `this.props`

2) Using `this.setState`

3) Using `this.state`

4) Using `this.context`

Answer : Option 2

What is the purpose of the `prop-types` package in React?

1) To manage state

2) To validate the props passed to a component

3) To handle side effects

4) To optimize performance

Answer : Option 2

How do you conditionally render components in React?

1) Using the `useEffect` hook

2) Using `this.state`

3) Using ternary operators or `if-else` statements

4) Using `this.props`

Answer : Option 3

How do you handle errors in React components?

1) Using try-catch in `render`

2) Using `useState`

3) Using Error Boundaries

4) Using `useEffect`

Answer : Option 3

What is the purpose of `React.memo`?

1) To handle side effects

2) To create refs

3) To optimize performance by memoizing components

4) To manage state

Answer : Option 3

How do you handle routing in a React application?

1) Using `react-router` library

2) Using `useEffect` hook

3) Using `useState` hook

4) Using `componentDidMount`

Answer : Option 1