Create New Post

React JS MCQs 5

  1. Which of the following lifecycle methods is called only once, after the initial render?

    • a) componentWillMount
    • b) componentDidMount (Correct Answer)
    • c) componentDidUpdate
    • d) componentWillUnmount
  2. In React, what is the purpose of the key prop when rendering a list of components?

    • a) It defines the style of each list item
    • b) It helps React identify which items have changed, are added, or are removed in the list (Correct Answer)
    • c) It sets the initial state of each list item
    • d) It specifies the order of the list items
  3. Which hook is used for memoizing the result of a function so that it is only recomputed when its dependencies change?

    • a) useMemo (Correct Answer)
    • b) useCallback
    • c) useEffect
    • d) useRef
  4. What is the purpose of the shouldComponentUpdate method in React?

    • a) To fetch data from an API before the component updates
    • b) To determine if a component should re-render by returning true or false (Correct Answer)
    • c) To update the component's state
    • d) To perform side effects after the component updates
  5. In React, what is the recommended way to handle forms?

    • a) Using regular HTML form elements
    • b) Using controlled components where form data is handled by React state (Correct Answer)
    • c) Using uncontrolled components where form data is handled by the DOM
    • d) Using Redux for form state management
  6. Which of the following is NOT a valid use case for React context?

    • a) Theme management
    • b) Localization
    • c) Managing application-wide state (Correct Answer)
    • d) Passing props between deeply nested components
  7. What is the purpose of the setState function in React?

    • a) To update the component's state (Correct Answer)
    • b) To define the initial state of a component
    • c) To fetch data from an API
    • d) To subscribe to context changes
  8. Which hook is used to perform side effects in functional components?

    • a) useState
    • b) useEffect (Correct Answer)
    • c) useContext
    • d) useReducer
  9. In React, how can you conditionally render components?

    • a) By using the if-else statement inside the render method
    • b) By using ternary operators inside JSX (Correct Answer)
    • c) By using switch statements inside JSX
    • d) By using the renderIf function
  10. Which of the following is a benefit of using React JS?

    • a) Improved performance due to the use of virtual DOM
    • b) Reusability of components
    • c) Easy integration with other libraries and frameworks
    • d) All of the above (Correct Answer)

Comments

Leave a Reply

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

57908