Kotlin Flashcards

1
Q

1) What is lateinit?
2) Can it be declared val or var?
3) Can lateinit be used with primitivae data types?
4) When to use lateinit?

A

1) The lateinit keyword stands for “late initialization.” When used with a class property, the lateinit modifier keeps the property from being initialized at the time of its class’ object construction.

Memory is allocated to lateinit variables only when they are initialized later in the program, rather than when they are declared.

The initialization takes place later when you see fit.

2) A lateinit property may change more than once throughout the program and is supposed to be mutable. That’s why you should always declare it as a var and not as a val or const.

3) No, it can’t be used with primitive data types. This is because whenever a lateinit property is accessed, Kotlin provides it a null value under the hood to indicate that the property has not been initialized yet.Primitive types can’t be null, so there is no way to indicate an uninitialized property

4)
With regular variable initialization, you have to add a dummy and, most likely, a null value. This will add a lot of null checks whenever they’re accessed.

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

What is the difference between primary constructor and secondary constructor?

https://kotlinlang.org/docs/classes.html#constructors

A

A class in Kotlin can have a primary constructor and one or more secondary constructors. The primary constructor cannot contain any code. Initialization code can be placed in initializer blocks prefixed with the init keyword. You can have many initialization code blocks During the initialization of an instance, the initializer blocks are executed in the same order as they appear in the class body, interleaved with the property initializers.

If the class has a primary constructor, each secondary constructor needs to delegate to the primary constructor, either directly or indirectly through another secondary constructor(s). Delegation to another constructor of the same class is done using the this keyword:
constructor(name: String, parent: Person) : this(name) {
parent.children.add(this)
}

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

What is NullSafety?

A

It is built-in mechanism that do not allow to assign null value to variable.

In an effort to rid the world of NullPointerException, variable types in Kotlin don’t allow the assignment of null. If you need a variable that can be null, declare it nullable by adding ? at the end of its type.

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

What is the difference between a normal class and a data class?

A

ALl types of classes are final by default.

  1. A data class must be declared with at least one primary constructor parameter which must be declared with val or var. A normal class can be defined with or without a parameter in its constructor.

2.Data classes have default implementations for the following methods using only properties that were declared in the primary constructor; toString(), hashCode(), copy(), componentN(), equals(). Implementation for those methods can be written in normal classes using properties that were and were not declared in the primary constructor.

  1. A data class cannot be extended by another class. They are final classes by default. Normal classes can be extended by other classes, including data classes. Certain conditions should however be met.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Companion object? Why it was introduced?

A

JAVA static:
class Foo {
public static int a() { return 1; }
}

KOTLIN “static”:
class Foo {
companion object {
fun a() : Int = 1
}
}

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

[1] What is default superclass for all classes in kotlin?
[2] What methods does this class have?

A

[1] Any
[2] equals(), hashCode(), and toString()

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

What is Unit type class?

A

unit is like Void in java

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

Can open class with open properties have private setter/getters?

A

No

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