menu

Kotlin Functions

Kotlin Functions - Important Points


6. What is the return type of a function that does not return a value in Kotlin?

A. Int

B. Unit

C. Void

D. Nothing

Discuss Work Space

Answer: option b

Explanation:

The return type of a function that does not return a value in Kotlin is Unit.

7. What is the purpose of the 'infix' modifier in a function declaration in Kotlin?

A. It allows the function to be called with or without argument names

B. It allows the function to be called using infix notation

C. It allows the function to be called with a variable number of arguments

D. It allows the function to be called using prefix notation

Discuss Work Space

Answer: option b

Explanation:

The 'infix' modifier in a function declaration allows the function to be called using infix notation, which means it can be called without parentheses and can appear between the operands.

8. Which of the following is not a valid way to define a function with a variable number of arguments in Kotlin?

A. fun sum(vararg nums: Int): Int = nums.sum()

B. fun sum(nums: Array): Int = nums.sum()

C. fun sum(nums: List): Int = nums.sum()

D. fun sum(nums: Set): Int = nums.sum()

Discuss Work Space

Answer: option b

Explanation:

Option B is not a valid way to define a function with a variable number of arguments in Kotlin. The correct syntax is to use the 'vararg' modifier in the parameter list, as in option

9. What is the purpose of the 'tailrec' modifier in a recursive function in Kotlin?

A. It allows the function to be called using tail recursion optimization

B. It allows the function to be called with a variable number of arguments

C. It allows the function to be called with or without argument names

D. It allows the function to be called using infix notation

Discuss Work Space

Answer: option a

Explanation:

The 'tailrec' modifier in a recursive function allows the function to be called using tail recursion optimization, which optimizes the memory usage and avoids stack overflow errors.

10. Which of the following is a valid way to declare and call an anonymous function in Kotlin?

A. { x: Int, y: Int -> x + y } (1, 2)

B. fun(x: Int, y: Int): Int { return x + y }(1, 2)

C. fun(x: Int, y: Int) = x + y (1, 2)

D. { x, y -> x + y }(1, 2)

Discuss Work Space

Answer: option d

Explanation:

Option D is a valid way to declare and call an anonymous function in Kotlin using the curly braces and parameters separated by commas.


Subscribe for Latest Career Trends
Subscribe Now
Use AI and ChatGPT for Career Guidance

Unlock Your Future

Join Now
Worried for Placements in 2024?

Join FAST TRACK Course

Join Now
Supercharge Your SUCCESS

Join All in One Placement Mock Tests-2024

Join Now