Top kotlin interview questions and answers for experienced
1. What are the various data types available in Kotlin? Explain them.
Primitive
data types are the most basic data types in Kotlin, and all others are
reference types like array and string. Kotlin contains all data types as
objects. Following are the different data types available in Kotlin:-
2. How are variables declared in Kotlin?
What are the different types of variables in Kotlin? Explain with examples.
Every variable in Kotlin must be declared before it can be used.
An attempt to use a variable without declaring it results in a syntax error.
The type of data you are authorized to put in the memory address is determined
by the variable type declaration. The type of variable can be determined from
the initialized value in the case of local variables.
For example,
var site =
"interviewbit"
3. What are data classes in Kotlin? Explain
with a proper example.
The Data class is a simple class that holds data and provides
typical functions. To declare a class as a data class, use the data
keyword.
·
equals() - The equals() function returns true if two
objects have the identical contents. It operates similarly to "==,"
although for Float and Double values it works differently.
·
hashCode() - The hashCode() function returns the
object's hashcode value.
·
copy() - The copy() function is used to
duplicate an object, changing only a few of its characteristics while leaving
the rest unaltered.
·
toString() - This function returns a string
containing all of the data class's parameters.
To
ensure consistency, data classes must meet the following requirements:
4. Explain the concept of null safety in
Kotlin.
Kotlin's type system aims to eradicate null references from the
code. If a program throws NullPointerExceptions at runtime it might result in
application failure or system crashes. If the Kotlin compiler finds a null
reference it throws a NullPointerException.
5. What is the
difference between the variable declaration with var and Val?
If you want to declare
some mutable(changeable) variable, then you can use var
. For the immutable variable, use val
i.e. val
variables can't be changed once assigned.
6. How to ensure
null safety in Kotlin?
One of the major
advantages of using Kotlin is null safety. In Java, if you access some null
variable then you will get a NullPointerException
. So, the following code in Kotlin will produce a compile-time error:
7. What is Elvis's
operator in Kotlin?
In Kotlin, you can assign
null values to a variable by using the null safety property. To check if a
value is having null value then you can use if-else or can use the Elvis
operator i.e. ?:
For
example:
8. What is a data
class in Kotlin?
Data classes are those
classes that are made just to store some data. In Kotlin, it is marked as data.
The following is an example of the same:
9. What is the
difference between lateinit and lazy in Kotlin?
- lazy can only be used for val properties, whereas lateinit can only
be applied to var because it can’t be compiled to a final field, thus no
immutability can be guaranteed.
- If you want your property to be initialized from outside in a way
probably unknown beforehand, use lateinit.
10. What is the difference
between ArrayAdapter and BaseAdapter in Android?
Here is the
difference: BaseAdapter is a very generic adapter that allows you to do pretty
much whatever you want. However, you have to do a bit more coding yourself to
get it working. ArrayAdapter is a more complete implementation that works well
for data in arrays or ArrayList
No comments:
Post a Comment