Random Questions Flashcards
What is the MVC?
Model, View, Controller
MVC allows us to separate the app’s data and logic, the view, and the controller.
The model is the data and logic for the app, while the view is the presentation. The controller’s job is to coordinate between the two to request and deliver data, and help choose a view type.
View is what the user sees and how they interact
Example of a view - Storyboard
Model is for storing the data. (what your app is - but not how it is displayed)
Example of a model - class files, databases, core data.
Controller is in charge of providing the view with data, and the model with any actions. It’s how the Model is presented to the user. (UI Logic)
Example of controller - ViewController.swift files
View has no idea about the data or the model.
What is the difference between a double and a float?
Float is 32-bit while double is 64-bit
Double has a precision of at least 15 decimal digits.
Float has a precision of 6 decimal digits.
So, doubles have double the precision of a float.
What is the difference between single equal and double equal?
Single equal is for assignment
var isTrue = true
Double equal is for comparison
if isTrue == false { //do this }
What is an integer?
A whole number that can be negative, positive, or 0
How would you convert an int to an Int32? Use 7 (I showed 2 answers)
var number: Int32 = 7
Int32(7) → this would convert it.
How would you convert an int to a double? var number: int = 7
double(number)
What is the value of an optional type that has not been assigned a value? var number: Int?
Nil
How can an if let be used for optionals?
It’s a way to see if the optional variable has a value vs being nil
What is a stride and how would it be used?
Returns the sequence of values
stride(from: 1, to: 10, by: 2)
What does concatenating two strings mean?
Adding them together let newString = "Hello" + " swift lovers"
Does viewDidLoad or viewWillAppear get called first?
viewDidLoad - is loaded when your uiviewcontroller is first loaded in memory, even before it is shown on screen.
viewWillAppear - is only presented when view controller is about to be presented on the screen, but before is show on the screen.
What inherits from what here - Class View: UIViewController { }
The class View inherits from UIViewController
What is a tuple?
Tuples group multiple values into a single compound value.
Let http404error = (404, “Not Found”)
http404error is of type (int, string)
What is an optional?
An optional has 2 possibilities, either there is a value, and you can unwrap the optional to access that value OR there isn’t a vale.
What is a nil value?
A valueless state
If you want to set a value to nil, it has to be optional first
What is optional binding?
It’s a way to find out if an optional contains a value.
If let contantName = someOptional {
statements
}
What happens when you implicitly unwrap something that is nil?
Runtime error
What is a compound assignment operator?
+=
What is a ternary conditional operator?
It’s a special operator with 3 parts.
Question ? answer1 : answer2
If question is true, then we get answer
Otherwise, we get answer2
What is the nil-coalescing operator?
a ?? b
This unwraps the optional a if it has a value or returns a default value of b if a is nil
What are the 2 ways to initialize an empty string?
Var emptyString = “”
Var anotherEmptyString = String()
What is string interpolation?
“(multiplier)”
How would you get the character count of a word?
Var word = “cafe”
Word.character.count
What are the 3 collection types and describe them?
Arrays - ordered collection of values
Sets - unordered collections of unique values
Dictionaries - unordered collections of key value associations
How could you add an integer to variable someInts?
someInts.append(3)
When would you need to use a dictionary?
When you need to look up values based on their identities
How would you create an empty dictionary called nameOfIntegers of key Ints and string values?
Var nameOfIntegers = Int: String
Break down the following for loop :
for index in 1…5 {
print(index)
}
The index is a constant value in the range of 1 through 5.
Index is a constant that is implicitly declared.
What is a while loop? Or was does it do?
It performs a set of statements until a condition becomes false
What are the 5 control transfer statements in swift?
Continue, break , fallthrough, return, throw
Describe how continue works.
Continue tells a loop to stop what it is doing and start again at the beginning of the next iteration through the loop (example page 158)
What does a break statement do?
A break statement ends execution of an entire control flow statement.
What does a fall through do?
Fall through is used when there is a case in a switch statement that applies and then if you put fallthrough in that one, it automatically does into the next one
Why do some functions require a return?
They have an arrow indicating a return value
What is the return type for a function that has no return type?
Func printHelloWorld() { print(“Hello World”) } Void
What is the structure of a function that has a return type?
func nameOfFunction(/* parameters */) -> Type { var valueToReturn: Type // Rest of function return valueToReturn }
What are properties?
Properties are the values associated with an object
An object’s data are “properties”
What are methods?
Methods are the functionality associated with an object
If you have a struct called Student with values such as name, age, school etc, what you create a variable or a constant with those values, what is that referred to?
var ayush = Student(name: "Ayush Saraswat", age: 19, school: "Udacity") Ayush is an instance of student type
var ayush = Student(name: "Ayush Saraswat", age: 19, school: "Udacity") For the above, how would you get the property name ayush?
Ayush.name
instanceName.propertyName
Where in the application is the natural starting point?
AppDelegate.swift file
When dealing with functions, show parameter in: Func sayHello(name: String) { }
When you right the function (name: String) shows us the parameter name and type
When dealing with functions, show argument in the function being called: sayHello(name: james)
James is the argument
What are 2 ways to initialize an empty string?
var characterPoorString = "" let stringWithPotential = String()
How would you initialize an array of Doubles called moreNumbers?
var moreNumbers = Double
When you initialize an array as such, let differentNumbers = [97.5, 98.5, 99.0], what is this called?
Array Literal Syntax
How would you initialize a dictionary of string, string?
var groupsDict = String:String
Explain how the nil-coalescing operator works in this situation:
let userDefinedColorName = "green" let defaultColorName = "red" colorNameToUse = userDefinedColorName ??defaultColorName
userDefinedColorName is not nil, so colorNameToUse is set to “green”
In an enum, what would the raw value of case astros be?
“astros”
What does persisting data mean?
Saving or storing the data.
What is the plist?
Plist, short for “property list”, is Apple’s own data format that makes it easy to store simple data like numbers, strings, and truth values.
What is a protocol?
A protocol is a description of functionality that can be adopted—think of it as a contract that a struct or object agrees to fulfill and conform to.
Write a protocol called PlayingCard with 2 variables: isFaceDown which is of type Bool and can be settable, shortName which is of type String and is gettable.
protocol PlayingCard {
var isFaceDown: Bool { get set }
var shortName: String { get }
}
What is a big difference between classes and structs regarding types?
Classes are reference types
Structs are value types
What are some things only classes can do that structs cannot?
Inheritance
Type casting
Define de-initialisers
Allow reference counting for multiple references.
What are some of the responsibilities of the UITableViewDelegate and UITableViewDataSource?
UITableViewDelegate - helps row rearranging & editing, determines row height and other row based display concerns
UITableViewDataSource - provides the tableview with its contents such as cells, and logic for updating data in the table
What is a closure?
A closure is a self-contained block of code that can be passed as an argument to a function.
What are 3 ways of going from one vc to another vc?
Code, no segue. You just have 2 vcs. 1 with identifier and it goes to the next vc based on that identifier matching.
Segue, no code. Set up a button on vc1 and segue it to the next vc to go to next one just by clicking it
Code and segue. You can also set up a button on vc1 to go to vc2 depending on what the identifier is for that segue.
What is a delegate?
An object that executes a bunch of methods on behalf of another object
Think of a text field delegate, where all the customization code is in the delegate file.
What is the difference between searching for a value that doesn’t exist in an array vs dictionary?
In an array, you get an error
In a dictionary, since it is optional, you receive nil.
What’s the return type of a function that doesn’t explicitly declare a return type?
Void
What is object orientated programming?
We model information into data structures as objects. The objects, like a struct, contain information in the form of stored properties. The pieces of information are tied together under the struct as opposed to just have two random constants outside of a struct.
Let x: int Let y: Int Vs. Struct { Let x: int Let y: Int }
When you have a superclass and you make a subclass, which do you initialize first in the subclass?
You initialize the subclass first
What are some differences between structs and classes?
Structs are value types. Which means the values are copied. When we create another instance of something, the values are copied over. Below will have different values if you change it.
someStudent.email
anotherStudent.email
Classes are references types. When we create an instance of a class, the values are references. So if you make an instance and change something for it, they both will have the same changes. Below will have the same exact values!
someStudent.email
anotherStudent.email
When we assign a variable, what is going on behind the scene?
The value is assigned in the memory(Ram), the name simply points to this place in memory where the data lives.
What is going on behind the scene when you are creating instance of a struct?
You are making a copy of the previous data and storing it in memory. So you have the original one and then the new one which is a copy. That is why they have 2 different values!
What is going on behind the scene when you are creating an instance of a class?
You are not making a copy of anything! You are only referencing whatever has been stored in memory. All instances of the class will point to the same place in memory
What are some more examples of value types?
Arrays, Dictionaries, strings, Ints, doubles, booleans
TF : A value type is a type whose value is copied when it is assigned to a variable or constant, or when it is passed to a function
True
TF : Reference types are not copied when they are assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used instead.
True
TF : An Int is a value type.
True
TF : An Array is a reference type.
False
What are the four layers of iOS?
Cocoa Touch
Media
Core Services
Core OS
Which of the following types support inheritance? Array, Class, Enum, Struct?
Class
TF : When we initialize a subclass, we first need to initialize the properties in our base class, then call the super class’ initializer
True
TF : Classes are provided with memberwise initializers by default
False
Views are instances of the _____ class?
UIView
A software _________ is a reusable code base that provides particular functionality in the context of a larger software platform
Framework
@IBOutlet weak var funFactLabel: UILabel! → decipher this
@IBOutlet lets xcode know this is an interface builder outlet
Weak deals with memory management
TF : Refactoring is the process of restructuring code by changing its behavior.
False
For each element we need to provide two categories of constraints: _________ and _____________
Position and Size
What is a view’s frame?
A rectangle that defines the view’s location and size in it’s superview.
What do we use optional binding for?
To see if an optional has a value.
What is the purpose of a guard statement?
An early exit or guard statement is a control flow concept where rather than checking for success cases first and worrying about errors last, you deal with the error case up front and exit the current scope.
What are the 2 ways to combine 2 strings together?
String Interpolation
String Concatenation
TF: A binary value is represented by either a 1 or 2
False. 0 or 1
The binary value for true is
1
Floating point numbers in Swift are represented by which type
Floats and doubles
What is a Unary Operator?
levelScore += 1
What is [1] called?
Index value