-
-
Save pradhuman7d1/07c0d546c188dbcab07a5a0f235ca3ae to your computer and use it in GitHub Desktop.
Kotlin Collection operators and exception handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Q. What are collection operator? | |
A. Collection operators are prebuilt functions that can perform certain operations on a collection. | |
Q. What is the difference in reduce and fold? | |
A. Reduce only performs the defined operation on the collection whereas fold have an initial value and performs the operation defined with it. | |
Q. What is exception handling? | |
A. When performing certain tasks, there are cases where some exceptions might occur, to prevent them we can use try and catch to. | |
Q. What is map operator? | |
A. map operator can update the values of a collection by following a given command. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Q. What will be the output? | |
fun main() { | |
val numList2 = arrayOf(1,2,3,4,5) | |
val listSum = numList2.fold(2) { x, y -> x * y } | |
println(listSum) | |
} | |
a) 120 | |
b) 15 | |
c) 122 | |
d) 240 | |
A. d) 240 | |
Q. If n is this list [2, 4]. What is x in the following code? | |
fun main() { | |
val numList = arrayOf(1,2,3,4,5) | |
val n = numList.x{it % 2 == 0} | |
} | |
a) reduce | |
b) fold | |
c) filter | |
d) map | |
A. c) filter | |
Q. What does the catch body does? | |
a) catches the message from thrown exception | |
b) catches the entire thrown exception | |
A. b) catches the entire thrown exception | |
Q. If the output is "Can't Divide by Zero". What is x in this code? | |
fun main() { | |
val divisor = 0 | |
try { | |
if (divisor == 0) { | |
throw IllegalArgumentException("Can't Divide by Zero") | |
} else { | |
println("5 / $divisor = ${5/divisor}") | |
} | |
} catch (e: IllegalArgumentException) { | |
println("${x}") | |
} | |
} | |
a) e | |
b) e.message | |
c) e.body | |
d) e.info | |
A. b) e.message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment