Kotlin Flashcards
What is kotlin
Is a modern language that runs on the JVM
What are the differences between Kotlin and Java
Null safety, concise syntax, extension functions, data classes, coroutines
Explain null safety in Kotlin
Kotlin prevents NullPointerExceptions by requiring explicit null checks
Kotlin types
Int, Long, Float, Double, Boolean, Char, String
What’s the difference between “val” and “var”
Val creates immutable variables, while var creates mutable variables
Explain the concept of type inference
The compiles automatically infers the type of a variable based on its initialization value
What are higher-order functions in Kotlin
Functions that take other functions as parameters, or returns functions as results
What is a lambda expression in Kotlin
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 }
Explain the concept of extension functions in Kotlin
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)
What are data classes in Kotlin
Classes that hold data, automatically generating equals(), hashCode(), toString(), and copy() methods
What is the difference between a primary and a secondary constructor
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
}
}
What is the purpose of companion objects in Kotlin
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()
Explain the concept of sealed classes in Kotlin
Restricted class hierarchy with subclasses known at compile time
What is the difference between a list and a mutable list in Kotlin
List is immutable, the mutable list can be modified
What is the difference between a map and a set in kotlin
A map stores key-value pairs, while sets stores unique elements