Kotlin Flashcards

1
Q

Provide a brief history of kotlin focusing on the years 2011, 2016, 2017, 2019

A

● July 2011: Project Kotlin was first unveiled by JetBrains after a
year of development. It was unveiled as a new language for the JVM (key objective: drive IntelliJ IDEA sales).
*Android Studio is based on IntelliJ IDEA
● February 2016: Kotlin 1.0 was released.
● October 2017: Kotlin introduced as an alternative to Java in Android Studio 3
● May 2019: Google announced adoption of Kotlin as its preferred language for Android app developers.
● As of today, Kotlin is used by over 60% 0f professional Android developers.

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

What are Kotlin’s Key Advantages (7)

A

● Kotlin is an object-oriented tool, therefore having advantages pertaining to this paradigm
● Kotlin is compatible and interoperable with
other languages, and more so Java (full compatibility) and JS.
● Given its compatibility and interoperability, Kotlin is easily adoptable and easy to learn.
● Kotlin is less bulky with regards to code-writing
● Kotlin’s support from JetBrains and the community is tremendous
● Kotlin is well documented
● Kotlin is suitable for multi-platform development
given it’s compatibility with many other tools

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

What are the Android Kotlin’s Advantages

A

● Expressiveness and conciseness: less code writing.
● Safer code: Reduces crashing possibility of Android apps by 20%
● Interoperability: Kotlin is 100% interoberable with Java, the other key Android tool
● Structured concurrency: Kotlin coroutines make asynchronous code easy to work with, thus
simplifying background task management.

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

Describe and illustrate Package Definition and Imports

A

● These should be done at the top of a source file, just like in most other languages

package my.demo
import kotlin.text.*

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

Give the various variable declarations in Kotlin

A

– val a: Int = 1 // immediate assignment
– val b = 2 // Int type is inferred
– val c: Int // Type is required when no initializer is provided
– c = 3 // deferred assignment

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

Explain the function of the main function in both a Kotlin application and an android function

A

Like many OOP tools, the main function is the entry point of a Kotlin application (this might not be so true for Android though, given the use of diverse methods to direct the Android app).

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

Define a simple main function without variables and one with a String of arguments accepted

A

//Simple main function without variables
fun main() {
println(“Hello World!”)
}

//Main function with a String of arguments accepted
fun main(args: Array<String>) {
println(args.contentToString())
}</String>

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

Describe and illustrate the use of the print function

A

There are options to either print with no line break, or to add a line break:

print(“Hello World”)
println(“Hello world!”)

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

Describe and illustrate the declaration of functions in Kotlin

A

● The return type, just like in variable declaration, comes after a function’s declaration.
● Same way some return types were inferred in variable declaration, is the same way we can have return types inferred in function declaration.

fun sum (a: Int, b: Int): Int {
return a + b
}

//An inferred return type on a function that is an expression
fun sum(a: Int, b: Int) = a + b

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

Describe and illustrate the declaration of classes in Kotlin

A

● The class keyword must be used to declare a class:
class Student
● Sometimes, properties can be listed inside the class’s declaration or body, with this providing the default constructor in the required format:

class Rectangle(var height: Double, var length: Double) {
var perimeter = (height + length) * 2
}

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

Describe and illustrate how inheritance is handled in kotlin

A

● Inheritance is declared by a colon (:)
● Classes are final by default, but to make a class inheritable, it should be marked by the key word open:
open class Shape

class Rectangle(var height: Double, var length: Double): Shape() {
var perimeter = (height + length) * 2
}

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

Describe Conditional Decision Making Statements in Kotlin

A

● Kotlin’s if, for and while statements are similar to Java’s.
● However, in Kotlin, if can also be used as an expression as below:
if (a > b) a else b

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

How is a range specified in Kotlin?

A

● The in operator can be used to check if a number is within a specified range:

val a = 10
val b = 9
if (a in 1..b+1) {
println(“fits in range”)
}

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

Describe and illustrate how Nullable Values and Null Checks are handled in Kotlin

A

● A reference must be explicitly marked as nullable when null value is possible.
● Nullable type names have ? at the end.

/* The following returns null if str does not hold an
integer. */

fun parseInt(str: String): Int? {
// …
}

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