menu

Kotlin Functions

Kotlin Functions - Important Points


16. Which of the following is a valid way to call a higher-order function in Kotlin?

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

B. sum(1, 2) { x -> x * x }

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

D. sum(1, 2, { x -> x * x })

Discuss Work Space

Answer: option c

Explanation:

Option C is a valid way to call a higher-order function in Kotlin, by passing a lambda expression or a function reference as the last argument.

17. What is the purpose of the 'operator' modifier in a function declaration in Kotlin?

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

B. It allows the function to be inlined at the call site to improve performance

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

D. It allows the function to be used as an overloaded operator

Discuss Work Space

Answer: option d

Explanation:

The 'operator' modifier in a function declaration in Kotlin allows the function to be used as an overloaded operator, with a syntax similar to built-in operators such as '+' or '*'.

18. What is a lambda expression in Kotlin?

A. A named function

B. A higher-order function

C. An anonymous function

D. A function with a single expression

Discuss Work Space

Answer: option c

Explanation:

A lambda expression in Kotlin is an anonymous function, that is, a function without a name.

19. What is the purpose of the 'crossinline' modifier in a function declaration in Kotlin?

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

B. It specifies that the function should not have a return value

C. It specifies that the function should not have a receiver parameter

D. It prevents the use of non-local returns in the function body

Discuss Work Space

Answer: option d

Explanation:

The 'crossinline' modifier in a function declaration in Kotlin prevents the use of non-local returns in the function body, which can lead to unexpected behavior and make the code harder to understand.

20. Which of the following is a valid way to declare an extension function in Kotlin?

A. fun String.capitalize(): String = this.toUpperCase()

B. fun capitalize(String str): String = str.toUpperCase()

C. fun String.capitalize(): String = toUpperCase()

D. fun capitalize(str: String): String = str.toUpperCase()

Discuss Work Space

Answer: option a

Explanation:

Option A is a valid way to declare an extension function in Kotlin, by prefixing the name of the receiver type ('String' in this case) to the function name.


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