-
-
Save pradhuman7d1/35a67f50774c5d26ab6a67bb9022058a to your computer and use it in GitHub Desktop.
Kotlin Tail Recursion and Higher Order Functions
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 is tailrec? | |
A. tailrec keyword is used for tail recursion in kotlin. | |
Q. What are higher order functions? | |
A. higher order functions are the functions that can return a function and can accept a function as argument. | |
Q. What is the use of higher order functions? | |
A. Higher order functions are used a lot in functional programming, they can help generate new functions as per requirements. | |
Q. What is it keyword? | |
A. When using a lambda function we can simply use it we have only a single value. |
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. Which of the following declaration is syntactically correct? | |
a) fun myFun(num1: Int): (Int) -> Int = { num2 -> num1 * num2} | |
b) fun myFun(num1: Int): (Int) : Int = { num2 -> num1 * num2} | |
c) fun myFun(num1: Int): (Int) -> Int : { num2 -> num1 * num2} | |
d) fun myFun(num1: Int): (Int) = Int -> { num2 -> num1 * num2} | |
A. a) fun myFun(num1: Int): (Int) -> Int = { num2 -> num1 * num2} | |
Q. What type of function is mult3? | |
fun main() { | |
val mult3 = makeMathFunc(3) | |
println("5 * 3 = ${mult3(5)}") | |
} | |
fun makeMathFunc(num1: Int): (Int) -> Int = { num2 -> num1 * num2} | |
a) higher order function taking function as input | |
b) lambda function | |
c) higher order function returning a function | |
d) a normal function | |
A. d) a normal function | |
Q. Where is the function myFunc doing? | |
fun main() { | |
val multiply2 = {num1: Int -> num1 * 2} | |
val numList2 = arrayOf(1,2,3,4,5) | |
mathOnList(numList2, multiply2) | |
} | |
fun mathOnList(numList: Array<Int>, myFunc: (num: Int) -> Int){ | |
for(num in numList){ | |
println("MathOnList : ${myFunc(num)}") | |
} | |
} | |
a) multiply the passed number num by num1 | |
b) multiply the passed number num by 2 | |
A. b) multiply the passed number num by 2 | |
Q. What is tail recursion? | |
a) starts calculating the values after reaching the base case | |
b) reaches the base case while calculating and storing the values. | |
A. b) reaches the base case while calculating and storing the values. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment