Swift iOS developer interview questions for fresher & experienced 2020 Part 1

Qu1 -What are the features of Swift ? 
-Closures unified with function pointers
-Automatic management of Memory is performed 
-Tuples and multiple return values
-More impressive structs and enums 
-Protocol oriented Optional 
-Types Type Safety and Type inference language 
-Much faster when compared to other languages
-Fast and concise iteration over a range or collection 
-Structs that support methods, extensions, and protocols 
-Functional programming patterns, e.g., map and filter 
-Powerful error handling built-in 
-Advanced control flow with do, guard, defer, and repeat keywords

Qu2 -What is a tuple? 
Tuple is a process of grouping multiple values together as a single variable.

Qu3 -What is a type alias?
A type alias allows you to provide a new name for an existing data type into your program. You define type aliases with the typealies keywordAfter a type alias is declared, the aliased name can be used instead of the existing type throughout the program.

Qu4 -What is the use of optional in Swift?
optional is used during the declaration of a property that can make a property optional. An optional in Swift is a type that can hold either a value or no value. Optionals are written by appending a ? to any type:

Qu5 -What is the significance of “?” in Swift? 

The question mark (?) is used during the declaration of a property can make a property optional. If the property does not hold a value.
         var crashValue : Int?

Qu6 -What is the use of double question marks ???
It provide a default value for a variable.

Qu7 -How to unwrap an optional?
It can be done in a few ways
Force unwrapping
Implicit unwrapping
Optional binding 
Qu8 -What is Optional binding?
Swift also provides a special syntax to access. Somes wrapped value called optional binding.
Write an optional binding for an if statement as follows:
  1. if let constantName = someOptional { //Syntax
  2. statements
  3. }
  4. apple swift documentation
Qu9 -Explain what is optional chaining?
Optional chaining is a Swift feature that allows the execution of a statement to stop and return nil at any point.it is the way by which we try to retrieve values from a chain of optional values. Let us take the following example classes.

Qu10 -What is the guard?
A guard statement is used to transfer program control out of scope if one or more conditions aren’t met. Simple definition in two simple words: EARLY EXIT

Qu11 -How to create multi-line string literals?
It starts a string literal, you need to write three double quotation marks, ”””, then press return.

Qu12 -What Is Dictionary In Swift?
It enables you to store the key-value pairs and access the value by providing the key. It is similar to that of the hash tables in other programming languages. Dictionary is an unordered collection of key-value associations.
Array - An array stores values of the same type in an ordered list.
Sets - A set stores distinct values of the same type in a collection with no defined ordering.

Apple documentation
Qu13 -What do you mean by the term “defer”? 
defer keyword used everything is called then defer will be active.

Qu14 -What is the difference between let and var in Swift?
The let keyword is used to declare constants while var is used for declaring variables.

Qu15 - What is the difference between Weak vs Strong ?

  • Weak- A weak reference is just a pointer to an object that doesn't protect the object from being deallocated by ARC
  • Does not increase the retain count 
  • On deallocation, weak objects will be set to nil, and that’s why all weak references are optional
  • Creates setters and getters 
  • Use of Weak :- 1. Delegates 2. Outlets 3. Subviews 4. Controls, etc.
  • strong
  • Creates ownership between property and assigned value.
  • Increases the retain count by 1
  • protects the object from being deallocated by arc
  • Creates setters and getters 
  • Object will be mutable 
  • Use of Strong :- Remaining everywhere which is not included in WEAK.
Qu16 - What is the difference between Copy vs Strong?
  • Copy
  • It creates new copy of an object, and once new object is created retain count will be 1
  • Use copy when you need the value of an object as it is, and you don’t want other object to update the value of your object
  • Creates setters and getters 
  • strong
  • Increases the retain count by 1
  • Creates setters and getters 
  • Object will be mutable
Qu17 -Explain some common execution states in iOS?
Not running – This state means that there is no code that is being executed and the application is completely switched off. 
Inactive – This state means that the application is running in the background and is not receiving any events. 
Active – This state means that the applications are running in the background and is receiving the events. 
Background – This state means that the application is executing the code in the background. 
Suspended – This state means that the application is in the background and is not executing. 

Qu18 -What’s the difference between a function and a method?
A function is just some code that will do a specific task. These functions must not be associated with a particular type. Basically, these are global functions and they can be added to any file outside of a particular type.
A method is a function but it is associated with a particular type.
There are two types of methods. 
Instance methods and type methods. An instance method is a function that will belong to a specific instance of a class, struct, or enum. Instance methods add functionality to an instance and work within that instance.
A type method is basically a Static method. It does not need an instance of the class to run.

Qu19 -Explain Enum in Swift?
An enum in swift is used to defines a common type for a group of related values. It enables you to work with those values in a type-safe way within your code.keyword enum is used to the defined enumeration data type.

Qu20 -What is the retain cycle?
Retain Cycle is the condition when 2 objects keep a reference to each other and are retained, it creates a retain cycle. 

Google Image
Qu21 -How to avoid the retain cycle?
Make one of the association/relationship as weak.

Qu22 -What is weak/unowned?
weak is a keyword that does not increase the reference count of the object.
a weak reference is used where there is a possibility for that reference to become nil at some point during its lifetime. 
A weak object must be optional.
unowned is similar to weak but never becomes nil so it should not be optional.
use unowned in closures when you are sure that the object never becomes nil.

Qu23 -What is ARC?
Swift introduced a powerful feature called Automatic Reference Counting (ARC) which handles most of an app’s memory management.
ARC is a compile-time mechanism to manage the memory of an application.
ARC keeps track of the reference counting of the object. when reference counting becomes zero then ARC releases the object.

Qu24-Delegate Vs Notification

  • Delegates: used in one to one communication
  • Notification: used in one to many communication 

Qu25-Atomic Vs NonAtomic

  1. atomic

    • atomic means only one thread access the variable (static type).
    • atomic is thread-safe.
    • But it is slow in performance
    • atomic is the default behavior
    • atomic accessors in a non-garbage collected environment (i.e. when using retain/release/autorelease) will use a lock to ensure that another thread doesn't interfere with the correct setting/getting of the value.
    • It is not actually a keyword.

  2. nonatomic

    • nonatomic means multiple thread access the variable (dynamic type).
    • nonatomic is thread-unsafe.
    • But it is fast in performance
    • nonatomic is NOT default behavior. We need to add the nonatomic keyword in the property attribute.
    • It may result in unexpected behavior when two different processes (threads) access the same variable at the same time.


    Post a Comment