Swift Basic Operators Quiz

Topics : Ternary, Nil-Coalescing, Range, and Logical

Swift Basic Operators

6 mins read

1. What is the output of the following code snippet?

 let x = true
let y = false
let z = x && y || x
print(z)

2. What is the value of "finalColor" in the following code snippet?

 let colorName: String? = “blue”
let defaultColor = “red”
let finalColor = colorName ?? defaultColor

3. What is the value of rowHeight in the following code snippet?

 let contentHeight = 40
let hasHeader = true
let rowHeight = contentHeight + (hasHeader ? 50 : 20)

4. What is the output of the following code snippet ?

 let range =5
print(range.contains(4))
print(range.contains(7))

5. What is the output of the following code snippet?

 let names = [“Anna”, “Alex”, “Brian”, “Jack”]
let count = names.count

for i in 2..<count {
print(names[i])
}

6. What is the output of the following code snippet?

 let range = ..<5
print(range.contains(5))

7. What is the output of the following code snippet?

 let names = [“Alice”, “Bob”, “Charlie”, “David”, “Eve”]
let selectedNames = names[13]
print(selectedNames.count)