Q. How can you force a re-render in a functional component?

  • (A) By updating state using useState hook
  • (B) By calling render() manually
  • (C) By refreshing the browser
  • (D) By modifying props directly
πŸ’¬ Discuss
βœ… Correct Answer: (A) By updating state using useState hook
Explanation: Changing state triggers a re-render.

Q. Which lifecycle method is called when a component is first added to the DOM in class components?

  • (A) componentDidMount
  • (B) componentWillUnmount
  • (C) shouldComponentUpdate
  • (D) render
πŸ’¬ Discuss
βœ… Correct Answer: (A) componentDidMount
Explanation: componentDidMount runs after the first render.

Q. How do you update the state in React functional components?

  • (A) Using the setter function returned by useState
  • (B) Directly assigning to the state variable
  • (C) Using this.setState()
  • (D) Using useEffect
πŸ’¬ Discuss
βœ… Correct Answer: (A) Using the setter function returned by useState
Explanation: useState returns a state variable and a function to update it.

Q. What is the use of the useCallback hook?

  • (A) To memoize functions to prevent unnecessary re-creation
  • (B) To store mutable values
  • (C) To fetch data from APIs
  • (D) To manage side effects
πŸ’¬ Discuss
βœ… Correct Answer: (A) To memoize functions to prevent unnecessary re-creation
Explanation: useCallback returns a memoized callback.

Q. How can you optimize performance in React when passing functions to child components?

  • (A) Use useCallback to memoize the functions
  • (B) Define functions inside the render method
  • (C) Use inline functions in JSX
  • (D) Avoid using functions in props
πŸ’¬ Discuss
βœ… Correct Answer: (A) Use useCallback to memoize the functions
Explanation: Memoizing functions prevents unnecessary re-renders.

Q. What is the purpose of keys in React lists?

  • (A) They help React identify which items have changed, are added, or removed
  • (B) They set the HTML id attribute
  • (C) They are used for CSS styling
  • (D) They are required for accessibility
πŸ’¬ Discuss
βœ… Correct Answer: (A) They help React identify which items have changed, are added, or removed
Explanation: Keys improve React's reconciliation performance.

Q. What does the ReactDOM.render() method do?

  • (A) Renders React elements into the DOM
  • (B) Creates new React components
  • (C) Updates the virtual DOM
  • (D) Fetches data from a server
πŸ’¬ Discuss
βœ… Correct Answer: (A) Renders React elements into the DOM
Explanation: ReactDOM.render attaches React components to DOM nodes.

Q. How do you create a controlled component in React?

  • (A) By managing form input values in React state
  • (B) By using uncontrolled form inputs
  • (C) By directly modifying the DOM input value
  • (D) By using refs only
πŸ’¬ Discuss
βœ… Correct Answer: (A) By managing form input values in React state
Explanation: Controlled components have their state managed by React.

Q. What is the role of the Context API in React?

  • (A) To provide a way to pass data through the component tree without props
  • (B) To handle routing
  • (C) To style components
  • (D) To fetch data from APIs
πŸ’¬ Discuss
βœ… Correct Answer: (A) To provide a way to pass data through the component tree without props
Explanation: Context API helps avoid prop drilling.

Q. Which of the following hooks is used to manage state in functional components?

  • (A) useState
  • (B) useEffect
  • (C) useContext
  • (D) useRef
πŸ’¬ Discuss
βœ… Correct Answer: (A) useState
Explanation: useState allows adding state to functional components.