1In React, how are event handlers generally named?
A.Lowercase (onclick)
B.CamelCase (onClick)
C.Kebab-case (on-click)
D.PascalCase (OnClick)
Correct Answer: CamelCase (onClick)
Explanation:React events are named using camelCase, rather than lowercase.
Incorrect! Try again.
2Which method is used to prevent the default behavior of a form submission in React?
A.return false
B.e.stopPropogation()
C.e.preventDefault()
D.e.cancelBubble = true
Correct Answer: e.preventDefault()
Explanation:In React, you cannot return false to prevent default behavior; you must call preventDefault on the event object explicitly.
Incorrect! Try again.
3What is the cross-browser wrapper around the browser's native event that React uses?
A.NativeEvent
B.BrowserEvent
C.SyntheticEvent
D.ReactEvent
Correct Answer: SyntheticEvent
Explanation:React uses SyntheticEvent to wrap native events to ensure cross-browser compatibility.
Incorrect! Try again.
4When passing a function to an event handler in React, why should you generally avoid calling it immediately (e.g., onClick={handleClick()})?
A.It causes a syntax error
B.It executes the function when the component renders, not when clicked
C.It prevents the event object from being passed
D.It changes the scope of 'this'
Correct Answer: It executes the function when the component renders, not when clicked
Explanation:Adding parentheses executes the function immediately during rendering. You should pass the function reference or use an arrow function wrapper.
Incorrect! Try again.
5What is 'State' in a React component?
A.External configuration passed from a parent
B.A static object that never changes
C.Internal data storage that is mutable and affects rendering
D.A global store for the entire application
Correct Answer: Internal data storage that is mutable and affects rendering
Explanation:State is an object managed within the component similar to variables declared in a function, but changing it triggers a re-render.
Incorrect! Try again.
6Which method is used to update state in a Class Component?
A.this.state = {...}
B.this.updateState(...)
C.this.setState(...)
D.this.changeState(...)
Correct Answer: this.setState(...)
Explanation:In class components, state must be updated using this.setState() to ensure React schedules a re-render.
Incorrect! Try again.
7What defines a 'Stateless Component' prior to React 16.8?
A.A component that has no props
B.A component defined as a class
C.A functional component that does not manage internal state
D.A component that does not render HTML
Correct Answer: A functional component that does not manage internal state
Explanation:Before Hooks, functional components were stateless and only received props. They were often called 'Dumb' or 'Presentational' components.
Incorrect! Try again.
8Why is this.setState() considered asynchronous?
A.It uses Promises internally only
B.React batches state updates for performance gains
C.JavaScript is single-threaded
D.It requires a server call
Correct Answer: React batches state updates for performance gains
Explanation:React may batch multiple setState() calls into a single update for performance, meaning state might not change immediately after the call.
Incorrect! Try again.
9Which lifecycle phase occurs when a component is being created and inserted into the DOM?
A.Updating
B.Unmounting
C.Mounting
D.rendering
Correct Answer: Mounting
Explanation:Mounting is the phase where the component is initialized and added to the DOM.
Incorrect! Try again.
10Which lifecycle method is the best place to make API calls in a Class Component?
A.componentWillMount
B.render
C.componentDidMount
D.constructor
Correct Answer: componentDidMount
Explanation:componentDidMount runs after the component output has been rendered to the DOM, making it the ideal place for side effects like API calls.
Incorrect! Try again.
11Which lifecycle method is called right before a component is removed from the DOM?
A.componentDidUnmount
B.componentWillUnmount
C.componentDidCatch
D.componentWillUpdate
Correct Answer: componentWillUnmount
Explanation:componentWillUnmount is invoked immediately before a component is unmounted and destroyed, used for cleanup.
Incorrect! Try again.
12Which lifecycle method can be used to optimize performance by preventing unnecessary re-renders?
A.shouldComponentUpdate
B.componentDidUpdate
C.getDerivedStateFromProps
D.render
Correct Answer: shouldComponentUpdate
Explanation:shouldComponentUpdate allows a developer to return false to prevent the rendering process if props or state haven't changed significantly.
Incorrect! Try again.
13What is the only required method in a React Class Component?
A.constructor
B.componentDidMount
C.render
D.setState
Correct Answer: render
Explanation:The render method is the only required method in a class component; it returns the React elements to display.
Incorrect! Try again.
14React Hooks were introduced in which version?
A.15.0
B.16.0
C.16.8
D.17.0
Correct Answer: 16.8
Explanation:Hooks were introduced in React 16.8 to allow state and other React features in functional components.
Incorrect! Try again.
15Which of the following is a 'Rule of Hooks'?
A.Hooks can be called inside loops
B.Hooks can be called inside nested functions
C.Hooks must be called at the top level of a React function
D.Hooks can be called in Class Components
Correct Answer: Hooks must be called at the top level of a React function
Explanation:Hooks must be called at the top level to ensure they run in the same order each time a component renders.
Incorrect! Try again.
16What does the useState hook return?
A.The current state value
B.A function to update the state
C.An array containing the current state and an updater function
D.An object containing state and setState
Correct Answer: An array containing the current state and an updater function
Explanation:useState returns an array with two items: the current state value and a function that lets you update it.
Incorrect! Try again.
17What is the correct syntax to initialize a state count to 0 using hooks?
Explanation:When new state depends on old state, it is best practice to pass a callback function to the setter to ensure you are working with the most recent value.
Incorrect! Try again.
19Which hook is used to perform side effects like data fetching or DOM manipulation?
A.useState
B.useReducer
C.useEffect
D.useContext
Correct Answer: useEffect
Explanation:useEffect is designed to handle side effects in functional components, replacing lifecycle methods like componentDidMount.
Incorrect! Try again.
20If you pass an empty array [] as the second argument to useEffect, when does the effect run?
A.On every render
B.Only when the component mounts
C.Only when the component unmounts
D.Whenever state changes
Correct Answer: Only when the component mounts
Explanation:An empty dependency array tells React that the effect doesn't depend on any values from props or state, so it only runs once on mount.
Incorrect! Try again.
21If the dependency array is omitted entirely in useEffect, when does the effect run?
A.Only on mount
B.After every completed render
C.Only on unmount
D.Never
Correct Answer: After every completed render
Explanation:Without a dependency array, the effect runs after every single render cycle.
Incorrect! Try again.
22What is the purpose of the return function inside a useEffect hook?
A.To return the JSX to be rendered
B.To update the state
C.To cleanup side effects (like clearing timers)
D.To stop the component from rendering
Correct Answer: To cleanup side effects (like clearing timers)
Explanation:The function returned by useEffect is the cleanup function, which runs before the component unmounts or before the effect runs again.
Incorrect! Try again.
23Which hook is primarily used to avoid prop drilling?
A.useReducer
B.useCallback
C.useContext
D.useRef
Correct Answer: useContext
Explanation:useContext allows components to subscribe to React context without introducing nesting, solving the issue of passing props down multiple levels.
Incorrect! Try again.
24What argument does useContext accept?
A.A string key
B.The initial state
C.A Context object (created by React.createContext)
D.A reducer function
Correct Answer: A Context object (created by React.createContext)
Explanation:useContext accepts the context object (the value returned from React.createContext) and returns the current context value.
Incorrect! Try again.
25Which hook allows you to access a DOM element directly?
A.useState
B.useEffect
C.useRef
D.useDom
Correct Answer: useRef
Explanation:useRef returns a mutable ref object whose .current property is initialized to the passed argument, often used to access DOM nodes.
Incorrect! Try again.
26Does changing the .current property of a useRef object trigger a re-render?
A.Yes, always
B.No
C.Only if dependencies change
D.Only in strict mode
Correct Answer: No
Explanation:useRef updates are synchronous and do not notify React of changes, so they do not trigger a re-render.
Incorrect! Try again.
27Which hook is preferable for complex state logic that involves multiple sub-values?
A.useState
B.useReducer
C.useEffect
D.useMemo
Correct Answer: useReducer
Explanation:useReducer is usually preferable to useState when you have complex state logic or when the next state depends on the previous one.
Incorrect! Try again.
28What are the two arguments accepted by useReducer?
A.reducer function and initial state
B.initial state and dispatch
C.callback and dependency array
D.reducer function and dependency array
Correct Answer: reducer function and initial state
Explanation:useReducer(reducer, initialState) accepts a reducer of type (state, action) => newState, and the initial state.
Incorrect! Try again.
29Which hook returns a memoized version of a callback function?
A.useMemo
B.useCallback
C.useEffect
D.useRef
Correct Answer: useCallback
Explanation:useCallback returns a memoized callback function that only changes if one of the dependencies has changed.
Incorrect! Try again.
30Which hook returns a memoized value?
A.useCallback
B.useMemo
C.useState
D.useReducer
Correct Answer: useMemo
Explanation:useMemo returns a memoized value (the result of a function) to avoid expensive calculations on every render.
Incorrect! Try again.
31When should you use useMemo?
A.For every calculation
B.To cache expensive calculations
C.To handle HTTP requests
D.To update the DOM
Correct Answer: To cache expensive calculations
Explanation:useMemo is an optimization tool used to avoid recalculating expensive functions on every render unless dependencies change.
Incorrect! Try again.
32What is a 'Custom Hook'?
A.A built-in React hook modified by the user
B.A JavaScript function whose name starts with 'use' and may call other hooks
C.A class component converted to a function
D.A component that returns JSX
Correct Answer: A JavaScript function whose name starts with 'use' and may call other hooks
Explanation:Custom hooks allow you to extract component logic into reusable functions. They must start with 'use' to let React check for violations of rules of hooks.
Incorrect! Try again.
33In the Class Component lifecycle, getSnapshotBeforeUpdate is called:
A.After render but before updates are committed to the DOM
B.Before render
C.After componentDidUpdate
D.During the mounting phase
Correct Answer: After render but before updates are committed to the DOM
Explanation:It enables the component to capture information (like scroll position) from the DOM before it is potentially changed.
Incorrect! Try again.
34Which of the following statements about useState is TRUE?
A.It merges the new state with the old state automatically
B.It replaces the old state with the new state completely
C.It can only be used once per component
D.It must be used inside a loop
Correct Answer: It replaces the old state with the new state completely
Explanation:Unlike this.setState in classes (which merges), the useState updater function replaces the state variable with the new value.
Incorrect! Try again.
35If you want to persist a value across renders without causing a re-render, which hook should you use?
A.useState
B.useMemo
C.useRef
D.useCallback
Correct Answer: useRef
Explanation:useRef holds a mutable value in its .current property that persists for the full lifetime of the component without triggering re-renders.
Incorrect! Try again.
36Can hooks be used inside conditional statements (if/else)?
A.Yes, always
B.No, never
C.Only useEffect
D.Only in development mode
Correct Answer: No, never
Explanation:Hooks rely on the order of execution to track state. Placing them in conditions breaks this order and causes errors.
Incorrect! Try again.
37In a reducer function used with useReducer, what is the return value?
A.The action object
B.The previous state
C.The new state
D.A boolean indicating success
Correct Answer: The new state
Explanation:A reducer function takes state and action, and returns the new state.
Incorrect! Try again.
38Which hook is equivalent to componentDidMount, componentDidUpdate, and componentWillUnmount combined?
A.useState
B.useEffect
C.useLayoutEffect
D.useLifecycle
Correct Answer: useEffect
Explanation:useEffect can replicate the behavior of these three lifecycle methods based on how the dependency array and return function are configured.
Incorrect! Try again.
39How do you access the value stored in a ref object named 'inputRef'?
A.inputRef.value
B.inputRef.current
C.inputRef.get()
D.inputRef.state
Correct Answer: inputRef.current
Explanation:The ref object returned by useRef has a single property called .current.
Incorrect! Try again.
40What happens if you update a state hook with the exact same value as the current state?
A.React triggers a re-render anyway
B.React throws an error
C.React bails out without rendering the children or firing effects
D.The component unmounts
Correct Answer: React bails out without rendering the children or firing effects
Explanation:React uses Object.is comparison algorithm. If the value is the same, React skips the render update.
Incorrect! Try again.
41Which lifecycle method is deprecated in recent React versions (16.3+)?
A.componentDidMount
B.componentWillMount
C.componentDidUpdate
D.componentWillUnmount
Correct Answer: componentWillMount
Explanation:componentWillMount (along with componentWillReceiveProps and componentWillUpdate) is considered unsafe and deprecated.
Incorrect! Try again.
42What is the primary use of the 'key' prop in lists which relates to component lifecycle/state?
A.Styling the list items
B.Helping React identify which items have changed, added, or removed
C.Passing data to the child
D.Setting the initial state
Correct Answer: Helping React identify which items have changed, added, or removed
Explanation:Keys help React manage state and the DOM efficiently by tracking identity. Poor key usage can lead to state issues.
Incorrect! Try again.
43When using useMemo, what determines when the value is recalculated?
A.Every 1000ms
B.Whenever the component re-renders
C.When one of the variables in the dependency array changes
D.When the user clicks a button
Correct Answer: When one of the variables in the dependency array changes
Explanation:useMemo only re-computes the memoized value when one of the dependencies has changed.
Incorrect! Try again.
44What is 'Prop Drilling'?
A.A tool to inspect props
B.The process of passing data from a parent to a deeply nested child through intermediate components
C.Defining default props
D.Validating props types
Correct Answer: The process of passing data from a parent to a deeply nested child through intermediate components
Explanation:Prop drilling refers to passing props through multiple layers of components that don't need the data themselves, just to reach a child that does.
Incorrect! Try again.
45Which hook serves as an alternative to useState for managing complex state transitions?
A.useContext
B.useReducer
C.useCallback
D.useLayoutEffect
Correct Answer: useReducer
Explanation:useReducer follows the Redux pattern and is ideal for complex state logic.
Incorrect! Try again.
46Can you use built-in hooks inside a Custom Hook?
A.Yes
B.No
C.Only useState
D.Only useEffect
Correct Answer: Yes
Explanation:Custom hooks are designed to compose logic using built-in hooks.
Incorrect! Try again.
47What is the signature of the dispatch function returned by useReducer?
A.dispatch(newState)
B.dispatch(action)
C.dispatch(reducer)
D.dispatch(state, action)
Correct Answer: dispatch(action)
Explanation:You call dispatch passing an action object (usually with a type property) to trigger a state update.
Incorrect! Try again.
48Which hook fires synchronously after all DOM mutations but before the browser paints?
A.useEffect
B.useLayoutEffect
C.useState
D.useInsertionEffect
Correct Answer: useLayoutEffect
Explanation:useLayoutEffect is identical to useEffect, but it fires synchronously after DOM mutations. Use it to read layout from the DOM and synchronously re-render.
Incorrect! Try again.
49In a class component, where should you initialize state?
A.In the render method
B.In componentDidMount
C.In the constructor
D.In componentWillUnmount
Correct Answer: In the constructor
Explanation:The constructor is the standard place to initialize this.state in a class component.
Incorrect! Try again.
50What is the naming convention for a custom hook file?
A.Must allow uppercase
B.Must start with 'use' (e.g., useForm)
C.Must end with 'Hook'
D.Must be in the hooks folder
Correct Answer: Must start with 'use' (e.g., useForm)
Explanation:The 'use' prefix allows the React linter to automatically enforce the Rules of Hooks.
Incorrect! Try again.
Give Feedback
Help us improve by sharing your thoughts or reporting issues.