CH 2 - Functions Flashcards

1
Q

In Kotlin, what value does an if expression return?

A

it returns a value based on the condition. Example:

val isHot = if (temperature > 40) true else false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Unit-Returning Functions
What is a Unit in Kotlin, and when is it used?

A

Unit is a type that represents no meaningful value, similar to void in other languages. It is used for functions that don’t return a value. Example:
fun printHello(name: String?) {
println(“Hi there!”)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do default parameters work in Kotlin?

A

Default parameters provide a fallback value if no argument is passed. Example

fun drive(speed: String = “fast”) {
println(“driving $speed”)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Required Parameters

Q: What happens if no default is specified for a parameter?
A

un tempToday(day: String, temp: Int) {
println(“Today is $day and it’s $temp degrees.”)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

why Named Arguments

A

Named arguments improve code readability by explicitly identifying the parameter name. Example:

reformat(str, divideByCamelHumps = false, wordSeparator = ‘_’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are compact or single-expression functions in Kotlin?

A

these functions are written concisely in a single line. Example:

fun double(x: Int): Int = x * 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a lambda expression in Kotlin?

A

A lambda is an anonymous/ no name function. Example:

val waterFilter = { level: Int -> level / 2 }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Higher-Order Functions

A

A higher-order function takes functions as parameters or returns a function. Example:

fun encodeMsg(msg: String, encode: (String) -> String): String {
return encode(msg)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you pass a function reference in Kotlin?

A

Use the :: operator. Example:

encodeMessage(“abc”, ::enc2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

List Filters.How do you filter a list in Kotlin?

A

Use the filter function with a condition. Example:

val books = listOf(“nature”, “biology”, “birds”)
println(books.filter { it[0] == ‘b’ })

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What’s the difference between eager and lazy filters in Kotlin?

A

eager filters evaluate immediately and return a new list, while lazy filters (using sequences) only evaluate when needed. Example of lazy filter:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

e.g of eager filter

A

val numbers = listOf(1, 2, 3, 4, 5)
val eager = numbers.filter { it > 2 } // Filters the list right away
println(eager) // Output: [3, 4, 5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

e.g of lazy filter

A

val numbers = listOf(1, 2, 3, 4, 5)
val lazy = numbers.asSequence().filter { it > 2 } // Doesn’t filter yet
println(lazy.toList()) // Only filters when converted to a list
// Output: [3, 4, 5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Dry run the following code and predict the output:

val dirtLevel = 30
val waterFilter = { level: Int -> level / 3 }
println(waterFilter(dirtLevel))

A

10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Rewrite the following function in a more compact form:

fun square(x: Int): Int {
return x * x
}

A

fun square(x: Int): Int = x * x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Create a function that takes a string and returns it in reverse using a lambda. Then, dry run the function:

fun reverseString(input: String, reverse: (String) -> String): String {
return reverse(input)
}

val reverse = { str: String -> str.reversed() }
println(reverseString(“Kotlin”, reverse))

A

niltoK

17
Q

Use a lazy sequence to filter and print only strings starting with ‘a’ from a list:

val fruits = listOf(“apple”, “banana”, “apricot”, “grape”)
val filtered = fruits.asSequence().filter { it[0] == ‘a’ }
println(filtered.toList())

A

[“apple”, “apricot”]