Master the essentials of Kotlin Functions with this quiz! Learn about declaring and calling functions, handling parameters, understanding return types, and exploring default and named arguments in Kotlin programming.
MCQs on Functions in Kotlin
Declaring and Calling Functions
Which keyword is used to declare a function in Kotlin? a) fun b) def c) func d) function
What does the following code snippet do in Kotlin? fun greet() { println("Hello") } a) Declares a variable b) Declares a function c) Throws an error d) Declares a class
Which of these is the correct syntax for calling a function named display? a) call display() b) display c) display() d) invoke display()
Can a function in Kotlin be declared outside of a class? a) Yes b) No c) Only inside interfaces d) Only in abstract classes
What is the purpose of the Unit keyword in Kotlin functions? a) Indicates a nullable type b) Represents a return type of nothing c) Defines a variable d) Indicates an abstract method
Function Parameters and Return Types
In Kotlin, what happens if a function does not specify a return type? a) It automatically returns null b) It defaults to Any c) It defaults to Unit d) The program throws an error
How do you specify the return type of a Kotlin function? a) By adding -> after the function name b) By using a colon : after the parentheses c) By declaring the type in the body d) By appending return
Identify the valid function with parameters in Kotlin: a) fun add(a: Int, b: Int) b) function add(Int a, Int b) c) fun add(a Int, b Int) d) add(a: Int, b: Int)
What type does the following function return? fun square(x: Int): Int { return x * x } a) Any b) Unit c) Int d) Float
Can a Kotlin function return multiple values directly? a) Yes, using Pair or Triple b) No, it can only return one value c) Yes, but only using arrays d) No, unless explicitly declared in an interface
Default and Named Arguments
What is a default argument in Kotlin? a) A parameter that is optional and has a default value b) A required parameter c) A type of variable d) An argument passed at runtime
How do you define a default argument for a function parameter? a) fun example(param = value: Type) b) fun example(param: Type = value) c) fun example(param default value: Type) d) fun example(param: Type value default)
What will happen if a default argument is not provided in the function call? a) The compiler throws an error b) It uses the default value c) It automatically assigns null d) It skips the parameter
Which of the following demonstrates a named argument in Kotlin? a) function(name: "John") b) fun example(name "John") c) example(name = "John") d) example("John" = name)
Can named arguments and positional arguments be mixed in a function call? a) Yes, always b) Yes, but named arguments must follow positional arguments c) No, they cannot be mixed d) Yes, but positional arguments must follow named arguments
Answers Table
Qno
Answer
1
a) fun
2
b) Declares a function
3
c) display()
4
a) Yes
5
b) Represents a return type of nothing
6
c) It defaults to Unit
7
b) By using a colon : after the parentheses
8
a) fun add(a: Int, b: Int)
9
c) Int
10
a) Yes, using Pair or Triple
11
a) A parameter that is optional and has a default value
12
b) fun example(param: Type = value)
13
b) It uses the default value
14
c) example(name = "John")
15
b) Yes, but named arguments must follow positional arguments