
Practice 50 Kotlin Advanced Topics Coding Questions
Q1. Write a Kotlin program to demonstrate the use of data class.
Input: Person(name = "Alice", age = 30)
Expected Output: Person(name=Alice, age=30)
Q2. Write a Kotlin program to demonstrate the use of sealed class.
Input: class Success(val message: String) : Result()
Expected Output: Success(message=Data retrieved successfully)
Q3. Write a Kotlin program to create a singleton class using object.
Input: object DatabaseConnection { val url = "jdbc://localhost" }
Expected Output: DatabaseConnection(url=jdbc://localhost)
Q4. Write a Kotlin program to demonstrate the use of extension functions.
Input: "Kotlin".reverse()
Expected Output: "niltok"
Q5. Write a Kotlin program to demonstrate the use of companion object.
Input: class User(val name: String) { companion object { const val userType = "Admin" } }
Expected Output: User.userType = Admin
Q6. Write a Kotlin program to demonstrate the use of inline functions.
Input: inline fun logExecution(block: () -> Unit) { println("Start"); block(); println("End") }
Expected Output: Start Function executed End
Q7. Write a Kotlin program to demonstrate the use of higher-order functions.
Input: fun processString(str: String, operation: (String) -> String) = operation(str)
Expected Output: operation result: "HELLO"
Q8. Write a Kotlin program to demonstrate the use of type aliases.
Input: typealias StringList = List<String>
Expected Output: List<String>
Q9. Write a Kotlin program to demonstrate the use of lateinit.
Input: lateinit var name: String
Expected Output: name is initialized
Q10. Write a Kotlin program to demonstrate the use of by lazy initialization.
Input: val greeting: String by lazy { "Hello, Kotlin!" }
Expected Output: "Hello, Kotlin!"
Q11. Write a Kotlin program to demonstrate destructuring declarations.
Input: val person = Person("Alice", 30); val (name, age) = person
Expected Output: "Name: Alice, Age: 30"
Q12. Write a Kotlin program to demonstrate the use of unsafe calls with !! operator.
Input: val name: String? = "Kotlin"; val length = name!!.length
Expected Output: 6
Q13. Write a Kotlin program to demonstrate the use of vararg.
Input: fun sum(vararg numbers: Int): Int = numbers.sum()
Expected Output: 15
Q14. Write a Kotlin program to demonstrate the use of apply scope function.
Input: val person = Person("Alice", 30).apply { name = "Bob" }
Expected Output: Person(name=Bob, age=30)
Q15. Write a Kotlin program to demonstrate the use of with scope function.
Input: val person = Person("Alice", 30); with(person) { println(name) }
Expected Output: Alice
Q16. Write a Kotlin program to demonstrate the use of let scope function.
Input: val name: String? = "Alice"; name?.let { println("Name: $it") }
Expected Output: Name: Alice
Q17. Write a Kotlin program to demonstrate the use of run scope function.
Input: val result = run { 5 * 5 }
Expected Output: 25
Q18. Write a Kotlin program to demonstrate the use of takeIf function.
Input: val number = 10; val result = number.takeIf { it > 5 }
Expected Output: 10
Q19. Write a Kotlin program to demonstrate the use of takeUnless function.
Input: val number = 4; val result = number.takeUnless { it > 5 }
Expected Output: 4
Q20. Write a Kotlin program to demonstrate the use of coroutines with GlobalScope.
Input: GlobalScope.launch { println("Hello, Kotlin Coroutines!") }
Expected Output: Hello, Kotlin Coroutines!
Q21. Write a Kotlin program to demonstrate the use of Channel in coroutines.
Input: val channel = Channel<Int>(); launch { channel.send(10) }
Expected Output: 10
Q22. Write a Kotlin program to demonstrate the use of CoroutineScope.
Input: val scope = CoroutineScope(Dispatchers.Default); scope.launch { println("Inside Coroutine") }
Expected Output: Inside Coroutine
Q23. Write a Kotlin program to demonstrate the use of suspend functions in coroutines.
Input: suspend fun fetchData(): String = "Data fetched"
Expected Output: Data fetched
Q24. Write a Kotlin program to demonstrate the use of async and await with coroutines.
Input: val result = async { 5 * 5 }; println(result.await())
Expected Output: 25
Q25. Write a Kotlin program to demonstrate RxKotlin with observables.
Input: Observable.just(1, 2, 3).subscribe { println(it) }
Expected Output: 1 2 3
Q26. Write a Kotlin program to create and test an immutable list.
Input: val list = listOf(1, 2, 3)
Expected Output: [1, 2, 3]
Q27. Write a Kotlin program to demonstrate inline classes.
Input: inline class Name(val name: String)
Expected Output: Name("Kotlin")
Q28. Write a Kotlin program to demonstrate type checks using is operator.
Input: val obj: Any = "Kotlin"; if (obj is String) println("It is a String")
Expected Output: It is a String
Q29. Write a Kotlin program to demonstrate the use of map and filter functions.
Input: val numbers = listOf(1, 2, 3, 4, 5); val result = numbers.filter { it > 3 }
Expected Output: [4, 5]
Q30. Write a Kotlin program to demonstrate lazy initialization of properties.
Input: val name: String by lazy { "Kotlin" }
Expected Output: "Kotlin"
Q31. Write a Kotlin program to demonstrate Pair and Triple.
Input: val pair = Pair("Hello", 123)
Expected Output: (Hello, 123)
Q32. Write a Kotlin program to demonstrate delegation using by keyword.
Input: class MyClass(val str: String); val myInstance by lazy { MyClass("Kotlin") }
Expected Output: MyClass(str=Kotlin)
Q33. Write a Kotlin program to demonstrate usage of StringBuilder.
Input: val sb = StringBuilder("Kotlin"); sb.append(" Programming")
Expected Output: Kotlin Programming
Q34. Write a Kotlin program to demonstrate creating a custom Iterator.
Input: class MyIterator(val range: IntRange) : Iterator<Int> { ... }
Expected Output: [1, 2, 3, 4, 5]
Q35. Write a Kotlin program to demonstrate Reflection to check class properties.
Input: val obj = MyClass::class.memberProperties.forEach { println(it.name) }
Expected Output: property1 property2
Q36. Write a Kotlin program to demonstrate Annotation usage.
Input: @Target(AnnotationTarget.CLASS) annotation class MyAnnotation
Expected Output: MyAnnotation applied to Class
Q37. Write a Kotlin program to demonstrate a Custom Exception.
Input: class MyException(message: String) : Exception(message)
Expected Output: throw MyException("Custom error message")
Q38. Write a Kotlin program to demonstrate an Observable pattern using LiveData.
Input: val liveData = MutableLiveData<Int>(0); liveData.observe(...)
Expected Output: LiveData updates
Q39. Write a Kotlin program to demonstrate LocalDateTime usage.
Input: val dateTime = LocalDateTime.now()
Expected Output: 2025-01-14T10:20:30
Q40. Write a Kotlin program to demonstrate UUID generation.
Input: val uuid = UUID.randomUUID()
Expected Output: Random UUID
Q41. Write a Kotlin program to demonstrate the use of try-catch blocks in exception handling.
Input: try { ... } catch (e: Exception) { println("Exception") }
Expected Output: Exception caught
Q42. Write a Kotlin program to demonstrate the use of Transaction management in databases.
Input: val transaction = connection.beginTransaction()
Expected Output: Transaction started
Q43. Write a Kotlin program to demonstrate SimpleDateFormat in date formatting.
Input: val sdf = SimpleDateFormat("yyyy-MM-dd"); val formattedDate = sdf.format(Date())
Expected Output: 2025-01-14
Q44. Write a Kotlin program to demonstrate Gson serialization and deserialization.
Input: val gson = Gson(); gson.toJson(person)
Expected Output: {"name":"Alice","age":30}
Q45. Write a Kotlin program to demonstrate JSON parsing with Moshi.
Input: val moshi = Moshi.Builder().build()
Expected Output: Moshi parsing result
Q46. Write a Kotlin program to demonstrate ObjectMapper for JSON handling.
Input: val objectMapper = ObjectMapper()
Expected Output: JSON object
Q47. Write a Kotlin program to demonstrate creating a custom Timer with delay.
Input: val timer = Timer()
Expected Output: Timer execution after delay
Q48. Write a Kotlin program to demonstrate Thread.sleep with concurrency.
Input: Thread.sleep(2000)
Expected Output: Delayed execution
Q49. Write a Kotlin program to demonstrate Thread with basic operations.
Input: val thread = Thread(Runnable { println("Thread executed") })
Expected Output: Thread executed
Q50. Write a Kotlin program to demonstrate the usage of CountDownLatch for synchronization.
Input: val latch = CountDownLatch(1)
Expected Output: Latch countdown completed