menu

Kotlin Functions

Kotlin Functions - Important Points


11. What is the purpose of the 'inline' 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 or without argument names

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

Discuss Work Space

Answer: option b

Explanation:

The 'inline' modifier in a function declaration allows the function to be inlined at the call site to improve performance, by reducing the overhead of function calls.

12. Which of the following is not a valid way to define a lambda function in Kotlin?

A. { x: Int, y: Int -> x + y }

B. { x: Int -> x * x }

C. { x -> x * x }

D. fun(x: Int, y: Int): Int = x + y

Discuss Work Space

Answer: option d

Explanation:

Option D is not a valid way to define a lambda function in Kotlin because it uses the 'fun' keyword and an equals sign, which are used to define regular functions. Lambdas are defined using curly braces and optional parameter types.

13. 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 allows the function to be inlined at the call site to improve performance

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

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

Discuss Work Space

Answer: option c

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.

14. What is the return type of a function in Kotlin if it does not return any value?

A. Int

B. Unit

C. Void

D. None

Discuss Work Space

Answer: option b

Explanation:

The return type of a function in Kotlin if it does not return any value is 'Unit', which is a type with only one value, represented by the keyword 'Unit'.

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

A. fun sum(x: Int, y: Int, op: (Int, Int) -> Int): Int = op(x, y)

B. fun sum(x: Int, y: Int, op: Int, Int -> Int): Int = op(x, y)

C. fun sum(x: Int, y: Int, op: Int.(Int) -> Int): Int = x.op(y)

D. fun sum(x: Int, y: Int, op: (Int) -> Int): Int = op(x + y)

Discuss Work Space

Answer: option a

Explanation:

Option A is a valid way to declare a higher-order function in Kotlin, by taking a function as a parameter.


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