0% found this document useful (0 votes)
5 views5 pages

Kotlin Interview Guide

The document serves as a comprehensive Kotlin interview guide covering key concepts such as null safety, coroutines, data classes, higher-order functions, and access modifiers. It highlights differences between Java and Kotlin, including built-in null safety and support for coroutines. Additionally, it explains advanced topics like delegation, lazy initialization, and scope functions with practical examples.

Uploaded by

beceve6826
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Kotlin Interview Guide

The document serves as a comprehensive Kotlin interview guide covering key concepts such as null safety, coroutines, data classes, higher-order functions, and access modifiers. It highlights differences between Java and Kotlin, including built-in null safety and support for coroutines. Additionally, it explains advanced topics like delegation, lazy initialization, and scope functions with practical examples.

Uploaded by

beceve6826
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Kotlin Interview Guide

1. Null Safety in Kotlin

Kotlin has built-in null safety to prevent NullPointerException.

Nullable and Non-nullable Types

var name: String = "John" // Non-nullable

var nullableName: String? = "Doe" // Nullable type

nullableName = null // Allowed

Safe Call (``)

println(nullableName?.length) // Prints length or null

Elvis Operator (``)

val length = nullableName?.length ?: 0

println(length) // 0 if nullableName is null

Non-null Assertion (``)

println(nullableName!!.length) // Throws NPE if null

2. Coroutines in Kotlin

Coroutines enable asynchronous programming without blocking threads.

Basic Example

import kotlinx.coroutines.*

fun main() = runBlocking {

launch {

delay(1000L)

println("World!")

println("Hello,")

Output:

Hello,

World!
Important Coroutine Concepts

Concept Description

launch Runs in the background

async Returns a Deferred<T> result

suspend Marks a function as suspendable

withContext(Dispatchers.IO) Switches threads

3. Data Classes & Sealed Classes

Data Class

data class User(val name: String, val age: Int)

Sealed Class

sealed class Result {

data class Success(val data: String) : Result()

data class Error(val message: String) : Result()

4. Higher-Order Functions & Lambda Expressions

Higher-Order Function

fun operateOnNumbers(a: Int, b: Int, operation: (Int, Int) -> Int): Int {

return operation(a, b)

val sum = operateOnNumbers(5, 10) { x, y -> x + y }

println(sum) // 15

5. Companion Object & Object Declarations

Companion Object

class Logger {

companion object {

fun log(message: String) {

println("Log: $message")
}

Logger.log("Application started")

Object Declaration (Singleton)

object Singleton {

fun show() = println("I am a singleton")

Singleton.show()

6. Extension Functions

fun String.addExclamation(): String {

return this + "!"

println("Hello".addExclamation()) // Hello!

7. Scope Functions (**, **, **, **, ``)

Function Returns Usage

let Lambda result Operate on nullable object

apply Object itself Used for initializing objects

run Lambda result Similar to let but for non-null objects

also Object itself Useful for logging

with Lambda result Operates on an object

Example:

val user = User("Alice", 25).apply {

println("User created: $this")

8. Delegation & Lazy Initialization

Delegation (``)
interface Printer {

fun printMessage()

class ConsolePrinter : Printer {

override fun printMessage() = println("Hello from Printer!")

class PrinterDelegate(private val printer: Printer) : Printer by printer

val printer = PrinterDelegate(ConsolePrinter())

printer.printMessage()

Lazy Initialization (** and **)

val lazyValue: String by lazy {

println("Initializing...")

"Hello, Lazy!"

println(lazyValue)

println(lazyValue) // Doesn't reinitialize

9. Access Modifiers in Kotlin

Modifier Visibility

public Visible everywhere (default)

private Visible only in the same file/class

protected Visible in subclasses

internal Visible within the same module

Example:

class Example {

private val privateVar = "Private"

protected val protectedVar = "Protected"


internal val internalVar = "Internal"

public val publicVar = "Public"

10. Differences Between Java & Kotlin

Feature Java Kotlin

Null Safety No built-in support Built-in null safety

Coroutines Requires Threads Built-in coroutines

Data Classes Manual methods data class auto-generates methods

Extension Functions Not available Available

Type Inference Explicit types Type inference supported

Functional Programming Limited Strong FP support

11. ** vs. ** in Kotlin

Feature const val val

Compile-time constant Yes No

Used in objects/classes No (only in top-level or companion object) Yes

Assigned at runtime No Yes

Example:

const val PI = 3.14 // Compile-time

val age = getAge() // Runtime initialization

You might also like