Sample questions
Question 1Easy
<List /> is rendered. What text does it show?
function Row({ label, value }) {
return <li>{label}: {value}</li>;
}
function List() {
const data = { label: 'Age', value: 21 };
return <ul><Row {...data} /></ul>;
}
- label: value
- Age: 21
- Age: undefined
- Age:
Answer: B. Age: 21
Explanation
The spread passes each key of data as a prop, so label is "Age" and value is 21.
Step-by-step solution
- {...data} expands to label and value props.
- Row renders "Age: 21".
Why the other options are wrong
- A. Props are values, not names.
- C. The spread passes both props.
- D. The spread includes value as well.
Question 2Easy
Why must a custom React component name start with a capital letter, as in <UserCard />?
- It is only a style convention and has no effect on rendering
- Lowercase names are reserved for class components in React
- Capitalised components are memoised and so render faster
- JSX treats lowercase tags as HTML elements, not your component
Answer: D. JSX treats lowercase tags as HTML elements, not your component
Explanation
JSX compiles <div> to the string "div" but <UserCard> to a reference to the UserCard variable. A lowercase name is treated as an HTML tag.
Step-by-step solution
- JSX turns a lowercase tag name such as div into the string "div", which React treats as an HTML element.
- JSX turns a capitalised tag name such as UserCard into a reference to the variable UserCard.
- So a capital letter tells JSX to use your component instead of an HTML element.
- The correct answer is option D.
Why the other options are wrong
- A. The capital letter changes how JSX compiles the tag, so it has an effect.
- B. Lowercase names are not reserved for class components.
- C. Capitalised names do not make a component memoised or faster.
Tip: Lowercase JSX tags mean HTML elements; capitalised tags mean components.
Question 3Easy
Can a component modify the props it receives (for example props.title = "x")?
- Yes, props are ordinary variables owned by the child
- Only when the prop's value is a string or number
- No, treat props as read-only; use state for changes
- Only in class components, through this.props
Answer: C. No, treat props as read-only; use state for changes
Explanation
Props are owned by the parent. A component that needs changing data keeps it in state or asks the parent to change it through a callback prop.
Step-by-step solution
- Props belong to the parent, and the child receives them as read-only values.
- Changing props.title inside the child breaks this rule and can cause hard-to-trace bugs.
- Data that the component must change belongs in state, or the parent changes it through a callback prop.
- So the correct answer is option C.
Why the other options are wrong
- A. The child does not own its props; the parent does.
- B. The type of the value does not decide whether props can be changed; props are read-only for every type.
- D. Class components do not allow changing props either; this.props is read-only as well.
Tip: Treat props as read-only; use state for values that change inside the component.
Question 4Easy
<Greet /> is rendered with no props. What text does it show?
function Greet({ name = 'Guest' }) {
return <h2>Hello, {name}</h2>;
}
- Hello, undefined
- Hello,
- Hello, Guest
- Hello, name
Answer: C. Hello, Guest
Explanation
The destructuring default "Guest" is used because name is undefined.
Step-by-step solution
- No name prop is passed.
- undefined triggers the default.
- Output: Hello, Guest.
Why the other options are wrong
- A. Defaults apply when the prop is undefined.
- B. The default fills the missing prop.
- D. name is a variable, not literal text.
Question 5Medium
<App /> is rendered. What text does it show (no separators)?
const Ctx = createContext('root');
function Reader() { return <strong>{useContext(Ctx)}</strong>; }
function App() {
return (
<Ctx.Provider value="A">
<Reader />
<Ctx.Provider value="B">
<Reader />
</Ctx.Provider>
</Ctx.Provider>
);
}
- AA
- rootroot
- AB
- BB
Answer: C. AB
Explanation
A component reads the value of its closest Provider: the first Reader is under A and the second under the nested B.
Step-by-step solution
- First Reader: closest Provider is A.
- Second Reader: nested Provider B.
- Output: AB.
Why the other options are wrong
- A. The inner provider overrides the value for its subtree.
- B. Both readers are inside providers.
- D. The first Reader is outside the inner provider.
Question 6Medium
What text does <Cart items={[]} /> render?
function Cart({ items }) {
return <div>{items.length && <p>You have items</p>}</div>;
}
- You have items
- 0
- false
- Nothing (an empty div)
Answer: B. 0
Explanation
items.length is 0, so && returns 0. React does not render false, null or undefined, but it does render the number 0. Use items.length > 0 && ... instead.
Step-by-step solution
- Items is [], so items.length is 0.
- 0 && (a paragraph element) evaluates to 0, because && returns the first falsy value.
- React does not render false, null or undefined, but it does render the number 0.
- So the output is the text 0, which is option B.
Why the other options are wrong
- A. The paragraph is not shown, because the && short-circuits on 0.
- C. False would render nothing; here the falsy value is the number 0, which React prints.
- D. The div is not empty; the number 0 is shown inside it.
Tip: Write items.length > 0 && ... to get a boolean, not a number, on the left of &&.
Question 7Medium
<Show items={[1, 2]} /> is rendered. What text does it show (all text nodes joined with no separator)?
function Show({ items }) {
if (!items.length) return <h2>Empty</h2>;
return <ul>{items.map(i => <li key={i}>{i}</li>)}</ul>;
}
- Empty
- 1, 2
- 12
- [1,2]
Answer: C. 12
Explanation
The array is not empty, so the list renders its items one after another.
Step-by-step solution
- items has two elements.
- The early return is skipped.
- Text content: 12.
Why the other options are wrong
- A. An array with elements renders the list.
- B. Text nodes are concatenated without separators.
- D. The list items render as text.
Question 8Medium
<App /> is mounted. Assume a click on the button is simulated if a handler exists. What is logged in total?
function Btn({ onClick }) {
return <button id="b" onClick={onClick}>go</button>;
}
function App() {
const hit = () => console.log('hit');
return <Btn onClick={hit()} />;
}
- hit, hit
- hit
- Nothing is logged
- TypeError
Answer: B. hit
Explanation
onClick={hit()} runs hit during render and passes its undefined result as the handler, so there is one log from rendering and the click does nothing.
Step-by-step solution
- hit() executes during render: logs once.
- The handler passed down is undefined.
- Clicking does nothing more.
Why the other options are wrong
- A. The click does not call anything, because the handler is undefined.
- C. hit() is called while rendering.
- D. Clicking with an undefined handler is a no-op in this harness.
Tip: Pass a function: onClick={hit} or onClick={() => hit()}.
Question 9Hard
<Probe>{["a", null, "b"]}</Probe> is rendered. What number does it show?
function Probe({ children }) {
return <h2>{React.Children.count(children)}</h2>;
}
- 2
- 3
- 4
- 1
Answer: B. 3
Explanation
Children.count counts strings, numbers, elements and empty nodes; arrays are flattened, so the items are a, null and b.
Step-by-step solution
- The array is flattened.
- Items: a, null, b.
- Three nodes are counted.
Why the other options are wrong
- A. Empty nodes such as null still count as individual nodes.
- C. The array itself is not counted in addition to its items.
- D. Each child is counted.
Question 10Hard
In React 19, what does this log after <Form /> renders?
import { useRef, useEffect } from "react";
function MyInput({ ref }) {
return <input ref={ref} />;
}
function Form() {
const inputRef = useRef(null);
useEffect(() => {
console.log(inputRef.current.tagName);
}, []);
return <MyInput ref={inputRef} />;
}
- null
- A TypeError, because forwardRef is required
- undefined
- INPUT
Answer: D. INPUT
Explanation
Since React 19, function components receive ref as a regular prop, so forwardRef is no longer needed. The ref is attached to the input before effects run.
Step-by-step solution
- In React 19, ref is passed to MyInput as a normal prop, so forwardRef is not needed.
- MyInput puts that ref on the input element.
- Form's effect runs after the commit, so inputRef.current is the input DOM node.
- TagName of an input is "INPUT", so the log shows INPUT, which is option D.
Why the other options are wrong
- A. The ref is attached before the effect runs, so it is not null.
- B. No error happens; React 19 accepts ref as a prop.
- C. The ref is set, so the value is not undefined; tagName is the string "INPUT".
Tip: In React 19, read ref from props for function components; forwardRef is optional now.
Practise all 18 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 →