0
0
Spring Bootframework~20 mins

DTO vs entity separation benefit in Spring Boot - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DTO vs Entity Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why separate DTOs from entities in Spring Boot?
In a Spring Boot application, why is it beneficial to separate Data Transfer Objects (DTOs) from entity classes?
ABecause entities cannot be serialized to JSON
BTo reduce the number of classes in the project
CTo protect the database schema from direct exposure and allow flexible API evolution
DBecause DTOs automatically handle database transactions
Attempts:
2 left
💡 Hint
Think about how exposing entities directly might affect your API and database design.
component_behavior
intermediate
2:00remaining
What happens if you expose entities directly in API responses?
Consider a Spring Boot REST API that returns entity objects directly as JSON responses. What is a likely consequence of this approach?
AEntities will be converted to DTOs automatically by Spring Boot
BThe API will automatically validate all input data
CThe application will run faster due to fewer classes
DClients may receive sensitive or unnecessary data fields tied to the database schema
Attempts:
2 left
💡 Hint
Think about what data entities contain and how that might affect clients.
state_output
advanced
2:30remaining
What is the output when mapping entity to DTO with missing fields?
Given an entity with fields id, name, and password, and a DTO with only id and name, what will be the output JSON when mapping the entity to the DTO and returning it in a Spring Boot REST controller?
Spring Boot
public record UserEntity(Long id, String name, String password) {}

public record UserDTO(Long id, String name) {}

// Mapping method
public UserDTO toDTO(UserEntity entity) {
  return new UserDTO(entity.id(), entity.name());
}

// Controller method returns toDTO(userEntity)
A{"id":1,"name":"Alice"}
B{"id":1,"name":"Alice","password":"secret"}
C{"name":"Alice"}
D{"id":1,"password":"secret"}
Attempts:
2 left
💡 Hint
Only fields declared in the DTO will be serialized in the JSON response.
📝 Syntax
advanced
2:00remaining
Identify the correct DTO mapping syntax in Spring Boot
Which of the following code snippets correctly maps a UserEntity to a UserDTO in Spring Boot using a constructor?
Spring Boot
public record UserEntity(Long id, String name, String password) {}
public record UserDTO(Long id, String name) {}
AUserDTO dto = new UserDTO(userEntity.id, userEntity.name);
BUserDTO dto = new UserDTO(userEntity.id(), userEntity.name());
CUserDTO dto = new UserDTO(userEntity.getId(), userEntity.getName());
DUserDTO dto = new UserDTO(userEntity.getId, userEntity.getName);
Attempts:
2 left
💡 Hint
Records use accessor methods without 'get' prefix.
🔧 Debug
expert
3:00remaining
Why does exposing entities cause a security risk in Spring Boot?
A developer returns JPA entity objects directly from a REST controller. Later, they notice sensitive fields like passwords are exposed in API responses. Why does this happen?
AEntities include all fields by default, so sensitive data is serialized unless explicitly excluded
BDTOs are required to serialize entities properly
CSpring Boot automatically encrypts entity fields, so passwords are safe
DEntities cannot be serialized to JSON, so this is impossible
Attempts:
2 left
💡 Hint
Think about what happens when an object is converted to JSON without filtering.