2. Which of the following is the correct way to annotate a string field that must not be null, empty, or only spaces in Spring Boot?
easy
A. @NotBlank private String name;
B. @NotEmpty private String name;
C. @NotNull @NotEmpty private String name;
D. @NotNull private String name;
Solution
Step 1: Identify annotation that forbids null, empty, and spaces
@NotBlank ensures the string is not null, not empty, and not just whitespace.
Step 2: Check other options
@NotNull allows empty or spaces; @NotEmpty forbids empty but allows spaces; only @NotBlank covers all three.
Final Answer:
@NotBlank private String name; -> Option A
Quick Check:
@NotBlank forbids null, empty, spaces [OK]
Hint: Use @NotBlank for strings with no spaces or empties [OK]
Common Mistakes:
Using @NotEmpty but ignoring spaces
Combining @NotNull and @NotEmpty unnecessarily
Assuming @NotNull forbids empty strings
3. Given the code snippet:
@NotEmpty
private List<String> tags;
What happens if tags is set to an empty list during validation?
medium
A. Validation passes because list is not null
B. Validation fails because list is empty
C. Validation fails only if list is null
D. Validation passes regardless of list content
Solution
Step 1: Understand @NotEmpty on collections
@NotEmpty forbids null or empty collections, so empty list fails validation.
Step 2: Compare with other annotations
@NotNull allows empty list; @NotEmpty forbids empty; @NotBlank is for strings only.
Final Answer:
Validation fails because list is empty -> Option B
Quick Check:
@NotEmpty forbids empty collections [OK]
Hint: @NotEmpty forbids empty lists or strings [OK]
Common Mistakes:
Thinking empty list passes @NotEmpty
Confusing @NotEmpty with @NotNull
Applying @NotBlank to collections
4. Consider this code snippet:
@NotBlank
private String title;
Which of the following values for title will cause validation to fail with @NotBlank but pass with @NotEmpty?
medium
A. " " (string with spaces only)
B. "Hello"
C. "" (empty string)
D. null
Solution
Step 1: Understand @NotBlank validation rules
@NotBlank forbids null, empty, and strings with only whitespace.
Step 2: Analyze each value
null and "" fail both @NotBlank and @NotEmpty; "Hello" passes both; " " passes @NotEmpty (length > 0) but fails @NotBlank (trimmed length = 0).
Final Answer:
" " (string with spaces only) -> Option A
Quick Check:
@NotBlank forbids spaces-only strings [OK]
Hint: @NotBlank forbids spaces-only strings [OK]
Common Mistakes:
Assuming empty string passes @NotBlank
Confusing @NotBlank with @NotEmpty
Ignoring spaces-only strings as invalid
5. You want to validate a user input field description that must not be null, empty, or only spaces, but also must allow strings like "0" or "false". Which annotation should you use in Spring Boot?
hard
A. @Size(min=1)
B. @NotEmpty
C. @NotNull
D. @NotBlank
Solution
Step 1: Understand requirements for description
Must not be null, empty, or spaces-only, but allow "0" or "false" strings.
Step 2: Match annotation behavior
@NotBlank forbids null, empty, and spaces-only strings but allows "0" and "false" as they are non-blank strings.
Step 3: Check alternatives
@NotNull allows empty; @NotEmpty forbids empty but allows spaces; @Size(min=1) forbids empty but not spaces-only.
Final Answer:
@NotBlank -> Option D
Quick Check:
@NotBlank fits all conditions [OK]
Hint: Use @NotBlank to forbid null, empty, and spaces-only [OK]