Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Cracking Kotlin Interview: Solutions to Your Basic to Advanced Programming Questions
Cracking Kotlin Interview: Solutions to Your Basic to Advanced Programming Questions
Cracking Kotlin Interview: Solutions to Your Basic to Advanced Programming Questions
Ebook165 pages1 hour

Cracking Kotlin Interview: Solutions to Your Basic to Advanced Programming Questions

Rating: 0 out of 5 stars

()

Read preview

About this ebook

This book covers all the possible interview and coding questions in Kotlin. This book is based on Kotlin programming language and its comparison to Java.

With a complete overview of OOPs, null safety, generics, and many other exciting features, this book is a perfect choice for fresher and experienced Java developers who want to learn more about this alternative JVM language.
LanguageEnglish
Release dateApr 18, 2020
ISBN9789389845273
Cracking Kotlin Interview: Solutions to Your Basic to Advanced Programming Questions

Read more from Swati Saxena

Related to Cracking Kotlin Interview

Related ebooks

Operating Systems For You

View More

Related articles

Reviews for Cracking Kotlin Interview

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Cracking Kotlin Interview - Swati Saxena

    CHAPTER 1

    Core Concepts

    Name some features which Kotlin supports but not Java?

    Ans. Important Kotlin features that Java doesn’t have are:

    Null Safety

    Operator Overloading

    Coroutines

    Range expressions

    Smart casts

    Companion Objects

    Name some features which Kotlin supports but not Java?

    Ans. Two strings in Kotlin can be compared as:

    (a) Using == operator

    fun main(args: Array) {

    val a: String = kotlin is easy

    val b: String = kotlin is + easy

    if(a==b){

    println( a and b are equal.)

    } else {

    println( a and b are not equal.)

    }

    }

    (b) Using compareTo()

    fun main(args: Array) {

    var a: String = apple

    var b: String = apple

    var result = a.compareTo(b)

    if(result==0){

    println(Strings ‘$a’ and ‘$b’ are equal.)

    } else if(result < 0){

    println(’$a’ is less than ‘$b’ lexically.)

    } else{

    println(’$a’ is less than ‘$b’ lexically.)

    }

    b = banana

    result = a.compareTo(b)

    if(result==0){

    println(Strings ‘$a’ and ‘$b’ are equal.)

    } else if(result < 0){

    println(’$a’ is less than ‘$b’ lexically.)

    } else{

    println(’$a’ is less than ‘$b’ lexically.)

    }

    // passing ignoreCase to compareTo

    a = appLE

    b = aPple

    println(\nIgnoring Case…)

    result = a.compareTo(b, true) // ignoreCase = true

    if(result==0){

    println(Strings ‘$a’ and ‘$b’ are equal.)

    } else if(result < 0){

    println(’$a’ is less than ‘$b’ lexically.)

    } else{

    println(’$a’ is less than ‘$b’ lexically.)

    }

    }

    Output

    Strings ‘apple’ and ‘apple’ are equal.

    ‘apple’ is less than ‘banana’ lexically.

    Ignoring Case…

    Strings ‘appLE’ and ‘aPple’ are equal.

    Differentiate among ==, ===, .equals() in Kotlin?

    Ans.

    Structural Equality (‘==’)

    == operator is used to compare the data of two variables.

    == operator in Kotlin only compares the data or variables, whereas in Java or other languages == is used to compare the references.

    The negated counterpart of == in Kotlin is != which is used to compare if both the values are not equal to each other.

    Referential equality (‘===’

    === operator is used to compare the reference of two variable or object.

    It will only be true if both the objects and variables pointing to the same object.

    The negated counterpart of === in Kotlin is !== which is used to compare if both the values are not equal to each other. For values which are represented as primitive types at runtime (for example, Int), the === equality check is equivalent to the == check.

    .equals method

    .equals(other: Any?) method is implemented in Any class and can be overridden in any extending class.

    .equals method compares the content of the variables or objects just like == operator but it behaves differently in case of Float and Double comparison.

    Example:

    val first = Integer(10)

    val second = Integer(10)

    println(first == second) //true

    println(first.equals(second)) //true

    println(first === second) //false

    ………….

    class Employee (val name: String)

    val emp1 = Employee(Swati)

    val emp2 = Employee(Swati)

    println(emp1 == emp2) //false

    println(emp1.equals(emp2)) //false

    println(emp1 === emp2) //false

    println(emp1.name == emp2.name) //true

    println(emp1.name.equals(emp2.name)) //true

    println(emp1.name === emp2.name) //true

    Explain the use of init() in Kotlin?

    Ans. A constructor is used to initialize the class properties. It is a special member function which is called when an object is instantiated (created).

    In Kotlin, there are two constructors:

    (a) Primary constructor:used to initialize a class

    (b) Secondary constructor:allows you to put additional initialization logic

    The primary constructor is part of the class header.

    Example:

    class Student(val subject: String, var fee: Int) {

    // class body

    }

    The primary constructor has a constrained syntax, and cannot contain any code.

    To put the initilization code (not only code to initialize properties), initializer block is used. It is prefixed with init keyword.

    Example:

    fun main(args: Array) {

    val student1 = Student(swati, 25)

    }

    class Student(Name: String, Age: Int) {

    valname:String

    var age: Int

    // initializer block

    init {

    name= Name.capitalize()

    age = Age

    println(First Name = $name)

    println(Age = $age)

    }

    }

    What is repeat() in kotlin?

    Ans. Repeat statement is like while loop, which executes a block of code, N-number of times (without any condition).

    If you want to print a statement for N times without condition or looping, repeat() is used in Kotlin.

    Example:

    fun main(args: Array) {

    repeat(5) {

    println(Swati Computers)

    }

    }

    How does the program written in Kotlin run and what is the entry point of Kotlin programs?

    Ans. The Kotlin program once compiled, can run on standard Java Virtual Machine (JVM) like other programming codes. And, like many other programming languages main() function is the entry point of the Kotlin.

    State the differences between Val and Var?

    Ans.

    Val: Val, which is the short form of value, is a constant and it cannot be changed once assigned.

    Var: Var, which is the short form of variable, is a storage location that accepts the reassignment of values that have the same data types.

    How to instantiate class in Kotlin?

    Ans. There is no new keyword in kotlin to instantiate a class.

    You can create class object as:

    class XYZ

    var a= XYZ()

    val b = XYZ()

    What is lazy and lateinit in kotlin?

    Ans. Both lazy and lafeinitare used to delay the property initialization in kotlin.

    Lazy is a method, which can be assign to val only. The value would be created at runtime when it is required.

    val x: Int by lazy{100 }

    Lateinit is a modifier, which is used to set the value to the var when required.

    Lateinit var y: String

    How to create static method in kotlin?

    Ans. Kotlin does not support static keyword. To create a static method, you can use a companion object.

    Example:

    In java:

    class Static

    {

    public static

    Enjoying the preview?
    Page 1 of 1