0
0
Spring Bootframework~20 mins

Entity to DTO mapping in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Entity to DTO Mapping 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 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);
A"JaneDoe"
B"Jane Doe"
C"Doe Jane"
D"Doe"
Attempts:
2 left
💡 Hint
Look at how the fullName is constructed by concatenating firstName and lastName with a space.
📝 Syntax
intermediate
2: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>
AList<UserDTO> dtos = users.stream().map(user -> mapToDTO(user)).collect(Collectors.toList());
BList<UserDTO> dtos = users.map(user -> mapToDTO(user)).toList();
CList<UserDTO> dtos = users.stream().map(mapToDTO).collect(Collectors.toList());
DList<UserDTO> dtos = users.stream().map(user -> mapToDTO).collect(Collectors.toList());
Attempts:
2 left
💡 Hint
Remember that List does not have a map method, streams do.
🔧 Debug
advanced
2: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?
AIt throws a NullPointerException because calling toUpperCase() on null causes an error.
BIt compiles but skips setting fullName.
CIt returns a UserDTO with fullName as " " (empty string).
DIt returns a UserDTO with fullName as "null null".
Attempts:
2 left
💡 Hint
What happens if you call a method on a null object in Java?
🧠 Conceptual
advanced
2: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?
ATo make the API responses larger and more detailed by including all entity fields.
BTo increase coupling between database schema and API contracts.
CTo avoid writing any mapping code and use entities directly everywhere.
DTo control and limit the data exposed to clients, improving security and decoupling internal models.
Attempts:
2 left
💡 Hint
Think about why you might not want to share your database structure directly with users.
state_output
expert
3: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);
A"Main St"
Bnull
C"Springfield"
DThrows NullPointerException
Attempts:
2 left
💡 Hint
Check how the city field is copied from Address to AddressDTO.