Bird
Raised Fist0
Kotlinprogramming~20 mins

Lazy property delegation in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Lazy Property Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Kotlin code using lazy delegation?
Consider the following Kotlin code snippet. What will be printed when the program runs?
Kotlin
class Example {
    val lazyValue: String by lazy {
        println("Computed!")
        "Hello"
    }
}

fun main() {
    val example = Example()
    println("Before accessing lazyValue")
    println(example.lazyValue)
    println(example.lazyValue)
}
A
Before accessing lazyValue
Computed!
Hello
Hello
B
Computed!
Before accessing lazyValue
Hello
Hello
C
Before accessing lazyValue
Hello
Computed!
Hello
D
Before accessing lazyValue
Hello
Hello
Computed!
Attempts:
2 left
💡 Hint
Remember that the lazy block runs only once, when the property is accessed the first time.
🧠 Conceptual
intermediate
2:00remaining
What happens if you access a lazy property from multiple threads without synchronization?
In Kotlin, if a lazy property is declared with the default lazy() delegate and accessed from multiple threads simultaneously, what is the expected behavior?
AThe lazy block is executed exactly once in a thread-safe manner.
BThe lazy block may be executed multiple times concurrently, causing race conditions.
CThe program will throw a runtime exception due to concurrent access.
DThe lazy property will always return null if accessed from multiple threads.
Attempts:
2 left
💡 Hint
Check the default thread safety mode of Kotlin's lazy delegate.
❓ Predict Output
advanced
2:00remaining
What is the output of this Kotlin code with custom lazy mode?
Analyze the following Kotlin code. What will be printed when main() runs?
Kotlin
class Test {
    val lazyValue: String by lazy(LazyThreadSafetyMode.NONE) {
        println("Calculating...")
        "Done"
    }
}

fun main() {
    val test = Test()
    println(test.lazyValue)
    println(test.lazyValue)
}
A
Done
Done
Done
B
Done
Calculating...
Done
C
Calculating...
Calculating...
Done
Done
D
Calculating...
Done
Done
Attempts:
2 left
💡 Hint
LazyThreadSafetyMode.NONE disables synchronization but does not change the single execution behavior in single-threaded code.
🔧 Debug
advanced
2:00remaining
Why does this Kotlin lazy property cause a NullPointerException?
Examine the code below. Why does accessing lazyValue cause a NullPointerException?
Kotlin
class Demo {
    val lazyValue: String by lazy {
        null!!.toString()
    }
}

fun main() {
    val demo = Demo()
    println(demo.lazyValue)
}
AThe NullPointerException occurs because lazyValue is accessed before the Demo object is fully constructed.
BThe lazy property is not initialized properly because lazy requires a non-null initializer.
CThe lazy block throws a NullPointerException because null!! forces a null dereference.
DThe code compiles but prints "null" without exception.
Attempts:
2 left
💡 Hint
Look at the expression inside the lazy block carefully.
🚀 Application
expert
2:00remaining
How many times is the initializer called in this Kotlin lazy property example with multiple accesses?
Given the code below, how many times will the initializer block inside lazy be executed?
Kotlin
class Counter {
    var count = 0
    val lazyValue: String by lazy {
        count++
        "Counted"
    }
}

fun main() {
    val c = Counter()
    println(c.lazyValue)
    println(c.lazyValue)
    println(c.lazyValue)
    println(c.count)
}
A0
B1
C3
D4
Attempts:
2 left
💡 Hint
The lazy initializer runs only once regardless of how many times the property is accessed.

Practice

(1/5)
1. What does lazy property delegation in Kotlin do?
easy
A. Deletes the property value after each use
B. Delays the creation of a property value until it is first accessed
C. Creates the property value immediately when the object is created
D. Allows multiple values to be stored in the same property

Solution

  1. Step 1: Understand lazy delegation concept

    Lazy delegation means the property value is not created when the object is made, but only when the property is accessed for the first time.
  2. Step 2: Compare options with this concept

    Delays the creation of a property value until it is first accessed matches this behavior exactly. Options A, B, and D describe different behaviors not related to lazy delegation.
  3. Final Answer:

    Delays the creation of a property value until it is first accessed -> Option B
  4. Quick Check:

    Lazy property = delayed creation [OK]
Hint: Lazy means create value only when first needed [OK]
Common Mistakes:
  • Thinking lazy creates value immediately
  • Confusing lazy with caching multiple values
  • Assuming lazy deletes values after use
2. Which of the following is the correct syntax to declare a lazy property in Kotlin?
easy
A. val name = lazy("John")
B. val name = lazy { "John" }
C. val name by lazy { "John" }
D. lazy val name = "John"

Solution

  1. Step 1: Recall Kotlin lazy syntax

    In Kotlin, lazy properties use the syntax: val propertyName by lazy { initializer }.
  2. Step 2: Match options with correct syntax

    val name by lazy { "John" } matches this exactly. val name = lazy { "John" } assigns lazy delegate but does not use 'by'. lazy val name = "John" uses invalid order. val name = lazy("John") uses wrong function call syntax.
  3. Final Answer:

    val name by lazy { "John" } -> Option C
  4. Quick Check:

    Use 'by lazy { }' for lazy property [OK]
Hint: Remember: val x by lazy { ... } [OK]
Common Mistakes:
  • Omitting 'by' keyword
  • Using lazy() as a function call incorrectly
  • Placing 'lazy' before 'val'
3. What will be the output of this Kotlin code?
val lazyValue by lazy {
    println("Computing...")
    10
}

fun main() {
    println(lazyValue)
    println(lazyValue)
}
medium
A. Computing...\n10\n10
B. 10\n10
C. Computing...\n10\nComputing...\n10
D. Computing...\nComputing...\n10\n10

Solution

  1. Step 1: Understand lazy initialization behavior

    The lambda inside lazy runs only once, when the property is first accessed. It prints "Computing..." and returns 10.
  2. Step 2: Trace the program output

    First println(lazyValue) triggers lazy initialization, printing "Computing..." then 10. Second println(lazyValue) uses cached value, printing only 10.
  3. Final Answer:

    Computing...\n10\n10 -> Option A
  4. Quick Check:

    Lazy runs once, then caches value [OK]
Hint: Lazy block runs once, then cached value used [OK]
Common Mistakes:
  • Assuming lazy block runs every access
  • Missing that println inside lazy prints once
  • Confusing output order
4. Identify the error in this Kotlin code using lazy delegation:
class Example {
    val number = lazy { 5 }
}

fun main() {
    val e = Example()
    println(e.number)
}
medium
A. The lazy block must return a String, not Int
B. The println statement is missing parentheses
C. The lazy delegate cannot be used inside a class
D. The property 'number' should be declared with 'by lazy', not assigned directly

Solution

  1. Step 1: Check lazy property declaration

    The property 'number' is assigned lazy { 5 } directly, but lazy delegation requires 'by lazy' syntax.
  2. Step 2: Identify correct syntax

    Correct declaration is 'val number by lazy { 5 }'. Without 'by', 'number' is a Lazy<Int> object, not the Int value.
  3. Final Answer:

    The property 'number' should be declared with 'by lazy', not assigned directly -> Option D
  4. Quick Check:

    Use 'by lazy' keyword for lazy properties [OK]
Hint: Lazy needs 'by lazy', not just assignment [OK]
Common Mistakes:
  • Omitting 'by' keyword
  • Confusing lazy delegate type with property type
  • Thinking lazy only works outside classes
5. You want to create a Kotlin class where a heavy computation is done only once and reused, but the computation depends on a constructor parameter. How should you use lazy delegation to achieve this?
class Calculator(val input: Int) {
    val result by lazy { /* ??? */ }
}
Which is the correct way to implement the lazy block?
hard
A. val result by lazy { compute() } // where compute() is a member function
B. val result by lazy { Calculator(input).compute() }
C. val result by lazy { input }
D. val result by lazy { input * 2 }

Solution

  1. Step 1: Understand lazy with constructor parameter

    The lazy block can access constructor parameters and member functions. Using a member function keeps code clean and reusable.
  2. Step 2: Evaluate options

    val result by lazy { input * 2 } uses input directly, which works but may duplicate logic. val result by lazy { Calculator(input).compute() } creates a new Calculator inside lazy, causing recursion. val result by lazy { input } just returns input, no computation. val result by lazy { compute() } // where compute() is a member function calls a member function compute(), which can use input and perform heavy work once.
  3. Final Answer:

    val result by lazy { compute() } // where compute() is a member function -> Option A
  4. Quick Check:

    Use member function inside lazy for complex computation [OK]
Hint: Call member function inside lazy for complex work [OK]
Common Mistakes:
  • Creating new instance inside lazy causing recursion
  • Using constructor parameter directly without logic
  • Returning parameter without computation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard