0
0
Spring Bootframework~20 mins

DTO pattern for data transfer in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DTO Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What is the main purpose of a DTO in Spring Boot?

In a Spring Boot application, why do developers use a Data Transfer Object (DTO)?

ATo handle HTTP requests directly from the client
BTo transfer data between layers without exposing the entity directly
CTo store data permanently in the database
DTo replace the service layer logic
Attempts:
2 left
💡 Hint

Think about how data is shared safely between parts of the app.

📝 Syntax
intermediate
2:00remaining
Which DTO declaration is syntactically correct in Spring Boot?

Choose the correct way to declare a simple DTO class in Spring Boot.

Apublic class UserDTO { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
Bpublic class UserDTO { String name; getName() { return name; } setName(String name) { this.name = name; } }
Cclass UserDTO { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
Dpublic UserDTO { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
Attempts:
2 left
💡 Hint

Remember Java class syntax requires access modifiers and method return types.

state_output
advanced
1:30remaining
What will be the output of this DTO mapping code?

Given this code snippet in a Spring Boot service, what will be the value of userDTO.getEmail() after execution?

Spring Boot
User user = new User("Alice", "alice@example.com");
UserDTO userDTO = new UserDTO();
userDTO.setName(user.getName());
// Note: Email is not set in userDTO

String email = userDTO.getEmail();
AThrows NullPointerException
B"alice@example.com"
CEmpty string ""
Dnull
Attempts:
2 left
💡 Hint

Check which fields are assigned values in the DTO.

🔧 Debug
advanced
2:00remaining
Why does this DTO cause a JSON serialization error?

Consider this DTO class used in a Spring Boot REST controller. Why does it cause a JSON serialization error?

Spring Boot
public class ProductDTO {
  private String name;
  private CategoryDTO category;
  // getters and setters
}

public class CategoryDTO {
  private String name;
  private ProductDTO product;
  // getters and setters
}
ABecause of circular reference between ProductDTO and CategoryDTO causing infinite recursion
BBecause fields are private without public getters and setters
CBecause DTO classes must be annotated with @Entity
DBecause JSON serialization requires fields to be static
Attempts:
2 left
💡 Hint

Think about what happens when JSON tries to convert linked objects referencing each other.

🧠 Conceptual
expert
2:30remaining
Which statement best describes the advantage of using DTOs in microservices?

In a microservices architecture, why is using DTOs beneficial when services communicate?

ADTOs allow direct database sharing between microservices
BDTOs automatically encrypt data between microservices for security
CDTOs reduce data transfer size by including only needed fields, improving performance and decoupling services
DDTOs replace the need for API gateways in microservices
Attempts:
2 left
💡 Hint

Consider how data is shared efficiently and safely between independent services.