Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
Which HTML element is primarily used to collect user input in a React application?
Not quite. Try another option.
Answer: <form>
While <input> elements capture specific data, the <form> element is the container used to group input elements and handle data submission.
Question 2 of 50
In React, what is the standard event handler used to listen for changes in an input field?
Not quite. Try another option.
Answer: onChange
React uses the onChange event to track every keystroke or change in value of form elements.
Question 3 of 50
What defines a 'Controlled Component' in React?
Not quite. Try another option.
Answer: A component where React state acts as the single source of truth
In a controlled component, the form data is handled by the React component's state, rather than the DOM maintaining its own state.
Question 4 of 50
Which attribute must be set on an input element to make it a Controlled Component?
Not quite. Try another option.
Answer: value
Setting the value attribute and binding it to a state variable makes the input controlled by React.
Question 5 of 50
If you provide a value prop to an input without an onChange handler, what is the result?
Not quite. Try another option.
Answer: The input becomes read-only
React renders the input with the specified value. Without an onChange handler to update that value, user input is ignored, effectively making it read-only.
Question 6 of 50
How do you access the value entered by the user in an onChange event handler?
Not quite. Try another option.
Answer: event.target.value
The event object contains a target property representing the element, which has a value property holding the current input.
Question 7 of 50
What is an 'Uncontrolled Component' in React?
Not quite. Try another option.
Answer: A component where form data is handled by the DOM itself
Uncontrolled components act more like traditional HTML form elements where the DOM stores the state, and React accesses it via refs.
Question 8 of 50
Which React Hook is commonly used to access the DOM node in an uncontrolled component?
Not quite. Try another option.
Answer: useRef
useRef is used to create a reference to the DOM element, allowing direct access to its current value.
Question 9 of 50
Which attribute is used to set the initial value of an uncontrolled input component?
Not quite. Try another option.
Answer: defaultValue
In uncontrolled components, defaultValue sets the initial text, but allows the user to change it subsequently since React is not forcing the value.
Question 10 of 50
Which method is called to prevent the default page reload behavior during form submission?
Not quite. Try another option.
Answer: event.preventDefault()
Calling event.preventDefault() inside the submission handler prevents the browser's default behavior of refreshing the page.
Question 11 of 50
Where should the onSubmit event handler be attached?
Not quite. Try another option.
Answer: The <form> element
The onSubmit handler should be placed on the <form> tag to capture submission triggered by buttons or the Enter key.
Question 12 of 50
How does React handle the <textarea> element differently than HTML?
Not quite. Try another option.
Answer: It uses a value attribute instead of defining text as children
In HTML, textarea content is between tags. In React, it behaves like an input element using the value attribute.
Question 13 of 50
In a controlled <select> component, where is the selected value defined?
Not quite. Try another option.
Answer: On the <select> element using the value attribute
React standardizes select inputs by using a value attribute on the root <select> tag to determine the currently selected option.
Question 14 of 50
Which syntax is useful for handling multiple inputs with a single onChange handler?
Not quite. Try another option.
Answer: Computed property names: [e.target.name]
Using computed property names (e.g., [name]: value) allows a single handler to update the corresponding state variable dynamically based on the input's name attribute.
Question 15 of 50
When using a checkbox input in React, which property of event.target should be used to get the value?
Not quite. Try another option.
Answer: checked
For input type='checkbox', the checked property (boolean) indicates whether the box is ticked, not value.
Question 16 of 50
Which input type is always an uncontrolled component in React?
Not quite. Try another option.
Answer: file
File inputs are read-only because their value can only be set by a user interaction, not programmatically by JavaScript, making them uncontrolled.
Question 17 of 50
What is the purpose of the htmlFor attribute in React?
Not quite. Try another option.
Answer: To replace the HTML for attribute on labels
Since for is a reserved keyword in JavaScript, React uses htmlFor to associate a <label> with an input ID.
Question 18 of 50
Ideally, where should validation logic reside in a Controlled Component approach?
Not quite. Try another option.
Answer: Inside the event handlers (onChange/onSubmit) updating the state
Validation is usually performed when the state updates (onChange) or when the form is submitted, and errors are stored in state.
Question 19 of 50
How can you conditionally display an error message only if an error exists?
Not quite. Try another option.
Answer: {error && <p>{error}</p>}
This is a standard JavaScript short-circuit evaluation pattern used in JSX to render the paragraph only if error is truthy.
Question 20 of 50
What represents the state of a form element that has been clicked on or focused and then blurred?
Not quite. Try another option.
Answer: Touched
In form libraries and manual validation, 'touched' usually indicates the user has interacted with the field (often triggered by onBlur), enabling validation messages to appear only after interaction.
Question 21 of 50
Which event is best used to validate a field specifically when the user leaves that field?
Not quite. Try another option.
Answer: onBlur
onBlur fires when an element loses focus, making it the ideal time to validate a field the user has finished editing.
Question 22 of 50
Why might you choose an uncontrolled component over a controlled one?
Not quite. Try another option.
Answer: For better performance with extremely large forms
Uncontrolled components avoid re-rendering the component on every keystroke, which can be beneficial for performance in very large forms or when integrating with non-React code.
Question 23 of 50
How do you handle a select element with the multiple attribute in React?
Not quite. Try another option.
Answer: Pass an array to the value attribute
For a multi-select box, the value attribute accepts an array of strings corresponding to the selected options.
Question 24 of 50
What is the correct way to reset a Controlled form after submission?
Not quite. Try another option.
Answer: Update the state variables to their initial empty values
Since the inputs are bound to state, you must reset the state variables to clear the form fields.
Question 25 of 50
If you need to validate an email address, which JavaScript feature is commonly used inside the validation function?
Not quite. Try another option.
Answer: Regular Expressions (Regex)
Regex patterns are the standard way to check if a string matches the format of an email address.
Question 26 of 50
What happens if you change a controlled input from undefined value to a defined value during the component lifecycle?
Not quite. Try another option.
Answer: React logs a warning about switching from uncontrolled to controlled
If value is undefined, React treats it as uncontrolled. If it later gets a value, it becomes controlled. React warns against this dynamic switch.
Question 27 of 50
In a controlled component, the source of truth is:
Not quite. Try another option.
Answer: The React Component State
Controlled components rely on React state to dictate exactly what is displayed in the input.
Question 28 of 50
To disable a submit button when a form is invalid, which attribute should be used?
Not quite. Try another option.
Answer: disabled
The disabled attribute prevents user interaction with the button. It can be set dynamically based on the validity state of the form.
Question 29 of 50
Which of the following is NOT a valid type for an <input> element?
Not quite. Try another option.
Answer: dropdown
dropdown is not an input type. The correct HTML element for a dropdown is <select>.
Question 30 of 50
When using useRef to access a form value, how do you access the actual DOM node?
Not quite. Try another option.
Answer: ref.current
The useRef hook returns an object with a current property that holds the actual DOM node once mounted.
Question 31 of 50
What is a common library used in React for managing complex forms and validation?
Not quite. Try another option.
Answer: Formik
Formik (and React Hook Form) are popular libraries specifically designed to handle form state, validation, and submission in React.
Question 32 of 50
In a radio button group, how does React know which buttons belong together?
Not quite. Try another option.
Answer: They share the same name attribute
Just like in HTML, radio buttons with the same name attribute form a group where only one can be selected at a time.
Question 33 of 50
What is the standard way to prevent a user from typing in an input field conditionally?
Not quite. Try another option.
Answer: Use the readOnly or disabled attribute
The readOnly or disabled props are the semantic and functional ways to prevent user input in React forms.
Question 34 of 50
If a form validation function is asynchronous (e.g., checking username availability), where is the best place to trigger it?
Not quite. Try another option.
Answer: In the onChange handler or useEffect
Asynchronous operations should occur in side-effect handlers like event handlers or useEffect, not during the render phase.
Question 35 of 50
When using the onChange handler for a text input, what data type is event.target.value typically?
Not quite. Try another option.
Answer: String
Even for type='number', the DOM value property is always a string. It must be parsed if a number is required.
Question 36 of 50
What does the aria-label attribute do in a form?
Not quite. Try another option.
Answer: It provides a label for screen readers for accessibility
Accessibility is crucial in forms. aria-label provides a text description for elements that might not have a visible label.
Question 37 of 50
Which approach is generally recommended by the React team for most forms?
Not quite. Try another option.
Answer: Controlled Components
Controlled components are generally recommended because they support instant validation, conditional disabling, and enforcing input formats.
Question 38 of 50
How can you optimize a form that lags due to validation running on every keystroke?
Not quite. Try another option.
Answer: Use Debouncing on the onChange handler
Debouncing ensures the validation logic only runs after the user stops typing for a specified delay, improving performance.
Question 39 of 50
What is the equivalent of the HTML autofocus attribute in React?
Not quite. Try another option.
Answer: autoFocus (camelCase)
React uses camelCase for attributes, so autofocus becomes autoFocus.
Question 40 of 50
If you have a form with 20 inputs, what is the downside of using separate useState hooks for each?
Not quite. Try another option.
Answer: Code becomes verbose and harder to manage
Managing 20 separate state variables and handlers is repetitive. Using a single object state with one generic handler is cleaner.
Question 41 of 50
What attribute allows an input to accept multiple files?
Not quite. Try another option.
Answer: multiple
The multiple attribute allows the user to select more than one file in a file input or multiple options in a select.
Question 42 of 50
In an uncontrolled form, how can you reset the form fields natively?
Not quite. Try another option.
Answer: event.target.reset()
If you are in the onSubmit handler, event.target refers to the form element, which has a native HTML reset() method.
Question 43 of 50
Which React concept allows passing a ref from a parent to a child's input element?
Not quite. Try another option.
Answer: Ref forwarding
React.forwardRef allows a component to take a ref attribute passed to it and forward it to a DOM element further down the tree.
Question 44 of 50
What is the primary role of a 'Submit Handler'?
Not quite. Try another option.
Answer: To process data (e.g., send to API) and manage UI state
The submit handler gathers the data, often validates it one last time, sends it to a server, and updates the UI (e.g., showing a success message).
Question 45 of 50
When validating, what does a 'Required' check ensure?
Not quite. Try another option.
Answer: The input field is not empty
A 'Required' validation simply checks that the value is not null, undefined, or an empty string.
Question 46 of 50
Can a div element typically fire an onChange event?
Not quite. Try another option.
Answer: No, only form input elements fire onChange
Standard div elements do not accept input and thus do not fire onChange events unless specialized (like contentEditable, which is complex in React).
Question 47 of 50
How do you capture the file object from a file input onChange event?
Not quite. Try another option.
Answer: event.target.files[0]
File inputs store selected files in a files FileList array on the target object. The first file is at index 0.
Question 48 of 50
Which of the following is true regarding validation feedback?
Not quite. Try another option.
Answer: It should be immediate and clear to the user
Good UX dictates that validation errors should be displayed clearly and typically near the field causing the error.
Question 49 of 50
What is the purpose of the placeholder attribute?
Not quite. Try another option.
Answer: To provide a hint to the user about what to type
The placeholder text is displayed in the input field before the user enters a value, acting as a hint.
Question 50 of 50
If a controlled input's state is set to null, what happens?
Not quite. Try another option.
Answer: It becomes uncontrolled
In React, passing null or undefined to the value prop tells React that the component is uncontrolled, potentially causing warnings if it was previously controlled.
That's unit 4
Answer a few questions and your score shows up here.
Nearly through. Did unit 4 of INT252 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 4 of INT252 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill;
the rest comes out of a student's own pocket: the domain, the storage, and keeping
the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason.
to try again.