MCQs on Functions | Kotlin

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

  1. Which keyword is used to declare a function in Kotlin?
    a) fun
    b) def
    c) func
    d) function
  2. 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
  3. Which of these is the correct syntax for calling a function named display?
    a) call display()
    b) display
    c) display()
    d) invoke display()
  4. Can a function in Kotlin be declared outside of a class?
    a) Yes
    b) No
    c) Only inside interfaces
    d) Only in abstract classes
  5. 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

  1. 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
  2. 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
  3. 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)
  4. What type does the following function return?
    fun square(x: Int): Int { return x * x } a) Any
    b) Unit
    c) Int
    d) Float
  5. 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

  1. 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
  2. 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)
  3. 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
  4. 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)
  5. 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

QnoAnswer
1a) fun
2b) Declares a function
3c) display()
4a) Yes
5b) Represents a return type of nothing
6c) It defaults to Unit
7b) By using a colon : after the parentheses
8a) fun add(a: Int, b: Int)
9c) Int
10a) Yes, using Pair or Triple
11a) A parameter that is optional and has a default value
12b) fun example(param: Type = value)
13b) It uses the default value
14c) example(name = "John")
15b) Yes, but named arguments must follow positional arguments

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top