Swift interview questions

Swift quiz questions

  • 1.

    When this code is executed, how many items will the result array contain?

    let numbers = [1, 3, 5, 7, 9]
    let result = numbers.filter { $0 >= 5 }

    Correct answer: 3.

    Explanation: The call to filter will return items from the numbers array only if they are greater than or equal to 5.

    View
  • 2.

    What output will be produced by the code below?

    func greet(names: String...) {
        print("Criminal masterminds:", names.joined(separator: ", "))
    }
    
    greet(names: "Malcolm", "Kaylee", "Zoe")

    Correct answer: "Criminal masterminds: Malcolm, Kaylee, Zoe".

    Explanation: The greet() method is declared as accepting a variadic string parameter, which means it should be called using one or more strings that will be made accessible as an array inside the function. The greet() method takes that array, joins it using commas, then prints it out as part of a larger message.

    View
  • 3.

    What output will be produced by the code below?

    func greet(_ name: inout String) {
        name = name.uppercased()
        print("Greetings, \(name)!")
    }
    
    var name = "Mal"
    greet(name)
    print("Goodbye, \(name)!")

    Correct answer: This code will not compile.

    Explanation: The greet() function declares its name parameter to be inout, which means it must be passed in using &. The code should be written greet(&name).

     

    View
  • 4.

    What output will be produced by the code below?

    var crew = ["Captain": "Malcolm", "Doctor": "Simon"]
    crew = [:]
    print(crew.count)

    Correct answer: 0.

    Explanation: [:] is shorthand syntax for "an empty dictionary", which causes the crew dictionary to be wiped.

     

    View
  • 5.

    When this code is executed, what will the numbers constant contain?

    let numbers = [1, 2, 3].map { [$0, $0] }

     

    Correct answer: [[1, 1], [2, 2], [3, 3]].

    Explanation: The code loops over every number in the numbers array, and creates a new array for each number that contains that number twice. So, 1 will be converted to [1, 1] and so on.

     

    View
  • 6.

    When this code is executed, what value will num have?

    let num = UInt.min

    Answer:

    Correct answer: 0.

    Explanation: UInt means "unsigned integer", which is a whole number that cannot be less than zero.

    View
  • 7.

    What output will be produced by the code below?

    for i in 1...3 {
        print(i)
    }

    Answer:

    Correct answer: 1, 2, 3.

    Explanation: This uses the closed range operator (...) to loop from 1 to 3 inclusive.

    View
  • 8.

    What output will be produced by the code below?

    for i in 3...1 {
        print(i)
    }

    Answer:

    Correct answer: This code will compile but crash.

    Explanation: This code will be compiled successfully, but crash at runtime: Swift does not allow you to generate ranges where the initial value is greater than the end value.

    View
  • 9.

    What output will be produced by the code below?

    let names = ["Chris", "Joe", "Doug", "Jordan"]
    
    if let name = names[1] {
        print("Brought to you by \(name)")
    }

    Answer:

     

    Correct answer: This code will not compile.

    Explanation: Subscripting an array of strings will return a String rather than a String?, which means it is a compile error to attempt to unwrap it using if let.

     

    View
  • 10.

    What output will be produced by the code below?

    let oneMillion = 1_000_000
    let oneThousand = oneMillion / 0_1_0_0_0
    print(oneThousand)

    Answer:

    Correct answer: 1000.

    Explanation: Swift allows you to use any number of leading zeroes before a number, and any number of underscores inside a number, in order to make reading easier. The example given, 0_1_0_0_0, is unlikely, but a perfectly valid way to write 1000.

    View
  • 11.

    What output will be produced by the code below?

    let names = ["Serenity", "Sulaco", "Enterprise", "Galactica"]
    
    for name in names where name.hasPrefix("S") {
        print(name)
    }

    Answer:

     "Serenity", "Sulaco".

    Explanation: The where condition for the loop will ensure that only names that start with the letter S will used inside the loop.

     

    View
  • 12.

    What output will be produced by the code below?

    let i = 3
    
    switch i {
    case 1:
        print("Number was 1")
    case 2:
        print("Number was 2")
    case 3:
        print("Number was 3")
    }

    Answer:

    Correct answer: This code will not compile.

    Explanation: Swift requires all switch statements to be exhaustive. This code will not compile because it does not have a default clause.

    View
  • 13.

    When this code is executed, what will the third constant contain?

    let first = ["Sulaco", "Nostromo"]
    let second = ["X-Wing", "TIE Fighter"]
    let third = first + second

    Answer:

    Correct answer: "Sulaco", "Nostromo", "X-Wing", "TIE Fighter".

    Explanation: Swift arrays can be joined together using the + operator, which adds the right-hand array to the end of the left-hand.

    View
  • 14.

    What output will be produced by the code below?

    var i = 2
    
    repeat {
        i *= i * 2
    } while i < 100
    
    print(i)

    Answer: 

    128.

    Explanation: Each time the loop goes around, the i is doubled then multiplied by itself. The first time through the loop it will be 8, and the second time it will be 128, at which point the loop will exit and print 128.

     

    View
  • 15.

    What output will be produced by the code below?

    enum Weather {
        case sunny
        case cloudy
        case windy(speed: Int)
    }
    
    let today: Weather = .windy(speed: 10)
    
    switch today {
    case .sunny, .cloudy:
        print("It's not that windy")
    case .windy(let speed) where speed >= 10:
        print("It's very windy")
    default:
        print("It's a bit windy")
    }

    Answer:

    Correct answer: "It's very windy".

    Explanation: The windy case value has an associated value to store the wind speed as an integer. In the code, this is set to 10, which means the "It's very windy" case will be triggered in the switch block.

     

    View
  • 16.

    Once this code is executed, how many items will numbers contain?

    var numbers = [1, 2, 3]
    numbers += [4]

    Answer:

    Correct answer: 4.

    Explanation: The += operator, when applied to array, serves to append one array to another in place. In this case, it appends an array containing one item (4) to the existing array of three items.

    View
  • 17.

    How many bits are used to store an Int?

    Answer:

    Correct answer: It depends on the device.

    Explanation: Swift's integers are 32-bit on 32-bit devices such as iPhone 5 and earlier, and 64-bit on 64-bit devices such as iPhone 5s and later.

    View
  • 18.

    What output will be produced by the code below?

    import Foundation
    let number = 16.0
    print("\(number) squared is \(number * number), and its square root is \(sqrt(number))")

    Answer:

     

    Correct answer: "16.0 squared is 256.0, and its square root is 4.0".

    Explanation: Using its type inference, Swift will consider number to be an Double, which will be interpolated correctly into the string even when sqrt() is called. When Doubles are interpolated into strings, they have .0 appended to their values when they have no fractional digits.

    View
  • 19.

    What output will be produced by the code below?

    import Foundation
    let number = 16.0
    print("\(number) squared is \(number * number), and its square root is \(sqrt(number))")

    Answer:

    Correct answer: "16.0 squared is 256.0, and its square root is 4.0".

    Explanation: Using its type inference, Swift will consider number to be an Double, which will be interpolated correctly into the string even when sqrt() is called. When Doubles are interpolated into strings, they have .0 appended to their values when they have no fractional digits.

    View
  • 20.

    When this code is executed, what is the value of the swift string?

    import Foundation
    let ns = NSString("Hello")
    let swift = String(ns)

    Answer:

    Correct answer: This code will not compile.

    Explanation: While it is possible to typecast an NSString into a String using an initializer without a label, it is not possible the other way around. The code should read let ns = NSString(string: "Hello").

    View

© 2017 QuizBucket.org