Swift Coding MCQ Questions and Answers

Welcome to Swift Programming Coding MCQ Questions. Here, we present 20 MCQs (Multiple-Choice Questions) with code snippets to test your understanding of Swift programming coding and logical stills. Let's get started!

1. What will be the output of the following Swift program?

var numbers = [Int]()
numbers.append(1)
numbers.append(2)
numbers.append(3)
print(numbers.count)
a) 3
b) 2
c) Compilation error
d) Runtime error

2. What does this Swift code snippet output?

let x = 8
let y = 3
print(x % y)
a) 2
b) 2.67
c) 3
d) None of the above

3. Consider the following Swift code. What will it print?

func modify(_ array: inout [Int]) {
    array.append(4)
}
var myArray = [1, 2, 3]
modify(&myArray)
print(myArray)
a) [1, 2, 3]
b) [1, 2, 3, 4]
c) Compilation error
d) Runtime error

4. What is the output of the following Swift function?

let s1 = "hello"
let s2 = "hello"
print(s1 === s2)
a) true
b) false
c) Error
d) nil

5. What will this Swift program output?

let numberString: String? = "5"
print(Int(numberString!) + 5)
a) 10
b) "10"
c) "55"
d) Compilation error

6. Analyze the following Swift code snippet. What is its output?

let value: Double = 10.5
print(type(of: value))
a) Double
b) Float
c) Int
d) Number

7. Consider the following Swift code. What will it print?

var i = 0
print(i += 1)
a) 0
b) 1
c) Compilation error
d) nil

8. What does this Swift code output?

let x = 5
print(x > 3 ? "Yes" : "No")
a) Yes
b) No
c) 3
d) 5

9. What will the following Swift code print?

let numbers = [1, 2, 3]
print(numbers.contains(2))
a) true
b) false
c) 1
d) Error

10. What does the following Swift program output?

var x = 5
var y = 10
x = y
print(x)
a) 5
b) 10
c) 15
d) None of the above

11. Analyze the following Swift code snippet. What will it output?

func greet(_ name: String, _ day: String) -> String {
    return "Hello \(name), today is \(day)."
}
print(greet("Bob", "Tuesday"))
a) Hello Bob, today is Tuesday.
b) Compilation error
c) Runtime error
d) "greet("Bob", "Tuesday")"

12. What will the following Swift code output?

let result = ["one", "two", "three"].map { $0.uppercased() }
print(result)
a) ["ONE", "TWO", "THREE"]
b) ["one", "two", "three"]
c) ["One", "Two", "Three"]
d) Error

13. Consider the following Swift code. What will it print?

var optionalString: String? = "Optional"
var greeting = "Hello, \(optionalString ?? "Anonymous")"
print(greeting)
a) Hello, Optional
b) Hello, Anonymous
c) Hello, nil
d) Compilation error

14. What is the output of the following Swift function?

func increment(_ number: inout Int) {
    number += 1
}
var myNum = 1
increment(&myNum)
print(myNum)
a) 1
b) 2
c) Error
d) nil

15. What will this Swift program output?

let languages = ["Swift", "Objective-C"]
if languages.contains("Swift") {
    print("Swift exists!")
    } else {
        print("No Swift")
    }
a) Swift exists!
b) No Swift
c) Error
d) nil

16. What does the following Swift code snippet output?

let numbers = [1, 2, 3]
let sum = numbers.reduce(0, +)
print(sum)
a) 6
b) 0
c) 3
d) Error

17. Consider the following Swift code. What will it print?

let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names[2...] {
    print(name)
}
a) Brian Jack
b) Anna Alex Brian Jack
c) Alex Brian Jack
d) Brian

18. What does the following Swift code output?

var score = 85
switch score {
    case 90...100:
    print("Excellent")
    case 80..<90:
    print("Good")
    default:
    print("Average")
}
a) Excellent
b) Good
c) Average
d) Error

19. What will the following Swift code print?

let data = [1, 2, 3, 4, 5]
let filteredData = data.filter { $0 % 2 == 0 }
print(filteredData)
a) [1, 2, 3, 4, 5]
b) [2, 4]
c) [1, 3, 5]
d) []

20. What does this code snippet output?

let optionalInt: Int? = nil
let result = optionalInt ?? 0
print(result)
a) nil
b) 0
c) Error
d) None of the above

Comments