Giter VIP home page Giter VIP logo

swift-classvsstruct-reading-swift-intro-000's Introduction

Classes & Structs Quiz

Success is not final, failure is not fatal: it is the courage to continue that counts. -Winston Churchill

Overview

This quiz will give you practice working with both classes and structs.

Quiz Time!

In the last few lessons, you've learned a lot about classes and structs in Swift. Now, challenge yourself with a brief quiz to test everything you've learned.

Question 1

Given this class that represents a giant:

class Giant {
    var name: String
    var weight: Double
    let homePlanet: String

    init(name: String, weight: Double, homePlanet: String) {
        self.name = name
        self.weight = weight
        self.homePlanet = homePlanet
    }
}

And this code that instantiates an instance of Giant:

let fred = Giant(name: "Fred", weight: 340.0, homePlanet: "Earth")

Will these three lines of code run? If not, why not?

fred.name = "Brick"
fred.weight = 999.2
fred.homePlanet = "Mars"

---> NO, BECAUSE HOME PLANET IS A LET

Question 2

Can you fix the class definition above so that it does work?

---> var homePlanet: String

Question 3

Take a look at this struct that represents an alien:

struct Alien {
    var name: String
    var height: Double
    var homePlanet: String
}

And this line of code that instantiates an Alien:

let bilbo = Alien(name: "Bilbo", height: 1.67, homePlanet: "Venus")

Will these three lines of code run? If so, why not?

bilbo.name = "Jake"
bilbo.height = 1.42
bilbo.homePlanet = "Saturn"

---> NO, BECAUSE let bilbo

Question 4

Can you change the declaration of bilbo so that the above three lines of code do work?

---> var bilbo...

Question 5

Consider this bit of code that uses the Giant class:

let edgar = Giant(name: "Edgar", weight: 520.0, homePlanet: "Earth")
let jason = edgar
jason.name = "Jason"

What will the value of edgar.name be after those three lines of code are run? What will the value of jason.name be? Why?

---> Jason, Jason

Question 6

Given this bit of code that uses the Alien struct:

var charles = Alien(name: "Charles", height: 2.25, homePlanet: "Pluto")
var charlesFromJupiter = charles
charlesFromJupiter.homePlanet = "Jupiter"

What will the value of charles.homePlanet be after the above code run? What about the value of charlesFromJupiter.homePlanet? Why?

---> Pluto, Jupiter

Question 7

Here's a struct that represents a bank account:

struct BankAccount {
    var owner: String
    var balance: Double

    func deposit(_ amount: Double) {
        balance += amount
    }

    func withdraw(_ amount: Double) {
        balance -= amount
    }
}

Does this code work? Why or why not?

---> NOPE, IMMUTABLE

Question 8

Can you fix the BankAccount struct so it does work?

---> mutating func

Question 9

Given this bit of code (which incorporates any fixes you made in Question 8):

var joeAccount = BankAccount(owner: "Joe", balance: 100.0)
var joeOtherAccount = joeAccount
joeAccount.withdraw(50.0)

What will the value of joeAccount.balance be after the above code runs? What about the value of joeOtherAccount.balance? Why?

---> joeAccount= 50, joeOtherAccount = 100. BECAUSE A COPY IS CREATED

Question 10

Here's a class that can track songs in a music library:

class MusicLibrary {
    var tracks: [String]

    init() {
        tracks = []
    }

    func add(track: String) {
        tracks.append(track)
    }
}

Given this bit of code that uses MusicLibrary:

let library1 = MusicLibrary()
library1.add(track: "Michelle")
library1.add(track: "Voodoo Child")
let library2 = library1
library2.add(track: "Come As You Are")

After this code runs, what are the contents of library1.tracks? What about the contents of library2.tracks? Why?

---> SAME, ("MICHELLE", "VOODOO CHILD", "COME AS YOU ARE") -> COS REFERENCE TYPE

View this lesson on Learn.co

swift-classvsstruct-reading-swift-intro-000's People

Contributors

jimcampagno avatar mdippery avatar annjohn avatar willsputra avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.