0
0
Spring Bootframework~20 mins

Cache configuration in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cache Configuration Master
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 cache example?
Consider a Spring Boot service method annotated with @Cacheable("books"). If the method is called twice with the same argument, what happens on the second call?
Spring Boot
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class BookService {
    @Cacheable("books")
    public String findBook(String isbn) {
        System.out.println("Fetching book for " + isbn);
        return "Book-" + isbn;
    }
}

// In main application or test:
// bookService.findBook("123");
// bookService.findBook("123");
AThe method prints "Fetching book for 123" twice and returns the same result both times.
BThe method throws a runtime exception because caching is not enabled by default.
CThe method prints "Fetching book for 123" once and returns cached result on the second call without printing.
DThe method returns null on the second call because cache is cleared automatically.
Attempts:
2 left
💡 Hint
Think about what @Cacheable does when the same argument is used multiple times.
📝 Syntax
intermediate
1:30remaining
Which cache configuration snippet correctly enables caching in Spring Boot?
Select the correct way to enable caching in a Spring Boot application.
A
@EnableCaching
@SpringBootApplication
public class App {}
B
@EnableCache
@SpringBootApplication
public class App {}
C
@EnableCaching
@Component
public class App {}
D
@Cacheable
@SpringBootApplication
public class App {}
Attempts:
2 left
💡 Hint
Look for the correct annotation to enable caching support.
🔧 Debug
advanced
2:30remaining
Why does the cache not store values in this Spring Boot example?
Given this code, why is the cache never hit and the method always executes?
Spring Boot
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Cacheable("users")
    public String getUser(int id) {
        System.out.println("Loading user " + id);
        return "User-" + id;
    }
}

// Called from the same class method:
// public void process() {
//     getUser(1);
//     getUser(1);
// }
ABecause the method parameter is a primitive type, caching does not work with primitives.
BBecause the cache name "users" is invalid and caching silently fails.
CBecause @Cacheable requires @CachePut to work properly in the same class.
DBecause the method is called from within the same class, Spring's proxy does not intercept the call, so caching is bypassed.
Attempts:
2 left
💡 Hint
Think about how Spring AOP proxies work and method calls inside the same class.
state_output
advanced
2:00remaining
What is the size of the cache after these calls?
Given a cache named "products" and this sequence of calls, how many entries are stored in the cache?
Spring Boot
productService.getProduct(1);
productService.getProduct(2);
productService.getProduct(1);
productService.getProduct(3);
productService.getProduct(2);
A5 entries
B3 entries
C2 entries
D4 entries
Attempts:
2 left
💡 Hint
Remember that cache stores unique keys and repeated calls with same key do not add new entries.
🧠 Conceptual
expert
3:00remaining
Which statement about Spring Boot cache eviction is correct?
Consider a method annotated with @CacheEvict(value = "sessions", allEntries = true). What does this annotation do when the method is called?
AIt removes all entries from the "sessions" cache immediately after the method executes.
BIt removes only the cache entry matching the method's argument from "sessions" before the method executes.
CIt disables caching for the "sessions" cache permanently.
DIt clears the cache only if the method throws an exception.
Attempts:
2 left
💡 Hint
Focus on the meaning of allEntries=true in @CacheEvict.