Challenge - 5 Problems
Entity to DTO Mapping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this DTO mapping method?
Given the following Spring Boot entity and DTO classes, what will be the value of
userDTO.getFullName() after calling mapToDTO(user)?Spring Boot
public class User { private String firstName; private String lastName; private int age; // getters and setters public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } public class UserDTO { private String fullName; private int age; // getters and setters public String getFullName() { return fullName; } public void setFullName(String fullName) { this.fullName = fullName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } public UserDTO mapToDTO(User user) { UserDTO dto = new UserDTO(); dto.setFullName(user.getFirstName() + " " + user.getLastName()); dto.setAge(user.getAge()); return dto; } // Usage: User user = new User(); user.setFirstName("Jane"); user.setLastName("Doe"); user.setAge(30); UserDTO userDTO = mapToDTO(user);
Attempts:
2 left
💡 Hint
Look at how the fullName is constructed by concatenating firstName and lastName with a space.
✗ Incorrect
The mapToDTO method concatenates user.getFirstName() + " " + user.getLastName(), so the fullName will be "Jane Doe".
📝 Syntax
intermediate2:00remaining
Which option correctly maps a list of entities to a list of DTOs using Java streams?
You want to convert a List to List using Java streams in Spring Boot. Which code snippet correctly performs this mapping?
Spring Boot
List<User> users = ...; // list of User entities
// Map to List<UserDTO>Attempts:
2 left
💡 Hint
Remember that List does not have a map method, streams do.
✗ Incorrect
Option A correctly uses users.stream(), then maps each user with mapToDTO(user), and collects to a list. Option A is invalid because List has no map method. Option A is invalid because mapToDTO is a method, not a variable or function reference without parentheses. Option A passes the method itself instead of calling it.
🔧 Debug
advanced2:00remaining
Why does this mapping code throw a NullPointerException?
Consider this mapping method in a Spring Boot service:
public UserDTO mapToDTO(User user) {
UserDTO dto = new UserDTO();
dto.setFullName(user.getFirstName().toUpperCase() + " " + user.getLastName().toUpperCase());
dto.setAge(user.getAge());
return dto;
}
If user.getFirstName() is null, what happens when this method runs?
Attempts:
2 left
💡 Hint
What happens if you call a method on a null object in Java?
✗ Incorrect
Calling toUpperCase() on a null string causes a NullPointerException at runtime.
🧠 Conceptual
advanced2:00remaining
What is the main benefit of using DTOs instead of exposing entities directly in Spring Boot APIs?
Why do developers often map entities to DTOs before sending data in API responses?
Attempts:
2 left
💡 Hint
Think about why you might not want to share your database structure directly with users.
✗ Incorrect
DTOs help control what data is sent out, hide sensitive fields, and allow internal models to change without breaking API contracts.
❓ state_output
expert3:00remaining
What is the value of
dto.getAddress().getCity() after mapping?Given these classes and mapping method, what will
dto.getAddress().getCity() return?
public class User {
private String name;
private Address address;
// getters/setters
}
public class Address {
private String city;
private String street;
// getters/setters
}
public class UserDTO {
private String name;
private AddressDTO address;
// getters/setters
}
public class AddressDTO {
private String city;
// getters/setters
}
public UserDTO mapToDTO(User user) {
UserDTO dto = new UserDTO();
dto.setName(user.getName());
AddressDTO addrDto = new AddressDTO();
addrDto.setCity(user.getAddress().getCity());
dto.setAddress(addrDto);
return dto;
}
// Usage:
User user = new User();
user.setName("Alice");
Address addr = new Address();
addr.setCity("Springfield");
addr.setStreet("Main St");
user.setAddress(addr);
UserDTO dto = mapToDTO(user);Attempts:
2 left
💡 Hint
Check how the city field is copied from Address to AddressDTO.
✗ Incorrect
The mapToDTO method copies the city from user.getAddress() to AddressDTO. The street is not copied, so dto.getAddress().getCity() returns "Springfield".