Swift - Programming Language

Skill Test Set 6: Closures, Enum, Generics

  1. Which keyword do you use to declare enumeration?
    enum
    enumeration
    Enum
    NSEnum

  2. When declaring an enumeration, multiple member values can appear on a single line, separated by which punctuation mark?

    ; [Semi-colon]
    : [Colon]
    , [Comma]
    ? [Question mark]

  3. Choose the correct syntax for generic function :

    func genericFunc(argument: T) { }
    func genericFunc(argument) { }
    generic func genericFunc(argument: T) { }
    func genericFunc(argument: T) { }

  4. Which of these is a valid definition of a generic function that incorporates inout parameters in Swift?

    func swap(inout a: T, inout b: T) { let temp = a a = b b = temp }
    func swap(inout a: U, inout b: T) { let temp = a a = b b = temp }
    func swap( a: U, b: T) { let temp = a a = b b = temp }
    func swap( a: T, b: T) { let temp = a a = b b = temp }

  5. Which of the followings could be used to indicate the Function Type of the following function:

    func joinStrings(stringOne: String, stringTwo: String) -> String {
    return stringOne + stringTwo
    }
    func(String, String -> String)
    (String, String) -> String
    {String, String} -> String
    {String, String}(String)

  6. How do closures capture references to variables by default?
    By weak reference.
    By strong reference.
    By unowned reference.
    By copy.

  7. Which keyword is used on a function inside an Enumeration to indicate that the function will modify self?

    modifier
    mutating
    mutable
    mod
    mut


  8. Swift extensions are similar to categories in Objective-C except :
    Swift extension might have a specific name.
    Swift extension does not add functionality to previously defined types.
    Swift can override methods from original type.
    Swift extensions are not named.

  9. If we have a class named MyClass with a nested enum called Status, declared like so:

    class MyClass {
    enum Status {
    case On, Off
    }
    }

    How would one indicate that a variable is an enum of type Status outside the context of MyClass?

    var status: MyClass.Status = .On
    var status: Status = .On
    var status: MyClass = .On
    var status: MyClass(Status) = .On


  10. Which one of the following types can be used as raw value types for an enumeration?

    Bool
    Array
    Int, String, Float
    Dictionary

  11. Swift extensions are similar to categories in Objective-C except :
    Swift extension might have a specific name.
    Swift extension does not add functionality to previously defined types.
    Swift can override methods from original type.
    Swift extensions are not named.

  12. What is a trailing closure?
    A closure expression that is called directly after another closure expression.
    A closure expression that is written outside of (and after) the parentheses of the function call is supports.
    A closure expression that is declared within the scope of another closure expression.
    A closure expression that is declared as the property of an object.

  13. Choose the answer that declares an optional closure.
    < var closureName: (parameterTypes) -> (returnType) >
    < typealias closureType: (parameterTypes) -> (returnType) >
    < var closureName: ((parameterTypes) -> (returnType)) ?>
    < let closureName: (closureTypes) = { ... } >

  14. How could the following closure be rewritten to use shorthand arguments? s2})>
    < reversed = sorted(names, { $0 ,$1 in $0 > $1 } ) >
    < reversed = sorted(names, { $0 > $1 } ) >
    < reversed = sorted(names, { $0 ,$1 } ) >
    < reversed = sorted( { $0 > $1 } ) >

  15. Which of the following statements is true regarding Swift closures and functions?
    Functions and Closures are not related.
    A Function is a Closure declared within the scope of a Class.
    A Function is a named Closure.
    Closures can’t be used as arguments, Functions can.

  16. What symbol is used like a tuple to access arguments in Abbreviated Swift Closure syntax?
    $
    *
    &
    @
    ~

  17. How could you call the following function that takes a closure as an argument using trailing closure syntax:
    ()) { // function body goes here }>
    < funcWithClosure ({
    //closure’s body goes here
    })>
    <func funcWithClosure ({
    //closure’s body goes here
    })>
    < funcWithClosure() {
    //closure’s body goes here
    }>
    < funcWithClosure {
    //closure’s body goes here
    ) >

  18. What is the name of the Swift language feature that Objective-C Blocks are translated into?
    Lambda
    Callback
    Closure
    Blocks

  19. Which is correct regarding Swift enumeration members when they are defined?
    Members are assigned a default integer value.
    Members are assigned a random default integer value.
    Members are not assigned default integer values.

  20. How can we use optional binding to determine if the variable string is not nil?
    < if let str = string {…} >
    < if string {…} >
    < if string as String {…} >
    < if let string {…} >

  21. Let’s assume “numbers” is an array of unsorted integers. Which of these could be used to sort numbers?
    numbers.sort({$0, $1 in $0 > $1})
    numbers.sort({s1 > s2})
    numbers.sort({$0 > $1})
    numbers.sort(){s1 > s2}

  22. Which of these could be an appropriate protocol declaration in Swift?
    @objc protocol someProtocal { optional var first: Int { get } }
    @objc protocol someProtocal { optional var first: Int { set } }
    protocol someProtocal { optional var first: Int { get } }
    protocol someProtocal { var first: Int { set } }

Note: Answers are not given intentionally.