0 ratings0% found this document useful (0 votes) 185 views9 pagesKotlin Interview Cheat Sheet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
arias asa Kolin Interview Cheat Sheat. Two Months ago when i started appearing... [by Kapil Vi | Oct, 2021 | Macium
openinaoe en
Kapil Vij
102 Followers About Caton) C)
Kotlin Interview Cheat Sheet
@© ‘015 027 - sminreas
K Kotlin
Interview Questions
‘Two Months ago when i started appearing in interviews I used to get blank in some of
tricky questions in Kotlin even when i spend some time coding in Kotlin in live projects.
But I kept collecting questions which are not mentioned anywhere under single
umbrella.
So i decided to collate some of important questions currently being asked for people who
gets little time to prepare for interviews. This article will help you clear kotlin questions
with ease and it's hardly a 15 mins read for beginners.
Here You go...
Q1. What is the difference between val and var?
var is like a general variable and can be assigned multiple times and is known as the
hitps:ikapiv. medium. convkoln-intrview-cheat-shaet-c6267850ba73 19Open inepp e
Q2. What is the difference between val and const val?
const and val both represents the immutability and read only values and act as final
keyword in java. val keyword must be used to declare for run time values and const
keyword must be used to declare compile time values.
Q3. What is Difference between setValue() and PostValue() in MutableLiveData?
setvalue() method must be called from the main thread. But if you need set a value
from a background thread, postvaiue() should be used.
Q4. How to check if lateinit property is initialized or not?
You can check if the lateinit variable has been initialized or not before using it with the
help of isInitialized() method. It returns true if the lateinit property has been i
otherwise false.
‘this: :variablenane. istnitializes
lateint initialized hosted with © by Githiub view raw
QS. When to use lateinit and lazy keywords in kotlin?
Lateinit is used with mutable, while lazy is used with immutable
Jateinit >
2 private lateinit var display : DisplayAdapter
2
2 // Sf you use above without initializing, 1t will throw UinitializedPropertyAccessException
Intent hosted wth © by Github view raw
lazy > Itinitializes variable only when it is required for the first time.
2 private val githubipiservice : Githubapiservice by lazy {
2 _-Retrofitchient.getsithubapiservice()
3}
Inzy hosted wth by Gittub view rae
Lazy :- There are certain classes whose object initialization is very heavy and so much
time taking that it results in the delay of the whole class creation process.
Q6. Whats difference between companion object and object?Companion Object is
initialized when class is loaded. But Object is initialized lazily by default — when
accessed for the first time.
hitps:ikapiv. medium. convkoln-intrview-cheat-shaet-c6267850ba73parva as Kotin Interview Cheat Shest Two Months ago when i started appearing... |by Kapil Vij | ct, 2021 | Mecium
Openinapp en
crash even if variable reference you are holding is null.
var variable: String? = null
variable?.replace(“x”, “z”)
Please note we have not initialized variable above, but it will not throw
NullPointerException as Safe call operator is used.
But in case of Non-Null Assertion, if you call any method on its reference it will throw
KotlinNullPointerException.
variable!!.replace(“”, “2”)
Q8. What are data classes in kotlin?
data class Person(val name: String)
2 var age: Int = @
3)
data class hosted with © by GitHub view raw
Only the property name will be used inside the toStringQ, equals(), hashCode(), and
copy() implementations. While two Person objects can have different ages, they will be
treated as equal for above example.
Q9. Why kotlin classes are final by default ?
Design and document for inheritance or else prohibit it — Core principle from book
Effective Java by Joshua Bloch’s
In Simple words -> If classes were open by default and we would forget to mark class as
final — (troubles might happen), but when we forget to mark class as open and try to
extend it — we will be notified (no trouble).
QUO. Difference between == operator and === operator?
The == operator is used to compare the values of variables but
check whether references of the variable is equal or not.
And in the case of primitive types, the --- operator also checks for the value and not
operator is used to
reference,
Please note both will result in same in case primitive data types
2 val number = Integer(2)
2. val anotherNunber = Integer(1)
3 number
notherwunber // true (structural equality)
htps:ikapivi. medium. convkoln-intrview-cheat-shaet-c6267850ba73parva as Kotin Interview Cheat Shest Two Months ago when i started appearing... |by Kapil Vij | ct, 2021 | Mecium
Openinapp e
Q11. Access/Visibility Modifiers in Kotlin
Four types of access modifiers
* protected: visible inside that particular class or file and also in the subclass of that
particular class where it is declared.
© private: visible inside that particular class or file containing the declaration.
* internal: visible everywhere in that particular module.
+ public: visible to everyone.
Note: By default, the visibility modifier in Kotlin is public.
Q12. What are extension functions in Kotlin?
Extension Function provides an option to “add” methods to class without inheriting a
class. The created extension functions are used as a regular function inside that class.
See Example below:
2 fun View mideview() (
2 ‘this.visibility = View.GONE
BD
Extension Function hosted with © by GitHub view raw
Now you can call this same method on any type of view directly on its reference. e.g.
textView hideView(
Q13. What are inline functions ?
Inline function instruct compiler to insert complete body of the function wherever that
function got used in the code. To use an Inline function, all you need to do is just add an
inline keyword at the beginning of the function declaration.
Q14. What are scope functions in Kotlin ?
Scoped functions are functions that execute a block of code within the context of an
object. There are five scoped functions in kotlin : let, run, with, also and apply.
‘The scope functions differ by the result they return:
* apply and also return the context object. So they can be used in chaining function
calls on the same object after them. They also can be used in return statements of
functions
hitps:ikapiv. medium. convkoln-intrview-cheat-shaet-c6267850ba73paras asa Kotin Interview Cheat Shest Two Months ago when i started appearing... |by Kapil Vij | Oct, 2021 | Mecium
openinaoe e
Learn more avout scope Luncuons nere.
Q15. What are sealed classes in kotlin?
Sealed classes are similar to sous classes: the set of values for an enum type is also
restricted, but each enum constant exists only as a single instance, whereas a subclass of
asealed class can have multiple instances, each with its own state.
To declare a sealed class or interface, put the scaled modifier before its name:
2 sealed interface Error
3. sealed class 1oError(): Error
5 class FileReaderror(val #: File): ToError()
6 class DatabaseError(val source: DataSource): TOError()
7
8
object Runtimetrror : Error
sealed class hosted with © by GitHub view raw
Asealed class is abstract by itself, it cannot be instantiated directly and can have
abstract members.
Q16. What is significance of annotations : @JvmStatic, @JvmOverloads, and
@JvmFiled in Kotlin?
-> @JvmStatic: This annotation is used to tell the compiler that the method is a static
method and can be used in Java code.
-> @JvmOverloads: To use the default values passed as an argument in Kotlin code
from the Java code.
-> @JvmField: To acces
getters and setters.
the fields of a Kotlin class from Java code without using any
Q17. What are infix functions?
An infix function is used to call the function without using any bracket or parenthesis.
You need to use the infix keyword to use the infix function.
eg.
2 infix fun Int-add(b : Int) : Int = this +
2
3 val y = 10 add 20 // infix function call
infix function hosted with © by GitHub view raw
hitps:ikapiv. medium. convkoln-intrview-cheat-shaet-c6267850ba73parva as Kotin Interview Cheat Shest Two Months ago when i started appearing... |by Kapil Vij | ct, 2021 | Mecium
Open in app e@
abject ASingleton
kottn singleton hosted with © by GitHub view raw
This is equivalent to below java code :
public final class ASingleton (
public static final ASingleton INSTANCE;
private asingleton() {
INSTANCE = (ASingleton)this;
static ¢
new ASingleton();
x
ui)
java singleton hosted with © by GitHub view raw
Q19. What are advantages to when over switch in kotlin?
It is more concise and powerful than a traditional switch. when can be used either as an
expression or as a statement.
hitps:ikapiv. medium. convkoln-intrview-cheat-shaet-c6267850ba73Kotin Interview Cheat Shest Two Months ago when i started appearing... |by Kapil Vij | ct, 2021 | Mecium
Openinapp e
parva as
Q20. What are primary and secondary constructors in Kotlin?
Primary Constructor is initialized in the class header, goes after the class name, using
the constructor keyword. The parameters are optional in the primary constructor.
The primary constructor cannot contain any code, the initialization code can be placed
in a separate initializer block prefixed with the init keyword.
Secondary Constructor — Kotlin may have one or more secondary constructors.
Secondary constructors allow initialization of variables and allow to provide some logic
hitps:ikapiv. medium. convkoln-intrview-cheat-shaet-c6267850ba73
19arias asa Kolin Interview Cheat Sheat. Two Months ago when i started appearing... [by Kapil Vi | Oct, 2021 | Macium
openinaoe e
Q21. What are Higher Order Functions?
A higher-order function is a function that takes functions as parameters, or returns a
function.
hitps:ikapiv. medium. convkoln-intrview-cheat-shaet-c6267850ba73arias asa Kolin Interview Cheat Sheat. Two Months ago when i started appearing... [by Kapil Vi | Oct, 2021 | Macium
openinaoe e
That’s all folks.
But Keep watching this space coz I will keep updating the same article
References :
Biggest References are actual Interviews with Product companies like Walmart, Byju’s,
OyoRooms, Deutsche Telekom, Zee Entertainment, GoJek and many more.
https://kotlinlang.org/docs/home.htm!
Kotlin Interview Questions Android App Development Android
hitps:ikapiv. medium. convkoln-intrview-cheat-shaet-c6267850ba73
89