Top 20 iOS interview questions for beginner to Intermediate level
Now a days interview standard is very high, even for freshers. So, we should concentrate more on the basics of Swift.
In this story I am going to share knowledge on Swift basics, some important output check questions which are mostly asked during interview. As we move on to questions, it will be more interesting.
So, stay tuned.
With no further adieu lets’s get started.
1. What is the difference between Swift and Objective-C. Which one is better and why?
Answer: Here we have few differences between Objective-C and Swift.
Both Swift and Objective has its pros and cons. But due to following reason Swift is plus over Objective C.
- Objective-C is outdated compared to Swift and lacks modern tool.
- Swift is faster, Safer, more readable, reliable
- Swift need less code and its close to other modern languages.
- Apple ongoing focus
Reference link
2. What are iOS application states?
Answer:
- Non-running — The app is not running.
- Inactive — The app is running in the foreground, but not receiving events. An iOS app can be placed into an inactive state, for example, when a call or SMS message is received.
- Active — The app is running in the foreground, and receiving events.
- Background — The app is running in the background, and executing code.
- Suspended — The app is in the background, but no code is being executed.
Reference link.
3. What is view controller lifecycle?
Answer: Lifecycle events order
init(coder:)
(void)loadView
(void)viewDidLoad
(void)viewWillAppear
(void)viewDidAppear
(void)didReceiveMemoryWarning
(void)viewWillDisappear
(void)viewDidDisappear
Reference link.
Let’s start with basic questions…..
4. What is optional in Swift?
Answer: You use the Optional
type whenever you use optional values, even if you never type the word Optional
. Swift’s type system usually shows the wrapped type’s name with a trailing question mark (?
) instead of showing the full type name. For example, if a variable has the type Int?
, that’s just another way of writing Optional<Int>
. The shortened form is preferred for ease of reading and writing code.
Reference Link
5. Difference between guard let and If let ?
Answer:
Guard let
- A guard statement is used to transfer program control out of a scope if one or more conditions aren’t met.
- The value of any condition in a guard statement must be of type Bool or a type bridged to Bool. The condition can also be an optional binding declaration.
guard condition else { //Generally return }
func submit() {guard let name = nameField.text else {show("No name to submit")return}
If let
- Also popular as optional binding
- For accessing optional object we use if let
if let roomCount = optionalValue {print("roomCount available")} else {print("roomCount is nil")}
6. Can we have Tuples with single value?
Answer: No.
7. List control transfer statement in Swift.
Answer:
continue
break
fallthrough
return
throw
8. How to modify parameter values of a function? Means what’s the way for a function to have an effect on parameter value outside of the scope of its function body.
Answer: In-out parameters are an alternative way for a function to have an effect outside of the scope of its function body.
Example:
func swapTwoInts(_ a: inout Int, _ b: inout Int) { let temporaryA = a a = b b = temporaryA}var someInt = 3var anotherInt = 107swapTwoInts(&someInt, &anotherInt)
9. Can we have a nested function in swift?
Answer: Yes
10. What is auto closure and when to use auto closure in Swift?
Answer: An autoclosure is a closure that’s automatically created to wrap an expression that’s being passed as an argument to a function. It doesn’t take any arguments, and when it’s called, it returns the value of the expression that’s wrapped inside of it. This syntactic convenience lets you omit braces around a function’s parameter by writing a normal expression instead of an explicit closure.
Usage: An autoclosure lets you delay evaluation, because the code inside isn’t run until you call the closure. Delaying evaluation is useful for code that has side effects or is computationally expensive, because it lets you control when that code is evaluated. The code below shows how a closure delays evaluation.
var customersInLine = [“Chris”, “Alex”, “Ewa”, “Barry”, “Daniella”]print(customersInLine.count) // Prints “5”let customerProvider = { customersInLine.remove(at: 0) }print(customersInLine.count) // Prints “5”print(“Now serving \(customerProvider())!”) // Prints “Now serving Chris!”print(customersInLine.count) // Prints “4”
Reference Link
Thanks for reading halfway.. Very helpful question are coming up now.
11. What is retain cycle and memory leak? — Mostly asked
Answer: A memory leak in iOS is when an amount of allocated space in memory cannot be deallocated due to retain cycles. Since Swift uses Automatic Reference Counting (ARC), a Retain cycle occurs when two or more objects hold strong references to each other. As a result these objects retain each other in memory because their retain count would never decrement to 0, which would prevent deinit from ever being called and memory from being freed.
Reference link
12. What are higher order functions and how to use them?
Answer: A higher-order function is a function that takes one or more functions as arguments or returns a function as its result. Here are some swift higher-order functions.
- map
- compactMap
- flatMap
- filter
- reduce
- forEach
- contains
- removeAll
- sorted
- split
Reference link
13. What are generics in Swift?
Answer: Generic code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define. You can write code that avoids duplication and expresses its intent in a clear, abstracted manner.
Example:
func swapTwoValues<T>(_ a: inout T, _ b: inout T) { let temporaryA = a a = b b = temporaryA}
In the two examples below, T
is inferred to be Int
and String
respectively:
var someInt = 3var anotherInt = 107swapTwoValues(&someInt, &anotherInt)// someInt is now 107, and anotherInt is now 3var someString = “hello”var anotherString = “world”swapTwoValues(&someString, &anotherString)// someString is now “world”, and anotherString is now “hello”
14. What is different ways of concurrent operations in Swift?
Answer: We have following ways of concurrent opeations in Swift.
- DispatchQueue
2. Operation and OperationQueues
3. Swift Concurrency
Reference link
15. What will be output of num variable following code?
var num = -1
num = +num
Answer: -1
16. What will be output of _value in following code?
let _value = 1.0_1111_1?
Answer: 1.011111
17. How we can set value of a variable in the scope of it main class only?
Answer: Make the variable private (set)
18. Write code to create a singleton class.
Answer:
class Student {static let shared = Student()private init() {}}//Use:let studentInstance = Student.shared
19. What will be printed on executing this code?
var a = 0
var b = 0
let closure = { [a] in
print(a)
print(b)
}
a = 10
b = 10
closure()
Answer:
0
10
20. Write your own sorting algorithm in swift.
Answer: We have difference type of sorting techniques but to provide best result we can use Merge Sort or Quick Sort.
Here we have link to check Bubble Sort, Insertion Sort and Merge Sort code.
Conclusion: Even if you are fresher or experience. It’s very important to keep updated with Basic of programming. I hope my blog helps you to crack your interview.