Create New Post

React JS MCQs 11

  • 101. In React, which lifecycle method is called when a component is about to update?
    • a) componentWillMount
    • b) componentDidUpdate
    • c) componentWillUpdate (Correct Answer)
    • d) componentDidMount
  • 102. How do you access route parameters in a React component using React Router?
    • a) By using the `this.params` object
    • b) By using the `this.props.params` object
    • c) By using the `useParams()` hook (Correct Answer)
    • d) By accessing the `params` object directly from the router
  • 103. What is the purpose of the `Fragment` component in React?
    • a) To create a new component
    • b) To group multiple elements without adding extra nodes to the DOM (Correct Answer)
    • c) To provide default props to a component
    • d) To define global styles for the entire application
  • 104. How do you prevent a component from re-rendering unnecessarily in React?
    • a) By using the `shouldComponentUpdate` method in class components
    • b) By using the `React.memo` higher-order component for functional components
    • c) By using the `PureComponent` class in class components
    • d) All of the above (Correct Answer)
  • 105. What does the `dangerouslySetInnerHTML` attribute do in React?
    • a) It allows you to define HTML attributes with dynamic values
    • b) It sets the inner HTML of a React element from a string (Correct Answer)
    • c) It allows you to define inline styles with dynamic values
    • d) It prevents XSS attacks in React components
  • 106. How do you conditionally apply styles to a React component?
    • a) By using the `style` attribute with an object containing conditional styles
    • b) By using the `className` attribute with conditional CSS classes
    • c) By using CSS-in-JS libraries like styled-components
    • d) All of the above (Correct Answer)
  • 107. What is the purpose of the `forwardRef` function in React?
    • a) To forward props from parent to child components
    • b) To forward DOM attributes from parent to child components
    • c) To forward refs from parent to child components (Correct Answer)
    • d) To forward context from parent to child components
  • 108. How do you perform code splitting in React?
    • a) By using dynamic imports with `React.lazy` and `Suspense`
    • b) By splitting the code manually into smaller chunks
    • c) By using the `SplitChunksPlugin` in Webpack
    • d) All of the above (Correct Answer)
  • 109. What is the purpose of the `useReducer` hook in React?
    • a) To manage component state
    • b) To manage complex state logic and perform state transitions (Correct Answer)
    • c) To create reusable custom hooks
    • d) To fetch data from an API
  • 110. How do you handle errors in React components?
    • a) By using the `try-catch` statement inside the `render` method
    • b) By using the `componentDidCatch` lifecycle method (Correct Answer)
    • c) By using the `ErrorBoundary` component provided by React
    • d) All of the above

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

97959