Swift interview questions

Swift quiz questions

  • 1.

    Is it compatible with iOS 6 & 7?

    Answer:

    Swift can be run on iOS 7, but not iOS 6.

    View
  • 2.

    What other languages is it similar to?

    Answer:

    Swift is probably most similar in look and feel to Ruby or Python. Though you’ll also probably recognize some C syntax.

    View
  • 3.

    How stable is Swift?

    Answer:

    As with any new language, there is a potential for bugs. While you may encounter some trouble with the Swift language, the majority of issues were addressed before the 1.0 release.

    The thing to most look out for is changes to the Swift language during each update. For example, when updating from 1.0 to 1.1, Apple introduced a new feature: failable initializers. You can expect that the language will change as more people use it and give feedback to Apple. Stay apprised of changes using the revision history for The Swift Programming Language.

    View
  • 4.

    Why was there a need to go from Objective-C to Swift?

    Answer:

    As mention in the answer to question five, after 20 years, Objective-C was starting to show its age. Plus, Objective-C is a difficult language for new programmers to learn, so the barrier to entry is pretty high. Swift provides a modern language tailor-made for Apple hardware.

    View
  • 5.

    Can I create an app in Swift and submit it to the app store?

    Answer:

    Absolutely! In fact, you were able to as soon as Xcode 6 and iOS 8 launched.

    View
  • 6.

    Why did Apple decide to create a new language?

    Answer:

    Objective-C has been Apple’s primary programming language for app writing since OS X was created. In that time, programming languages and practices changed drastically, especially in mobile development. Rather than adopt a new, already existing language, Apple created a new language tailored specifically for development on their own hardware.

    View
  • 7.

    Is it fast?

    Answer:

    Apple boasts that Swift is up to 2.6x faster than Objective-C and 8.4x faster than Python 2.7. And why should you care about how quickly code executes? Well, faster running code makes for more efficient and smoother running apps, which makes for a better experience for your user.

    View
  • 8.

    How easy is Swift to learn?

    Answer:

    Swift was designed to be friendly for new programmers, and as a result it is incredibly easy to learn. According to Apple, Swift is the “first industrial-quality systems programming language that is as expressive and enjoyable as a scripting language.” Some have even called Swift the new BASIC.

    View
  • 9.

    Should I learn Swift or Objective-C?

    Answer:

    Whether you should learn Swift or Objective-C was the basis of a discussion back in September, and the answer has not changed – Swift! Apple has made it clear that Swift is the cornerstone of the future of iOS development. Plus, you can still utilize Objective-C files alongside Swift code, so you won’t miss out on any pre-existing libraries and code.

    View
  • 10.

    Why is the language called Swift?

    Answer:

    We could make the obvious popular culture joke, but in all seriousness, the language was designed with two goals in mind:

    • swift to code
    • swift to execute

    In terms of speed, Swift uses the LLVM compiler, and compiles Swift code to optimized native code depending on target device. In terms of learning curve, the Swift syntax was designed to be clean and easy to read.

    View
  • 11.

    More Questions to Come as Swift Evolves

    Answer:

    You might be thinking, “Wow. This article is pretty short. Only 5 questions? Really?” Well, that’s because Swift and its community are still growing. It’s a baby compared to programming languages like Objective-C and C. In fact, many of the programmers/employers we reached out to had trouble coming up with a high number of Swift interview questions. However, this will change as Swift evolves. If you go into an iOS job interview and have to answer a question that is not in this article, please tell us what it was! That way we can update our list and make sure other developers don’t get surprised while they’re trying to land a job.

    View
  • 12.

    Would you consider yourself a Swift expert?

    Answer:

    This is a trick question. Because Swift is still being improved, the only true “Swift Experts” are the people who developed it at Apple. Even if you’ve produced lots of great apps and mastered a number of advanced courses and tutorials, you simply can’t be an expert at this point. Remember, Swift has only been around since June. If your interviewer asks this, argue that you could not possibly be an expert until the language is finished. Explain how you, like most other iOS Developers, are constantly learning.

    View
  • 13.

    Do you have any experience coding in Swift? If so, how did you like it?

    Answer:

    If you’ve produced functional apps in Swift besides the basic ones they teach you in tutorials, be ready to display them and describe your experience coding them. If not, that’s ok. However, make sure you have at least fiddled around with Swift so that you have enough knowledge for the interview. If for the issue of liking it or not, be honest even if you prefer Objective-C. Don’t be overly negative or appear unwilling to improve your skills in it if necessary.

    View
  • 14.

    Should the newest generation of developers still learn Objective-C?

    Answer:

    For now, the safest answer is “Absolutely Yes! I think developers should learn both Objective-C and Swift.” Even if you believe that Swift will completely replace Objective-C in the near future, you may appear cocky if you say that new developers should not bother learning Objective-C. After all, your employer may use Objective-C or at least have employees that rely heavily on it. Remember that Swift, while full of potential, is still not completely finalized. Objective-C is the standard for professional iOS development at the moment.

    View
  • 15.

    What are Cocoa and Cocoa Touch and how are they relevant to Swift?

    Answer:

    Cocoa and Cocoa Touch are frameworks that run on OS X and iOS respectively. Both are primarily implemented with Objective-C and integrated with Xcode (you should also be able to explain Xcode if you get a question on that). They consist of libraries, APIs, and runtimes. Because Swift is an iOS language, it also works with Cocoa and Cocoa Touch. Read the Apple Developer page on Cocoa for more info.

    Getting Your Opinion on Swift

    View
  • 16.

    What is Swift?

    Answer:

    For a more thorough definition and history, check out our previous article introducing Swift.

    We won’t be able to concisely train you on all the technical terms you may need during this interview. However, we can tell you that you’ll need an understanding of Objective-C (and even C to an extent) in order to properly explain what Swift is. This is because, as Craig Federighi, Apple’s Senior Vice President of Software Engineering, once said, Swift was designed to be like “Objective-C without the baggage of C.” Swift condenses code in Objective-C, works parallel to it, eliminates the possibility of many common Objective-C programming errors, adds new features, and offers an interface that is more user-friendly. Be ready to explain the most noteworthy of these new features.

    View
  • 17.

    The following code snippet results in a compile time error:

    struct IntStack {
      var items = [Int]()
      func add(x: Int) {
        items.append(x) // Compile time error here.
      }
    }
    

    Explain why a compile time error occurs. How can you fix it?

    Answer:

    Structures are value types. By default, the properties of a value type cannot be modified from within its instance methods.

    However, you can optionally allow such modification to occur by declaring the instance methods as ‘mutating’; e.g.:

    struct IntStack {
      var items = [Int]()
      mutating func add(x: Int) {
        items.append(x) // All good!
      }
    }
    View
  • 18.

    Consider the following code:

    class Master {
        lazy var detail: Detail = Detail(master: self)
        
        init() {
            println("Master init")
        }
        
        deinit {
            println("Master deinit")
        }
    }
    
    class Detail {
        var master: Master
        
        init(master: Master) {
            println("Detail init")
            self.master = master
        }
        
        deinit {
            println("Detail deinit")
        }
    }
    
    func createMaster() {
        var master: Master = Master()
        var detail = master.detail
    }
        
    createMaster()
    

    What is the bug and how does it affect memory? How can it be fixed?

    Answer:

    There is a strong reference cycle between Master and Detail, with Master creating an instance of Detail and storing a reference to it, and Detail storing a reference to the instance of its Master creator. In both cases, references are strong, which means that none of the 2 instances will ever be deallocated, causing a memory leak.

    To solve the problem it’s necessary to break at least one of the 2 strong relationships, by using either the weak or unowned modifier. The differences between the 2 modifiers is:

    • unowned: the reference is assumed to always have a value during its lifetime - as a consequence, the property must be of non-optional type.
    • weak: at some point it’s possible for the reference to have no value - as a consequence, the property must be of optional type.

    In the above code example, the proper solution is to define in Detail the reference to Master as unowned:

    class Detail {
        unowned var master: Master
        ...
    }
    View
  • 19.

    Consider the following code:

    struct Planet {
        var name: String
        var distanceFromSun: Double
    }
    
    let planets = [
        Planet(name: "Mercury", distanceFromSun: 0.387),
        Planet(name: "Venus", distanceFromSun: 0.722),
        Planet(name: "Earth", distanceFromSun: 1.0),
        Planet(name: "Mars", distanceFromSun: 1.52),
        Planet(name: "Jupiter", distanceFromSun: 5.20),
        Planet(name: "Saturn", distanceFromSun: 9.58),
        Planet(name: "Uranus", distanceFromSun: 19.2),
        Planet(name: "Neptune", distanceFromSun: 30.1)
    ]
    
    let result1 = planets.map { $0.name }
    let result2 = planets.reduce(0) { $0 + $1.distanceFromSun }
    

    What are the types and values of the result1 and result2 variables? Explain why.

    Answer:

    result1 is an array of strings, containing the list of the planet names result2 is a double, calculated as the sum of the distance of all planets

    The map method of the Array<T> struct type performs a transformation of the source array into an array of another type, whose values are obtained by executing the closure passed as parameter to each element of the array. In the above code, the closure returns the name property, so the map method in the above code returns an array of planet names.

    Given an initial value and a closure, the reduce method of the Array<T> struct type returns a single value obtained by recursively applying the closure to each element of the array. The closure takes the value calculated at the previous step (or the initial value if it’s the first iteration) and the current array element, and is expected to return a value of the same type of the initial value.

    In the above code, the closure returns the sum of what calculated at the previous step, plus the value of the distanceFromSun property for the current element. The end result is the sum of the distances of all planets.

    View
  • 20.

    Swift defines the AnyObject type alias to represent instances of any reference type, and it’s internally defined as a protocol.

    Consider the following code:

    var array = [AnyObject]()
    struct Test {}
    array.append(Test())
    

    This code generates a compilation error, with the following error message:

    Type 'Test' does not conform to protocol 'AnyObject'
    

    The failure is obvious because a struct is a value and not a reference type, and as such it doesn’t implement and cannot be cast to the AnyObject protocol.

    Now consider the following code:

    var array = [AnyObject]()
    array.append(1)
    array.append(2.0)
    array.append("3")
    array.append([4, 5, 6])
    array.append([7: "7", 8: "8"])
    
    struct Test {}
    array.append(Test())
    

    The array array is filled in with values of type respectively int, double, string, array and dictionary. All of them are value types and not reference types, and in all cases no error is reported by the compiler. Why?

    Answer:

    The reason is that swift automatically bridges:

    • number types to NSNumber
    • strings to NSString
    • arrays to NSArray
    • dictionaries to NSDictionary

    which are all reference types.

    View

© 2017 QuizBucket.org