React MCQ questions
Take a short practice test with instant feedback, then revise every question with its answer and the reason behind it.
- Questions
- 136
- From lessons
- 116
- Your last test
- —
Practice test
Which React Router hook reads :id from /products/:id?
All questions with answers
What does JSX compile to?
Show answer
Answer: B. React.createElement / jsx() calls
JSX is syntax sugar for function calls that create React elements.
Which attribute sets a CSS class in JSX?
Show answer
Answer: B. className
class is a reserved word in JavaScript, so JSX uses className.
What must a component name start with?
Show answer
Answer: B. An uppercase letter
Lowercase tags are treated as HTML elements.
What is wrong with this handler?
TSX Illustrative <button onClick={save()}>Save</button>Show answer
Answer: B. save runs during render instead of on click
Pass the function itself: onClick={save} or onClick={() => save(id)}.
Props are…
Show answer
Answer: B. Read-only inputs from the parent
A component must never modify its props.
What does useState return?
Show answer
Answer: B. A pair: the value and a setter
const [value, setValue] = useState(initial).
When does an effect with an empty dependency array run?
Show answer
Answer: B. After the first render (and cleanup on unmount)
[] means the effect has no dependencies, so it runs once after mounting.
What renders when count is 0?
TSX Illustrative {count && <span>{count} new</span>}Show answer
Answer: B. 0
0 is a valid child and is rendered. Use count > 0 && … instead.
Which hook gives a mutable value that does not trigger re-renders?
Show answer
Answer: C. useRef
Changing ref.current does not re-render the component.
What is the best key for list items?
Show answer
Answer: C. A stable unique id from the data
Stable ids let React match items between renders.
How do you update an object in state correctly?
Show answer
Answer: B. setUser({ ...user, name: "Asha" })
Create a new object so React sees a new reference.
Which hook reads a context value?
Show answer
Answer: B. useContext
useContext(MyContext) returns the nearest provider’s value.
What does React.memo do?
Show answer
Answer: B. Skips re-rendering when props are shallowly equal
It wraps a component; useMemo memoises values inside one.
Which hook memoises a callback function?
Show answer
Answer: B. useCallback
useCallback(fn, deps) returns the same function until deps change.
What is a controlled input?
Show answer
Answer: B. An input whose value comes from state and updates via onChange
React state is the single source of truth for its value.
Which component catches errors thrown while rendering its children?
Show answer
Answer: B. An error boundary
Error boundaries are class components with getDerivedStateFromError or componentDidCatch.
Which React Router hook reads :id from /products/:id?
Show answer
Answer: B. useParams
useParams returns an object of URL parameters.
What is count after add() runs once?
TSX Illustrative const [count, setCount] = useState(0); const add = () => { setCount(count + 1); setCount(count + 1); }; // after add() runs once, count is…Show answer
Answer: A. 1
Both calls read the same count (0) from this render, so the state becomes 1.
What does <Suspense fallback={…}> show?
Show answer
Answer: B. The fallback while lazy children are loading
Suspense displays the fallback until lazy components or suspending data are ready.
Which command creates a production build in a Vite React app?
Show answer
Answer: B. npm run build
vite build outputs optimised static files into dist/.
count starts at 0. What is it after one click?
JSX Illustrative <button onClick={() => { setCount(count + 1); setCount(count + 1); }}>Add</button>Show answer
Answer: B. 1
Both calls read count from the same render (0), so both set it to 1. Use setCount(c => c + 1) to get 2.
From the lesson: useStateCalling items.push(newItem) and then setItems(items) reliably updates the UI.
Show answer
Answer: B. False
The array reference has not changed, so React may skip the update. Create a new array: setItems([...items, newItem]).
From the lesson: useStateWhen does an effect with an empty dependency array [] run?
Show answer
Answer: B. Only after the first render (and again in dev Strict Mode)
An empty array means no reactive dependencies, so it runs after mount. Strict Mode intentionally mounts twice in development.
From the lesson: useEffectWhy does this component render forever?
JSX Illustrative const [count, setCount] = useState(0); useEffect(() => { setCount(count + 1); }, [count]);Show answer
Answer: B. The effect updates count, which is its own dependency, so it re-runs endlessly
Each run changes count, which triggers another run. Usually the fix is to remove the effect and update count in an event handler.
From the lesson: useEffectYou need useEffect to calculate a filtered list from props.
Show answer
Answer: B. False
Compute it during render (wrapping in useMemo only if it is genuinely expensive).
From the lesson: useEffect