Unit 3: State, Events, Hooks, and Side Effects - Practice Quiz
1 Which prop is commonly used to handle a button click in React?
onChange
onClick
onSubmit
onLoad
2
How should a function named handleClick normally be assigned as a React click handler?
onClick={return handleClick}
onClick="handleClick"
onClick={handleClick()}
onClick={handleClick}
3
What does the useState hook return?
4
Which code correctly creates a state variable named count with an initial value of 0?
const [count] = useEffect(0);
const count = new useState(0);
const count = useState.setValue(0);
const [count, setCount] = useState(0);
5 What does React batching do with multiple state updates in the same event handler?
6
Which update is preferred when the next count depends on its previous value?
setCount(count = count + 1)
count = count + 1
setCount(useEffect(count))
setCount(c => c + 1)
7 Which action normally triggers a React component to re-render?
8 What is derived state?
9 How should an array stored in React state usually be updated?
10 Which built-in hook reads a value from a React context?
useMemo
useContext
useEffect
useState
11
Which task is a typical side effect handled with useEffect?
12
When does a useEffect callback normally run?
13
What happens when useEffect is used without a dependency array?
14
What does [count] mean in useEffect(() => { ... }, [count])?
count
count
count
count
15 How does an effect provide a cleanup operation?
16
What does useCallback memoize?
17
What does useMemo commonly memoize?
18 What is the main purpose of a custom hook?
19 Where should hooks be called in a React function component?
20 Which action is a common React state anti-pattern?
21 Which event handler correctly passes an item identifier to a React function component when the button is clicked?
22 What is the best way to update a counter when the new value depends on the previous value?
23
Given setCount(count + 1); setCount(count + 1); inside one event handler, what usually happens when count is initially 0?
24 Which change normally causes a function component to render again?
25
A component receives price and quantity as props and displays their product. Which approach is generally preferred?
26 A form has several related fields that must be updated together based on the previous form state. Which approach is most appropriate?
27 Which hook is most suitable for reading a theme value supplied by a React context?
28 A component must subscribe to a browser event when it mounts. Which pattern is most appropriate?
29
What does an effect with dependency array [userId] do when userId changes?
30 Which cleanup is needed for an interval created inside an effect?
31
When is useMemo most useful in a component?
32
Why might a parent component use useCallback when passing a function to a memoized child?
33
What is the main purpose of a custom hook such as useWindowWidth?
34 Which example follows the Rules of Hooks?
35 What is a common problem with using an effect to calculate a value that can be computed from props during rendering?
36 Which code correctly increments a counter three times within one handler?
37
A controlled input uses value={name}. Which handler correctly updates its value?
38 A child component receives an inline object prop from its parent on every render. Why can this affect a memoized child?
39 Why should state updates avoid directly mutating an existing array?
40 A fetch request starts in an effect and may finish after unmounting. What is an appropriate concern to address?
41
A button is rendered inside a form using <button onClick={handleSave}>Save</button>. The handleSave function calls an asynchronous API and then updates state. Which change best prevents an unintended form submission while preserving React's event handling?
event.stopPropagation() inside handleSave
handleSave in useMemo before passing it
onClick with onSubmit on the button
event.preventDefault() inside handleSave
42
Consider const [user, setUser] = useState({ name: "Ava", age: 20 });. Which update correctly changes only age while preserving the other properties?
setUser(user.age = 21)
setUser(() => ({ age: 21 }))
setUser({ ...user, age: 21 })
setUser({ age: 21 })
43
Inside one React event handler, the component executes setCount(count + 1); setCount(count + 1);. If count is initially 0, what value will normally be rendered after the handler completes?
2, because React batches both updates
undefined, because the state is temporarily cleared
1, because both updates read the same snapshot
0, because both updates are ignored
44
A click handler runs setCount(c => c + 1); setCount(c => c + 1);. What is the resulting value if the initial state is 0?
2, because updater functions compose sequentially
1, because only the first updater runs
0, because updates are batched
undefined, because functional updates require an effect
45 A parent component re-renders because its local state changed. A memoized child receives a primitive prop whose value is unchanged. Under normal React behavior, what is the most accurate result?
useCallback
46
A component contains const data = { enabled: true }; and passes data to a child wrapped in React.memo. The parent re-renders for unrelated reasons. Why can the child still re-render?
47
A component stores items and also stores filteredItems, which is recalculated whenever a search term changes. What is generally the best design when filtering is inexpensive and deterministic?
filteredItems and reconstruct the source list
items and searchTerm, then derive the filtered list during render
filteredItems in context to avoid recalculation
48
Which state structure best represents a form with fields email and password when updates may arrive independently?
49
A component reads theme with useContext(ThemeContext) and also calls useState and useEffect. Which statement is correct when the provider value changes?
useState re-render
50
A component fetches data in useEffect(() => { fetchData(id); }, [id]). What is the primary reason the fetch belongs in an effect rather than directly in the component body?
51
A search component starts a request whenever query changes. An older request may finish after a newer one. Which approach best prevents stale results from overwriting current results?
useCallback
AbortController or an active flag
52
What is the semantic difference between useEffect(effect) and useEffect(effect, []) in a component that mounts once and later re-renders?
53
An effect subscribes to a socket using roomId but declares useEffect(() => subscribe(roomId), []). What is the key defect?
54
Which cleanup behavior is correct for useEffect(() => { const id = setInterval(tick, 1000); return () => clearInterval(id); }, [])?
55
A memoized child receives a callback from its parent. The callback does not need to change unless userId changes. Which implementation most directly supports child memoization?
useCallback(() => loadUser(userId), [userId])
useMemo(() => loadUser(userId), [])
() => loadUser(userId) inline
56
A component computes an expensive sorted list from items and sortOrder. Which use of useMemo is correct?
useMemo(() => sort(items, sortOrder), [sort])
useMemo(sort(items, sortOrder), [items, sortOrder])
useMemo(() => sort(items, sortOrder), [])
useMemo(() => sort(items, sortOrder), [items, sortOrder])
57
A custom hook useOnlineStatus subscribes to a browser online-status event. Which property is essential for making the hook reusable across multiple components?
58 Which component correctly follows the Rules of Hooks when a feature is enabled conditionally?
useState inside the event handler when needed
useEffect only inside if (enabled)
59
An effect contains setTotal(price * quantity) and declares [price, quantity, total] as dependencies. What problem can this create?
price after the first render
60
A component uses useEffect(() => { document.title = title; }, []), where title is a prop that can change. What is the most accurate correction?
title into a ref and keep the array empty
title to the dependency array
useCallback(title, [])
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →