Complete the code to define a single-expression function that returns the square of a number.
fun square(x: Int): Int = [1]The single-expression function uses the '=' sign followed by the expression. Here, x * x correctly returns the square.
Complete the code to define a single-expression function that returns the length of a string.
fun lengthOf(str: String): Int = [1]length() with parentheses which is invalid in Kotlin.size which is for collections, not strings.In Kotlin, the length of a string is accessed using the length property without parentheses.
Fix the error in the single-expression function that returns true if a number is even.
fun isEven(num: Int): Boolean = num [1] 2 == 0
/ instead of modulo %.The modulo operator % gives the remainder. If num % 2 == 0, the number is even.
Complete the code to create a single-expression function that returns the last character of a string.
fun lastChar(str: String): Char = str[1]str. prefix before length.To get the last character, use the indexing operator [] with str.length - 1 which gives the last valid index.
Fill all three blanks to define a single-expression function that returns a greeting message with the user's name.
fun greet(name: String): String = "Hello, [1]!".[2]() + [3]
uppercase() which is Kotlin 1.5+ but not always available.The function inserts the name inside the greeting string, converts it to uppercase with toUpperCase(), and adds a friendly message.