Builder Pattern — Concept Check
Questions on Builder Pattern. The Builder pattern constructs complex objects step-by-step with a fluent API, solving the telescoping constructor problem.
Questions in this quiz
8 questions on Builder Pattern — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer is using the Builder pattern to create a DatabaseConfig object. They forget to set the database field. What will happen when they call build()?
- AThe build() method returns null
- BAn IllegalStateException is thrown
- CA compile-time error occurs
- DA default database is used
2.While using Lombok's @Builder annotation, a developer sets a default value for a timeout field. What happens if they don't explicitly set a timeout in the builder?
- AThe timeout is set to null
- BThe default value is used
- CThe build() method throws an exception
- DA compile-time error occurs
3.A team is using a record in Java 16+ for data transfer objects. They want to modify one field of an existing record. Which approach should they use?
- AModify the field directly
- BCreate a new record with a different constructor
- CUse reflection to change the field
- DUse a wither method
4.A developer claims that using the Builder pattern makes it impossible to have a mutable object. Why is this incorrect?
- ABuilders can only create mutable objects
- BImmutable objects are not supported by the Builder pattern
- CThe pattern enforces mutability
- DThe Builder pattern creates immutable objects, but mutability depends on implementation
5.You are tasked with implementing a Builder pattern for a class with many optional parameters. What problem does this pattern help solve?
- AMemory leak problem
- BTelescoping constructor problem
- CConcurrency issue
- DCircular dependency problem
6.A developer is using Lombok's @Builder annotation but forgets to add @Builder.Default to a field with a default value. What is the consequence?
- AThe field will not have a default value
- BThe field will be set to null
- CA compile-time error will occur
- DThe build() method will fail
7.In a classic Builder implementation, why is the target class constructor made private?
- ATo ensure objects are only created via the Builder
- BTo enforce immutability
- CTo optimize memory usage
- DTo prevent inheritance
8.A developer thinks that using a static inner Builder class is unnecessary because Lombok can generate it. What is a potential downside of relying solely on Lombok?
- ALombok may not cover all customization needs
- BLombok requires manual memory management
- CLombok generates mutable objects
- DLombok increases runtime overhead