Bird
Raised Fist0
Spring Bootframework~20 mins

Database and app orchestration in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Spring Boot Database Orchestration 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 REST controller method?
Consider this Spring Boot controller method that fetches a user by ID from a database using JPA. What will be the HTTP response status if the user is not found?
Spring Boot
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;

@RestController
@RequestMapping("/users")
public class UserController {

    private final UserRepository userRepository;

    public UserController(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @GetMapping("/{id}")
    public ResponseEntity<User> getUser(@PathVariable Long id) {
        Optional<User> user = userRepository.findById(id);
        if (user.isPresent()) {
            return ResponseEntity.ok(user.get());
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}
AHTTP 200 OK with user data if found, HTTP 404 Not Found if user missing
BHTTP 500 Internal Server Error if user missing
CHTTP 204 No Content if user missing
DHTTP 400 Bad Request if user missing
Attempts:
2 left
💡 Hint
Look at the ResponseEntity methods used for the missing user case.
❓ state_output
intermediate
2:00remaining
What is the value of the transaction status after this Spring Boot service method runs?
Given this service method annotated with @Transactional, what will be the transaction status after the method completes successfully?
Spring Boot
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderService {

    @Transactional
    public void placeOrder(Order order) {
        // save order to database
        // update inventory
        // send confirmation
    }
}
ATransaction is rolled back
BTransaction is left open and active
CTransaction is committed and closed
DTransaction status is unknown
Attempts:
2 left
💡 Hint
What does @Transactional do when no exceptions occur?
🔧 Debug
advanced
2:00remaining
Why does this Spring Boot application fail to connect to the database?
This Spring Boot app throws a connection error at startup. What is the most likely cause?
Spring Boot
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=secret
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
AUsername and password are incorrect
BDriver class name is outdated; should be com.mysql.cj.jdbc.Driver
CDatabase URL is missing the port number
DDatasource URL should start with jdbc:postgresql://
Attempts:
2 left
💡 Hint
Check the driver class name for MySQL Connector/J 8+
📝 Syntax
advanced
2:00remaining
Which option correctly defines a Spring Data JPA repository interface?
You want to create a repository interface for the entity Product with Long as ID type. Which option is syntactically correct?
Apublic interface ProductRepository extends JpaRepository<Product, Long> {}
Bpublic class ProductRepository implements JpaRepository<Product, Long> {}
Cpublic interface ProductRepository extends Repository<Product> {}
Dpublic interface ProductRepository extends JpaRepository<Product> {}
Attempts:
2 left
💡 Hint
JpaRepository requires two generic parameters: entity and ID type.
🧠 Conceptual
expert
3:00remaining
What happens if a Spring Boot @Transactional method calls another @Transactional method in the same class?
Consider a Spring Boot service class where a public @Transactional method calls another public @Transactional method within the same class. What is the behavior of the transaction management in this case?
AAn exception is thrown due to nested @Transactional calls
BBoth methods run in separate transactions
CThe inner method starts a new nested transaction automatically
DThe inner @Transactional method runs without a transaction because Spring proxies are bypassed
Attempts:
2 left
💡 Hint
Think about how Spring AOP proxies work for self-invocation.

Practice

(1/5)
1. What is the main role of a Spring Boot repository in database orchestration?
easy
A. To provide methods for saving, reading, updating, and deleting data
B. To define the database schema manually
C. To handle user interface rendering
D. To manage application security settings

Solution

  1. Step 1: Understand repository purpose

    Spring Boot repositories act as a bridge between the app and the database, offering easy data operations.
  2. Step 2: Identify repository capabilities

    They provide built-in methods like save(), findById(), delete(), which handle data safely without manual SQL.
  3. Final Answer:

    To provide methods for saving, reading, updating, and deleting data -> Option A
  4. Quick Check:

    Repository = Data operations [OK]
Hint: Repositories manage data actions like save and find [OK]
Common Mistakes:
  • Confusing repositories with UI components
  • Thinking repositories define database schema
  • Assuming repositories handle security
2. Which of the following is the correct way to declare a Spring Data JPA repository interface for an entity User with primary key type Long?
easy
A. public interface UserRepository extends Repository<User> {}
B. public interface UserRepository extends JpaRepository {}
C. public class UserRepository implements JpaRepository {}
D. public interface UserRepository extends CrudRepository {}

Solution

  1. Step 1: Recall correct interface extension

    Spring Data JPA repositories extend JpaRepository to get full CRUD support.
  2. Step 2: Check syntax correctness

    public interface UserRepository extends JpaRepository {} correctly extends JpaRepository with User entity and Long ID type; others misuse implements or wrong generics order.
  3. Final Answer:

    public interface UserRepository extends JpaRepository {} -> Option B
  4. Quick Check:

    JpaRepository syntax [OK]
Hint: Use extends JpaRepository for repositories [OK]
Common Mistakes:
  • Using implements instead of extends
  • Swapping generic type order
  • Extending incomplete Repository interface
3. Given the following Spring Boot repository method call:
Optional<User> user = userRepository.findById(5L);

What will user.isPresent() return if no user with ID 5 exists in the database?
medium
A. true
B. Throws an exception
C. null
D. false

Solution

  1. Step 1: Understand findById return type

    findById returns an Optional which is empty if no matching record is found.
  2. Step 2: Check isPresent behavior

    isPresent() returns false when Optional is empty, meaning no user found.
  3. Final Answer:

    false -> Option D
  4. Quick Check:

    Optional empty means isPresent() = false [OK]
Hint: Optional.isPresent() false means no data found [OK]
Common Mistakes:
  • Assuming isPresent() returns true always
  • Expecting null instead of Optional
  • Thinking it throws exception if not found
4. Consider this Spring Boot repository interface:
public interface ProductRepository extends JpaRepository {
List<Product> findByName(String name);
}

Which of the following issues will cause a runtime error when calling findByName(null)?
medium
A. No error; it returns an empty list
B. Method signature is invalid and won't compile
C. Calling findByName with null returns all products
D. Passing null causes a NullPointerException in query generation

Solution

  1. Step 1: Analyze null parameter in derived queries

    Spring Data JPA derived query methods accept null parameters and generate SQL like WHERE name = null, which matches no rows (since NULL = NULL is false).
  2. Step 2: Confirm behavior

    No exception is thrown at runtime or compile time; it safely returns an empty list.
  3. Final Answer:

    No error; it returns an empty list -> Option A
  4. Quick Check:

    findByName(null) = empty list [OK]
Hint: Null args in derived queries return empty lists safely [OK]
Common Mistakes:
  • Thinking null causes NullPointerException
  • Believing signature won't compile
  • Assuming null returns all products
5. You want to update a user's email in the database using Spring Boot. Which sequence correctly orchestrates this update using repository methods?
1. Retrieve user by ID
2. Change user's email
3. Save updated user back to database

Which code snippet correctly implements this?
hard
A. userRepository.save(new User(id, newEmail));
B. userRepository.updateEmailById(id, newEmail);
C. var user = userRepository.findById(id).orElseThrow(); user.setEmail(newEmail); userRepository.save(user);
D. userRepository.findById(id).getEmail(newEmail); userRepository.save();

Solution

  1. Step 1: Retrieve user safely

    Use findById(id).orElseThrow() to get user or throw if missing.
  2. Step 2: Modify user object

    Set new email on retrieved user object.
  3. Step 3: Save updated user

    Call save(user) to persist changes back to database.
  4. Final Answer:

    var user = userRepository.findById(id).orElseThrow(); user.setEmail(newEmail); userRepository.save(user); -> Option C
  5. Quick Check:

    Retrieve, modify, save = update pattern [OK]
Hint: Retrieve entity, change fields, then save [OK]
Common Mistakes:
  • Trying to save new object without ID
  • Using non-existent updateEmailById method
  • Calling getEmail() instead of setEmail()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard