Test yourself in Swift: a puzzle for puzzle lovers

We, the mobile development team at SimbirSoft, use the Swift language extensively when creating iOS applications - it’s quite safe, fast and expressive. For those familiar with Swift, we have prepared some puzzler puzzles. Try to determine the result of each code fragment and share your results!



Pazzler number 1


How many times will the text β€œfoo was set” be printed?



Answer Options:


  • A. 1 time
  • B. 2 times
  • C. Never
  • D. Compilation error
  • E. Runtime Error

Answer

: B





didSet , .


Pazzler number 2


Let's continue with didSet : how many times will β€œbar was set” be printed?



Answer Options:


  • A. 1 time
  • B. 2 times
  • C. Never
  • D. Compilation error
  • E. Runtime Error

Answer

: A





didSet β€” defer.


Pazzler number 3


Sort the Bool array. What will output the code below?



Answer Options:


  • A. [false, false, true]
  • B. [true, false, false]
  • C. Compilation error
  • D. Runtime Error

Answer

: C





Bool Comparable, β€” Referencing instance method 'sort()' on 'MutableCollection' requires that 'Bool' conform to 'Comparable'


Pazzler number 4


What will be the result of the code below?



Answer Options:


  • A. nil
  • B. Optional (ArraySlice ([1, 2, 3, nil, nil]))
  • C. Optional (ArraySlice ([1, 2, 3]))
  • D. Compilation error
  • E. Runtime Error

Answer

:





Optional(ArraySlice([1, 2, 3])), ArraySlice β€œ ” .


Pazzler number 5


Let's play with the equalities. What will we see when executing the code below? What happens if you replace β€œ===” with β€œ==”?



Answer Options:


  • A. true and false
  • B. false and false
  • C. false and true
  • D. true and true
  • E. false and compilation error
  • F. true and compilation error
  • G. Compilation error in both cases
  • H. Compilation error and false
  • I. Compilation error and true

Answer

: E







β€œ===” Bool , , . , false. β€œ==” : , Equatable.


Pazzler number 6


A bit about polymorphism. What will be displayed in the console?



Answer Options:


  • A. foo and bar
  • B. bar and bar
  • C. foo and foo
  • D. Runtime Error


Checking ...
Answer

: A.





Swift: , , .


Pazzler number 7


Finally, a rather simple question: will there be a compilation error here?



Answer Options:


  • A. Yes
  • B. No

Answer

: .





, . generic- :




Conclusion


So, we have examined several fragments of code and assumptions regarding their implementation, as well as compared expectations and reality. We hope you find it interesting and useful!

Write in the comments how many questions you managed to answer without peeping, and what you had to smash your head at) And if you want to test yourself in other languages ​​- write your wishes!

All Articles