Kotlin Basics Flashcards

1
Q

.
How do you define a simple printHello() function in Kotlin?

A

fun printHello() {
println(“Hello”)
}

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

What are the basic mathematical operators in Kotlin?

A

+, -, *, /, %

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

What are the increment and decrement operators in Kotlin?

nb// remeber pre and post

A

: ++ (increment), – (decrement)

pre = ++a . shows me exact value of a but if I print later it will increase

post =a++

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

What is the syntax for calling numeric operator methods on numbers in Kotlin?

A

2.times(3) // Output: 6
3.5.plus(4) // Output: 7.5
2.4.div(2) // Output: 1.2

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

What are the integer data types in Kotlin?

A

Long (64 bits)
Int (32 bits)
Short (8 bits)

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

What is type casting in Kotlin? Provide an example of casting Int to Byte.

A

he process of converting an object from one data type to another,
val i: Int = 6
println(i.toByte()) // Output: 6

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

How do you use underscores for long numbers in Kotlin?

A

val oneMillion = 1_000_000
val idNumber = 999_99_9999L

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

How do you perform string concatenation in Kotlin using variable interpolation?

A

val numberOfDogs = 3
val numberOfCats = 2
“I have $numberOfDogs dogs and $numberOfCats cats”

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

What is a string template in Kotlin? Provide an example.

A

allows embedding variables or expressions inside a string using the $ symbol

val i = 10
println(“i = $i”) // Output: i = 10

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

Q: What are the mutable and immutable variables in Kotlin?

A

Mutable (can be changed): var, e.g., var count = 1
n
Immutable(cant change): val, e.g., val name = “Jennifer”

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

How does an if/else statement work in Kotlin? Provide an example.

A

val numberOfCups = 30
val numberOfPlates = 10

if (numberOfCups > numberOfPlates) {
println(“Too many cups!”)
} else {
println(“Not enough cups!”)
}

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

What is a when statement in Kotlin? Provide an example.

A

a conditional expression that acts like a switch-case, allowing multiple conditions to be checked in a concise and readable way.

when (results) {
0 -> println(“No results”)
in 1..39 -> println(“Got results!”)
else -> println(“That’s a lot of results!”)
}

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

How do you declare and iterate over an array in Kotlin?

A

val pets = arrayOf(“dog”, “cat”, “canary”)
for (element in pets) {
print(element + “ “)
}
// Output: dog cat canary

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

What is the purpose of the safe call operator (?) in Kotlin?

A

it indicates that a variable can be null. Example:

numberOfBooks = numberOfBooks?.dec() ?: 0

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

What is the Elvis operator (?:) used for in Kotlin?

A

It’s used to provide a default value when a nullable variable is null:

numberOfBooks = numberOfBooks?.dec() ?: 0

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

How do you create a for loop with a step size in Kotlin?

A

for (i in 1..10 step 2) {
println(i)
}
// Output: 1, 3, 5, 7, 9

17
Q

How do you use a for loop with indices in Kotlin?

A

val pets = arrayOf(“dog”, “cat”, “canary”)
for ((index, element) in pets.withIndex()) {
println(“Item at $index is $element”)
}

18
Q

How do you combine two arrays in Kotlin using a for loop?

A

val numbers1 = intArrayOf(1, 2, 3)
val numbers2 = intArrayOf(4, 5, 6)
val combined = numbers1 + numbers2
for (num in combined) {
println(num)
}
// Output: 1, 2, 3, 4, 5, 6

19
Q

How can you iterate over a list in Kotlin using a for loop?

A

val instruments = listOf(“guitar”, “drums”, “piano”)
for (instrument in instruments) {
println(instrument)
}

20
Q

What is the purpose of the repeat function in Kotlin?

A

It repeats a block of code a specified number of times. Example:

repeat(3) {
println(“Hello!”)
}
// Output: Hello! Hello! Hello!

21
Q

How does a do-while loop work in Kotlin?

A

var bicycles = 50
do {
bicycles–
} while (bicycles > 0)
println(“$bicycles bicycles left”)
// Output: 0 bicycles left