0
0
Spring Bootframework~20 mins

CRUD methods (save, findById, findAll, delete) in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spring Data CRUD Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does the save method do in a Spring Data repository?

Consider a Spring Data JPA repository interface. What is the main behavior of the save method?

AIt inserts a new entity or updates an existing one in the database.
BIt only inserts a new entity and throws an error if the entity exists.
CIt deletes the entity from the database.
DIt retrieves an entity by its ID.
Attempts:
2 left
💡 Hint

Think about whether save can handle both new and existing data.

state_output
intermediate
2:00remaining
What is the output of findById when the entity is not found?

Given a Spring Data repository, what does findById return if no entity matches the given ID?

Spring Boot
Optional<Entity> result = repository.findById(999L);
AAn empty <code>Optional</code> object.
BA <code>null</code> value.
CThrows <code>NoSuchElementException</code> immediately.
DReturns a default entity instance.
Attempts:
2 left
💡 Hint

Remember how Optional works in Java.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly deletes an entity by ID using Spring Data repository?

Choose the correct way to delete an entity by its ID using a Spring Data repository.

Arepository.deleteEntityById(entityId);
Brepository.deleteById(entityId);
Crepository.delete(entityId);
Drepository.remove(entityId);
Attempts:
2 left
💡 Hint

Check the official Spring Data repository method names.

🔧 Debug
advanced
2:00remaining
Why does this findAll call cause a runtime error?

Given this code snippet, why does calling findAll cause a runtime error?

List list = repository.findAll();
Spring Boot
List<Entity> list = repository.findAll();
AThe <code>findAll</code> method returns a Stream, not a List.
BThe <code>findAll</code> method requires an argument specifying a filter.
CThe entity class is missing a no-argument constructor.
DThe repository interface does not extend <code>JpaRepository</code> or <code>CrudRepository</code>.
Attempts:
2 left
💡 Hint

JPA entities require a no-argument constructor for instantiation.

🧠 Conceptual
expert
2:00remaining
What happens if you call deleteById with a non-existing ID?

In Spring Data JPA, what is the behavior when deleteById is called with an ID that does not exist in the database?

AIt silently does nothing without throwing an error.
BIt creates a new entity with that ID and then deletes it.
CIt returns a boolean indicating success or failure.
DIt throws <code>EmptyResultDataAccessException</code> immediately.
Attempts:
2 left
💡 Hint

Think about how Spring Data handles deleting non-existing records.