MCQs on Functions | Swift

Dive into Swift programming with this detailed exploration of functions. Learn about defining functions, parameters, return types, and advanced topics like default and variadic functions. Test your knowledge with these 30 MCQs!


Defining and Calling Functions (Questions 1-10)

  1. What is the correct syntax to define a function in Swift?
    a) func name() {}
    b) function name() {}
    c) def name() {}
    d) function name(): {}
  2. Which keyword is used to define a function in Swift?
    a) define
    b) func
    c) function
    d) declare
  3. What is the minimum requirement to call a function in Swift?
    a) Function definition and return type
    b) Function name
    c) Function name followed by parentheses
    d) Function name and arguments
  4. In Swift, can a function call another function?
    a) Yes
    b) No
  5. What does the following code do?swiftCopy codefunc greet() { print("Hello!") } greet() a) Prints “Hello!”
    b) Does nothing
    c) Throws an error
    d) Returns a value
  6. Can Swift functions exist without parameters?
    a) Yes
    b) No
  7. What is the return type of the following function?swiftCopy codefunc test() -> Int { return 5 } a) Void
    b) Int
    c) String
    d) Float
  8. In Swift, what happens if you do not provide a return type?
    a) Function throws an error
    b) Default return type is Void
    c) Return type is inferred
    d) Function does not compile
  9. What does the -> symbol indicate in a function declaration?
    a) Function name
    b) Parameter type
    c) Return type
    d) Function body
  10. Which statement is false about Swift functions?
    a) Functions must have a return type
    b) Functions can return nothing
    c) Functions can have no parameters
    d) Functions can call themselves

Function Parameters and Return Types (Questions 11-17)

  1. Can a Swift function have multiple parameters?
    a) Yes
    b) No
  2. What separates parameters in a Swift function definition?
    a) Space
    b) Colon
    c) Comma
    d) Semicolon
  3. Which of the following is the correct way to pass two parameters to a function?swiftCopy codefunc sum(a: Int, b: Int) -> Int { return a + b } a) sum(a:2, b:3)
    b) sum(2,3)
    c) sum a:2, b:3
    d) sum(2, b=3)
  4. Can Swift functions return multiple values?
    a) Yes, using tuples
    b) No, only one value
  5. What is the return type of the function below?swiftCopy codefunc greet(name: String) -> String { return "Hello, \(name)" } a) Void
    b) String
    c) Int
    d) Error
  6. Which keyword is used for explicitly returning a value in Swift?
    a) output
    b) yield
    c) return
    d) send
  7. What happens if a function declares a return type but doesn’t include a return statement?
    a) Compile-time error
    b) Returns nil
    c) Infers a default value
    d) Executes successfully

Default Parameters and Argument Labels (Questions 18-24)

  1. How do you set a default parameter value in Swift?
    a) param: Type = value
    b) param=Type value
    c) param -> Type = value
    d) param Type: value
  2. What does an argument label do in Swift functions?
    a) Specifies argument data type
    b) Changes the order of arguments
    c) Provides external names for parameters
    d) Allows omission of parameters
  3. Can a parameter in Swift have both an argument label and a parameter name?
    a) Yes
    b) No
  4. What does this function call output?swiftCopy codefunc greet(person name: String) { print("Hello, \(name)!") } greet(person: "Alice") a) Error
    b) Prints “Hello, Alice!”
    c) Prints “Hello, person!”
    d) Prints “Hello, !”
  5. Which statement about default parameters is true?
    a) They must always be the last parameter
    b) They can be overridden during the function call
    c) They throw an error if not explicitly passed
    d) They must be integers
  6. Can argument labels be omitted during a function call?
    a) Yes, using _ in the function definition
    b) No
  7. How can you call a function with a default parameter without overriding the default value?
    a) Omit the argument during the call
    b) Pass nil explicitly
    c) Use _ as the argument
    d) Leave the function call incomplete

Variadic Functions (Questions 25-30)

  1. What is a variadic parameter in Swift?
    a) A parameter with a default value
    b) A parameter that accepts a variable number of values
    c) A parameter with a fixed range of values
    d) A parameter of type Any
  2. How do you define a variadic parameter in Swift?
    a) Using *
    b) Using ...
    c) Using ,
    d) Using var
  3. What is the type of a variadic parameter in Swift?
    a) Array
    b) Tuple
    c) Dictionary
    d) Set
  4. Can a function have multiple variadic parameters?
    a) Yes
    b) No
  5. What is the output of the following code?swiftCopy codefunc sum(numbers: Int...) -> Int { return numbers.reduce(0, +) } print(sum(numbers: 1, 2, 3)) a) Error
    b) 6
    c) 123
    d) 0
  6. Can a variadic parameter be empty during a function call?
    a) Yes
    b) No

Answer Key

QNoAnswer (Option with Text)
1a) func name() {}
2b) func
3c) Function name followed by parentheses
4a) Yes
5a) Prints “Hello!”
6a) Yes
7b) Int
8b) Default return type is Void
9c) Return type
10a) Functions must have a return type
11a) Yes
12c) Comma
13a) sum(a:2, b:3)
14a) Yes, using tuples
15b) String
16c) return
17a) Compile-time error
18a) param: Type = value
19c) Provides external names for parameters
20a) Yes
21b) Prints “Hello, Alice!”
22b) They can be overridden during the function call
23a) Yes, using _ in the function definition
24a) Omit the argument during the call
25b) A parameter that accepts a variable number of values
26b) Using ...
27a) Array
28b) No
29b) 6
30a) Yes

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