Bird
Raised Fist0
Spring Bootframework~20 mins

@Min, @Max for numeric 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
🎖️
Spring Boot Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a value violates @Min constraint?
Consider a Spring Boot REST controller receiving a JSON with a field annotated with @Min(5). What happens if the client sends a value of 3 for that field?
Spring Boot
@RestController
public class SampleController {
  @PostMapping("/test")
  public String test(@Valid @RequestBody InputData data) {
    return "Value accepted";
  }
}

public class InputData {
  @Min(5)
  private int number;

  // getters and setters
}
AThe value is automatically adjusted to 5 before processing.
BThe request succeeds and the controller returns "Value accepted".
CThe server throws a NullPointerException.
DThe request is rejected with a 400 Bad Request and a validation error message.
Attempts:
2 left
💡 Hint
Think about how Spring Boot handles validation annotations with @Valid.
📝 Syntax
intermediate
1:30remaining
Which code snippet correctly applies @Max to a field?
Select the code snippet that correctly uses @Max to restrict a numeric field to a maximum value of 100.
A
@Max(limit=100)
private int score;
B
@Max(100)
private int score;
C
@Max(max=100)
private int score;
D
@Max(value=100)
private int score;
Attempts:
2 left
💡 Hint
Check the official annotation parameter name for @Max.
state_output
advanced
2:00remaining
What is the validation result for a field annotated with both @Min and @Max?
Given a field annotated with @Min(10) and @Max(20), what happens if the input value is 25?
Spring Boot
public class Data {
  @Min(10)
  @Max(20)
  private int value;

  // getters and setters
}
AValidation fails with a message about exceeding the maximum value.
BValidation fails with a message about being below the minimum value.
CValidation passes because only one constraint is checked at a time.
DValidation fails with messages about both minimum and maximum constraints.
Attempts:
2 left
💡 Hint
Consider which constraint is violated by the input value.
🔧 Debug
advanced
2:30remaining
Why does this @Min annotation not work as expected?
Identify the issue in this code snippet where @Min(1) is applied but negative values are still accepted.
Spring Boot
public class Product {
  @Min(1)
  private Integer quantity;

  // getters and setters
}
AThe @Min annotation only works on primitive types, not wrapper classes.
BThe field is an Integer object, so @Min does not validate it properly.
CThe validation is not triggered because @Valid is missing on the containing object.
DThe @Min annotation requires a message attribute to work.
Attempts:
2 left
💡 Hint
Think about how validation is triggered in Spring Boot controllers or services.
🧠 Conceptual
expert
3:00remaining
How does Spring Boot handle @Min and @Max on floating-point numbers?
If a field is a double and annotated with @Min(1) and @Max(10), which statement is true about validation behavior?
Spring Boot
public class Measurement {
  @Min(1)
  @Max(10)
  private double value;

  // getters and setters
}
A@Min and @Max treat the double as a long, so fractional parts are ignored during validation.
B@Min and @Max validate the exact double value including decimals.
CValidation fails because @Min and @Max do not support floating-point types.
D@Min and @Max only work if the double is wrapped in Double, not primitive double.
Attempts:
2 left
💡 Hint
Consider how Bean Validation API defines @Min and @Max behavior for floating types.

Practice

(1/5)
1.

What is the main purpose of using @Min and @Max annotations in Spring Boot?

easy
A. To define the length of a string
B. To enforce minimum and maximum numeric values on fields
C. To format dates in a specific pattern
D. To mark a method as a REST endpoint

Solution

  1. Step 1: Identify purpose of @Min and @Max

    @Min and @Max set numeric limits on fields to ensure values stay within a range. Formatting dates, string length, and REST endpoints are unrelated.
  2. Final Answer:

    To enforce minimum and maximum numeric values on fields -> Option B
  3. Quick Check:

    @Min/@Max = numeric limits [OK]
Hint: Remember: @Min/@Max control numbers, not strings or dates [OK]
Common Mistakes:
  • Confusing @Min/@Max with string length annotations
  • Thinking they format dates
  • Assuming they define REST endpoints
2.

Which of the following is the correct way to apply @Min and @Max annotations on an integer field age to restrict it between 18 and 65?

public class Person {
    // Which is correct?
    private int age;
}
easy
A. @Min(18) @Max(65) private int age;
B. @Min=18 @Max=65 private int age;
C. @Min{18} @Max{65} private int age;
D. @Min:18 @Max:65 private int age;

Solution

  1. Step 1: Verify annotation syntax

    Annotations use parentheses with values, e.g., @Min(18), not =, {} or :. @Min(18) @Max(65) private int age; is correct; others invalid.
  2. Final Answer:

    @Min(18) @Max(65) private int age; -> Option A
  3. Quick Check:

    Annotations use (value) [OK]
Hint: Annotations always use parentheses for values [OK]
Common Mistakes:
  • Using = or {} instead of () in annotations
  • Forgetting to import javax.validation.constraints.*
  • Placing annotations incorrectly outside the field
3.

Given the following Spring Boot entity snippet, what will happen if score is set to 105?

public class GameScore {
    @Min(0)
    @Max(100)
    private int score;

    // getters and setters
}
medium
A. Validation will fail because 105 is greater than the max 100
B. The value 105 will be accepted without error
C. Validation will fail because 105 is less than the min 0
D. The application will throw a NullPointerException

Solution

  1. Step 1: Evaluate constraint for value 105

    @Min(0) requires >= 0; @Max(100) requires <= 100. 105 > 100 violates @Max, triggering validation failure.
  2. Final Answer:

    Validation will fail because 105 is greater than the max 100 -> Option A
  3. Quick Check:

    Value > @Max = error [OK]
Hint: Values outside @Min/@Max cause validation errors [OK]
Common Mistakes:
  • Assuming values above max are accepted
  • Confusing min and max roles
  • Expecting runtime exceptions instead of validation errors
4.

Identify the error in this code snippet that uses @Min and @Max:

public class Product {
    @Min(1)
    @Max(100)
    private String quantity;

    // getters and setters
}
medium
A. No error, code is correct
B. The values 1 and 100 are invalid for @Min and @Max
C. Missing @NotNull annotation on quantity
D. Annotations @Min and @Max cannot be applied to String fields

Solution

  1. Step 1: Check field type compatibility

    @Min/@Max apply only to numeric types (int, long, etc.), not String. quantity is String, causing validation error.
  2. Final Answer:

    Annotations @Min and @Max cannot be applied to String fields -> Option D
  3. Quick Check:

    @Min/@Max require numeric fields [OK]
Hint: Use @Min/@Max only on numbers, not strings [OK]
Common Mistakes:
  • Applying @Min/@Max on non-numeric types
  • Assuming @NotNull fixes type issues
  • Ignoring type mismatch errors
5.

You want to create a Spring Boot model field rating that only accepts values from 1 to 5 inclusive. Which of the following code snippets correctly enforces this using @Min and @Max?

hard
A. public class Review { @Min(0) @Max(5) private int rating; }
B. public class Review { @Min(1) @Max(6) private int rating; }
C. public class Review { @Min(1) @Max(5) private int rating; }
D. public class Review { @Min(1) @Max(5) private String rating; }

Solution

  1. Step 1: Select correct range and type

    1-5 inclusive requires @Min(1) @Max(5) on int. public class Review { @Min(1) @Max(5) private int rating; } matches; @Min(0) allows 0, @Max(6) allows 6, String invalid.
  2. Final Answer:

    public class Review { @Min(1) @Max(5) private int rating; } -> Option C
  3. Quick Check:

    @Min(1)/@Max(5) on int [OK]
Hint: Use int with @Min(1) and @Max(5) for rating 1-5 [OK]
Common Mistakes:
  • Using wrong numeric ranges
  • Applying annotations on String fields
  • Setting min or max outside desired range