iOS Interview Question

Shubham Jain
10 min readMar 29, 2018

Question : Why is design pattern very important ?
Answer :
Design patterns are reusable solutions to common problems in software design. They’re templates designed to help you write code that’s easy to understand and reuse. Most common Cocoa design patterns:

  • Creational: Singleton.
  • Structural: Decorator, Adapter, Facade.
  • Behavioral: Observer, and, Memento

Question: What is Singleton Pattern ?
Answer :
The Singleton design pattern ensures that only one instance exists for a given class and that there’s a global access point to that instance. It usually uses lazy loading to create the single instance when it’s needed the first time.

Question : What is Facade Design Pattern ?
Answer : The Facade design pattern provides a single interface to a complex subsystem. Instead of exposing the user to a set of classes and their APIs, you only expose one simple unified API.

Question : What is Decorator Design pattern ?
Answer
: The Decorator pattern dynamically adds behaviors and responsibilities to an object without modifying its code. It’s an alternative to subclassing where you modify a class’s behavior by wrapping it with another object.

In Objective-C there are two very common implementations of this pattern: Category and Delegation. In Swift there are also two very common implementations of this pattern: Extensions and Delegation.

Question : What is Adapter design pattern ?
Answer
: An Adapter allows classes with incompatible interfaces to work together. It wraps itself around an object and exposes a standard interface to interact with that object.

Question : What is Observer design pattern ?
Answer :
In the Observer pattern, one object notifies other objects of any state changes.

Cocoa implements the observer pattern in two ways: Notifications and Key-Value Observing (KVO).

Question : What is Memento design pattern ?
Answer :
In Memento Pattern saves your stuff somewhere. Later on, this externalized state can be restored without violating encapsulation; that is, private data remains private. One of Apple’s specialized implementations of the Memento pattern is Archiving other hand iOS uses the Memento pattern as part of State Restoration.

Question: Explain MVC design pattern ?

  • Models — responsible for the domain data or a data access layer which manipulates the data, think of ‘Person’ or ‘PersonDataProvider’ classes.
  • Views — responsible for the presentation layer (GUI), for iOS environment think of everything starting with ‘UI’ prefix.
  • Controller/Presenter/ViewModel — the glue or the mediator between the Model and the View, in general responsible for altering the Model by reacting to the user’s actions performed on the View and updating the View with changes from the Model.

Question : Explain MVVM design pattern ?
Answer : UIKit independent representation of your View and its state. The View Model invokes changes in the Model and updates itself with the updated Model, and since we have a binding between the View and the View Model, the first is updated accordingly.

Your view model will actually take in your model, and it can format the information that’s going to be displayed on your view.

There is a more known framework called RxSwift. It contains RxCocoa, which are reactive extensions for Cocoa and CocoaTouch.

Question : What is Regular expressions ?
Answer
: Regular expressions are special string patterns that describe how to search through a string.

Question : What’s the difference between the frame and the bounds?
Answer
: The bounds of an UIView is the rectangle expressed as a location (x,y) and size (width,height) relative to its own coordinate system (x=0,y = 0).
The frame of an UIView is the rectangle expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.

Question : What is the difference strong, weak, read only and copy ?Answer: strong, weak, assign property attributes define how memory for that property will be managed.

Strong means that the reference count will be increased and
the reference to it will be maintained through the life of the object

Weak, means that we are pointing to an object but not increasing its reference count. It’s often used when creating a parent child relationship. The parent has a strong reference to the child but the child only has a weak reference to the parent.

Read only, we can set the property initially but then it can’t be changed.

Copy, means that we’re copying the value of the object when it’s created. Also prevents its value from changing.

Question : What’s the difference between a xib and a storyboard?
Answer
: Both are used in Xcode to layout screens /view controllers. A xib defines a single View or View Controller screen, while a storyboard shows many view controllers and also shows the relationship between them.

Question : What is Xcode Bot?
Answer
: Xcode Server uses bots to automatically build your projects. A bot represents a single remote repository, project, and scheme. We can also control the build configuration the bot uses and choose which devices and simulators the bot will use.

Question :Explain Sequence in Swift
Answer : Sequence is a basic type in Swift for defining an aggregation of elements that distribute sequentially in a row.
All collection types inherit from Sequence such as Array, Set, Dictionary.

Question : What is the difference between KVO and NotificationCenter ?
Answer : The biggest difference between KVO and NotificationCenter is that KVOtracks specific changes to an object or property , while NotificationCenter is used to track generic events.

Question : Explain JSONEncoder and JSONDecoder in Swift4 ?
Answer : JSONEncoder and JSONDecoder classes which can easily convert an object to encoded JSON representation.

Question : What is deeplinking in mobile application?
Answer : Using Deep linking user can pass data to the application from any platform like, website or any other application. By tapping on the link, user can pass necessary data to the application and user will redirect to the application.

Question : What are the table view delegate methods?
Answer :

Datasources methods
@required

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

@optional

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

Delegate methods : all optional methods

@optional

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
(nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

Question :What is Guard keyword in swift ?
Answer :
Guard Keyword : Guard is basically a safe way to create a constant that is non optional and if it is nil , if it does return nil, we are just going to go ahead and return out of that function so that we don’t have a crash.

Question : What are benefits of Guard ?
Answer :
There are two big benefits to guard. One is avoiding the pyramid of doom, as others have mentioned — lots of annoying if let statements nested inside each other moving further and further to the right. The other benefit is provide an early exit out of the function using break or using return

Question : What is the question mark ? in Swift?
Answer : The question mark ? is used during the declaration of a property, as it tells the compiler that this property is optional. The property may hold a value or not, in the latter case it’s possible to avoid runtime errors when accessing that property by using ?. This is useful in optional chaining

var optionalName : String? = “John”

if optionalName != nil {

print(“Your name is \(optionalName!)”)

}

Question : What is the use of double question marks ???

Answer :
To provide a default value for a variable.
let missingName : String? = nil
let realName : String? = “John Doe”
let existentName : String = missingName ?? realName

Question : What is the use of exclamation mark !

Answer :
Highly related to the previous keywords, the ! is used to tell the compiler that I know definitely, this variable/constant contains a value and please use it (i.e. please unwrap the optional). From question 1, the block that executes the ifcondition is true and calls a forced unwrapping of the optional’s value. There is a method for avoiding forced unwrapping which we will cover below.

Question : What is the difference between let and var in Swift?

Answer : The let keyword is used to declare constants while var is used for declaring variables.

let someConstant = 10
var someVariable : String

Here, we used the : string to explicitly declare that someVariable will hold a string. In practice, it’s rarely necessary — especially if the variable is given an initial value — as Swift will infer the type for you. It is a compile-time error trying to use a variable declared as a constant through let and later modifying that variable.

Question : What is the difference between functions and methods in Swift?

Functions are globally scoped while methods belong to a certain type.Question : Difference between function and method in swift ?
Answer :
when a function is defined with in the curly braces of a class it is no longer called a function but instead it is called a method.
Methods is always associated with the object of the class.
Function are not associated with the object of the class. To called a function there is no need to create object of any class.

print (arc4random(3)) is a function not a method

Question : What is “if let” statement in swift 4 ?
Answer :
if let userSetDestibnation = Destination
{
print(“driving towards “+ userSetDestination)
}
if let is basically the syntax of optional binding in swift 4 , if we examin the above statement little closer

using the let keyword we create the new constant and the value will be set to the value of the destination optional variable and the line with in the curly braces of the if let statement will be execute only if the destination variable does not nil.
Benefit to use the if let statement or Optional binding is there is no need to force unwrapping and code is safer , will not lead to any bug or crash.

Question : What is the Swift main advantage ?
Answer
: To mention some of the main advantages of Swift:

  • Optional Types, which make applications crash-resistant
  • Built-in error handling
  • Closures
  • Much faster compared to other languages
  • Type-safe language
  • Supports pattern matching

Question : What are tuples in swift language?
Answer : Tuples are used to return multiple values from a function call e.g
let person = (name : “jain”, age: “10”)
person.name — → jain
person.age — → 10

Question : What is dynamic dispatch in iOS?
Answer : Dynamic dispatch is the process of selection polymorphic operation (method or function )at run time.
Dynamic dispatch is used to decide which polymorphic method will call at run time.

Question : What is KVO and KVC in iOS ?
Answer :
KVO stands for Key value observer that allow a controller or class to observe the changes to a property. In KVO, an object can ask to be notified of any changes to a property , whenever a property changes its value, then the observer is automatically notified.

KVC stands for key value coding by which an objects property can be accessed using strings at run time rather than having to statically know the property at development time.

Question : What is the purpose of using IBAction and IBOutlets in iOS ?
Answer : IBAction and IBOutlets are macros defined to denote methods and variables in interface builder.
IBAction is associated with methods and IBOutlets are associated with variables/properties.
IBAction and IBOutlets signify that variables and methods can be used in interface builder to link the UI element.

Question : What is Grand Central Dispatch (GCD) Objective -C ?
Answer :
GCD is a library that provide low level object based API ti run task concurrently while managing the threads

Types Of GCD :
Dispatch_Queue : Dispatch queue is responsible to execute the task in first in first out order (FIFO).
Serial Dispatch Queue : This queue executes one task at a time in serial order.
Concurrent Dispatch Queue : This Queue executes as many task as it can execute without waiting for the started task to finish.
Main Dispatch Queue : Globally available serial queue that executes task on the applications main thread.

Question : What is size class in ios ?
Answer : Size Class is a new technology iOS 8 onwards to allow user tio customize application UI for a given device based on its orientation and screen size.
Before iOS 8 developer had to use multiple checks to handle the UI Elements for the different screen size.

Question : What is the lifecycle of the viewController?
Answer :

i). LoadView : It is only called when the view controller is created programmatically. This makes it a good place to create your views in code.
ii). ViewDidLoad
iii). ViewWillAppear
iv). ViewDidAppear
v). ViewWillDisappear
vi). ViewDidDisappear

vii). ViewWillLayoutSubviews : If you are not using constraints or Auto Layout you probably want to update the subviews here
viii). ViewDidLayoutSubviews : This event notifies the view controller that the subviews have been setup. It is a good place to make any changes to the subviews after they have been set.

Question : What is the application lifecycle of iOS ?
Answer :
1. didFinishLaunchingWithOptions : Very first method of the ios application , it is invoked when the application launches.
2. applicationWillEnterForeground : this method will invoked when the application enter in foreground state from the background or killed state.
3 . applicationDidBecomeActive :
This method is call after the application enter in foreground to finish up the transition to foreground.
4 . applicationWillResignActive :
This method will called when the application is about to inactive e.g when the user receives a phone call and press the home the home button.
5 . applicationWillTerminate :
this method invoked when the user killed the application from the memory.

Question : What are the differences in table and collection in iOS ?
Answer :
1 . table is consists of single column layout and have top to down scrolling.
2. collection is consists of grid layout/multiple column layout and also have vertical and horizontal scrolling.
3. Collection view uses customizable layout while table view uses single column layout.

Question : What is the difference ANY and ANYOBJECT ?
Answer : According to Apple’s Swift documentation:

  • Any can represent an instance of any type at all, including function types and optional types.
  • AnyObject can represent an instance of any class type.

Question : What’s the difference between a xib and a storyboard?Answer : Both are used in Xcode to layout screens (view controllers). A xib defines a single View or View Controller screen, while a storyboard shows many view controllers and also shows the relationship between them.

Question : What’s the difference optional between nil and .None?
Answer
: There is no difference. Optional.None (.None for short) is the correct way of initializing an optional variable lacking a value, whereas nil is just syntactic sugar for .None. Check this out.

--

--