Unit 4: Form Handling and Validation - Practice Quiz
1 In React, what makes a form input a controlled component?
2 Which React feature is commonly used to access the current value of an uncontrolled input?
3 Which React Hook is commonly used to manage a simple form field value?
4 What is a common way to manage several related form fields in one state value?
5 Which form event is normally handled when a React form is submitted?
6
Why is event.preventDefault() commonly called in a React form submission handler?
7 How can a reusable input component receive its label text?
8 What is a key benefit of creating reusable form field components?
9 What does conditional rendering allow a React form to do?
10
Which JSX expression displays an error message only when error has a truthy value?
error || <span>Error</span>
error && <span>Error</span>
error = <span>Error</span>
error + <span>Error</span>
11 Which HTML element should identify the purpose of a form input for users and assistive technologies?
<footer>
<label>
<canvas>
<section>
12 Which attribute can indicate to assistive technologies that an input currently has an invalid value?
aria-expanded
aria-invalid
aria-hidden
aria-checked
13 Where does client-side form validation normally run?
14 Why should an application still validate form data on the server?
15 Which validation rule checks that a field is not left empty?
16 Which validation pattern is commonly used for a password confirmation field?
17 What does a validation schema describe?
18 What is one benefit of using a shared validation schema?
19 Which Zod expression defines a required string value?
z.number()
z.array()
z.string()
z.boolean()
20
What is a useful result of calling Zod's safeParse when validating form data?
21 A React text input receives its value from component state. Users can type into the input, but the displayed text never changes. Which change most directly fixes the problem?
value prop with a name prop
ref while keeping the state unchanged
onChange handler that updates the state
22 A form contains a file input and should access the selected file only when the user submits the form. Which approach is most appropriate?
23
A form stores all field values in one object named formData. Which update correctly changes only the email field without removing the other fields?
setFormData(formData.email = value)
setFormData({ email: value })
setFormData([...formData, email, value])
setFormData({ ...formData, email: value })
24
Several inputs share one change handler and each input has a unique name attribute. Which expression should the handler use to update the matching property in state?
{ ...prev, [event.target.name]: event.target.value }
{ ...prev, name: event.target.value }
{ ...prev, value: event.target.name }
{ ...prev, event.target.name: event.target.value }
25
A React form should validate data and send it with fetch without causing a page reload. What should the submit handler do first?
event.target.reset()
event.preventDefault()
event.persist()
event.stopPropagation()
26 A registration form sends an asynchronous request. Which workflow best prevents accidental duplicate submissions?
27
A reusable TextField component must display a label, an input, and an error message. Which API best supports reuse across forms?
label, input props, and an error prop
28 A custom input component must allow its parent form to focus the underlying DOM input after validation fails. Which React design is most suitable?
29
A shipping-address section should appear only when useBillingAddress is false. Which JSX condition expresses this requirement?
useBillingAddress && <ShippingFields />
!useBillingAddress || <ShippingFields />
!useBillingAddress && <ShippingFields />
useBillingAddress || <BillingFields />
30
A form conditionally removes a required companyName field when the user selects "Personal account". What should the form logic generally do with that hidden field?
31
A text input shows the error message "Email is required" in an element with id="email-error". Which input attribute best creates an accessible relationship to that message?
aria-controls="email-error"
aria-label="email-error"
aria-hidden="email-error"
aria-describedby="email-error"
32 After a failed submission, which behavior most improves keyboard and screen-reader usability when several fields are invalid?
33 A checkout form validates card details in the browser before sending them to the server. Why must the server still validate the submitted data?
34 A form currently displays validation errors after every keystroke, distracting users before they finish typing. Which strategy offers a more balanced experience?
35 A password confirmation field must match the password field. What kind of validation is required?
36 A form accepts an optional website URL. Which rule correctly handles an empty value?
37 A team uses the same user schema to validate form data and infer TypeScript types. What is the main benefit of this approach?
38
A signup form requires password and confirmPassword to match. Why is object-level schema validation appropriate?
39
Given const schema = z.object({ age: z.coerce.number().int().min(18) }), what happens when it validates { age: "21" }?
min(18) requires 18 digits
21
"21"
40
A submit handler uses const result = schema.safeParse(formData). What should it do when result.success is false?
result.data and clear the current errors
41
A file input is implemented as a controlled React input by assigning value={fileName} and updating fileName after onChange. What is the most accurate issue with this design?
onChange handler only works after form submission
42
A controlled text input initially receives value={undefined} and later receives value="admin@example.com". Which behavior should be expected?
43
A form stores values, touched, errors, isSubmitting, and submitError in separate state variables. Several event handlers update multiple variables based on the previous form state. Which refactoring most directly reduces inconsistency risk?
44
A field update handler is written as setValues({ ...values, [name]: value }), and two updates are queued during the same event. What change best guarantees that both updates use the latest state?
setValues(current => ({ ...current, [name]: value }))
document.forms[0]
setValues twice with the same captured object
useMemo
45
A React form submits data through fetch, but the page still navigates and the browser performs a native submission. Which correction is required?
event.preventDefault() at the start of the submit handler
event.stopPropagation() after the fetch resolves
async to the component function
noValidate on the form
46 A user submits a form twice before the first request finishes. Which design most reliably prevents an older response from overwriting newer state?
47
A reusable TextField component needs to support labels, descriptions, errors, arbitrary input attributes, and stable accessibility relationships. Which API design is strongest?
label and input elements
id, render a linked label, and derive described-by IDs
48
A reusable form component exposes onSubmit, onChange, and errors, but it also silently converts server errors into local field errors. What is the main design concern?
49
A billing address section is conditionally removed when sameAsShipping becomes true. When it is shown again, its previous values are gone. Which explanation is most accurate?
50
A payment form renders card fields when method === "card" and bank fields otherwise. Both branches contain an input with the same position but different meanings. What prevents React from incorrectly reusing field state?
key values for the mutually exclusive branches
name attribute
fieldset
autoComplete="off" on both inputs
51 A field has an inline validation message that appears after blur. Which implementation best communicates the error to assistive technologies?
aria-describedby and set aria-invalid="true"
52 A form displays a server-side error summary after submission. What behavior most improves keyboard and screen-reader usability?
53 Why should client-side validation be treated as a usability feature rather than a security boundary?
54 A password confirmation rule reports an error while the user is still typing the first password. Which validation strategy generally gives the best experience without weakening final correctness?
55
A numeric field accepts "", "0", and "12" as strings. Which approach avoids incorrectly treating zero as missing while still recognizing an empty field?
Boolean(value)
if (!value) to detect missing input
56 A schema is used both to validate a form and to transform a text field into a number. What architectural property is most important when using the parsed result?
57
A registration schema validates password and confirmPassword independently, but never checks that they match. What kind of rule is missing?
58 In Zod, a form field is received from an HTML input as a string, but the domain model requires a positive integer. Which schema best expresses the requirement?
z.string().email().positive()
z.boolean().int().positive()
z.coerce.number().int().positive()
z.number().int().positive()
59
A Zod object uses .refine() to ensure confirmPassword matches password, but the resulting issue appears at the form root. Which option gives the best field-level error display?
z.string() on the entire form instead
path containing "confirmPassword" in the refinement options
60
A form calls schema.parse(values) inside an asynchronous submit handler. Invalid input throws before the handler can populate field errors. Which integration pattern is appropriate?
safeParse and map result.error.issues into field errors
Did this save you a night before the exam?
LPU Notes is free, and it stays free. 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.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →