Skip to content

Instantly share code, notes, and snippets.

@csfelipe
Created April 16, 2020 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csfelipe/02d601c3562d4e911ef2d4b4c6bd6804 to your computer and use it in GitHub Desktop.
Save csfelipe/02d601c3562d4e911ef2d4b4c6bd6804 to your computer and use it in GitHub Desktop.
This was the first round test I did for Lab49 in NY.
import UIKit
// Options #2
// a) Program Crashes
// b) [1,2,3,4]
// c) [1,2,5,4] (X)
func question2() {
var originalArray = [1,2,3,4]
var secondArray = originalArray
secondArray[2] = 5
print(secondArray)
}
print("-----------------")
print("Question 2")
question2() //Correct
// Options #4
// a) ["T","H","D","B"]
// b) ["B","D","H","T"]
// ["B","D","H","T"]
// ["T","H","D","B"]
// c) Nothing - program will crash
// d) ["T","H","D","B"] (X)
// ["T","H","D","B"]
// ["B","D","H","T"]
// e) ["T","D","H","B"]
// ["T","D","H","B"]
// ["T","D","H","B"]
func question4() {
let names = ["Tom", "Dick", "Harry", "Bob"]
let names1 = names.sorted(by: {s1, s2 in s1 > s2})
let names2 = names.sorted(by: {$0 > $1})
let names3 = names.sorted(by: <)
print(names1)
print(names2)
print(names3)
}
print("-----------------")
print("Question 4")
question4() //Correct
// Options
// a) [1,2,4]
// b) Code will not compile
// c) [5,9] (X)
// d) [[5,9]]
func question5() {
let numbers = [1,2,4,5,9]
let result = numbers.filter { $0 >= 5}
print(result)
}
print("-----------------")
print("Question 5")
question5() //Correct
// Options
// a) 10
// b) 0
// c) 11
// d) 9 (X)
func question6() {
let numbers = Array(1..<10)
print(numbers.count)
}
print("-----------------")
print("Question 6")
question6() // Correct
// Options
// a) Nothing, the code will not compile
// b) Jane
// c) Albert (X)
func question7() {
class Person {
var name: String
init(initialName: String) {
name = initialName
}
}
let person = Person(initialName: "Jane")
person.name = "Albert"
print(person.name)
}
print("-----------------")
print("Question 7")
question7() // Correct
// Options
// a) [1,1,2,2,3,3] (X)
// b) 1,2,3
// c) [[1,1],[2,2],[3,3]]
// d) [1,2,3]
func question8() {
let numbers = [1,2,3].flatMap{[$0,$0]}
print(numbers)
}
print("-----------------")
print("Question 8")
question8() // Correct
// Options
// a) 1,5,10,15,20
// b) 1,6,11,16 (X)
// c) 1,20
// d) 5,10,15,20
func question9() {
for i in stride(from: 1, to: 21, by: 5) {
print(i)
}
}
print("-----------------")
print("Question 9")
question9() // Possibly Correct
// Options
// a) Not a Dog
// b) Dog
// c) Nothing (X)
func question10() {
let name = "Garfield"
switch name {
// I didn't know about `fallthrough`
// Basically, fallthrough forces your case to be handled as the next case
case "Garfield":
fallthrough
case "Pluto", "Scooby", "Lassie":
print("Dog")
default:
print("Not a dog")
}
print("EoF Question 10")
}
print("-----------------")
print("Question 10")
question10() // Wrong
// Options
// a) Greene
// b) nil
// c) Rachel (X)
// d) Nothing - code won't compile
// e) Nothing - code will crash
func question11() {
func fullName() -> (fistname: String, lastName: String) {
return ("Rachel", "Greene")
}
let result = fullName().0
print(result)
}
print("-----------------")
print("Question 11")
question11() // Correct
// Options
// a) Jake (X)
// b) Jane
// c) Nothing - code won't compile
// d) nil
func question12() {
let (freshman, junior, senior) = ("Jane", "Jake", "June")
print(junior)
}
print("-----------------")
print("Question 12")
question12() // Possibly Correct
// Options
// a) 0
// b) 22 (X)
// c) Code won't compile
func question13() {
// This example doesn't compile
// func double<T>(value: T) -> T {
// return value * 2
// }
// print(double(value: 11))
}
print("-----------------")
print("Question 13")
question13() // Wrong
// Options
// a) nil
// b) []
// c) [(1,2,3), ("NY", "London", "Paris")]
// d) [(1,"NY"),(2,"London"),(3,"Paris")] (X)
func question14() {
let ids = [1,2,3]
let cities = ["NY", "London", "Paris"]
let result = Array(zip(ids, cities))
print(result)
}
print("-----------------")
print("Question 14")
question14() // Correct
// Options
// a) "Red", "Apple", 42 (X)
// b) Nothing
// c) nil
// d) "Red", nil, "Apple", 42
func question15() {
// This example doesn't compile
// let data: [Any?] = ["Red", nil, "Apple", 42]
// for datum in data where !(datum is Hashable) {
// print(datum)
// }
}
print("-----------------")
print("Question 15")
question15() // Possibly Wrong
// Options
// a) 5
// b) Nothing - code will not compile
// c) Nothing - code will crash
// d) 11 (X)
func question16() {
func myFunc(x: inout Int) {
var localX = x
defer { x = localX }
x = 11
}
var arg = 5
myFunc(x: &arg)
print(arg)
}
print("-----------------")
print("Question 16")
question16() // Wrong
// Options
// a) Condition not met
// b) Code will not compile
// c) Condition met
// d) Nothing
func question17() {
// EVAL_NOTE:
// This code snippet doesn't compile because there's
// no fall through inside the guard to escape scope
// guard false else {
// print("Condition not met")
// }
// print("Condition met")
}
print("-----------------")
print("Question 17")
question17() // Possibly Correct
// Options
// a) 2,2,2
// b) Code will not compile
// c) 2,0,2
func question18() {
// EVAL_NOTE:
// I didn't know that variable names could be declared with "()"
let a = 2
let (b) = 2
let (c): Int = 2
print(a,b,c)
}
print("-----------------")
print("Question 18")
question18() // Wrong
// Options
// a) 3 "First Name Kanye" 3 (X)
// b) 4 "First Name Kanye" 3
// c) 4 "First Name Kanye" 4
// d) 3 "First Name Lil Uzi Vert" 3
func question19() {
var names = ["Kanye", "Lil Uzi Vert", "Tyga", "Drake"]
// EVAL_NOTE:
// I didn't know that having remove(at:) captured in the closure
// wouldn't alter names composition
let nameTruncator = { names.remove(at: 0) }
print(names.count)
print("First Name \(nameTruncator)")
print(names.count)
}
print("-----------------")
print("Question 19")
question19() // Wrong
// Options
// a) None of the above
// b) Nothing
// c) Banana (X)
// d) Grape
func question20() {
// EVAL_NOTE:
// The thought process here might have been just like an array
// rather than paying attention that the enum started at 1 and not 0
enum Fruit: Int {
case Apple = 1
case Orange
case Grape
case Banana
}
let test = Fruit(rawValue: 3)
print(test)
}
print("-----------------")
print("Question 20")
question20() // Wrong
// Options
// a) Nothing - program will crash
// b) Nothing - program won't compile
// c) Code was terrible! (X)
// d) Error caught
func question21() {
// enum ErrorEnum: Error {
// case bad
// case worse
// case terrible
// }
//
// // EVAL_NOTE:
// // I forgot that this function should be markes as throws
// func badFunction() {
// throw ErrorEnum.terrible
// }
//
// do {
// try badFunction()
// } catch ErrorEnum.terrible {
// print("Code was terrible!")
// } catch {
// print("Error Caught")
// }
}
print("-----------------")
print("Question 21")
question20() // Wrong
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment