Unit 3 - Practice Quiz

INT252 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 In React, how are event handlers generally named?

A. Kebab-case (on-click)
B. PascalCase (OnClick)
C. Lowercase (onclick)
D. CamelCase (onClick)

2 Which method is used to prevent the default behavior of a form submission in React?

A. e.stopPropogation()
B. return false
C. e.cancelBubble = true
D. e.preventDefault()

3 What is the cross-browser wrapper around the browser's native event that React uses?

A. SyntheticEvent
B. ReactEvent
C. BrowserEvent
D. NativeEvent

4 When passing a function to an event handler in React, why should you generally avoid calling it immediately (e.g., onClick={handleClick()})?

A. It changes the scope of 'this'
B. It causes a syntax error
C. It prevents the event object from being passed
D. It executes the function when the component renders, not when clicked

5 What is 'State' in a React component?

A. External configuration passed from a parent
B. A global store for the entire application
C. A static object that never changes
D. Internal data storage that is mutable and affects rendering

6 Which method is used to update state in a Class Component?

A. this.updateState(...)
B. this.state = {...}
C. this.setState(...)
D. this.changeState(...)

7 What defines a 'Stateless Component' prior to React 16.8?

A. A component defined as a class
B. A component that has no props
C. A functional component that does not manage internal state
D. A component that does not render HTML

8 Why is this.setState() considered asynchronous?

A. It requires a server call
B. It uses Promises internally only
C. React batches state updates for performance gains
D. JavaScript is single-threaded

9 Which lifecycle phase occurs when a component is being created and inserted into the DOM?

A. rendering
B. Mounting
C. Unmounting
D. Updating

10 Which lifecycle method is the best place to make API calls in a Class Component?

A. componentWillMount
B. componentDidMount
C. constructor
D. render

11 Which lifecycle method is called right before a component is removed from the DOM?

A. componentWillUpdate
B. componentDidUnmount
C. componentWillUnmount
D. componentDidCatch

12 Which lifecycle method can be used to optimize performance by preventing unnecessary re-renders?

A. componentDidUpdate
B. shouldComponentUpdate
C. getDerivedStateFromProps
D. render

13 What is the only required method in a React Class Component?

A. setState
B. render
C. constructor
D. componentDidMount

14 React Hooks were introduced in which version?

A. 16.8
B. 17.0
C. 15.0
D. 16.0

15 Which of the following is a 'Rule of Hooks'?

A. Hooks must be called at the top level of a React function
B. Hooks can be called inside loops
C. Hooks can be called in Class Components
D. Hooks can be called inside nested functions

16 What does the useState hook return?

A. A function to update the state
B. An array containing the current state and an updater function
C. An object containing state and setState
D. The current state value

17 What is the correct syntax to initialize a state count to 0 using hooks?

A. useState(count = 0);
B. const {count, setCount} = useState(0);
C. const [count, setCount] = useState(0);
D. const count = useState(0);

18 How do you update state based on the previous state value using the setCount function?

A. count = count + 1
B. setCount(prevCount => prevCount + 1)
C. setCount(count + 1)
D. setCount(this.state.count + 1)

19 Which hook is used to perform side effects like data fetching or DOM manipulation?

A. useEffect
B. useState
C. useContext
D. useReducer

20 If you pass an empty array [] as the second argument to useEffect, when does the effect run?

A. Only when the component unmounts
B. Whenever state changes
C. Only when the component mounts
D. On every render

21 If the dependency array is omitted entirely in useEffect, when does the effect run?

A. After every completed render
B. Only on unmount
C. Never
D. Only on mount

22 What is the purpose of the return function inside a useEffect hook?

A. To cleanup side effects (like clearing timers)
B. To return the JSX to be rendered
C. To update the state
D. To stop the component from rendering

23 Which hook is primarily used to avoid prop drilling?

A. useReducer
B. useRef
C. useCallback
D. useContext

24 What argument does useContext accept?

A. A reducer function
B. A string key
C. The initial state
D. A Context object (created by React.createContext)

25 Which hook allows you to access a DOM element directly?

A. useDom
B. useState
C. useEffect
D. useRef

26 Does changing the .current property of a useRef object trigger a re-render?

A. No
B. Yes, always
C. Only in strict mode
D. Only if dependencies change

27 Which hook is preferable for complex state logic that involves multiple sub-values?

A. useState
B. useMemo
C. useReducer
D. useEffect

28 What are the two arguments accepted by useReducer?

A. reducer function and initial state
B. reducer function and dependency array
C. callback and dependency array
D. initial state and dispatch

29 Which hook returns a memoized version of a callback function?

A. useMemo
B. useCallback
C. useRef
D. useEffect

30 Which hook returns a memoized value?

A. useMemo
B. useCallback
C. useReducer
D. useState

31 When should you use useMemo?

A. To handle HTTP requests
B. For every calculation
C. To update the DOM
D. To cache expensive calculations

32 What is a 'Custom Hook'?

A. A built-in React hook modified by the user
B. A class component converted to a function
C. A JavaScript function whose name starts with 'use' and may call other hooks
D. A component that returns JSX

33 In the Class Component lifecycle, getSnapshotBeforeUpdate is called:

A. After render but before updates are committed to the DOM
B. After componentDidUpdate
C. Before render
D. During the mounting phase

34 Which of the following statements about useState is TRUE?

A. It merges the new state with the old state automatically
B. It must be used inside a loop
C. It replaces the old state with the new state completely
D. It can only be used once per component

35 If you want to persist a value across renders without causing a re-render, which hook should you use?

A. useMemo
B. useState
C. useCallback
D. useRef

36 Can hooks be used inside conditional statements (if/else)?

A. Only in development mode
B. Only useEffect
C. No, never
D. Yes, always

37 In a reducer function used with useReducer, what is the return value?

A. The previous state
B. The action object
C. A boolean indicating success
D. The new state

38 Which hook is equivalent to componentDidMount, componentDidUpdate, and componentWillUnmount combined?

A. useState
B. useLifecycle
C. useLayoutEffect
D. useEffect

39 How do you access the value stored in a ref object named 'inputRef'?

A. inputRef.state
B. inputRef.value
C. inputRef.get()
D. inputRef.current

40 What happens if you update a state hook with the exact same value as the current state?

A. React bails out without rendering the children or firing effects
B. The component unmounts
C. React triggers a re-render anyway
D. React throws an error

41 Which lifecycle method is deprecated in recent React versions (16.3+)?

A. componentDidUpdate
B. componentWillUnmount
C. componentWillMount
D. componentDidMount

42 What is the primary use of the 'key' prop in lists which relates to component lifecycle/state?

A. Styling the list items
B. Setting the initial state
C. Passing data to the child
D. Helping React identify which items have changed, added, or removed

43 When using useMemo, what determines when the value is recalculated?

A. When one of the variables in the dependency array changes
B. When the user clicks a button
C. Every 1000ms
D. Whenever the component re-renders

44 What is 'Prop Drilling'?

A. A tool to inspect props
B. Defining default props
C. The process of passing data from a parent to a deeply nested child through intermediate components
D. Validating props types

45 Which hook serves as an alternative to useState for managing complex state transitions?

A. useReducer
B. useContext
C. useCallback
D. useLayoutEffect

46 Can you use built-in hooks inside a Custom Hook?

A. Only useEffect
B. No
C. Yes
D. Only useState

47 What is the signature of the dispatch function returned by useReducer?

A. dispatch(reducer)
B. dispatch(action)
C. dispatch(newState)
D. dispatch(state, action)

48 Which hook fires synchronously after all DOM mutations but before the browser paints?

A. useState
B. useEffect
C. useInsertionEffect
D. useLayoutEffect

49 In a class component, where should you initialize state?

A. In the render method
B. In componentWillUnmount
C. In the constructor
D. In componentDidMount

50 What is the naming convention for a custom hook file?

A. Must be in the hooks folder
B. Must allow uppercase
C. Must start with 'use' (e.g., useForm)
D. Must end with 'Hook'