linkedin-skill-assessments-quizzes

Swift

Q1. What is this code an example of?

let val = (Double)6

Reference: The Swift Programming Language: Language Guide: The Basics: Constants and Variables

Q2. What is the error in this code?

let x = 5
guard x == 5 { return }

Reference: The Swift Programming Language: Language Guide: Control Flow: Early Exit

Q3. What is the raw/underlying type of this enum?

enum Direction {
  case north, south, east, west
}

Reference: The Swift Programming Language: Language Guide: Enumerations: Raw Values

Q4. Why is dispatchGroup used in certain situations?

Reference: Apple Developer: Documentation: Dispatch: Dispatch Group

Q5. What is this code an example of?

let val = 5
print("value is: \(val)")

Reference: The Swift Programming Language: Language Guide: Strings and Characters: String Interpolation

Q6. What are the contents of vals after this code is executed?

var vals = [10, 2]
vals.sort { (s1, s2) -> Bool in
  s1 > s2
}

Reference: Apple Developer: Documentations: Swift: Array: sort()

Q7. What does this code print?

typealias Thing = [String: Any]
var stuff: Thing
print(type(of: stuff))

Reference: The Swift Programming Language: Language Reference: Types: Type Identifier

Q8. What is the value of y?

let x = ["1", "2"].dropFirst()
let y = x[0]

explanation
dropFirst() from Swift.Collection.Array returns a type of ArraySlice<Element>
as in the documentation pages:
@inlinable public func dropFirst(_ k: Int = 1) -> ArraySlice<Element>

The ArraySlice type makes it fast and efficient for you to perform operations on sections of a larger array. Instead of copying over the elements of a slice to new storage, an ArraySlice instance presents a view onto the storage of a larger array. And because ArraySlice presents the same interface as Array, you can generally perform the same operations on a slice as you could on the original array.

Slices Maintain Indices
Unlike Array and ContiguousArray, the starting index for an ArraySlice instance isn’t always zero. Slices maintain the same indices of the larger array for the same elements, so the starting index of a slice depends on how it was created, letting you perform index-based operations on either a full array or a slice.
The above code returns a slice of value ["2"] but the index did not change. let y = x[1] would give the expected result.
To safely reference the starting and ending indices of a slice, always use the startIndex and endIndex properties instead of specific values.
Reference

Q9. What is the value of test in this code?

var test = 1 == 1

Reference: The Swift Programming Language: Language Guide: Basic Operators: Comparison Operators

Q10. What is the value of y?

var x: Int?
let y = x ?? 5

Reference: The Swift Programming Language: Language Guide: Basic Operators: Nil-Coalescing Operators

Q11. What is the type of this function?

func add(a: Int, b: Int) -> Int { return a+b }

Reference: The Swift Programming Language: Language Guide: Functions: Function Types

Q12. What is the correct way to call this function?

func myFunc(_ a: Int, b: Int) -> Int {
  return a + b
}

Reference: The Swift Programming Language: Language Guide: Functions: Function Argument Labels and Parameter Names

Q13. The Codable protocol is _?

References:

Q14. What is the type of value1 in this code?

let value1 = "\("test".count)"

Reference: The Swift Programming Language: Language Guide: Strings and Characters: String Interpolation

Q15. When a function takes a closure as a parameter, when do you want to mark is as escaping?

Reference: The Swift Programming Language: Language Guide: Closures: Escaping Closures

Q16. What’s wrong with this code?

class Person {
  var name: String
  var address: String
}

Reference: The Swift Programming Language: Language Guide: Initialization: Class Inheritance and Initialization

Q17. What is the value of names after this code is executed?

let names = ["Bear", "Joe", "Clark"]
names.map { (s) -> String in
  return s.uppercased()
}

Q18. What describes this line of code?

let val = 5

Reference: The Swift Programming Language: Language Guide: The Basics: Type Safety and Type Inference

Q19. What is the error in this code?

extension String {
  var firstLetter: Character = "c" {
    didSet {
      print("new value")
    }
  }
}

Reference: The Swift Programming Language: Language Guide: Extensions: Computed Properties

Q20. didSet and willSet are examples of _?

Reference: The Swift Programming Language: Language Guide: Properties

Q21. What is wrong with this code?

self.callback = {
  self.attempts += 1
  self.downloadFailed()
}

Reference: The Swift Programming Language: Language Guide: Automatic Reference Counting: Strong Reference Cycles for Closures

Q22. How many values does vals have after this code is executed?

var vals = Set<String> = ["4", "5", "6"]
vals.insert("5")

Reference: The Swift Programming Language: Language Guide: Collection Types: Sets

Q23. How can you avoid a strong reference cycle in a closure?

Reference: The Swift Programming Language: Language Guide: Automatic Reference Counting

Q24. What is wrong with this code?

if let s = String.init("some string") {
  print(s)
}

Reference: The Swift Programming Language: Language Guide: The Basics: Optionals

Q25. Which code snippet correctly creates a typealias closure?

Reference: The Swift Programming Language: Language Reference: Declarations: Type Alias Declaration

Q26. How do you reference class members from within a class?

Reference: The Swift Programming Language: Language Guide: Methods: Instance Methods

Q27. All value types in Swift are _ under the hood?

Reference: The Swift Programming Language: Language Guide: Structures and Classes

Q28. What is the correct way to add a value to this array?

var strings = [1, 2, 3]

Reference: The Swift Programming Language: Language Guide: Collection Types: Arrays

Q29. How many times will this loop be executed?

for i in 0...100 {
  print(i)
}

References:

Q30. What can AnyObject represent?

Reference: The Swift Programming Language: Language Guide: Type Casting: Type Casting for Any and AnyObject

Q31. What is the value of t after this code is executed?

let names = ["Larry", "Sven", "Bear"]
let t = names.enumerated().first().offset

References:

Q32. What is the value of test after this code executes?

let vt = (name: "ABC", val: 5)
let test = vt.0

References:

Q33. What is the base class in this code?

class LSN: MMM {
}

Reference: The Swift Programming Language: Language Guide: Inheritance: Subclassing

Q34. What does this code print to the console?

var userLocation: String = "Home" {
  willSet(newValue) {
    print("About to set userLocation to \(newValue)...")
  }

  didSet {
    if userLocation != oldValue {
      print("userLocation updated with new value!")
    } else {
      print("userLocation already set to that value...")
    }
  }
}

userLocation = "Work"

Reference: The Swift Programming Language: Language Guide: Properties: Property Observers

Q35. What must a convenience initializer call?

Reference: The Swift Programming Language: Language Guide: Initialization: Class Inheritance and Initialization

Q36. Which object allows you access to specify that a block of code runs in a background thread?

Reference: Apple Developer: Documentation: Dispatch: DispatchQueue

Q37. What is the inferred type of x?

let x = ["a", "b", "c"]

Reference: The Swift Programming Language: Language Guide: Collection Types: Arrays

Q38. What is the value of oThings after this code is executed?

let nThings: [Any] = [1, "2", "three"]
let oThings = nThings.reduce("") { "\($0)\($1)" }

Reference: Apple Developer: Documentation: Swift: Array: reduce(_:_:)

Q39. How would you call a function that throws errors and also returns a value?

Reference: The Swift Programming Language: Language Guide: Error Handling: Handling Errors

Q40. What is wrong with this code?

protocol TUI {
  func add(x1: Int, x2: Int) -> Int {
    return x1 + x2
  }
}

Reference:

Q41. In this code, what are wheels and doors examples of?

class Car {
  var wheels: Int = 4
  let doors = 4
}

Reference:

Q42. How do you designated a failable initializer?

Reference:

Q43. What is printed when this code is executed?

let dbl = Double.init("5a")
print(dbl ?? ".asString()")

Reference:

Q44. In the function below, what are this and toThat examples of?

func add(this x: Int, toThat y: Int) { }

Reference: The Swift Programming Language: Language Guide: Functions

Q45. What is wrong with this code?

for (key, value) in [1: "one", 2: "two"] {
  print(key, value)
}

Reference: The Swift Programming Language: Language Guide: Control Flow: For-In Loops

Q46. Which of these choices is associated with unit testing?

Reference:

Q47. In the code below, what is width an example of?

class Square {
  var height: Int = 0
  var width: Int {
    return height
  }
}

Reference:

Q48. What data type is this an example of?

let vals = ("val", 1)

Reference:

Q49. What is wrong with this code?

var x = 5
x = 10.0

Reference: The Swift Programming Language: Language Guide: The Basics

Q50. What will this code print to the console?

var items = ["a": 1, "b": 2, "c": "test"] as [String: Any]
items["c"] = nil
print(items["c"] as Any)

References:

Q51. What is wrong with this code?

let val = 5.0 + 10

Reference: The Swift Programming Language: Language Guide: The Basics: Type Safety and Type Inference

Q52. How many parameters does the initializer for Test have?

struct Test {
  var score: Int
  var date: Date
}

Reference: The Swift Programming Language: Language Guide: Initialization

Q53. What prints to the console when executing this code?

let x = try? String.init("test")
print(x)

References:

Q54. How can you sort this array?

var vals = [1, 2, 3]

Reference: Apple Developer: Documentation: Swift: Array: sort()

Q55. DispatchQueue.main.async takes a block that will be

Reference: Apple Developer: Documentation: Dispatch: DispatchQueue: async(group:qos:flags:execute:)

Q56. When is deinit called?

Reference: The Swift Programming Language: Language Guide: Deinitialization

Q57. How do you declare an optional String?

Reference: The Swift Programming Language: Language Guide: The Basics: Optionals

Q58. How many times this code will be executed? / How many times will this loop be performed?

for i in ["0", "1"] {
  print(i)
}

Reference: The Swift Programming Language: Language Guide: Control Flow: For-In Loops

Q59. What does this code print?

let names = ["Bear", "Tony", "Svante"]
print(names[1] + "Bear")

References:

Q60. What is true of this code?

let name: String?

Reference: The Swift Programming Language: Language Guide: The Basics: Optionals

Q61. What is the value of val after this code is executed?

let i = 5
let val = i * 6.0

Reference: The Swift Programming Language: Language Guide: The Basics: Type Safety and Type Inference

Q62. What does this code print?

enum Positions: Int {
  case first, second, third, other
}

print (Positions.other.rawValue)

Reference: The Swift Programming Language: Language Guide: The Basics: Raw Values

Q63. What is printed to the console when this code is executed?

"t".forEach { (char) in
  print(char)
}

References:

Q64. What prints when this code is executed?

let s1 = ["1", "2", "3"]
  .filter { $0 > "0" }
  .sorted { $0 > $1 }
print(s1)

References:

Q65. What enumeration feature allows them to store case-specific data?

Reference: The Swift Programming Language: Language Guide: Enumerations: Associated Values

Q66. In the code below, AOM must be a(n)?

class AmP: MMM, AOM { }

References:

Q67. What is the value of numbers in the code below?

let numbers = [1, 2, 3, 4, 5, 6].filter { $0 % 2 == 0 }

Reference: Apple Developer: Documentation: Swift: Swift Standard Library: Collections: Sequence and Collection Protocols: Sequence: filter()

Q68. What is the type of vals in this code?

let vals = ["a", 1, "Hi"]

Reference: The Swift Programming Language: Language Guide: Type Casting

Q69. How can you extract val to x in tuple vt

let vt = (name: "ABC", val: 5)

Reference: The Swift Programming Language: Language Guide: The Basics: Tuples

Q70. What is the type of x?

let x = try? String.init(from: decoder)

Reference: The Swift Programming Language: Language Guide: Error Handling: Handling Errors

Q71. How many times is this loop executed?

let loopx = 5
repeat {
  print (loopx)
} while loopx < 6

Reference: The Swift Programming Language: Language Guide: Control Flow: While Loops

Q72. How many values does vals have after this code is executed?

var vals: Set<String> = ["4", "5", "6"]
vals.insert("5")

Reference: The Swift Programming Language: Language Guide: Collection Types: Sets

Q73. What is the base class in this code ?

class LSN: MMM{ }