You have a request ? Contact Us Join Us

Programming Fundamentals in Swift | Coursera Quiz Answers

Answer Coursera: Meta iOS Developer Professional Certificate. Programming Fundamentals in Swift, Data structures, Functions and Closures, Classes.
Estimated read time: 19 min
Coursera: Programming Fundamentals in Swift
Programming Fundamentals in Swift | Coursera Meta

This course is perfect for beginners eager to grasp the foundational concepts that support the Swift programming language. You'll delve into essential programming ideas and data structures common to all languages, while also exploring the distinctive features that make Swift versatile today.

Throughout the course, you'll engage in practical exercises to apply these concepts. You will learn to work with constants and variables of various data types, and understand how to organize and manage information using collection types like arrays, tuples, and dictionaries. Additionally, you'll discover how to make your code more reusable and expressive through the use of functions and closures.

Designed for beginners aiming for a career in iOS development, this course requires no previous web development experience. All you need are basic internet navigation skills and a willingness to start coding.


Notice!
Always refer to the module on your course for the most accurate and up-to-date information.

Attention!
If you have any questions that are not covered in this post, please feel free to leave them in the comments section below. Thank you for your engagement.

Introduction to programming in Swift

1. If you were to store the balance of a bank account, which would be the best to store this in?
  • A variable
  • A constant
2. Which of the following would be ideal to store in a constant?
  • The number of words in the English language.
  • The number of letters in the English alphabet.
  • The number of pages in the English dictionary.
3. The keyword var is used to declare what in Swift?
  • A variable
  • A constant
4. What operator do you use to check if two strings are the same?
  • ==
  • !=
5. Swift’s Strings are Unicode compliant.
  • True
  • False
6. Which operator means greater than?
  • >
  • <
  • <=
  • >=
7. Switch statements do require a default keyword.
  • True
  • False
8. A “for loop” can use ranges to define the number of loops.
  • Yes
  • No
9. Which of the following operators are used to define an optional?
  • ?
  • !
10. 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.

Data structures

1. Which of the following is optimized for looping over an array?
  • a for in loop
  • a while loop
  • a repeat while loop
2. Which of the following is optimized for looping over a dictionary?
  • a while loop
  • a for in loop
  • a repeat while loop
3. Individual elements of a tuple have index numbers.
  • True
  • False
4. Which method would you use to add a value 5 to the end of an array called numbers? Select all that apply.
  • numbers += [5]
  • numbers.append(5)
  • numbers[5] = 3
5. Individual elements of a tuple can be accessed using a name/label if they were added when it was created.
  • True
  • False
6. You can extract all the values from a dictionary using the following syntax:
  • travelMiles.keys
  • travelMiles.values
7. Which of the following will remove keys and values from a dictionary? Select all that apply.
  • travelMiles.deleteValue(forKey:"George")
  • travelMiles["George"] = nil
8. The following code block terminates code execution:
var weeklyTemperatures = ["Monday": 70, "Tuesday": 75, "Wednesday": 80, "Thursday": 85, "Friday": 90]
weeklyTemperatures["Sunday"]! += 10
  • False
  • True 
9. 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!
  • The code will produce a compile error.
10. How do you access the elements of the following tuple?
let dailyTemperature = ("Monday", 70)
  • Indices.
  • Labels

Functions and Closures

1. Functions are special cases of closures with a name and don't capture any values.
  • True
  • False
2. Which of the following is the correct layout when defining a function in Swift?
  • func function-name (argument list) -> return type
  • func function-name: return type <- (argument list)
  • functionname (argument list) : return type
3. 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)
4. Which of the following is the way in which you can call this function?
  • func hiThere(firstNamefn:String, secondNamesn:String)
  • hiThere(fn:"John", sn:"Smith")
  • hiThere(firstNamefn:"John", secondNamesn:"Smith")
  • hiThere("John", "Smith")
5. Closures are self-contained blocks of functionality that can be stored in a variable.
  • True
  • False
6. A function can be identified by a combination of its name, parameter types and return types.
  • True
  • False
7. You can pass a closure as a function parameter.
  • True
  • False
8. You cannot use an argument label on an in-out parameter.
  • True
  • False
9. Function parameters are immutable by default.
  • True
  • False
10. Only one in-out parameter is allowed for each function.
  • True
  • False 

Structures and Classes

1. A struct is a value type. What does this mean?
  • Each instance keeps a unique copy of its data
  • Instances share a single copy of the data
2. What is the output of the following code?
struct Employee {
    var salary: Double
    var tax = 0.2
    mutating func deductTax() {
        salary = salary - (tax * salary)
    }
}
var emp = Employee(salary: 100)
emp.deductTax()
print(emp.salary)
  • 20
  • 80.0
  • 100
  • 0.2
3. What is the output of the following code?
struct Tax {
    var amount: Int = 5
}
var tax1 = Tax()
var tax2 = tax1
tax1.amount = 20
print("\(tax1.amount), \(tax2.amount)")
  • 5, 5
  • 20, 5
  • 20, 20
  • 5, 20
4. What is the output of the following code?
class Product   {
    var price: Int = 5
}
var product1 = Product()
var product2 = product1
product1.price = 20
print("\(product1.price), \(product2.price)")
  • 5, 5
  • 20, 20
  • 5, 20
  • 20, 5
5. What is the output of the following code?
class Vehicle {
    var type: String
    var noOfWheels: Int
    init(type: String, wheels: Int) {
        self.type = type
        noOfWheels = wheels + 1
    }
}
let car = Vehicle(type: "Jeep", wheels: 3)
print(car.type, "has", car.noOfWheels, "wheels")
  • Jeep has 4 wheels
  • Jeep has 3 wheels
  • Jeep has 5 wheels
6. What is the output of the following code?
class Square {
    var width: Double = 0
    var area: Double {
        return width * width
    }
}
let square = Square()
square.width = 2
print(square.area)
  • 4.0
  • 0.0
  • 2.0
7. Which of the following statements best describes the differences between structures and classes in Swift? Select all that apply.
  • Structures come with an auto-generated memberwise initializer, while with classes you must create your own.
  • Classes are known as reference types while structures are known as value types.
  • Structures can use inheritance and by that extend the subclass.
8. Why should a structure be used as a starting point when creating a new data type and not a class? Select all that apply.
  • Because in Swift standard library you will find that reference types are primary used.
  • Because a structure is a value type, and a class is a reference type.
  • Because a local change in the code when using a structure will not affect other parts of the code.
9. What are the correct statements regarding stored and computed properties in classes?
  • A computed property is read-only
  • A stored property is primary used when there is a need to encapsulate a block of computational code.
  • A computed property does not store a value.
10. When creating a class initializer, when would it be necessary to use the self keyword?
  • When a class property is assigned a default value.
  • When there are more than 4 parameters in the class initializer.
  • When name of the property in the class is the same as the name of the parameters in the initializer.

Final Graded Quiz

1. The while loop requires the "in" keyword.
  • True
  • False
2. When you loop through a dictionary, only the key value is returned.
  • True
  • False
3. Which of the following type is the number 5.6 automatically inferred to in Swift?
  • Double
  • Integer
4. You can pass multiple arguments to a function.
  • True
  • False
5. One use case of the “>” operator is to compare the lexicographical order between two strings.
  • True
  • False
6. 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"]
7. You can declare an array of strings type as [String].
  • True
  • False
8. Tuples can hold any standard type in Swift.
  • True
  • False
9. Tuples, like dictionaries, do not follow any particular order.
  • True
  • False
10. 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) 

Related Articles

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.