Bird
Raised Fist0
Spring Bootframework~20 mins

@Size for length constraints in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Size Constraint Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a string field annotated with @Size(min=5, max=10) receives a 3-character input?
Consider a Spring Boot entity with a field annotated as @Size(min=5, max=10). What will be the validation result if the input string length is 3?
Spring Boot
public class User {
    @Size(min=5, max=10)
    private String username;

    // getters and setters
}
AValidation fails because the input length is less than the minimum size.
BValidation fails because the input length is greater than the maximum size.
CValidation passes because the input is a string regardless of length.
DValidation passes because @Size only checks maximum length.
Attempts:
2 left
💡 Hint
Remember that @Size checks both minimum and maximum length constraints.
📝 Syntax
intermediate
2:00remaining
Which @Size annotation usage is syntactically correct for a list field in Spring Boot?
You want to validate a list to have between 2 and 5 elements using @Size. Which option is correct?
Spring Boot
public class Group {
    @Size(???)
    private List<String> members;
}
A@Size(min=2, max=5)
B@Size(2, 5)
C@Size(minLength=2, maxLength=5)
D@Size(length=2..5)
Attempts:
2 left
💡 Hint
Check the official @Size annotation parameters for min and max.
🔧 Debug
advanced
2:00remaining
Why does this @Size annotation not enforce the maximum length?
Given the code below, why does the validation allow strings longer than 10 characters?
Spring Boot
public class Product {
    @Size(min=3)
    private String code;

    // getters and setters
}
ABecause the field is private and not accessible for validation.
BBecause min attribute is ignored for strings.
CBecause @Size only works on collections, not strings.
DBecause max attribute is missing, so no maximum length is enforced.
Attempts:
2 left
💡 Hint
Check if both min and max are specified to enforce length limits.
state_output
advanced
2:00remaining
What is the validation result for a null string annotated with @Size(min=1, max=5)?
If a string field is annotated with @Size(min=1, max=5) and the input value is null, what happens during validation?
Spring Boot
public class Item {
    @Size(min=1, max=5)
    private String label;

    // getters and setters
}
AValidation passes only if @NotNull is also present.
BValidation passes because @Size ignores null values.
CValidation throws NullPointerException.
DValidation fails because null length is less than min.
Attempts:
2 left
💡 Hint
Think about how @Size treats null values by default.
🧠 Conceptual
expert
2:00remaining
How does @Size behave differently on a String vs a Collection in Spring Boot validation?
Which statement correctly describes the difference in how @Size validates Strings and Collections?
A@Size only works on Strings, Collections are validated by @Length.
B@Size validates the byte size of Strings and the memory size of Collections.
C@Size checks the length of a String and the number of elements in a Collection.
D@Size requires separate annotations for Strings and Collections.
Attempts:
2 left
💡 Hint
Think about what length means for strings and collections.

Practice

(1/5)
1. What does the @Size annotation do in Spring Boot validation?
easy
A. It ensures a field is not null.
B. It validates if a number is within a range.
C. It checks if a string or collection length is within specified min and max limits.
D. It checks if a string matches a regular expression.

Solution

  1. Step 1: Understand the purpose of @Size

    @Size is used to validate the length of strings or collections, not numeric ranges or null checks.
  2. Step 2: Identify what @Size checks

    It uses min and max to set length limits on text or collections.
  3. Final Answer:

    It checks if a string or collection length is within specified min and max limits. -> Option C
  4. Quick Check:

    @Size validates length = C [OK]
Hint: Remember @Size controls length, not value or null checks [OK]
Common Mistakes:
  • Confusing @Size with @Min/@Max for numbers
  • Thinking @Size checks null values
  • Assuming @Size validates patterns
2. Which of the following is the correct way to use @Size to require a string between 5 and 10 characters?
easy
A. @Size(min=5, max=10)
B. @Size(length=5-10)
C. @Size(minLength=5, maxLength=10)
D. @Size(range={5,10})

Solution

  1. Step 1: Recall correct @Size syntax

    The correct attributes are min and max for length limits.
  2. Step 2: Check each option

    Only @Size(min=5, max=10) uses valid attribute names and syntax.
  3. Final Answer:

    @Size(min=5, max=10) -> Option A
  4. Quick Check:

    Use min and max attributes = A [OK]
Hint: Use min and max, not length or range attributes [OK]
Common Mistakes:
  • Using invalid attribute names like length or range
  • Trying to pass a range as a string
  • Confusing @Size with other annotations
3. Given this code snippet:
@Size(min=3, max=6)
private String code;

Which input value will pass validation?
medium
A. "ab"
B. "abcde"
C. "abcdefg"
D. ""

Solution

  1. Step 1: Understand the length limits

    The string must have length between 3 and 6 characters inclusive.
  2. Step 2: Check each input length

    "ab" length is 2 (too short), "abcdefg" length is 7 (too long), "abcde" length is 5 (valid), "" length is 0 (too short).
  3. Final Answer:

    "abcde" -> Option B
  4. Quick Check:

    Length between 3 and 6 = "abcde" [OK]
Hint: Count characters; must be between min and max [OK]
Common Mistakes:
  • Ignoring inclusive limits
  • Counting characters incorrectly
  • Assuming empty string passes
4. Identify the error in this code snippet:
@Size(min=2, max=5)
private int number;
medium
A. @Size cannot be applied to primitive types like int.
B. min and max values are reversed.
C. Missing @NotNull annotation.
D. max value should be greater than 10.

Solution

  1. Step 1: Check @Size target types

    @Size works on strings, collections, arrays, but not on primitive types like int.
  2. Step 2: Analyze the code

    The field is an int, so @Size is invalid here and will cause an error.
  3. Final Answer:

    @Size cannot be applied to primitive types like int. -> Option A
  4. Quick Check:

    @Size only for strings/collections = D [OK]
Hint: Use @Size only on strings or collections, not primitives [OK]
Common Mistakes:
  • Applying @Size to numbers
  • Confusing @Size with @Min/@Max for numbers
  • Ignoring type compatibility
5. You want to validate a list of usernames where each username must be between 4 and 12 characters. Which is the correct way to apply @Size in your Spring Boot model?
hard
A. @Size(min=4, max=12) private List<@Size(min=4, max=12)> usernames;
B. @Size(min=4, max=12) private String[] usernames;
C. @Size(min=4, max=12) private List<String> usernames;
D. private List<@Size(min=4, max=12) String> usernames;

Solution

  1. Step 1: Understand @Size on collections vs elements

    @Size on a collection checks the collection size, not each element's length.
  2. Step 2: Apply @Size to elements inside the collection

    To validate each username's length, use @Size on the generic type parameter or element level.
  3. Step 3: Analyze options

    private List<@Size(min=4, max=12) String> usernames; correctly applies @Size to each String element in the List.
  4. Final Answer:

    private List<@Size(min=4, max=12) String> usernames; -> Option D
  5. Quick Check:

    Use @Size on elements for per-item length = D [OK]
Hint: Put @Size on list elements, not just the list itself [OK]
Common Mistakes:
  • Applying @Size only on the list, not elements
  • Using invalid syntax for generic annotations
  • Confusing collection size with element length