-
-
Save pradhuman7d1/b0d8bc79a08d75505443dab5fcba8e19 to your computer and use it in GitHub Desktop.
Kotlin 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 Unit return type? | |
A. Unit return type is used when we have nothing to return from a function. | |
Q. Is it possible to return multiple values from a function? | |
A. Yes, it can be done using Pair or Triple, Pair returns two values and Triple returns three values. | |
Q. What to do if we need multiple parameters in a funtion but are not sure of the exact number? | |
A. In such a case, we can use varargs that allows us to pass any number of parameters. | |
Q. What type of function is this fun add(num1: Int, num2: Int) : Int = num1 + num2? | |
A. This is called single line function, it does not have a complete body and it return the values written after the equals sign. |
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 is a correct function declaration? | |
a) fun add(num1 :Int, num2 :Int) : Int = num1 + num2 | |
b) fun subtract(num1: Int = 1, num2: Int = 1) = num1 - num2 | |
c) val mult = {num1 : Int, num2 : Int -> num1 * num2} | |
d) All of these | |
e) None of these | |
A. d) All of these | |
Q. Which of the following code will work correctly? | |
1. fun main() { | |
println("5 + 7 = ${add(5, 7)}") | |
fun add(num1 :Int, num2 :Int) : Int = num1 + num2 | |
} | |
2. fun main() { | |
fun add(num1 :Int, num2 :Int) : Int = num1 + num2 | |
println("5 + 7 = ${add(5, 7)}") | |
} | |
3. fun main() { | |
println("5 + 7 = ${add(5, 7)}") | |
} | |
fun add(num1 :Int, num2 :Int) : Int = num1 + num2 | |
a) 1 and 2 | |
b) 2 and 3 | |
c) 3 and 1 | |
d) None of these | |
A. b) 2 and 3 | |
Q. Which is the correct syntax for using variable arguments in a function? | |
a) vararg nums: Int | |
b) Vararg nums: Int | |
c) nums : varargs | |
d) None of these | |
A. a) vararg nums: Int | |
Q. What will be the output? | |
fun main() { | |
print(myFun()) | |
} | |
fun myFun() : Int { | |
print("Hello") | |
return 5 | |
} | |
a) 5Hello | |
b) Hello5 | |
A. b) Hello5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment