Q. Which attribute is used in React to give CSS classes?

  • (A) class
  • (B) className
  • (C) css
  • (D) styleName
πŸ’¬ Discuss
βœ… Correct Answer: (B) className
Explanation: In JSX, 'className' is used instead of 'class' to avoid conflicts with JavaScript reserved keywords.

Q. What is the main advantage of using keys in React lists?

  • (A) Faster search
  • (B) Easy sorting
  • (C) Helps React identify which items have changed
  • (D) Avoids duplicate rendering
πŸ’¬ Discuss
βœ… Correct Answer: (C) Helps React identify which items have changed
Explanation: Keys help React identify which items have changed, been added, or removed.

Q. Which method is used to change the state in a React class component?

  • (A) this.setChange()
  • (B) this.changeState()
  • (C) this.setState()
  • (D) this.updateState()
πŸ’¬ Discuss
βœ… Correct Answer: (C) this.setState()
Explanation: this.setState() is used to update the state in class components.

Q. How many elements can a React component return?

  • (A) Only one element
  • (B) Only two elements
  • (C) Multiple elements using an array or fragment
  • (D) None
πŸ’¬ Discuss
βœ… Correct Answer: (C) Multiple elements using an array or fragment
Explanation: React components can return multiple elements using React fragments or arrays.

Q. Which of the following is used for performance optimization in React?

  • (A) React.delay()
  • (B) React.cache()
  • (C) React.memo()
  • (D) React.optimize()
πŸ’¬ Discuss
βœ… Correct Answer: (C) React.memo()
Explanation: React.memo() is used to prevent unnecessary re-renders of functional components.

Q. How do you pass data from parent to child component in React?

  • (A) Using setState
  • (B) Using props
  • (C) Using refs
  • (D) Using context
πŸ’¬ Discuss
βœ… Correct Answer: (B) Using props
Explanation: Props are used to pass data from parent to child components.

Q. What will `useEffect(() => {}, [])` do?

  • (A) Run after every render
  • (B) Run only once after the initial render
  • (C) Never run
  • (D) Run before every render
πŸ’¬ Discuss
βœ… Correct Answer: (B) Run only once after the initial render
Explanation: An empty dependency array means the effect runs only once after the initial render.

Q. Which hook is best for accessing DOM elements directly?

  • (A) useEffect
  • (B) useMemo
  • (C) useRef
  • (D) useState
πŸ’¬ Discuss
βœ… Correct Answer: (C) useRef
Explanation: useRef can be used to access DOM elements directly using ref.current.

Q. What is a React fragment?

  • (A) A single root element
  • (B) A placeholder for props
  • (C) A tool for debugging
  • (D) A way to group elements without extra nodes
πŸ’¬ Discuss
βœ… Correct Answer: (D) A way to group elements without extra nodes
Explanation: React fragments let you group multiple elements without adding extra nodes to the DOM.

Q. Which React component lifecycle method is called before rendering?

  • (A) componentDidMount
  • (B) componentWillUnmount
  • (C) render
  • (D) getDerivedStateFromProps
πŸ’¬ Discuss
βœ… Correct Answer: (D) getDerivedStateFromProps
Explanation: getDerivedStateFromProps is called before render and helps update state based on props.