Swift Strings and Characters Quiz

Topics: String literals, Characters, and String manipulations

Swift Quiz : Swift Strings and Characters

6 mins read

1. What is the Output?

 let str = "Hello, World!"
let substring = str.prefix(5)
print(substring)

2. What is the Output?

 let str = #"This is a "quoted" string."#
print(str)

3. What is the Output?

  let str = "Hello, World!"
let count = str.components(separatedBy: "o").count - 1
print(count)

4. What will be the value of the 'str' variable after executing the following code?

 var str = "Hello, World!"
str.remove(at: str.index(str.startIndex, offsetBy: 7))
print(str)

5. What is the output?

 let str = "Hello, World!"
let range = str.index(str.startIndex, offsetBy: 7)..<str.endIndex
let substring = str[range]
print(substring)

6. What is the output?

 let str = "Hello, World!"
let index = str.firstIndex(of: ",") ?? str.endIndex
let substring = str[..<index]
print(substring)

7. What is the output?

 let str = "Apple, Banana, Cherry"
let components = str.split(separator: ",")
let count = components.count
print(count)

8. Which String function can be used to remove the white spaces?

 let str = "   Hello, World!   "

9. What is the output?

  let number = 42
let str = String(number)
let result = str.count
print(result)

10. What is the output?

var str1 = "" 
var str2 = String()

if (str1 == str2) {
print("Equal")
} else {
print("Not Equal")
}