0
0
Kotlinprogramming~20 mins

Property-based testing concept in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Property Testing 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 property-based test snippet?
Consider this Kotlin code using the property-based testing library Kotest. What will be printed when the test runs?
Kotlin
import io.kotest.property.checkAll
import io.kotest.property.arbitrary.int

fun main() {
    checkAll<Int> { a ->
        println(a * 2 >= a)
    }
}
APrints 'true' for all generated integers
BCompilation error due to missing import
CPrints 'false' for some integers
DRuntime exception due to overflow
Attempts:
2 left
💡 Hint
Consider negative integers: for a = -1, is -2 >= -1?
🧠 Conceptual
intermediate
1:30remaining
What is the main advantage of property-based testing?
Choose the best description of the main advantage of property-based testing compared to example-based testing.
AIt tests many random inputs automatically to find edge cases
BIt replaces the need for unit tests entirely
CIt only tests the user interface of applications
DIt requires writing fewer tests because it uses examples
Attempts:
2 left
💡 Hint
Think about how property-based testing generates inputs.
🔧 Debug
advanced
2:30remaining
Why does this property-based test fail with a false negative?
This Kotlin property-based test is supposed to check if reversing a list twice returns the original list. Why might it fail unexpectedly?
Kotlin
import io.kotest.property.checkAll

fun main() {
    checkAll<List<Int>> { list ->
        assert(list.reversed().reversed() == list)
    }
}
AThe test fails because reversed() changes the original list in place
BThe test fails because reversed() does not return a new list
CThe test fails because assert throws an exception instead of reporting failure
DThe test fails because List<Int> is not a valid generator type without specifying an arbitrary
Attempts:
2 left
💡 Hint
Consider how property-based testing generates inputs for generic types.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a property test for reversing a string twice?
Select the Kotlin code snippet that correctly tests if reversing a string twice returns the original string using Kotest property testing.
AcheckAll<String> { s -> assertEquals(s, s.reversed().reversed()) }
BcheckAll<String> { s -> assert(s.reversed().reversed() == s) }
CcheckAll<String> { s -> assert(s == s.reversed().reversed()) }
DcheckAll<String> { s -> assert(s.reversed() == s) }
Attempts:
2 left
💡 Hint
Consider the best assertion function to use in Kotlin tests.
🚀 Application
expert
1:30remaining
How many test cases will be generated by this property test?
Given this Kotlin property-based test code, how many test cases will Kotest generate by default?
Kotlin
import io.kotest.property.checkAll

fun main() {
    checkAll<Int> { a ->
        println(a)
    }
}
A50 test cases
B100 test cases
C10 test cases
DUnlimited test cases until failure
Attempts:
2 left
💡 Hint
Check Kotest default number of generated cases for checkAll.