Swift : Programming Language

Skill Test Set 3: Basic operators, Branch, Repetitions

  1. What would be the output of following code snippet in Swift?

    var a = 12
    if a >= 12
    print("Good after noon")
    else
    print("Good Morning!")

    Good after noon
    Good Morning!
    Compiler error
    12

  2. What would be the output of following code snippet in Swift?

    var fullUrl = 1
    if (fullUrl) {
    print("http://www.codingbull.com")
    } else {
    print("codingbull.com")
    }

    http://www.codingbull.com
    codingbull.com
    Compiler error
    Runtime crash

  3. What set of keywords is most commonly used to iterate over a collections of items?
    for each
    switch case
    repeat while
    for in

  4. Which is not a control transfer statement is Swfit?
    goto
    fallthrough
    break
    return
    continue

  5. Which keyword in the context of a Switch statement is required to force the execution of a subsequent case?
    fallthrough
    continue
    break
    return

  6. Which of the following could be used to loop through the range of numbers 5-12 including 12?
    for i in 512
    for i in 5,i>12,i++
    for i in 5..<12
    for i in 4..<12

  7. To loop through a range of the numbers 1-9 without using 9, you would write…
    for i in 0…8
    for i in 0>..9
    for i in 1<..8
    for i in 1..<9

  8. What would be the output of following swift code snippet?

    var i = 10
    repeat {
    print(, separator: " ", terminator: ",")
    i -= 1
    } while i > 0

    10,9,8,7,6,5,4,3,2,1,
    10,9,8,7,6,5,4,3,2,1
    10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
    10, 9, 8, 7, 6, 5, 4, 3, 2, 1

  9. Which of these operators is used to check whether or not two instances are identical?
    ==
    =
    equalsTo
    ===
    identicalTo

  10. Which keyword in the context of Swift statement is required to force the execution of a subsequent case?
    Fallthrough
    Continue
    Release
    DropEnd

  11. Which of these is a valid syntax for iterating through the keys and values of a dictionary:

    let dictionary = [keyOne : valueOne, keyTwo : valueTwo]

    for (key, value) in dictionary { println("Key: \(key) Value: \(value)") }
    for (key, value) in enumerate(dictionary) { println("Key: \(key) Value: \(value)") }
    for (key, value) in (dictionary.keys, dictionary.values) { println("Key: \(key) Value: \(value)") }
    for (key, value) in dictionary.enumerate() { println("Key: \(key) Value: \(value)") }

  12. What is the output of this segment of code?

    var x = 0
    for index in 1...5 {
    ++x
    }
    println("\(x)")

    0
    compile error
    5
    4

  13. 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 {…} >

Note: Answers are not given intentionally.