0
0
Spring Bootframework~20 mins

JpaRepository interface in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JpaRepository 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 JpaRepository method call?
Given a JpaRepository for an entity Product, what will be the result of calling findById(10L) if no product with ID 10 exists?
Spring Boot
Optional<Product> product = productRepository.findById(10L);
System.out.println(product.isPresent());
Afalse
BThrows NoSuchElementException
Cnull
Dtrue
Attempts:
2 left
💡 Hint
Remember that findById returns an Optional, which may be empty if no entity is found.
📝 Syntax
intermediate
2:00remaining
Which option correctly declares a JpaRepository for an entity User with primary key type Long?
Choose the correct interface declaration for a JpaRepository managing User entities with Long IDs.
Apublic interface UserRepository implements JpaRepository<User, Long> {}
Bpublic interface UserRepository extends JpaRepository<User, Long> {}
Cpublic class UserRepository extends JpaRepository<User, Long> {}
Dpublic interface UserRepository extends JpaRepository<Long, User> {}
Attempts:
2 left
💡 Hint
JpaRepository is an interface and uses generics with entity type first, then ID type.
state_output
advanced
2:00remaining
What is the state of the database after calling deleteById(5L) if no entity with ID 5 exists?
Assuming productRepository is a JpaRepository, what happens when productRepository.deleteById(5L) is called but no product with ID 5 exists?
AThrows EmptyResultDataAccessException
BThrows NullPointerException
CDeletes all entities in the repository
DNo change; no exception is thrown
Attempts:
2 left
💡 Hint
Check the behavior of deleteById when the ID is not found.
🧠 Conceptual
advanced
2:00remaining
Which JpaRepository method returns a paginated list of entities?
You want to retrieve entities in pages instead of all at once. Which JpaRepository method should you use?
AsaveAll(Iterable<T> entities)
BfindById(Long id)
CdeleteAll()
DfindAll(Pageable pageable)
Attempts:
2 left
💡 Hint
Look for a method that accepts a Pageable parameter.
🔧 Debug
expert
3:00remaining
Why does this JpaRepository query method cause a runtime error?
Consider this method in a JpaRepository interface: List findByAgeGreaterThan(int age); When called, it throws an exception. What is the most likely cause?
Spring Boot
public interface UserRepository extends JpaRepository<User, Long> {
    List<User> findByAgeGreaterThan(int age);
}
AThe method name is invalid syntax for Spring Data JPA
BThe parameter type should be Integer, not int
CThe User entity does not have a field named 'age'
DJpaRepository does not support query methods with parameters
Attempts:
2 left
💡 Hint
Check if the entity has the field used in the method name.