Kotlin Flashcards

1
Q

What is kotlin

A

Is a modern language that runs on the JVM

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

What are the differences between Kotlin and Java

A

Null safety, concise syntax, extension functions, data classes, coroutines

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

Explain null safety in Kotlin

A

Kotlin prevents NullPointerExceptions by requiring explicit null checks

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

Kotlin types

A

Int, Long, Float, Double, Boolean, Char, String

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

What’s the difference between “val” and “var”

A

Val creates immutable variables, while var creates mutable variables

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

Explain the concept of type inference

A

The compiles automatically infers the type of a variable based on its initialization value

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

What are higher-order functions in Kotlin

A

Functions that take other functions as parameters, or returns functions as results

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

What is a lambda expression in Kotlin

A

A function literal that can be passed as an argument or assigned to a variable
Val sayHello = { println(“Hello world”) }
Val add = { x: Int, y: Int -> x + y }

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

Explain the concept of extension functions in Kotlin

A

Add new functionality to existing classes without modifying their source code
class ExClass() {some_code_here}

fun ExClass.extensionFunction(parameter: Int): Int {
return parameter
}

val exClass = ExClass()
exClass.extensionFunction(5)

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

What are data classes in Kotlin

A

Classes that hold data, automatically generating equals(), hashCode(), toString(), and copy() methods

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

What is the difference between a primary and a secondary constructor

A

Primary constructor is defined with the class name, while secondary constructors delegate to the primary constructor

class Person(thisIsThePrimaryConstructor){}

Class Student {
// Some properties
constructor(thisIsSecondaryConstructor){
This.property = propertyInTheConstructor
}
}

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

What is the purpose of companion objects in Kotlin

A

Crate static-like members for a class

class ClassName {
companion object {
const val propertyName= some_value
fun funName(){function_code}
}
}

val property = ClassName.propertyName
ClassName.funName()

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

Explain the concept of sealed classes in Kotlin

A

Restricted class hierarchy with subclasses known at compile time

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

What is the difference between a list and a mutable list in Kotlin

A

List is immutable, the mutable list can be modified

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

What is the difference between a map and a set in kotlin

A

A map stores key-value pairs, while sets stores unique elements

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

Explain the concept of generics in Kotlin

A

Create type-safe code by using placeholders for specific types

Class ClassName<GenericTypeName>(private val value: GenericTypeName){}</GenericTypeName>

17
Q

What is the purpose of the “when” expression in kotlin

A

A concise way to perform conditional checks
when(x) {
1 -> print(“x == 1”)
2 -> print(“x == 2”)
else -> {
print(“x is neither 1 nor 2”)
}
}

18
Q

What is the purpose of ?. operator in Kotlin

A

Safe call operator to prevent NullPointerExceptions

19
Q

What is the purpose of the !! operator in Kotlin

A

Non-null assertion operator, use with caution

20
Q

Explain the concept of coroutines in Kotlin

A

Asynchronous programming model for writing non-blocking code