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)
What is the correct syntax to define a function in Swift? a) func name() {} b) function name() {} c) def name() {} d) function name(): {}
Which keyword is used to define a function in Swift? a) define b) func c) function d) declare
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
In Swift, can a function call another function? a) Yes b) No
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
Can Swift functions exist without parameters? a) Yes b) No
What is the return type of the following function?swiftCopy codefunc test() -> Int { return 5 } a) Void b) Int c) String d) Float
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
What does the -> symbol indicate in a function declaration? a) Function name b) Parameter type c) Return type d) Function body
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)
Can a Swift function have multiple parameters? a) Yes b) No
What separates parameters in a Swift function definition? a) Space b) Colon c) Comma d) Semicolon
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)
Can Swift functions return multiple values? a) Yes, using tuples b) No, only one value
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
Which keyword is used for explicitly returning a value in Swift? a) output b) yield c) return d) send
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)
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
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
Can a parameter in Swift have both an argument label and a parameter name? a) Yes b) No
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, !”
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
Can argument labels be omitted during a function call? a) Yes, using _ in the function definition b) No
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)
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
How do you define a variadic parameter in Swift? a) Using * b) Using ... c) Using , d) Using var
What is the type of a variadic parameter in Swift? a) Array b) Tuple c) Dictionary d) Set
Can a function have multiple variadic parameters? a) Yes b) No
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
Can a variadic parameter be empty during a function call? a) Yes b) No
Answer Key
QNo
Answer (Option with Text)
1
a) func name() {}
2
b) func
3
c) Function name followed by parentheses
4
a) Yes
5
a) Prints “Hello!”
6
a) Yes
7
b) Int
8
b) Default return type is Void
9
c) Return type
10
a) Functions must have a return type
11
a) Yes
12
c) Comma
13
a) sum(a:2, b:3)
14
a) Yes, using tuples
15
b) String
16
c) return
17
a) Compile-time error
18
a) param: Type = value
19
c) Provides external names for parameters
20
a) Yes
21
b) Prints “Hello, Alice!”
22
b) They can be overridden during the function call
23
a) Yes, using _ in the function definition
24
a) Omit the argument during the call
25
b) A parameter that accepts a variable number of values