Kotlin Interview Questions

Kotlin: Prepare Kotlin Interview Questions, Answers, Tips, news, tutorials. Find Kotlin Questions asked in various companies, Get Career advice & Interview Procedures from Kotlin experts.

  • What is the difference between object { } block and companion object { } code block in Kotlin?
    Kotlin

    Posted By: Pearl

  • What is the lateinit modifier used for?
    Kotlin

    Posted By: Ajay

    lateinit modifier in Kotlin is used to delay the initialization of a variable.

    The requirement of using lateinit modifier

    • These variables must be declared using var keyword, val is not allowed.
    • Must be either a property inside the body of a class or a top-level property
    • Can only be of non-null type
    • Primitive types disallowed

    Declaration Syntax

    lateinit var myVar: MyObject
    
    

  • What is Elvis Expression in Kotlin?
    Kotlin

    Posted By: Carlos

    The Elvis operator in Kotlin is an operator that receives two inputs and returns the first argument if it is non-null or the second one otherwise. It is fancily called the null-coalescing operator. It is a variant of the ternary operator but for null-safety checking.

    Example

    x ?: y // yields `x` if `x` is not null, `y` otherwise.
    

  • For what vararg keyword in Kotlin used for?
    Kotlin

    Posted By: Woodie

  • What are High Level Functions in Kotlin?
    Kotlin

    Posted By: FedorNob

    A higher order or high level function is a function that accepts or return another function.

  • What are Coroutines?
    Kotlin

    Posted By: PashkevichAcem

    Coroutines are light weight thread in Kotlin that convert async callbacks for long-running tasks, such as database or network access, into sequential code.

    Defining a simple Coroutine

    coroutine {
        progress.visibility = View.VISIBLE
     
        val user = suspended { userService.doLogin(username, password) }
        val currentFriends = suspended { userService.requestCurrentFriends(user) }
     
        val finalUser = user.copy(friends = currentFriends)
        toast("User ${finalUser.name} has ${finalUser.friends.size} friends")
     
        progress.visibility = View.GONE
    }
    

    Above is comman structure for a coroutine that have have a coroutine builder, and a set of suspending functions that will suspend the execution of the coroutine until they have the result.

  • What is diffrence between Val and Var?
    Kotlin

    Posted By: Arlington

    The major difference between var and val in Kotlin is:

    A variable declared with var is a mutable variable that value can be changed any time whereas variable declared with val keyword are final their value that not be changed.

  • What are different types of constructors available in Kotlin?
    Kotlin

    Posted By: Darwin

  • Why is Kotlin preferred over Java?
    Kotlin

    Posted By: KevinViali

  • Explain Kotlin's Null safety?
    Kotlin

    Posted By: Lokesh Ekbote

  • What it the main the difference between lazy and lateinit in Kotlin?
    Kotlin

    Posted By: ramesh

  • How to define Constant in Kotlin.
    Kotlin

    Posted By: Ali Hyder

    Val keyword is used to define a Constant in Kotlin.

    Syntax for defining a Local Constant in Kotlin.

    val firstName: String = "Chike"
    

    Creating a public constant in Kotlin like Java public static final

    companion object{
    
         const val MY_CONSTANT = "Constants"
    
    }
    
    

  • What is difference between inline and infix functions in Kotlin?
    Kotlin

    Posted By: Dallas

  • What is suspend in Kotlin?
    Kotlin

    Posted By: Sharad Jaiswal

    Suspend is a function in Kotlin Coroutines. A suspending function is simply a function that can be paused and resumed at a later time. They can execute a long-running operation and wait for it to complete without blocking.

  • How to create AsyncTask in Android with Kotlin ?
    Kotlin

    Posted By: Sharad Jaiswal

  • Explain Launch vs Async in Kotlin ?
    Kotlin

    Posted By: Sharad Jaiswal

    Both Launch and async are coroutines in Kotlin.

    Launch: It is used to fire and forget coroutine. It is like starting a new thread. If the code inside the launch terminates with an exception, then it is treated like an uncaught exception in a thread -- usually printed to stderr in backend JVM applications and crashes Android applications.

    Async: It is used to start a coroutine that computes some results. The result is represented by an instance of Deferred and you must use await on it. An uncaught exception inside the async code is stored inside the resulting Deferred and is not delivered anywhere else, it will get silently dropped unless processed.