Misc. Flashcards

1
Q

How does Modulo % work?

A

How many times does the RIGHT side go into the LEFT side, then the remainder is the result.

or,

Left side DIVIDED BY Right side, return the remainder.

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

String Raw multiline?

A
Use triple quotes...
E.g.
""" 
Hello,
My name is James.
How are you?
"""
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

&& AND Short-circuiting

A

Do the most complex boolean check last because if the first check fails then the last one will not compute.
E.g.
if (quickCheck && complexLongCheck) {
// Do stuff
}

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

|| OR Short-circuiting

A
Do the most complex boolean check last because if the first check returns true then the last one will not compute.
E.g.
if (quickCheck && complexLongCheck) {
    // Do stuff
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Structural Equality check

A

==
and
!=

e.g.

var name 1 = “James”
var name 2 = “Dougie”

println(name1 == name2)
// Prints false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Referential Equality check

A

===
and
!===

e.g.

var person1 = Person("jim")
var person2 = Person("jim")
println(person1 === person2)
// Prints false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the terms for the two types of equality in Kotlin?

A

Structural and Referential

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

Why don’t referential checks work for ‘primitives’?

A

In Kotlin traditional primitives (numbers, chars, bools) are converted from Objects to actual primitives at runtime. So you can use the Structural equality check ‘==’ for those.
Strings use the String pool and so have their own logic for equality.
All other ‘custom’ objects work with the referential equality check ‘===’.

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

What will be the value of strLength after the ‘Nullable Safe’ call if A. myString is null and B. myString is not null.

val strLength = myString?.length

A

A. null

B. The length of myString.

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

What does the ‘Elvis’ operator do? ( ?: )

A

If the anything on the left-hand side of the operator is null, then return the value on the right-hand side.

It's shorthand for...
if(person != null) {
    return person.name
} else {
    return "Anon" // Some default value
}

Useful when used with Nullable Safe calls…
return person?age ?: “Anon”

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

Unit vs Void vs Nothing

A

TODO

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

What does invoke() do?

A

TODO

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

Can you define functions within a function? From WHEN and WHERE can you call this function?

A

Yes.
You can only call it after you’ve defined the function,
The ‘inner’ function is scoped to the function it’s defined in so you can’t call it from outside the function.

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

Can you store a function in a variable?

A

Yes!

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

When to use a Single Line Expression?

E.g. fun myFunc = println(“hey”)

A

Whenever the body of the function can be written on a single line WITHOUT going over the line character limit.

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

What is a Trailing Lambda and when can it be used?

A

When a function’s last parameter is a function then when calling that function you can pass the final function argument after the parentheses.

E.g.
someFunction(1stArg, 2ndArg) { println(“3rdArg”) }

If the only parameter a function has is a function then you can omit the parentheses entirely.

E.g.
someFunction { println(“I’m the only Arg”)

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

How does you define a var args parameter?

A
With the 'vararg' keyword.
E.g.
fun myFunction(vararg authors: String)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What can you do in Kotlin to avoid method overloading?

A

Use default arguments instead.

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

How do you add an access modifier to a Class’s primary constructor?

A

You have to use the ‘constructor’ keyword (which is optional if you don’t need to change the access modifier) and then you place your access modifier before it.

E.g.
class Person internal constructor(val name: String) { }
20
Q

A class is allowed multiple init { } blocks. How do you control the order in which they are called?

A

They operate in the order in which they are defined in the class. Top-most / first init { } block is called first and so on.

21
Q

In what order do multiple class constructors bodies run?

A

The primary constructor/init blocks run first, then recursively back down to the initial constructor that the client called.

When a secondary construct calls another constructor with “: this(args)” it’s body is not run, rather it is added to the stack until the primary constructor has run (and presumably, the object is instantiated) then the stack is popped until all secondary constructors have run their bodies code.

E.g.
fun main() {
    val person = Person()
}
class Person(val a: Int, val b: Int) {
    init {
        println("first")
    }
    constructor(a: Int): this(a, 2) {
        println("Second")
    }
    constructor(): this(1) {
        println("Third")
    }
}

This will print:
first
Second
Third

22
Q

In what order to init blocks and property initialisers run?

A

In the order they are defined.

E.g.

init {
println(myValue)
}

val myValue = ‘x’

The init block will not know about myValue as it is initialised after the init block.

Take-away: init blocks should be defined after property initialisers.

23
Q

How do you override a the get() method of a field?

A

Define a get() method under the field.

E.g.
var name = “James”
get() = “Mr. $field”

24
Q

What is the ‘field’ of a get() override?

A

It is the field’s backing field. Basically it contains the contents of the field and the ‘field’ is the way to reference and use that contents.

25
Q

How do you override a the set() method of a field?

A

Define a set() method under the field.

E.g.
var name = "James"
    set(value) {
        field = value
    }
26
Q

What is ‘value’ in a field’s set() override?

A

It is the value the client passed in to set the new value to.

27
Q

What’s a good alternative to consider for list.first()?

A

list.firstOrNull() If the list is empty it will return null, potentially saving you having to check the list’s size first.

28
Q

How should you format constants?

A

Using the const keyword and the name should be in all uppercase characters like in Java.

29
Q

Where/how should you define a constant in the following scenarios?

  1. The value needs to be accessed anywhere in the application and has cross-cutting concerns in many places.
  2. The value is very specific to a certain class but other parts of the app need to use it.
  3. The value is very specific to a certain class and other classes don’t/shouldn’t need to access it.
A
1. In a constants Singleton class. E.g.
Filename: Constants.kt
object Constants {
    const val MY_CONST = 1
}
  1. In a Companion Object of the class.
  2. In a Companion Object of the class with the access modifier marked as private.
30
Q

How do you create a Singleton?

A

Using ‘object’ instead of ‘class’. E.g.

object MySingleton { }

31
Q

What happens if a lateinit property is accessed before it is initialised?

A

A UnititalizedPropertyAccessException will be thrown.

32
Q

What does the lateinit modifier do?

A

It tells the compiler that you won’t know the value of the property when the object is created however you promise to initialise it before you use it.

33
Q

When might you want to use a nested class?

A

When the class relates directly to the containing class and doesn’t make sense to be used anywhere else. You can also make private to the containing class to enforce this.

34
Q

Do nested classes have access to the properties of their outer class?

A

No. You must use an inner class instead.

35
Q

How can you allow a nested class to access the properties of its enclosing class?

A

Change the nested class to be an inner class. E.g.

inner class MyInner Class { }

36
Q

Why can you not statically access an inner class like you can a nested class? E.g.

OuterClass.InnerClass()

A

Because the inner class has access to the outer class’s properties this means it must have a reference to the outer class. Referencing a inner class like so… Outerclass.InnerClass doesn’t make sense. Which instance of OuterClass should the InnerClass be referencing? The compiler doesn’t know. That’s why you must use it via an object instance and not via a static class reference. E.g. OuterClass().InnerClass().

37
Q

Nested class vs Inner class?

A

A nested class is simply an alternative approach to storing a class (that is somehow related to its outer class) in its own file within the project whereas an inner class is a property of the outer class.

38
Q

How can you get an Enum type from String/Number?

A
Use valueOf().
E.g.
enum class AccountType {
BRONZE, SILVER, GOLD
}
val valueFromApi = "gold"
val accountType = AccountType.valueof(valueFromApi.toUpper)
39
Q

Example of Enum constructor parameters.

A
enum class AccountType(val discountPercent: Int) {
    BRONZE(10),
    SILVER(15),
    GOLD(20)
}
40
Q

Example of Enum Abstract Functions.

A
enum class AccountType(val discountPercent: Int) {
    BRONZE {
        override fun calculateDiscountPercent = 5
    };
abstract fun calculateDiscountPercent(): Int }
41
Q

How to get a list of all type in an Enum?

A

YourEnum.values()

42
Q

Can an Enum have a companion object? And therefore, static like function behaviour?

A

Yes.

43
Q

Does the condition check in a when statement alway have to be defined on the first line?

A

No. You provide any number of conditional checks wihtin each case of your when statement. E.g.

when {
user.firstName == “James” -> println(“Hey James”)
user.lastName == “Baggins” && isShort -> println(“Hey Frodo”)
}

44
Q

var vs val

A

var can be changed whereas val is final. You prefer val where possible.

45
Q

How to get the class type of a variable?

A

myVar::class