REVISION DOC BY RABSON Flashcards
26.What is Null safety and how do we test for it?
Kotlin avoids null pointer exceptions by requiring nullable types to be explicitly declared with ?. Test for null using
?.
?:
!!.
25.Demonstrate how you implement an array, and a list, how do they differ.
Array: val arr = arrayOf(1, 2, 3)
List: val list = listOf(1, 2, 3)
Arrays have a fixed size, lists can be mutable (mutableListOf()).
- If/Else Statements:
Used to execute code based on a condition.
if (x > 10) {
println(“Greater than 10”)
} else {
println(“Less than or equal to 10”)
}
23.When Statements
Used as a replacement for switch-case, checking multiple conditions.
when (x) {
1 -> println(“One”)
2 -> println(“Two”)
else -> println(“Other”)
}
23.While Loops:
Repeats code while a condition is true.
var i = 0
while (i < 5) {
println(i) // Prints 0 to 4
i++
}
23.For Loops:
Used to iterate over collections or ranges.
for (i in 1..5) {
println(i) // Prints 1 to 5
}
- Demonstrate how to use when, range, for loop, while loop, repeat loop, demonstrate with examples.