Programming Fundamentals in Swift Coursera Quiz Answers

Programming Fundamentals in Swift Coursera Quiz Answers

This course is ideal for beginners eager to learn the fundamental concepts that underpin the Swift programming language. Explore the basic programming concepts and data structures that are core to any language, while discovering the unique aspects that make Swift as versatile as it is today.

In this course, you will receive hands-on practice utilizing these concepts. More specifically, you will learn how to use constants and variables with different data types and explore how to sort and store information in collection types such as arrays, tuples and dictionaries. Finally, you will discover how to make your code reusable and more expressive by using functions and closures.

Enroll on Coursera

Week 01: Programming Fundamentals in Swift Coursera Quiz Answers

Quiz 1: Self review: Working with constants and variables

Q1. In the exercise, you were prompted to make a note of the code output. What was the last printed statement of the output?

  • The average temperature this week is 75°F.
  • Today is Monday. Rise and shine!
  • The temperature on Monday is 75°F.
  • The temperature on Monday morning is 70°F.

Q2. What did you use to store the fixed temperature value of a specific day?

  • A constant
  • A variable

Q3. What did you use to store the current temperature during a certain period of the current day?

  • A variable
  • A constant

Q4. How many times can you change the value of the temperature variable?

  • Only once
  • Many times

Quiz 2: Self-review: Operators and data types

Q1. What is the output of the following code?

let levelScore = 10
var gameScore = 0
gameScore += levelScore
print("The game's score is (gameScore).")
  • The game’s score is 10.
  • The game’s score is 0.

Q2. What is the difference between 20 and 20.0?

  • They are values of different types.
  • There is no difference between them.

Q3. What is the output of the following code?

let levelScore = 5
var gameScore = 10.4
gameScore += Double(levelScore)
print("The game's score is (gameScore).")
  • The game’s score is 15.4.
  • The game’s score is 15.

Q4. What is the result of 20 / 5?

  • 4
  • 4.0

Quiz 3: Knowledge check: Constants, variables, data types and operators

Q1. If you were to store the name of a planet, which would be the best to store this in?

  • A constant
  • A variable

Q2. Which of the following would be ideal to store in a variable?

  • Your current location
  • The starting point of the journey
  • The end point of the journey

Q3. The keyword ‘let’ is used to declare what in Swift?

  • A variable
  • A constant

Q4. Which of the following would be inferred as a double by Swift?

  • “Hello”
  • 100
  • 3.2

Q5. Are 3 and 3.0 the same in Swift?

  • Yes
  • No

Quiz 4: Self review: Working with strings in Swift

Q1. In the exercise, you were prompted to make a note of the code output. What was the last printed statement of the output?

  • It is 6:15 PM on Monday.
  • It is 6:15 PM PST on Mon.
  • It is 6:15 PM PST on Monday.

Q2. What did you use to determine the current time from its components?

  • String interpolation
  • String concatenation

Q3. What did you use to print the current day and time to the console?

  • String concatenation
  • String interpolation

Quiz 5: Knowledge check: Strings

Q1. Which operator is used to concatenate two strings together?

  • x (multiplication sign)
  • – (minus sign)
  • + (plus sign)
  • / (forward slash)

Q2. Which of the following would be used to put the variable name in a string?

  • (name)
  • /(name)
  • name
  • /name

Q3. Which of the following would be used to return the number of characters in a string called name?

  • count(name)
  • length(name)
  • name.length
  • name.count

Q4. A character is a one-letter string. 

  • True
  • False

Q5. What line of code should you insert in line 4 to concatenate the strings in lines 1, 2 and 3 to ensure the printed message reads correctly?

let veggies = "carrots"
let fruits = "strawberries"
let snacks = "crisps"

print("I have (picnicBasket) in my picnic basket.")
  • let picnicBasket = veggies + fruits + snacks
  • let picnicBasket = veggies + “, ” + fruits + ” and ” + snacks

Quiz 6: Self-review: Work with conditional statements in Swift

Q1.What do you call the type of value that’s been assigned to the freeApp constant?

let freeApp = true
if freeApp == true {
  print("You are using the free version of the app. Buy the full version of the app to get access to all of its features.")
}
  • Boolean
  • String

Q2. What is the output of the following code?

let temperatureDegree = "Celsius"
if temperatureDegree == "Fahrenheit" {
  print("The weather app works with Fahrenheit degrees.")
} else {
  print("The weather app works with Celsius degrees.")
}
  • The weather app works with Celsius degrees.
  • The weather app works with Fahrenheit degrees.

Q3. What is the output of the following code?

let temperatureDegree = "Fahrenheit"
if temperatureDegree == "Celsius" || temperatureDegree == "Fahrenheit" {
  print("The weather app is configured properly.")
} else {
  print("The weather app isn't configured properly.")
}
  • The weather app isn’t configured properly.
  • The weather app is configured properly.

Q4. What type of condition did you use to check if you are using the free version of the app?

  • If statement
  • If else statement
  • Else if statement

Q5. What type of condition did you use to check what kind of degrees the weather app uses?

  • If else statement
  • If statement
  • Else if statement

Q6. What logical operator did you use to evaluate the default temperature degree settings?

  • AND
  • OR

Quiz 7: Knowledge check: Conditional statements

Q1. Suppose you are using a conditional statement to check if a user’s age is less than another user’s age. Which operator would you use?

  • <
  • <=
  • >=
  • >

Q2. Which of the following operators means “OR”?

  • !=
  • &&
  • ||
  • ==

Q3. What is the value of the count variable after the code below is executed?

var count = 10
if count < 5 {
    count += 1
} else if count > 10 {
    count -= 1
}
  • 10
  • 11
  • 9​

Q4. What is the output of the following code?

let degrees = "Fahrenheit"
switch degrees {
    case "Fahrenheit": print("The weather app is configured for the US.")
    case "Celsius": print("The weather app is configured for Europe.")
    default: break
}
  • The weather app is configured for the US.
  • The weather app is configured for Europe.

Q5. Which is more efficient for checking many values?

  • Else if statements
  • Switch statements

Quiz 8: Knowledge check: Loops

Q1. Which of the following is optimized to loop a set number of times?

  • a for in loop
  • a while loop
  • a repeat while loop

Q2. Which of the following is best for looping an unknown number of times?

  • a while loop
  • a repeat while loop
  • a for in loop

Q3. Which of the following will always loop at least once?

  • a for loop
  • a repeat while loop
  • a while loop

Q4. The for in loop requires the in keyword.

  • True
  • False

Q5. Are for in loops optimized for iterating over a string?

  • Yes
  • No

Quiz 9: Self-review: Using conditions and loops

Q1. In the exercise, you were prompted to make a note of the code output.

What was the third level of the output?

  • Skip bonus level 3.
  • Play level 2.
  • You have played all 4 free levels. Buy the game to play all of the other 6 levels.

Q2. Which statement do you use to make sure only the free levels are playable?

  • The break statement
  • The continue statement

Q3. Which statement do you use to skip any bonus levels of the game?

  • The break statement
  • The continue statement

Q4. Which type of loop is best to implement the game logic?

  • For in loop
  • While and repeat while loop

Quiz 10: Self-review: Work with optionals in Swift

Q1. In the exercise, you were prompted to review the code output of your program.

What was the last printed statement of the output?

  • The first passcode of the app is 1111 and the second passcode of the app is 2222.
  • Invalid passcodes!
  • The passcode of the app is 1111.

Q2. When do you use the forced unwrapping operator?

  • With nil values.
  • With non-nil values.

Q3. When do you use optional binding?

  • With non-nil values.
  • With nil values.

Quiz 11: Knowledge check: Optionals

Q1. An optional for an Int type can only hold integers.

  • False
  • True

Q2. Which of the following operators are used to force unwrap an optional?

  • ?
  • |
  • !

Q3. he if let statement is used in optional binding.

  • False
  • True

Q4. Which of the following operators are used to define an implicitly unwrapped optional?

  • ?
  • !

Q5. What is the output of the following code?

let text = "10"
if let number = Int(text) {
  print("The number is (number).")
} else {
  print("Invalid number!")
}
  • The number is 10.
  • Invalid number!

Q6. What is the output of the following code?

let text = "text"
if let number = Int(text) {
  print("The number is (number).")
} else {
  print("Invalid number!")
}
  • The number is 10.
  • Invalid number!

Quiz 12: Module quiz: Introduction to programming in Swift

Q1. If you were to store the balance of a bank account, which would be the best to store this in?

  • A variable
  • A constant

Q2. Which of the following would be ideal to store in a constant?

  • The number of pages in the English dictionary.
  • The number of letters in the English alphabet.
  • The number of words in the English language.

Q4. The keyword var is used to declare what in Swift?

  • A constant
  • A variable

Q5. What operator do you use to check if two strings are the same?

  • ==
  • !=

Q6. Swift’s Strings are Unicode compliant.

  • True
  • False

Q6. Which operator means greater than?

  • >
  • <
  • <=
  • >=

Q7. Switch statements do require a default keyword.

  • True
  • False

Q8. A “for loop” can use ranges to define the number of loops.

  • Yes
  • No

Q9. Which of the following operators are used to define an optional?

  • ?
  • !

Q10. What is the output of the following code?

let text = "text"
if let number = Int(text) {
  print("The number is (number).")
} else {
  print("Invalid number!")
}
  • Invalid number!
  • The number is 4.

Week 02: Programming Fundamentals in Swift Coursera Quiz Answers

Quiz 1: Self-review: Arrays in Swift

Q1. In the exercise, you were prompted to make a note of the code output.

What was the last printed statement of the output?

  • You have finished playing the free version of the game. Buy the game to play its full version.
  • Game restarted!
  • Start playing the game!

Q2. What is the index of the first element in an array?

  • 0
  • 1

Q3. H​ow do you create an empty array to store integer values?

  • var levelScores = [Int]
  • var levelScores = []
  • var levelScores: [Int] = []

Quiz 2: Self-review: Tuple

Q1. In the exercise, you were prompted to review the code output.

What was the first printed statement of the output?

  • Invalid credentials!
  • The password is pass and the passcode is 1111.

Q2. Tuples, like arrays, follow a particular order.

  • True
  • False

Q3. Tuples are defined by placing a list of items within brackets, for example, ().

  • True
  • False

Q4. What is the index of the first element in a tuple?

  • 0
  • 1

Q5. What type of elements can a tuple have?

  • Different types
  • Same type

Quiz 3: Knowledge check: Arrays and tuples

Q1. Which of the following is an array literal?

  • {1, 2, 3, 4}
  • [1, 2, 3, 4]
  • (1, 2, 3, 4)

Q2. Which method would add a 5 to the end of an array called numbers? Select all that apply.

  • numbers + [5]
  • numbers = numbers + [5]
  • numbers = [5] + numbers
  • numbers.append(5)
  • numbers += [5]

Q3. Which method would you use to change the value held at index 1 of an array called numbers?

  • numbers[1] = 3
  • numbers.insert(3, at: 1)

Q4. Assuming an array called numbers isn’t empty, which method would you use to change the last index of an array?

  • numbers[numbers.count] = 10
  • numbers[numbers.count – 1] = 10

Q5. Individual elements of a tuple can be named when it is created.

  • True
  • False

Q6. How do you declare an empty array of integers?

  • var emptyArray: [Int] = []
  • var emptyArray = []

Q7. What is the output of the following code?

let emptyArray: [Int] = []
if emptyArray.count == 0 {
  print("The array is empty!")
} else {
  print("The array isn’t empty!")
}
  • The array is empty!
  • The array isn’t empty!

Q8. What is the output of the following code?

let emptyArray: [Int] = [0]
if emptyArray.count == 0 {
  print("The array is empty!")
} else {
  print("The array isn’t empty!")
}
  • The array isn’t empty!
  • The array is empty!

Q9. How do you access the elements of the following tuple?

let dailyTemperature = (“Monday”, 70)

  • Labels
  • Indices

Q10. How do you access the elements of the following tuple?

let dailyTemperature = (day: “Monday”, temperature: 70)

Select all that apply:

  • Labels
  • Indices

Quiz 4: Self-review: Dictionaries in Swift

Q1. In the exercise, you were prompted to make a note of the code output.

What was the last printed statement of the output?

  • Reset weekly temperatures for next week!
  • You have access to the weather forecast of the whole week.
  • The temperature on Sunday is 100°F.

Q2. When accessing a key’s associated value in a dictionary, force unwrapping terminates code execution if the value isn’t present.

  • False
  • True

Q3. When do you use optional binding with dictionaries?

  • To access elements that already exist in the dictionary.
  • To access elements that don’t exist in the dictionary.

Quiz 5: Self review: Using loops with collection types

Q1. In the exercise, you were prompted to run the program and review the output to the console.

Which of the following options best resembles the last printed statement in the console?

  • The score of level 7 is 70.
  • The temperature on Sunday is 100°F.
  • The game’s score is 280.

Q2. What type of loop did you use with arrays?

  • For in loop.
  • While loop.
  • Repeat while loop.

Q3. What type of loop did you use with dictionaries?

  • For in loop.
  • Repeat while loop.
  • While loop.

Quiz 6: Knowledge check: Collections

Q1. Which of the following is a dictionary literal?

  • [“A”:1, “B”:2, “C”:3, “D”:4]
  • {“A”:1, “B”:2, “C”:3, “D”:4}
  • (“A”:1, “B”:2, “C”:3, “D”:4)

Q2. Which method would you use to add the key-value pair to a dictionary called travelMiles?

  • travelMiles[“Yolanda”] = 3000
  • travelMiles.append(3000, at:”Yolanda”)

Q3. Which method would you use to modify a value held by a key in a dictionary? Select all that apply.

  • gameScore.updateValue(59, forKey: “George”)
  • gameScore[“George”] = 59

Q4. The following command [“A”:1, “B”:2, “C”:3, “D”:4].count will return what value?

  • 8
  • 4

Q5. You can extract all the keys from a dictionary using the following syntax:

  • travelMiles.keys
  • travelMiles.values

Q6. How do you declare an empty dictionary of string keys and integer values?

  • let emptyDictionary = [:]
  • let emptyDictionary: [String: Int] = [:].

Q7. What is the output of the following code?

let emptyDictionary: [String: Int] = [:]
if emptyDictionary.count == 0 {
  print("The dictionary is empty!")
} else {
  print("The dictionary isn’t empty!")
}
  • The dictionary is empty!
  • The dictionary isn’t empty!

Q8. What is the output of the following code?

let emptyDictionary: [String: Int] = ["": 0]
if emptyDictionary.count == 0 {
 print("The dictionary is empty!")
} else {
 print("The dictionary isn’t empty!")
}
  • The dictionary isn’t empty!
  • The dictionary is empty!

Quiz 7: Module quiz: Data structures

Q1. Which of the following is optimized for looping over an array?

  • a while loop
  • a for in loop
  • a repeat while loop

Q2. Which of the following is optimized for looping over a dictionary?

  • a repeat while loop
  • a while loop
  • a for in loop

Q3. Individual elements of a tuple have index numbers.

  • True
  • False

Q4. Which method would you use to add a value 5 to the end of an array called numbers? Select all that apply.

  • numbers.append(5)
  • numbers[5] = 3
  • numbers += [5]

Q5. Individual elements of a tuple can be accessed using a name/label if they were added when it was created.

  • True
  • False

Q6. You can extract all the values from a dictionary using the following syntax:

  • travelMiles.values
  • travelMiles.keys

Q7.Which of the following will remove keys and values from a dictionary? Select all that apply.

  • travelMiles.deleteValue(forKey:”George”)
  • travelMiles[“George”] = nil

Q8. The following code block terminates code execution:

var weeklyTemperatures = ["Monday": 70, "Tuesday": 75, "Wednesday": 80, "Thursday": 85, "Friday": 90]
weeklyTemperatures["Sunday"]! += 10
  • False
  • True

Q9. What is the output of the following code?

let emptyArray: [Int] = [0]
if emptyArray.count == 0 {
  print("The array is empty!")
} else {
  print("The array isn’t empty!")
}
  • The array is empty!
  • The array isn’t empty!
  • The code will produce a compile error.

Q10. How do you access the elements of the following tuple?

let dailyTemperature = (“Monday”, 70)

  • Indices.
  • Labels

Week 03: Programming Fundamentals in Swift Coursera Quiz Answers

Quiz 1: Self-review: Practice using functions

Q1. Which keyword defines a function?

  • let
  • var
  • func

Q2. In func unlockTreasureChest(inventory: Int) -> Int, the inventory is a:

  • parameter
  • signature
  • body

Q3. You can omit the return keyword in a function that returns an implicit expression.

  • False
  • True

Quiz 2: Self-review: Practice writing succinctly expressive functions

Q1. In func incrementInventory(inventory: Int, by amount: Int) -> Int, how many parameters are there?

  • 1
  • 2
  • 3
  • 4

Q2. In func incrementInventory(inventory: Int, by amount: Int) -> Int, the by keyword is a(n):

  • parameter name
  • argument label

Q3. You can set a default value for an in-out parameter.

  • True
  • False

Quiz 3: Knowledge check: Functions

Q1. Which of the following is the correct layout when defining a function in Swift? 

  • func function-name (return type) <- argument list 
  • fun function-name (argument list) : return type
  • func function-name (argument list) -> return type 

Q2. Functions must always return a value when called. 

  • True
  • False

Q3. You can use () and Void interchangeably.

  • True
  • False

Q4. Which way is valid for calling the following function?

func hiThere(firstName: String, lastName: String) {
}
  • hiThere(“John”, “Smith”) 
  • hiThere(firstName: “John”, lastName: “Smith”)

Q5. Which way is valid for calling the following function?

func hiThere(_ firstName: String, _ lastName: String) {
}
  • hiThere(“John”, “Smith”)
  • hiThere(firstName: “John”, secondName: “Smith”)

Q6. In func incrementInventory(inventory: Int, by amount: Int), what is the return type?

  • inventory
  • Int
  • Void
  • amount

Q7. What do you call the block of code that comes right after a function’s signature declaration?

  • Return type
  • Expression
  • Body
  • Parameter

Q8. You cannot use an argument label on an in-out parameter.

  • True
  • False

Quiz 4: Self review: Practice using closures

Q1. You can create a closure in one of three forms.

  • True
  • False

Q2. You can set a default parameter value in a closure.

  • True
  • False

Q3. Closure expressions have a name.

  • True
  • False

Quiz 5: Self-review: Use functions to modularize a program

Q1. A closure expression written in a function’s final parameter is called a trailing closure.

  • True
  • False

Q2. When would you want to use the Void return type in a closure?

  • When a function needs to return a value.
  • When a function doesn’t need to return a value.

Q3. You can have only one closure parameter inside a function signature.

  • True
  • False

Quiz 6: Module quiz: Functions and closures

Q1. Functions are special cases of closures with a name and don’t capture any values.

  • True 
  • False

Q2. Which of the following is the correct layout when defining a function in Swift? 

  • func function-name: return type <- (argument list) 
  • functionname (argument list) : return type 
  • func function-name (argument list) -> return type 

Q3. Which of the following is the correct layout when defining a closure in Swift?

  • ( (argument list) -> return type in code to be executed) 
  • [ (argument list) -> return type in code to be executed] 
  • { (argument list) -> return type in code to be executed} 

Q4. Which of the following is the way in which you can call this function? 

func hiThere(firstNamefn:String, secondNamesn:String) 

  • hiThere(“John”, “Smith”) 
  • hiThere(firstName:”John”, secondName:”Smith”) 
  • hiThere(fn:”John”, sn:”Smith”) 

Q5. Closures are self-contained blocks of functionality that can be stored in a variable. 

  • True
  • False

Q6. A function can be identified by a combination of its name, parameter types and return types. 

  • True
  • False

Q7. You can pass a closure as a function parameter. 

  • True
  • False

Q8. You cannot use an argument label on an in-out parameter.

  • True
  • False

Q9. Function parameters are immutable by default.

  • True
  • False

Q10. Only one in-out parameter is allowed for each function.

  • True
  • False

Week 04: Programming Fundamentals in Swift Coursera Quiz Answers

Quiz 1: Part 1: Self-review

Q1. In the exercise, you were prompted to make a note of the code output.

What was the first printed statement of the output?

  • Credit account.
  • Bank account.
  • Debit account.
  • Which option do you choose? (1, 2 or 3)

Q2. What type of loop did you use to create the bank account

  • Repeat while loop
  • While loop
  • For in loop

Q3. Why do you need a default case inside a switch statement?

  • To make the switch statement exhaustive.
  • To exit the switch statement.

Q4. Which case is the default case inside a switch statement?

  • The last case.
  • The first case.

Q5. Why did you use a break statement inside the default case of the switch statement?

  • To make the switch statement exhaustive.
  • To exit the switch statement.

Quiz 2: Part 2: Self-review

Q1. Which of the following examples demonstrate chaining multiple if statements together?

  • if balance == 0 { print(“Paid off account balance.”) }
  • if balance == 0 { print(“Paid off account balance.”) } else if balance > 0 { print(“Overpaid account balance.”) }

Q2. What did you use when you defined a function?

  • Arguments.
  • Parameters

Q3. What did you use when you called a function?

  • Arguments
  • Parameters.

Q4. Where did you use the parameters of a function?

  • In the function’s prototype
  • In the function’s call

Q5. Where did you use the arguments of a function?

  • In the function’s call
  • In the function’s prototype.

Q6. What is the relationship between the parameters and arguments of a function?

  • The function’s arguments don’t have the same order, aren’t of the same type and aren’t as many as the function’s parameters.
  • The function’s arguments have the same order, are of the same type and are as many as the function’s parameters.

Q7. When can you assign the result of a function to a constant or variable?

  • If the function has a return type
  • If the function doesn’t have a return type

Quiz 3: Part 3: Self-review

Q1. In the exercise, you were prompted to make a note of the code output.

What was the sixth printed statement of the output?

  • Check bank account
  • Deposit money
  • Close the system
  • Which option do you choose? (1, 2, 3, or 4)
  • Withdraw money

Q2. What type of loop did you use to perform operations for your bank account? 

  • While loop
  • For in loop
  • Repeat while loop

Q3. What type of conditional statement can you use to rewrite a switch statement? 

  • If else statement
  • If statement
  • Else if statement

Quiz 4: Final assessment: Programming fundamentals

Q1. If you were to store a date of birth, which would be the best to store this in? 

  • A constant 
  • A variable 

Q2. Which of the following would be inferred as a Bool by Swift?

  • False
  • 100 
  • “Hello” 

Q3. An Optional for a Bool type can only hold integers.

  • True
  • False

Q4. You can use an “if” statement for optional binding. 

  • True
  • False

Q5. One use case of the “>” operator is to compare the lexicographical order between two strings.

  • True
  • False 

Q6. Which method would you use to add a value “last item” to the end of an Array called strings? Select all that apply.

  • strings+= [“last item”] 
  • strings.append(“last item”) 
  • strings+ [“last item”] 

Q7. Which of the following is a greater than or equal to operator? 

  • =< 
  • <= 
  • => 
  • >=

Q8. Functions must always store a return value in a variable when called. 

  • True 
  • False 

Q9. Tuples, like dictionaries, do not follow any particular order. 

  • True 
  • False 

Q10. Which of the following is a dictionary literal? 

  • (“A”:true, “B”:false, “C”:true, “D”:false) 
  • [“A”:true, “B”:false, “C”:true, “D”:false] 
  • {“A”:true, “B”:false, “C”:true, “D”:false} 
Conclusion:

I hope this Programming Fundamentals in Swift Coursera Quiz Answers would be useful for you to learn something new from the Course. If it helped you, don’t forget to bookmark our site for more Quiz Answers.

This course is intended for audiences of all experiences who are interested in learning about new skills in a business context; there are no prerequisite courses.

Keep Learning!

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!