Posts

Showing posts with the label Kotlin interview questions and answers

Collection based Kotlin interview questions and answers

What is the difference between List and Array types?  What is the difference between List and Array types? Mid Answer The major difference from usage side is that  Arrays  have a fixed size while  (Mutable)List can adjust their size dynamically. Moreover  Array  is mutable whereas  List  is not. Furthermore  kotlin.collections.List  is an interface implemented among others by  java.util.ArrayList . It's also extended by  kotlin.collections.MutableList to be used when a collections that allows for item modification is needed. On the jvm level  Array  is represented by arrays.  List  on the other hand is represented by  java.util.List  since there are no immutable collections equivalents available in Java. What is basic difference between fold and reduce in Kotlin? When to use which?  What is basic difference between fold and reduce in Kotlin? When to use which? Junior Answer fold  takes an...

Coroutines based Kotlin interview questions and answers

Image
Kotlin Coroutines on Android Scopes in Kotlin Coroutines Dispatchers in Kotlin Coroutines Launch vs Async in Kotlin Coroutines Suspend Function In Kotlin Coroutines Singleton Class in Kotlin withContext in Kotlin Coroutines runBlocking in Kotlin Coroutines with Example Jobs, Waiting, Cancellation in Kotlin Coroutines Getting Started with Paging Library v3 in Android using Kotlin Coroutines What are kotlin coroutines? Kotlin's coroutines  are lightweight threads. It provides a good way to write asynchronous code that is perfectly readable and maintainable. Kotlin provides only minimal low-level APIs in its standard library to enable various other libraries to utilize coroutines. What are coroutines in Kotlin?  What are coroutines in Kotlin? Mid Answer Unlike many other languages with similar capabilities, async and await are not keywords in Kotlin and are not even part of its standard library. kotlinx.coroutines  is a rich library for coroutines developed by JetBrains. It ...

Delegation based Kotlin interview questions and answers

What is Kotlin delegation? Kotlin supports the “delegation” design pattern by introducing a new keyword “by”. Using the keyword or delegation methodology. Kotlin allows the derived class to access all of the implemented public methods of an interface through a specific object. Example Live Demo interface Base { fun printMe ( ) //abstract method } class BaseImpl ( val x: Int ) : Base { override fun printMe ( ) { println ( x ) } //implementation of the method } class Derived ( b: Base ) : Base by b // delegating the public method on the object b fun main ( args: Array<String> ) { val b = BaseImpl ( 10 ) Derived ( b ) . printMe ( ) // prints 10 :: accessing the printMe() method } Output 10

Ranges based Kotlin interview questions and answers

Integer type range Kotlin Working Ranges What is Ranges operator in Kotlin? Ranges operator helps to iterate through a range. Its operator form is (..)  For Example for (i in 1..15) print(i) It will print from 1 to 15 in output. Kotlin Utility Function What is Ranges operator in Kotlin? Ranges operator helps to iterate through a range. Its operator forms are (..)). Example for ( i in 1 .. 4 ) print ( i ) Aaaaas

Functions based Kotlin interview questions and answers

Image
Function Kotlin Function  What is the difference between a function and a method in Kotlin? A function is a named code block invoked from other locations within the source code. A method is a function associated with an object and can be invoked from other code with the dot notation. Explain Functions In Kotlin? Kotlin functions are first-class functions that are easily stored in variables and data structures and can be pass as arguments and returned from other higher-order functions. Sample function declaration and usage in Kotlin fun double(x: Int): Int { return 2 * x } val result = double(2) How many types are used in kotlin?   there are to type function used in kotlin: 1. Standard library function 2. User-defined function   What is the Standard library function and User-defined function?   1. A  standard library function  is built-in library functions that are implicitly present in the library and available for use. 2. A  user-defined func...