Spring MVC - Form Validation with Bean Validation MCQ
0/13
0✓
0✗
1. What is the main purpose of Jakarta Bean Validation?
Why: Jakarta Bean Validation lets you add constraint annotations to object fields. The fields are then checked automatically against those rules.
2. Where do you normally place Bean Validation constraint annotations such as @NotEmpty and @Size?
Why: Constraint annotations are declared on the fields of the backing object. The validator reads them to decide whether the object is valid.
3. What does Spring MVC register automatically as the global validator when a Bean Validation implementation (such as Hibernate Validator) is on the classpath?
Why: When a Bean Validation implementation is found on the classpath, Spring MVC registers LocalValidatorFactoryBean as the global validator.
4. Which annotation on a controller method parameter tells Spring to validate that object automatically?
Why:@Valid (from Jakarta Bean Validation) marks an object for automatic validation. Spring's @Validated can also be used for this.
5. When does Spring run the validation for a method parameter marked with @Valid?
Why: Spring calls the configured validator on the object before entering the handler method body. The method then runs with the validation results available.
6. What is BindingResult used for?
Why:BindingResult stores binding and validation errors. It also stores field-specific error messages.
7. Where must the BindingResult parameter be placed in a controller method signature?
Why: Spring links each BindingResult to the validated object that comes right before it. The position is important.
8. What happens if the BindingResult parameter is not placed immediately after the validated object?
Why: Spring cannot associate the BindingResult with the validated object when the order is wrong. In Spring MVC, omitting or misplacing BindingResult causes Spring to throw a BindException or MethodArgumentNotValidException. By default, Spring's exception handling maps that exception to an HTTP 400 (Bad Request) response
9. A controller method has two validated model objects. How many BindingResult instances does Spring create?
Why: Spring creates a separate BindingResult for each validated model object. Each one must follow its own object in the method signature.
10. Which method of BindingResult tells you if any validation errors were found?
Why:hasErrors() returns true when at least one validation or binding error exists.
11. Which constraint annotation makes sure a text value is not null and not empty?
Why:@NotEmpty checks that the value is not null and has at least one character or element.
12. Which constraint annotation checks that a text value has a length between a minimum and a maximum?
Why:@Size accepts min and max attributes to limit the length of a text value (or the size of a collection).
13. Which constraint annotation checks a text value against a regular expression?
Why:@Pattern uses the regexp attribute to define the format that the value must match.
14. Which constraint annotation checks that a text value looks like a valid email address?
Why:@Email checks that the value follows a valid email address format.
15. Which attribute is used to set a custom error message on a constraint annotation?
@NotEmpty(____ = "Name is required")
Why: Every standard constraint annotation has a message attribute. It sets the text shown when the validation fails.
16. Copying field errors from BindingResult into the model by hand is not ideal. What is a better alternative?
Why: Spring's form tag library (spring-form.tld) can show validation errors next to the matching form fields. You do not need to copy each error into the model.