Here are a set of problems designed to help you reinforce and retain some useful JavaScript knowledge. If you are an Anki or Quizlet fan, consider adding some of these questions into a deck. 😀
Who created Swift and in what year?Chris Lattner and others at Apple started working on it in 2010 and first released it in 2014.Swift was designed to replace what language?Objective-CThe three main goals for Swift was that it be safe, fast, and ________________.Expressive (or fun)Does Swift use curly braces or indentation for structure?CurliesWhat is the difference between the range operators ... and ..<?The former (“through”) includes the end value; the latter (“upto”) excludes it.What is the syntax for string interploation?Wrap the interpolated value in \( and )Parameters in Swift have both a parameter name and an ________________?Argument labelHow do you mark that a change to a parameter changes it argument?inoutHow do you mark the argument passed to an inout parameter?Prefix it with &Many languages use split to break a string into a list of characters, and join to turn an array of characters into a string. How do you do these two operations in Swift?Mutable variables are introduced with ________ and immutable variables with ________.var, letWe use the print function to write to standard output. But how do we write to standard error?How are the command line arguments obtained?It is legal to write var x = 1 but not legal to write var a = []. Why?The type of the empty array cannot be inferred.When reading a line from a file in Python, an exception is raised when the file has been fully read. What happens in Swift? (In other words, What is the return type of readLine and why?)How do you access the second element of the tuple t?
t.1
List all the integer types of Swift.Int8, Int16, Int32, Int64, Int, UInt8, UInt16, UInt32, UInt64, UInt.Are the built-in types Float and Double guaranteed to have a fixed implementation size, or are they implementation-dependent as in C++? If they do, what are these sizes?Fixed size, 32 bits, 64 bits.What are the six type kinds?Structure, Class, Enum, Tuple, Function, Protocol.How do structs and classes differ?Why is it that the type Int is acutally a struct?What is the nicer way of writing the type expressions Array<Int>, Dictionary<String, Bool>, and Optional<Double>?
[Int]
[String:Bool]
Double?
Are arrays, sets, and dictionaries structs or classes?StructsDo the equality operators == and != for arrays, sets, and dictionaries compare the elements of these objects or is the comparison done on object identities?What is the method for getting the number of array elements, the number of set elements, and the number of dictionary entries?countHow do you determine if a value is contained in a given array?How does the read-onliness of an array, set, or dictionary in Swift differ from the const-ness of an object in JavaScript?How do you arrange for a function call to not require the external name of a parameter?Use the argument label _.Why are argument labels often prepositions, but local names are often nouns and noun phrases? Give a trivial example (both a definition and a call) that backs up your answer.For a function definition using func, are you allowed to omit the types of the parameters? Are you allowed to omit the return type?What is a closure in Swift?Write a closure that squares its argument with both the in-notation and the form with the shorthand argument names.
{ x in x * x }
{ $0 * $0 }
What are the four kinds of struct/class members?Initializers, properties, methods, subscriptsThe two different kinds of properties are stored properties and ________________ properties.ComputedSuppose we had a struct called Square with a stored property called sideLength. Define the computed property area (which can be read and written).How do we employ the shorthand syntax for read-only computed properties? (That is, what exactly is omitted?)Give a definition for a Swift enumeration of the additive primary colors red, green, and blue.
enum PrimaryColor {
case red
case green
case blue
}
How do you obtain the raw value of an object of an enumeration type?How do you construct a value of an enumeration type from its raw value?Give an enumeration definition for a type whose values cover (1) the two boolean values, (2) the 8-bit signed integers, and (3) the value file_not_found.Define your very own generic linked list as an indirect enumeration, using the definition “A list is either empty or a value (called the head) connected to a list (called the tail).” (Just define the type, you don’t need to include any methods.)Write a length method for a generic linked list enumeration type defined according to the rule “A list is either empty or a value (called the head) connected to a list (called the tail).”Write a head for a generic linked list enumeration type defined according to the rule “A list is either empty or a value (called the head) connected to a list (called the tail).” If your type is called MyLinkedList then your method should return a T?.What is the syntax for a generic type parameter that is constrained to types that adopt a certain protocol?The three main syntactic forms for unwrapping optionals are if-let, while-let and _______________________.What is the ?? operator called?The nil-coalescing operatorWhat does it mean that the ?? operator is short-circuit?It means that if the left operand is a wrapped optional then the right operand is not evaluated.Are if-let, while-let, or guard-else constructs required to access an element of an optional array? If not, what is a more concise way to access the element?Are if-let, while-let, or guard-else constructs required to access a property of an optional struct/class instance? If not, what is a more concise way to access the property?The guard-else handles scope weirdly compared to if-let and while-let. What is this odd scope rule, and why is it odd?If a function is defined to return a String?, can we simply place the statement return "Hello" in the body, or will this be a compile time error, or perhaps a run time error?What is a common alternative to throwing errors from a function to indicate failure?Returning Result objectsWhat is the difference between the operator + and the operator &+?There is actually a shorter way to write the expression reduce(0, {$0+$1}). What is it?In Swift, the operator + both concatenates string and adds numbers. This is known to lead to huge headaches and funny surprises in JavaScript, and yet, it is not really a big deal in Swift. Why not?Swift does not have an exponentation operator. Could you, in principle, define ** yourself?True or false? Swift’s standard library has over 100 structs/classes/enums.Swift is the world’s first protocol-oriented language. What exactly is a protcol?If you declare your own class/struct/enum, how do you specify that your type adopt (conform to) one or more protocols? Give an example.How does Swift guarantee that any type you create to conform to the Equatable protocol require that you only define == and not != as well?If you are using the dictionary type [K:V] for types K, and V, what protocol must type K adopt?Why are extensions so awesome? In particular, what do they allow you to do, that you can’t do, say, in Java?You can add properties and methods without creating a subclass.Both Swift and Python programmers use self a lot. But their use is quite different. What is the essential difference between the two languages in the use of this keyword?How does Swift mark a property or method as belonging to the type instead of to an instance?staticHow do extensions solve the famous expression problem?Why and when do we extend protocols?What is the difference between a definitive initialization language and a default implicit initialization language?Do Swift compilers generally give warnings if they detect a variable might not have been initialized when it is used?Actually it is an error.What happens in Swift if a pattern match is not exhaustive?Compile-time error.Why does ARC mean no tracing garbage collector is required?What is the advantage of ARC over tracing garbage collectors?You can make reliable performance predictions because you know exactly when instances are deallocated (there are no surprises with garbage collection kicking in at seemingly arbitrary times).How do Swift programmers avoid memory leaks due to cycles of references on the heap? (Hint: they use a particular kind of reference. What is this reference called and how does it work?)