menu

Kotlin Functions

Kotlin Functions - Important Points


1. What is the keyword used to define a function in Kotlin?

A. fun

B. function

C. define

D. declare

Discuss Work Space

Answer: option a

Explanation:

The keyword used to define a function in Kotlin is 'fun'.

2. Which of the following is not a valid function declaration in Kotlin?

A. fun add(x: Int, y: Int): Int = x + y

B. fun add(x: Int, y: Int) = x + y

C. fun add(x: Int, y: Int): Unit = println(x + y)

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

Discuss Work Space

Answer: option d

Explanation:

The last option is not a valid function declaration in Kotlin. The correct syntax for returning a value in Kotlin is the equals sign '=' instead of the '=>' symbol.

3. How is a function that takes no arguments declared in Kotlin?

A. fun noArgs()

B. fun noArgs(): Unit

C. fun noArgs(): Void

D. fun noArgs(): Nothing

Discuss Work Space

Answer: option b

Explanation:

A function that takes no arguments is declared with an empty parameter list followed by a colon and the Unit type, as in option B.

4. What is a higher-order function in Kotlin?

A. A function that returns a value

B. A function that takes another function as a parameter or returns a function as a result

C. A function that has a variable number of arguments

D. A function that can be called without any arguments

Discuss Work Space

Answer: option b

Explanation:

A higher-order function is a function that takes another function as a parameter or returns a function as a result.

5. Which of the following is not a valid way to call a function in Kotlin?

A. add(1, 2)

B. add(x = 1, y = 2)

C. add(y = 2, x = 1)

D. x.add(2)

Discuss Work Space

Answer: option d

Explanation:

In Kotlin, functions can be called using the function name followed by parentheses, with or without argument names. Option D is not a valid way to call a function because it assumes that the function is a member of an object 'x', which is not declared in the question.


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