0
0
Spring Bootframework~20 mins

Business logic in services in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spring Boot Service Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Spring Boot service method?

Consider this service method that calculates a discount based on customer type:

public double calculateDiscount(String customerType, double amount) {
    if ("VIP".equals(customerType)) {
        return amount * 0.2;
    } else if ("Regular".equals(customerType)) {
        return amount * 0.1;
    } else {
        return 0;
    }
}

What will calculateDiscount("VIP", 1000) return?

A0.0
B100.0
C200.0
DThrows NullPointerException
Attempts:
2 left
💡 Hint

Check the condition for the "VIP" customer type and the multiplication.

state_output
intermediate
2:00remaining
What is the state of the order after calling this service method?

Given this service method that updates an order status:

public void processOrder(Order order) {
    if (order.getItems().isEmpty()) {
        order.setStatus("FAILED");
    } else {
        order.setStatus("PROCESSED");
    }
}

If order.getItems() returns an empty list, what will be the order status after processOrder(order)?

A"PROCESSED"
B"PENDING"
Cnull
D"FAILED"
Attempts:
2 left
💡 Hint

Think about what happens when the items list is empty.

📝 Syntax
advanced
2:00remaining
Which option correctly implements a transactional service method?

Choose the correct way to annotate a Spring Boot service method to ensure it runs within a transaction:

A
@Transactional
public void updateInventory() { /* code */ }
B
@Transaction
public void updateInventory() { /* code */ }
C
@Transactional(readOnly = true)
public void updateInventory() { /* code */ }
D
@Transactional(propagation = Propagation.NEVER)
public void updateInventory() { /* code */ }
Attempts:
2 left
💡 Hint

Look for the correct annotation spelling and usage for a writable transaction.

🔧 Debug
advanced
2:00remaining
Why does this service method throw a NullPointerException?

Examine this service method:

public double calculateTotal(Order order) {
    return order.getItems().stream()
        .mapToDouble(item -> item.getPrice() * item.getQuantity())
        .sum();
}

Sometimes this method throws a NullPointerException. What is the most likely cause?

AThe getItems() method returns null instead of an empty list.
BThe order object is null when passed to the method.
CThe item price or quantity is null causing the exception.
DThe stream() method is not supported on the items list.
Attempts:
2 left
💡 Hint

Consider what happens if getItems() returns null and you call stream() on it.

🧠 Conceptual
expert
2:00remaining
Which statement best describes the role of services in Spring Boot architecture?

In a typical Spring Boot application, what is the main purpose of the service layer?

ATo handle HTTP requests and responses directly.
BTo contain business logic and coordinate data operations between controllers and repositories.
CTo manage database connections and execute SQL queries.
DTo render HTML views and manage user interface components.
Attempts:
2 left
💡 Hint

Think about where the business rules and logic should live in the app.