Book a Call

Edit Template

Programming Fundamentals of Swift Coursera quiz answers

Introduction to Programming Fundamentals

  1. If you were to store the balance of a bank account, which would be the best to store this in?
  • A constant
  • A variable
  1. Which of the following would be ideal to store in a constant?
  • The number of words in the English language.
  • The number of pages in the English dictionary.
  • The number of letters in the English alphabet.
  1. The keyword var is used to declare what in Swift?
  • A constant
  • A variable
  1. What operator do you use to check if two strings are the same?
  • ==
  • !=
  1. Swift’s Strings are Unicode compliant.
  • True
  • False
  1. Which operator means greater than?
  • >
  • <
  • <=
  • >=
  1. Switch statements do require a default keyword.
  • True
  • False
  1. A “for loop” can use ranges to define the number of loops.
  • Yes
  • No
  1. Which of the following operators are used to define an optional?
  • ?
  • !
  1. What is the output of the following code? 
  1. let text = “text
  2. if let number = Int(text) {
  3.   print(“The number is \(number).”)
  4. } else {
  5.   print(“Invalid number!”)
  6. }
  • Invalid number!
  • The number is 4
If you want to know about " Programming Fundamentals of Swift ", then you can visit my original course. The link has been provided below.

Data structures

  • a repeat while loop
  • a while loop
  • a for in loop
  • travelMiles.deleteValue(forKey:"George")
  • travelMiles["George"] = nil
  1. var weeklyTemperatures = ["Monday": 70, "Tuesday": 75, "Wednesday": 80, "Thursday": 85, "Friday": 90]
  2. weeklyTemperatures["Sunday"]! += 10

  • True
  • False
  1. let empty array: [Int] = [0]
  2. if empty array .count == {
  3.   print("The array is empty!")
  4. else {
  5.   print("The array isn’t empty!")
  6. }

  • The array isn’t empty!
  • The array is empty!
  • The code will produce a compile error.
  1. let daily temperature = ("Monday", 70)

  • Indices.
  • Labels

Functions and closures

  • functionname (argument list) : return type
  • func function-name: return type <- (argument list)
  • func function-name (argument list) -> return type
  • ( (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}
  1. func hiThere(firstNamefn:String, secondNamesn:String) 

  • hiThere(fn:"John", sn:"Smith")
  • hiThere(firstNamefn:"John", secondNamesn:"Smith")
  • hiThere("John", "Smith")

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
  1. What is the output of the following code?
  1. struct Employee {
  2.   var salary: Double
  3.   var tax = 0.2
  4.   mutating func deductTax() {
  5.     salary = salary – (tax * salary)
  6.   }
  7. }
  8. var emp = Employee(salary: 100)
  9. emp.deduct tax()
  10. print(emp.salary)
  • 0.2
  • 20
  • 80.0
  • 100

3.  What is the output of the following code?

  1. struct Tax {
  2.   var amount: Int = 5
  3. }
  4. var tax1 = Tax()
  5. var tax2 = tax1
  6. tax1.amount = 20
  7. print(“\(tax1.amount), \(tax2.amount)”)
  • 5, 5
  • 20, 5
  • 20, 20
  • 5, 20

4. What is the output of the following code?

  1. class Product   {
  2.   var price: Int = 5
  3. }
  4. var product1 = Product()
  5. var product2 = product1                        
  6. product1.price = 20
  7. print(“\(product1.price), \(product2.price)”)
  • 5, 5
  • 20, 5
  • 20, 20
  • 5, 20

5. What is the output of the following code?

  1. class Vehicle {
  2.   var type: String
  3.   var noOfWheels: Int
  4.   init(type: String, wheels: Int) {
  5.     self.type = type
  6.     noOfWheels = wheels + 1
  7.   }
  8. }
  9. let car = Vehicle(type: “Jeep“, wheels: 3)
  10. 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?

  1. class Square {
  2.   var width: Double = 0
  3.   var area: Double {
  4.     return width * width
  5.   }
  6. }
  7. let square = Square()
  8. square.width = 2
  9. print(square.area)
  • 2.0
  • 4.0
  • 0.0

7. Which of the following statements best describes the differences between structures and classes in Swift? Select all that apply.

  • Classes are known as reference types while structures are known as value types.
  • Structures come with an auto-generated memberwise initializer, while with classes you must create your own.
  • 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 a local change in the code when using a structure will not affect other parts of the code.
  • Because a structure is a value type, and a class is a reference type.
  • Because in Swift standard library you will find that reference types are primary used.

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 there are more than 4 parameters in the class initializer.
  • When the name of the property in the class is the same as the name of the parameters in the initializer.
  • When a class property is assigned a default value.

Final graded quiz: Programming fundamentals

PROGRAMMING IN PYTHON COURSERA QUIZ ANSWERS

Leave a Reply

Coursera quiz

Coursera quizzes are an integral part of the learning process on the platform. They can include multiple-choice questions, coding exercises, problem-solving tasks, and peer-reviewed assignments.

Most Recent Posts

Category

Coursera quizzes are an integral part of the learning process on the platform. These quizzes assess learners’ understanding of the course material and help reinforce key concepts.

Company

Contact Us

Terms & Conditions

Privacy Policy

Help

Copyright

Privacy Policy

Mailing List

© 2023 Created with Coursera Quiz