Language Elements

Data Types

Kotlin Classes

Kotlin Operators

Kotlin Constructors

Kotlin Null Safety

Extension Functions

Lambda Functions

Object Oriented Kotlin

Data Classes

Coroutines

Kotlin Collections

Kotlin Data Structures

Kotlin Algorithms

Delegation

Lateinit and Lazy Initialization

Kotlin Scope Functions

Kotlin Key Words

Kotlin Example Codes

Kotlin Interview Questions

Kotlin Syntax and Language Basics

What are the main differences between Kotlin and Java?

Kotlin has null-safety, type inference, data classes, coroutines, and a more concise syntax. Java is more verbose but has wider legacy support.

How do I declare a variable in Kotlin?

Use val for immutable and var for mutable variables.
Example:
val name = "Alice"
var age = 30

What is the difference between val and var?

val is like a final variable in Java (read-only). var can be reassigned.

Is Kotlin statically or dynamically typed?

Kotlin is a statically typed language, meaning type checking is done at compile time. However, it supports type inference to reduce verbosity.

Can I use default arguments in Kotlin functions?

Yes, Kotlin supports default parameter values:
fun greet(name: String = "Guest") { println("Hello, $name!") }


Collections and Data Structures

What are the key differences between Java and Kotlin data structures?

Kotlin wraps Java collections but adds null-safety, immutability via List, Map, Set, and has extension functions for manipulation. Java lacks many of these features directly.

How do I create an immutable list in Kotlin?

Use listOf() to create an immutable list:
val names = listOf("Alice", "Bob")

Can I use Java collections in Kotlin?

Yes, Kotlin is interoperable with Java, so you can use ArrayList, HashMap, etc., but be careful with nullability.

What is the difference between mutableListOf and listOf?

mutableListOf() creates a list that can be modified (add/remove items), while listOf() creates an immutable list.

Are Kotlin collections thread-safe?

No, standard Kotlin collections are not thread-safe. Use concurrent collections from Java or Kotlinx libraries for thread safety.


Null Safety and Type System

What is Kotlin's null safety feature?

Kotlin distinguishes between nullable and non-nullable types. String vs String?. This helps catch null pointer exceptions at compile time.

How do I handle nulls in Kotlin?

You can use safe call ?., the Elvis operator ?:, or !! to assert non-null.
Example:
val length = name?.length ?: 0

What is the !! operator in Kotlin?

The !! operator forces a nullable variable to be non-null. If it is null, it throws a NullPointerException. Use it with caution.

How does Kotlin infer types?

Kotlin uses type inference to deduce variable or return types when not explicitly specified. This keeps code concise and readable.


Coroutines and Asynchronous Programming

What are coroutines in Kotlin?

Coroutines provide a way to write asynchronous, non-blocking code. They are lightweight threads managed by the Kotlin runtime.

How do I launch a coroutine?

You can use GlobalScope.launch { } or use structured concurrency with lifecycleScope or viewModelScope in Android.

What's the difference between launch and async?

launch returns Job and does not return a result. async returns Deferred, which you can await() to get a result.

Can I use coroutines in plain Kotlin/JVM (non-Android)?

Yes, Kotlin coroutines work on JVM, Android, JS, and Native platforms. Just add the appropriate coroutine library.


Interoperability and Compatibility

Is Kotlin fully interoperable with Java?

Yes, you can call Kotlin from Java and vice versa. Kotlin uses annotations like @JvmStatic, @JvmOverloads, etc., to enhance compatibility.

Can Kotlin be used for backend development?

Yes, with frameworks like Ktor or Spring Boot (which supports Kotlin), you can write full-stack apps in Kotlin.

Can Kotlin call Java code with annotations or generics?

Yes. Kotlin supports calling Java code using annotations and generics, though sometimes type casts or nullability handling are required.

How do I convert Java code to Kotlin?

Android Studio has a built-in tool: Paste Java code into a Kotlin file, and it will auto-convert. Or use Code > Convert Java File to Kotlin File.


Copyright © by Zafar Yasin. All rights reserved.