0
0
Spring Bootframework~20 mins

@NotNull, @NotBlank, @NotEmpty in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spring Boot Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Difference between @NotNull and @NotEmpty

Which statement correctly describes the difference between @NotNull and @NotEmpty annotations in Spring Boot validation?

A<code>@NotNull</code> ensures the value is not null, but allows empty strings or collections; <code>@NotEmpty</code> ensures the value is not null and not empty (for strings, collections).
B<code>@NotNull</code> only works on strings; <code>@NotEmpty</code> only works on collections.
C<code>@NotNull</code> ensures the value is not null and not empty; <code>@NotEmpty</code> only checks for null values.
D<code>@NotNull</code> and <code>@NotEmpty</code> are interchangeable and have the same effect.
Attempts:
2 left
💡 Hint

Think about whether empty strings or empty collections are allowed.

component_behavior
intermediate
1:30remaining
Validation behavior of @NotBlank on a string field

Given a Spring Boot model with a string field annotated with @NotBlank, what input value will cause validation to fail?

Spring Boot
public class User {
    @NotBlank
    private String username;

    // getters and setters
}
A"user123" (a normal non-empty string)
B" " (a string with only spaces)
C"null" (the string literal 'null')
D"user_1" (a string with underscore)
Attempts:
2 left
💡 Hint

Remember what @NotBlank checks for in strings.

📝 Syntax
advanced
2:00remaining
Correct usage of @NotEmpty on a collection

Which code snippet correctly applies @NotEmpty to a list field in a Spring Boot model to ensure it is not null and not empty?

A
public class Order {
    @NotEmpty
    private List&lt;String&gt; items;
}
B
public class Order {
    @NotEmpty
    private String items;
}
C
public class Order {
    @NotEmpty
    private Map&lt;String, String&gt; items;
}
D
public class Order {
    @NotEmpty
    private int[] items;
}
Attempts:
2 left
💡 Hint

Consider which data types @NotEmpty supports.

🔧 Debug
advanced
1:30remaining
Why does @NotBlank not work on an Integer field?

Consider this Spring Boot model:

public class Product {
    @NotBlank
    private Integer quantity;
}

Why will validation fail or not work as expected?

A<code>@NotBlank</code> only works on primitive types, not wrapper classes.
B<code>@NotBlank</code> works on all object types, so this should work fine.
C<code>@NotBlank</code> requires the field to be annotated with @NotNull as well.
D<code>@NotBlank</code> only works on CharSequence (strings), so it cannot validate Integer fields.
Attempts:
2 left
💡 Hint

Check the supported types for @NotBlank.

state_output
expert
2:30remaining
Validation result for a complex object with mixed annotations

Given this Spring Boot model:

public class UserProfile {
    @NotNull
    private String id;

    @NotBlank
    private String name;

    @NotEmpty
    private List roles;

    // getters and setters
}

Which input will pass all validations?

A{"id": "456", "name": "Bob", "roles": []}
B{"id": null, "name": "Alice", "roles": ["user"]}
C{"id": "789", "name": "Carol", "roles": ["editor"]}
D{"id": "123", "name": "", "roles": ["admin"]}
Attempts:
2 left
💡 Hint

Check each annotation's requirement for each field.