Skip to content

Instantly share code, notes, and snippets.

@pradhuman7d1
Last active November 26, 2021 08:24
Show Gist options
  • Save pradhuman7d1/1df0e3a9060e8088ac81960dc0701276 to your computer and use it in GitHub Desktop.
Save pradhuman7d1/1df0e3a9060e8088ac81960dc0701276 to your computer and use it in GitHub Desktop.
Kotlin Conditionals
Q. What is when?
A. When works as switch cases.
Q. What is the use of conditional operators?
A. These are used to check some conditions and if they satisfy then only our code block will work.
Q. Can we use a range in when for comparision?
A. Yes, we can use range by using the in keyword.
Q. What if no condition of when is satisfied?
A. In that case if you have a default case using else than that will execute otherwise the when would not execute.
Q. What will be the output of this code?
fun main() {
var a = 10
var b = 10.0
println(a == b)
}
a) true
b) false
c) compilation error
d) 10 == 10.0
A. c) compilation error // == can't be applied between different types.
Q. What will be the output?
fun main(){
var a = -1
if(a < 0) {
println(a--)
} else {
println(a++)
}
}
a) -2
b) 0
c) -1
A. c) -1
Q. What will be the output?
fun main() {
var a = 5.0
when(a) {
5 -> println("a = $a")
else -> println("Hello")
}
}
a) a = 5.0
b) Hello
c) a = $a
d) compilation error
A. d) compilation error // Incompatible types
Q. What is the output?
fun main() {
val a = 1
if(a < 4 && a > 5) {
println("a = $a")
}
}
a) a = 1
b) a = $a
c) compilation error
d) no output
A. d) no output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment