🔹MCQs of Class 10: useEffect Basics

Rashmi Mishra
0

  ðŸ”¹MCQs of Class 10

useEffect Basics


1. When does useEffect with an empty dependency array run?

a) Every render
b) Only on mount
c) Only on update
d) Never

Answer: b


2. What hook is used to perform side effects in functional components?

a) useState
b) useEffect
c) useRef
d) useContext

Answer: b


3. What will happen if you don’t provide a dependency array to useEffect?

a) It runs only once
b) It never runs
c) It runs after every render
d) It throws an error

Answer: c


4. How do you run an effect only when a specific state changes?

a) Pass an empty array
b) Pass the state variable in the dependency array
c) Don’t pass any dependency array
d) Use useState instead

Answer: b


5. What is the purpose of the cleanup function returned by useEffect?

a) To run the effect again
b) To reset state
c) To clean resources like timers or event listeners
d) To initialize variables

Answer: c


6. What will this code do?

jsx

CopyEdit

useEffect(() => {

  alert("Hello");

});

a) Alert only once on mount
b) Alert every render
c) Alert only when a state changes
d) Does nothing

Answer: b


7. How to clear an interval inside useEffect?

a) Return clearInterval function inside the cleanup function
b) Use setTimeout
c) Use useState
d) Clear it outside useEffect

Answer: a


8. What is the correct syntax for an effect that runs only when count changes?

a) useEffect(() => {}, [])
b) useEffect(() => {}, [count])
c) useEffect(() => {}, [ ])
d) useEffect(() => {}, count)

Answer: b


9. Which lifecycle phase does useEffect without dependencies mimic?

a) componentDidMount
b) componentDidUpdate
c) componentWillUnmount
d) componentDidMount + componentDidUpdate

Answer: d


10. How to update the document title on input change using useEffect?

a) Update title in render function
b) Use useEffect with input as dependency
c) Use useState only
d) Use useEffect without dependencies

Answer: b


11. What happens if you return a function inside useEffect?

a) That function runs after render
b) That function runs before the next effect or on unmount
c) It causes an error
d) Nothing

Answer: b


12. How to simulate componentWillUnmount with useEffect?

a) Use cleanup function with empty dependencies
b) Use effect without dependencies
c) Use useState
d) useEffect without return function

Answer: a


13. Which hook is used to track state in React functional components?

a) useEffect
b) useState
c) useContext
d) useReducer

Answer: b


14. What will happen if you omit the dependency array from useEffect?

a) Effect runs once
b) Effect runs after every render
c) Effect never runs
d) Effect runs only on unmount

Answer: b


15. Which of these is a side effect suitable for useEffect?

a) Calculating UI layout
b) Updating the DOM or API calls
c) Returning JSX
d) Declaring variables

Answer: b


16. How to update state inside setInterval in useEffect correctly?

a) Use setState(count + 1)
b) Use updater function: setState(prev => prev + 1)
c) Directly mutate state
d) Call setInterval without cleanup

Answer: b


17. What will this useEffect do?

useEffect(() => {

  console.log('Updated');

}, []);

a) Logs only once after mount
b) Logs every render
c) Logs on every update
d) Never logs

Answer: a


18. How can you cleanup event listeners in useEffect?

a) Add them without cleanup
b) Return a cleanup function that removes listeners
c) Use useState
d) Use setTimeout

Answer: b


19. What does an empty dependency array mean?

a) Run on mount and update
b) Run only on mount
c) Run on every state change
d) Run on every render

Answer: b


20. Can useEffect return a cleanup function?

a) Yes
b) No
c) Only in class components
d) Only when using useState

Answer: a


21. Which hook replaces lifecycle methods like componentDidMount in function components?

a) useState
b) useEffect
c) useContext
d) useReducer

Answer: b


22. What’s the main reason to use useEffect cleanup?

a) To prevent memory leaks
b) To update state
c) To fetch data
d) To render JSX

Answer: a


23. What does this dependency array do? [name, age]?

a) Runs effect only on mount
b) Runs effect whenever name or age changes
c) Runs effect every render
d) Never runs effect

Answer: b


24. How to trigger an effect every time a specific prop changes?

a) Add the prop to dependency array
b) Leave dependency array empty
c) Use useState
d) Use a class component

Answer: a


25. Which is NOT true about useEffect?

a) It can perform side effects
b) It runs after render
c) It blocks the browser from painting
d) It can clean up resources

Answer: c


26. To update the title with user input, you should put the input value in?

a) useState only
b) useEffect dependency array
c) The return function of useEffect
d) The component render method

Answer: b


27. What happens if you update state inside useEffect without dependencies?

a) Causes infinite loop
b) Runs once
c) Nothing
d) Throws an error

Answer: a


28. Which method cancels setTimeout or setInterval inside useEffect?

a) clearTimeout or clearInterval inside cleanup function
b) stopTimeout
c) setTimeout again
d) No cancellation needed

Answer: a


29. What is the default behavior of useEffect?

a) Runs before render
b) Runs after render
c) Runs synchronously with render
d) Never runs

Answer: b


30. If useEffect has multiple dependencies, when does it run?

a) When any dependency changes
b) When all dependencies change simultaneously
c) Only once
d) Every render

Answer: a


31. How to ensure effect runs only once on mount?

a) No dependency array
b) Empty dependency array
c) Put variables in dependency array
d) Put state setter in dependency array

Answer: b


32. What does useEffect cleanup function mimic?

a) componentWillUnmount
b) componentDidMount
c) componentDidUpdate
d) render method

Answer: a


33. How to use useEffect to respond to window resize events?

a) Add event listener in effect, remove in cleanup
b) Add event listener in render
c) Use useState only
d) Add event listener without cleanup

Answer: a


34. Which of these is NOT a side effect?

a) Updating the DOM
b) Fetching data
c) Calculating pure functions
d) Setting up subscriptions

Answer: c


35. How often does useEffect run if no dependency array is passed?

a) Every render
b) Only once
c) Never
d) Only on unmount

Answer: a


36. Can you have multiple useEffect hooks in one component?

a) Yes
b) No
c) Only one per component
d) Only if using classes

Answer: a


37. What is the best way to update the title with multiple input values?

a) Single useEffect with all input values in dependency array
b) Multiple useEffects each for one input
c) Update title in render
d) Use useState only

Answer: a


38. If you want to clear an event listener, where should you place the cleanup?

a) Inside the return function of useEffect
b) Inside render
c) Outside the component
d) In useState hook

Answer: a


39. What will happen if you pass null as dependency array?

a) Effect runs once
b) Throws error
c) Effect runs every render
d) Effect never runs

Answer: b


40. Can you update multiple state variables inside one useEffect?

a) Yes
b) No
c) Only in class components
d) Only using useReducer

Answer: a


41. What happens when you update a state variable inside useEffect with that variable as dependency?

a) May cause infinite loop if not handled properly
b) Runs only once
c) Runs before render
d) Never runs

Answer: a


42. To clean up timers or listeners, where should you put the cleanup logic?

a) At the end of the component
b) Inside the return callback of useEffect
c) Inside useState
d) Inside render

Answer: b


43. Which hook allows performing cleanup on component unmount?

a) useEffect with cleanup function
b) useState
c) useContext
d) useRef

Answer: a


44. How do you prevent running useEffect infinitely when updating state inside it?

a) Add appropriate dependencies
b) Use empty dependency array always
c) Don’t update state inside effect
d) Use useRef

Answer: a


45. What is the role of dependency array in useEffect?

a) Controls when the effect runs
b) Defines initial state
c) Cleans up side effects
d) Defines component props

Answer: a


46. How to run an effect on multiple state changes?

a) List all states in dependency array
b) Leave dependency array empty
c) Use multiple effects
d) Use a class component

Answer: a


47. What will happen if you forget to clean up a timer in useEffect?

a) Memory leak and unexpected behavior
b) Timer stops automatically
c) React throws an error
d) Nothing

Answer: a


48. When useEffect dependencies don’t change, what happens?

a) Effect does not rerun
b) Effect reruns anyway
c) Component unmounts
d) Component remounts

Answer: a


49. Why is it recommended to use the cleanup function in useEffect?

a) To reset DOM
b) To prevent resource leaks like open timers or event listeners
c) To re-render component
d) To update state

Answer: b


50. Can useEffect replace all class lifecycle methods?

a) Yes, for side effects only
b) No, only for state management
c) No, it can’t replace any lifecycle
d) Yes, fully

Answer: a

 

Tags

Post a Comment

0Comments

Post a Comment (0)