Sample questions
Question 1Easy
Which rule of hooks does this code break?
function App({ on }) {
if (on) {
const [n, setN] = useState(0);
}
return null;
}
- Hooks may only be used in class components
- useState must be given an object
- Hooks must not be called conditionally; call them at the top level every render
- A hook name must start with a capital letter
Answer: C. Hooks must not be called conditionally; call them at the top level every render
Explanation
React tracks hooks by call order, so they must run in the same order on every render, never inside conditions or loops.
Step-by-step solution
- Hooks are matched by order.
- A conditional hook changes the order.
Why the other options are wrong
- A. Hooks are for function components.
- B. Any value type is fine.
- D. Hooks start with the prefix use.
Question 2Easy
What happens when you change ref.current for a ref created with useRef?
- The component re-renders with the new value
- The value changes, but no re-render is triggered
- React throws an error because refs are immutable
- The ref is reset to its initial value on next render
Answer: B. The value changes, but no re-render is triggered
Explanation
A ref is a mutable box that persists across renders. Changing it is not tracked by React, which makes it suitable for values that should not affect rendering.
Step-by-step solution
- Refs from useRef are plain mutable objects.
- Changing ref.current mutates the value directly.
- React does not track it, so no re-render happens.
Why the other options are wrong
- A. Re-render happens with state, not refs.
- C. Refs are mutable by design.
- D. The ref is not reset.
Question 3Easy
The button is clicked once. What is logged?
function Box() {
const [n, setN] = useState(0);
return <button id="b" onClick={() => { setN(5); console.log(n); }}>{n}</button>;
}
- 5
- undefined
- Nothing is logged
- 0
Answer: D. 0
Explanation
n is a snapshot from this render, so the log still shows 0 although the state is set to 5.
Step-by-step solution
- setN(5) schedules a re-render.
- The current handler still sees n = 0.
- Logs 0.
Why the other options are wrong
- A. State updates do not change the variable in the current render.
- B. n is defined.
- C. The handler logs n.
Tip: State is a snapshot per render.
Question 4Easy
What does useState return?
- A Promise that resolves to the state
- An object with get and set methods
- Only the current state value
- An array: [value, setValue]
Answer: D. An array: [value, setValue]
Explanation
const [value, setValue] = useState(initial). Calling setValue schedules a re-render with the new value.
Step-by-step solution
- useState(initial) returns a two-item array.
- The first item is the current value and the second is the setter function.
- So the correct answer is option D: [value, setValue].
Why the other options are wrong
- A. UseState does not return a Promise; it returns the array right away.
- B. It returns an array, not an object with get and set methods.
- C. It returns the current value and a setter, not just the value.
Tip: Array destructuring names the pair: const [value, setValue] = useState(0).
Question 5Medium
The button is clicked once. What does it display afterwards?
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
function handleClick() {
setCount(c => c + 1);
setCount(c => c + 1);
}
return <button onClick={handleClick}>{count}</button>;
}
- 4
- 1
- 2
- 0
Answer: C. 2
Explanation
Updater functions are queued and each receives the result of the previous one: 0 to 1, then 1 to 2.
Step-by-step solution
- Each call setCount(c => c + 1) is an updater function.
- React queues both updaters and applies them in order to the latest value.
- 0 becomes 1, then 1 becomes 2.
- The button shows 2.
Why the other options are wrong
- A. 4 would need doubling twice; updates add 1 each.
- B. 1 happens with setCount(count + 1) twice, which uses a stale count.
- D. 0 ignores that the state update ran.
Tip: Use the updater form when the new state depends on the old one.
Question 6Medium
The button is clicked once. What does it show?
function reducer(state, action) {
switch (action.type) {
case 'inc': return { n: state.n + 1 };
case 'dec': return { n: state.n - 1 };
default: return state;
}
}
function App() {
const [s, dispatch] = useReducer(reducer, { n: 0 });
return <button id="b" onClick={() => { dispatch({ type: 'inc' }); dispatch({ type: 'inc' }); dispatch({ type: 'dec' }); dispatch({ type: 'noop' }); }}>{s.n}</button>;
}
- 1
- 2
- 0
- 3
Answer: A. 1
Explanation
The actions run in order: +1, +1, -1, then noop returns the state unchanged, so n = 1.
Step-by-step solution
- 0 -> 1 -> 2 -> 1.
- noop leaves the state alone.
- Result 1.
Why the other options are wrong
- B. The decrement reduces the total by one.
- C. Two increments and one decrement do not cancel out.
- D. Only inc and dec change the value.
Question 7Medium
The button is clicked twice. What does it show?
function App() {
const [list, setList] = useState([1, 2]);
return <button id="b" onClick={() => setList([...list, list.length + 1])}>{list.join('-')}</button>;
}
- 1-2
- 1-2-2
- 3
- 1-2-3-4
Answer: D. 1-2-3-4
Explanation
Spread copies the old items and appends the new length-based value; two clicks give 1-2-3-4.
Step-by-step solution
- Click 1: [1,2,3].
- Click 2: [1,2,3,4].
Why the other options are wrong
- A. The new array triggers a re-render.
- B. The new value is list.length + 1 = 3.
- C. The old items are kept via spread.
Question 8Medium
Two components both call the same custom hook useCounter(). What happens to the state?
- Each component gets its own independent state
- They share one state value
- The second call overwrites the first
- React throws an error about duplicate hooks
Answer: A. Each component gets its own independent state
Explanation
A custom hook is a reusable function; every call creates its own state slots in the calling component.
Step-by-step solution
- Each call to useState inside creates new state.
- State is per component instance.
Why the other options are wrong
- B. Hooks share logic, not state.
- C. There is no overwriting.
- D. Using a hook in many components is the whole purpose.
Question 9Hard
<App /> is mounted and the button is clicked once. What is logged?
function App() {
const [n, setN] = useState(0);
const [m, setM] = useState(0);
console.log('render');
return <button id="b" onClick={() => { setN(n + 1); setM(m + 1); }}>{n + m}</button>;
}
- render, render, render
- render
- render, render, render, render
- render, render
Answer: D. render, render
Explanation
Two state updates in one event handler are batched, so the click causes just one re-render: render on mount, render after click.
Step-by-step solution
- Mount: render.
- Click: two setters batched.
- One more render.
Why the other options are wrong
- A. Updates in one event handler are batched into one render.
- B. Mount renders once and the click renders again.
- C. Only two renders happen.
Question 10Hard
The component renders once and the button is then clicked twice. How many times is "init" logged?
import { useState } from "react";
function init() {
console.log("init");
return 0;
}
function Counter() {
const [count, setCount] = useState(init());
return <button onClick={() => setCount(count + 1)}>{count}</button>;
}
- 0
- 1
- 2
- 3
Answer: D. 3
Explanation
init() is an ordinary call that runs on every render (3 renders), even though React uses its result only the first time. Pass the function itself, useState(init), to run it once.
Step-by-step solution
- init() is called in the useState argument, so it runs on every render of Counter.
- The component renders once on mount, then re-renders after each of the two clicks.
- So init runs on 3 renders, and "init" is logged 3 times.
- The correct answer is option D.
Why the other options are wrong
- A. Init is called at least once on the first render, so it logs at least once.
- B. There are three renders in total, so init is logged more than once.
- C. The mount plus two click renders make 3 calls, not 2.
Tip: Pass a function to useState (useState(init)) so it runs only on the first render.
Practise all 31 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →