0
0
Spring Bootframework~20 mins

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

Choose your learning style9 modes available
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.