Introduction to Programming Fundamentals
- If you were to store the balance of a bank account, which would be the best to store this in?
- A constant
- A variable
- 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.
- The keyword var is used to declare what in Swift?
- A constant
- A variable
- What operator do you use to check if two strings are the same?
- ==
- !=
- Swift’s Strings are Unicode compliant.
- True
- False
- Which operator means greater than?
- >
- <
- <=
- >=
- Switch statements do require a default keyword.
- True
- False
- A “for loop” can use ranges to define the number of loops.
- Yes
- No
- Which of the following operators are used to define an optional?
- ?
- !
- 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
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
- a repeat while loop
- a while loop
- a for in loop
- numbers += [5]
- numbers.append(5)
- numbers[5] = 3
- travelMiles.keys
- travelMiles.values
- travelMiles.deleteValue(forKey:"George")
- travelMiles["George"] = nil
- var weeklyTemperatures = ["Monday": 70, "Tuesday": 75, "Wednesday": 80, "Thursday": 85, "Friday": 90]
- weeklyTemperatures["Sunday"]! += 10
- True
- False
- let empty array: [Int] = [0]
- if empty array .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.
- 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}
- func hiThere(firstNamefn:String, secondNamesn:String)
- hiThere(fn:"John", sn:"Smith")
- hiThere(firstNamefn:"John", secondNamesn:"Smith")
- hiThere("John", "Smith")
- True
- False
Structures and classes
- 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
- 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.deduct tax()
- print(emp.salary)
- 0.2
- 20
- 80.0
- 100
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, 5
- 20, 20
- 5, 20
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)
- 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
- class
- tupple
- array
- struct
- False
- 100
- “Hello”
- func hiThere(firstName:String, _ secondName:String)
- hiThere(firstName:"John", secondName:"Smith")
- hiThere(firstName:"John", "Smith")
PROGRAMMING IN PYTHON COURSERA QUIZ ANSWERS