Top Interview Questions For Kotlin in TCS
Top Basic Kotlin Interview Questions and Answers for TCS
Introduction
Preparing for a Kotlin interview at TCS? Kotlin has become a preferred language for Android development, backend services, and cross-platform applications. TCS frequently asks fundamental Kotlin questions to evaluate a candidate’s knowledge of the language.
This blog provides a comprehensive list of basic Kotlin interview questions to help freshers and experienced professionals succeed in their TCS technical interview.
✔ Covers essential Kotlin concepts
✔ Includes real-world examples
✔ Ideal for freshers and experienced developers
📌 Bookmark this page and review these questions before your TCS interview! 🚀
Top Basic Kotlin Interview Questions and Answers
1. What is Kotlin?
Answer: Kotlin is a modern, statically typed programming language developed by JetBrains. It is interoperable with Java and widely used for Android, web, and backend development.
2. What are the key features of Kotlin?
Answer:
- Concise – Reduces boilerplate code
- Null Safety – Prevents
NullPointerException
- Interoperability – Works seamlessly with Java
- Coroutines – Simplifies asynchronous programming
- Smart Casts – Automatic type conversion
- Extension Functions – Adds functions to existing classes
3. What is the difference between var
and val
?
Answer:
var
– Mutable (value can be changed)val
– Immutable (likefinal
in Java)
Example:
var age = 25
age = 30 // Allowed
val name = "TCS"
// name = "Other" // Error
4. What is type inference in Kotlin?
Answer: Kotlin can automatically infer the data type of variables, eliminating the need for explicit declarations.
Example:
val language = "Kotlin" // Automatically inferred as String
val number = 10 // Inferred as Int
5. How does Kotlin handle null safety?
Answer: Kotlin prevents NullPointerException
by not allowing null values unless explicitly declared as nullable.
Example:
var name: String? = null // Nullable variable
println(name?.length) // Safe call operator
6. What is the Elvis operator (?:
) in Kotlin?
Answer: The Elvis operator provides a default value if a nullable expression is null.
Example:
val name: String? = null
val length = name?.length ?: 0 // If name is null, return 0
println(length) // Output: 0
7. What is a when
expression in Kotlin?
Answer: when
is an enhanced version of Java’s switch
statement.
Example:
val number = 3
val result = when (number) {
1 -> "One"
2 -> "Two"
3 -> "Three"
else -> "Unknown"
}
println(result) // Output: Three
8. How do you define a function in Kotlin?
Answer: Functions are declared using the fun
keyword.
Example:
fun greet(name: String): String {
return "Hello, $name!"
}
println(greet("TCS")) // Output: Hello, TCS!
9. What is a default parameter in Kotlin?
Answer: Kotlin allows functions to have default values for parameters.
Example:
fun greet(name: String = "Guest") {
println("Hello, $name!")
}
greet() // Output: Hello, Guest!
greet("TCS") // Output: Hello, TCS!
10. What is the difference between ==
and ===
in Kotlin?
Answer:
==
– Compares values (like.equals()
in Java)===
– Compares object references
Example:
val a = "Kotlin"
val b = "Kotlin"
println(a == b) // true (values are equal)
println(a === b) // true (same object reference)
11. How do you declare a class in Kotlin?
Answer:
class Person(val name: String, val age: Int)
Example:
val person = Person("John", 25)
println(person.name) // Output: John
12. What is a data class in Kotlin?
Answer: A data class
is used to store data and automatically generates equals()
, hashCode()
, and toString()
.
Example:
data class User(val name: String, val age: Int)
val user = User("Alice", 30)
println(user) // Output: User(name=Alice, age=30)
13. What are extension functions in Kotlin?
Answer: Extension functions allow adding functions to existing classes without modifying them.
Example:
fun String.addExclamation(): String {
return this + "!"
}
println("Hello".addExclamation()) // Output: Hello!
14. What is a Singleton in Kotlin?
Answer: A singleton is created using the object
keyword.
Example:
object Database {
val name = "MyDatabase"
fun connect() = println("Connected to $name")
}
Database.connect() // Output: Connected to MyDatabase
15. What is the difference between apply
, let
, run
, and with
?
Function | Description |
---|---|
apply | Used for configuring objects |
let | Used for executing code blocks with the object |
run | Similar to let , but returns the lambda result |
with | Used when performing multiple operations on an object |
Example:
data class Person(var name: String, var age: Int)
val person = Person("Alice", 25).apply {
age = 30
}
println(person.age) // Output: 30
16. What is the difference between lazy
and lateinit
?
Answer:
lazy
– Used for immutable properties (initialized only when accessed).lateinit
– Used for mutable properties (initialized later).
Example:
val message: String by lazy {
"Hello, TCS!"
}
println(message) // Initialized and printed when accessed
17. What is sealed class
in Kotlin?
Answer: A sealed class
restricts inheritance to a fixed set of subclasses.
Example:
sealed class Result {
class Success(val data: String) : Result()
class Error(val message: String) : Result()
}
18. What is Kotlin Coroutines?
Answer: Coroutines are used for asynchronous programming without blocking threads.
Example:
import kotlinx.coroutines.*
fun main() = runBlocking {
launch {
println("Hello from Coroutine!")
}
}
19. What is the use of super
keyword in Kotlin?
Answer: The super
keyword is used to refer to the parent class’s properties and methods.
Example:
open class Parent {
open fun show() {
println("Parent class")
}
}
class Child : Parent() {
override fun show() {
super.show()
println("Child class")
}
}
20. What is the difference between first()
and find()
in Kotlin?
Answer:
first()
– Returns the first element, throws an exception if empty.find()
– Returns the first matching element, returnsnull
if no match is found.