Control flow based Kotlin interview questions and answers


if Expression

Why Doesn’t Kotlin Feature Explicit Ternary Conditionals?


Kotlin doesn’t offer any specific ternary operator of the form c = (a < b) ? a : b; like Java or C. It omits this option because you can do the same with the if expression in Kotlin. Since the above ternary operator is essentially an expression of the form (condition ? then : else), Kotlin simply allows you to do this using its standard if keyword.

val c = if (a < b) a else b

This line of code does the same thing in Kotlin as the ternary operator does in Java. You can also pack blocks inside if-else branches.


when Expression

What is the equivalent of switch expression in Kotlin? How does it differ from switch?

when is the equivalent of switch in Kotlin.
The default statement in a when is represented using the else statement.


var num = 10
when (num) {
0..4 -> print("value is 0")
5 -> print("value is 5")
else -> {
print("value is in neither of the above.")
}
}

when statments have a default break statement in them.

What we use in substitution to switch explanation in Kotlin?
 
We use when in kotlin instead of the switch in java:-
When(x)
{
caseA: code executed if x=A:
break:
caseB: code executed fi x=B:
break;
Default code executed if x does not match any case
}


Explain advantages of "when" vs "switch" in Kotlin Explain advantages of "when" vs "switch" in Kotlin

Mid
Answer

In Java we use switch but in Kotlin, that switch gets converted to when. When has a better design. It is more concise and powerful than a traditional switchwhen can be used either as an expression or as a statement.

Some examples of when usage:

  • Two or more choices
    when(number) {
      1 -> println("One")
      2, 3 -> println("Two or Three")
      4 -> println("Four")
      else -> println("Number is not between 1 and 4")
    }
  • "when" without arguments
    when {
      number < 1 -> print("Number is less than 1")
      number > 1 -> print("Number is greater than 1")
    }
  • Any type passed in "when"
    fun describe(obj: Any): String =
      when (obj) {
        1 -> "One"
        "Hello" -> "Greeting"
        is Long -> "Long"
        !is String -> "Not a string"
        else -> "Unknown"
      }
  • Smart casting
    when (x) {
      is Int -> print("X is integer")
      is String -> print("X is string")
    }
  • Ranges
    when(number) {
      1 -> println("One") //statement 1
      2 -> println("Two") //statement 2
      3 -> println("Three") //statement 3
      in 4..8 -> println("Number between 4 and 8") //statement 4
      !in 9..12 -> println("Number not in between 9 and 12") //statement 5
      else -> println("Number is not between 1 and 8") //statement 6
    }


Aaaaaaa

for Loop

Describe For Loops in Kotlin


Loops are a crucial programming construct that allows us to iterate over things as our program requires. Kotlin features all the commonly used loops such as for, while, and do-while. We’re describing the for loop in a nutshell in the following section.

val sports = listOf("cricket", "football", "basketball")
for (sport in sports) { // for loop
println("Let's play $sport!")
}

The above snippet illustrates the use of the for loop in Kotlin. It’s quite similar to Python and Ruby.

What is the forEach in Kotlin?

In Kotlin, to use the functionality of a for-each loop just like in Java, we use a forEach function. The following is an example of the same:

var listOfMindOrks = listOf("mindorks.com", "blog.mindorks.com", "afteracademy.com")
listOfMindOrks.forEach {
 Log.d(TAG,it)
}


aaaaaaaaaaaaaaaaaa

while Loop

Describe While and Do-While Loops


The while and do-while loops work quite similar but have a specific distinction. The do-while loop executes at least once, even if the first conditional expression is returned as false. Contrary to this, while loops will stop execution if the loop isn’t true at a given time.

var i = 1
while (i < 5) { // while loop
println(i)
i++
}

This code will print the numbers 1 to 4 and then terminate. Now, take a look at the below do-while snippet.

var i = 6
do{ // do-while
println(i)
i++
}while(i<5)

Although the condition to while is false, it will print the number 6 as its output. This happens since the execution of the do block takes place without checking the condition first.

Do while Loop

Structural Expression:

Return and Jump

Continue Structure

What do you understand by structural expressions?

Structural expressions refer to the representative form of the loops in Kotlin. There are three important expressions which are:

  • Break- this expression helps the user to break the nearest enclosing loop
  • Return- this expression helps the user to return the nearest functions or any other default functions.
  • Continue- this expression helps the user to proceed further for the next loop.

Explain the Structural Expressions of Kotlin


Kotlin has three different structural expressions – namely return, break, and continue. We’re discussing each one of them with short notes.

  • return – this expression halts the program execution and returns from the enclosing function
  • break – it is used for terminating the nearest enclosing loop in Kotlin
  • continue – it allows the execution to proceed to the next iteration without performing the current operation

The second snippet of the previous example prints the value 6 since we’ve used continue. If we had used break instead, it would print nothing.


Aasa

Comments

Popular posts from this blog

Jetpack Compose based Android interview and questions

Null safety based Kotlin interview questions and answers

CustomView based Android interview questions and answers