Challenge - 5 Problems
Factory Pattern Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of factory method with companion object
What is the output of this Kotlin code using a factory pattern with a companion object?
Kotlin
class Vehicle private constructor(val type: String) { companion object { fun create(type: String): Vehicle { return Vehicle(type) } } override fun toString() = "Vehicle type: $type" } fun main() { val car = Vehicle.create("Car") println(car) }
Attempts:
2 left
💡 Hint
Look at how the companion object creates an instance with the given type.
✗ Incorrect
The companion object has a factory method 'create' that calls the private constructor with the given type. The toString method returns 'Vehicle type: Car' when printed.
❓ Predict Output
intermediate2:00remaining
Output when factory method returns different subclasses
What will be printed when running this Kotlin code that uses a factory method in a companion object to return different subclasses?
Kotlin
sealed class Animal { abstract fun sound(): String companion object { fun create(type: String): Animal = when(type) { "dog" -> Dog() "cat" -> Cat() else -> throw IllegalArgumentException("Unknown animal") } } } class Dog : Animal() { override fun sound() = "Woof" } class Cat : Animal() { override fun sound() = "Meow" } fun main() { val animal = Animal.create("cat") println(animal.sound()) }
Attempts:
2 left
💡 Hint
Check which subclass is created by the factory method for the input "cat".
✗ Incorrect
The factory method returns a Cat instance when the input is "cat". Calling sound() on Cat returns "Meow".
🔧 Debug
advanced2:00remaining
Identify the error in this factory pattern with companion object
This Kotlin code tries to implement a factory pattern with a companion object but fails to compile. What is the cause of the error?
Kotlin
class Product private constructor(val name: String) { companion object { fun create(name: String): Product { return Product(name) } } } fun main() { val p = Product.create("Book") println(p.name) }
Attempts:
2 left
💡 Hint
Check the return type and value of the create function.
✗ Incorrect
The create function creates a Product but does not return it, so p is assigned Unit, causing a compilation error when accessing p.name.
📝 Syntax
advanced2:00remaining
Which option correctly defines a factory method in a companion object?
Choose the Kotlin code snippet that correctly defines a factory method named 'build' inside a companion object that returns an instance of the class 'Gadget'.
Attempts:
2 left
💡 Hint
The factory method must return an instance of Gadget explicitly.
✗ Incorrect
Option D correctly defines a function returning a Gadget instance. Option D has a block body but missing explicit return type and may cause confusion. Option D returns the class reference, not an instance. Option D misses the return statement in the block body.
🚀 Application
expert3:00remaining
How many instances are created and what is printed?
Consider this Kotlin code using a factory pattern with companion object and caching. How many instances of 'CacheItem' are created, and what is printed?
Kotlin
class CacheItem private constructor(val id: Int) { companion object { private val cache = mutableMapOf<Int, CacheItem>() fun get(id: Int): CacheItem { return cache.getOrPut(id) { CacheItem(id) } } } override fun toString() = "CacheItem #$id" } fun main() { val a = CacheItem.get(1) val b = CacheItem.get(2) val c = CacheItem.get(1) println(a === c) println(a === b) println(a) println(b) }
Attempts:
2 left
💡 Hint
Check how many times the constructor is called and how getOrPut works.
✗ Incorrect
The cache stores instances by id. The first call creates id=1, second creates id=2, third returns cached id=1. So two instances total. 'a === c' is true because they reference the same object; 'a === b' is false.