InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
You are done with the implementation of the map-based navigation application. How you will test navigation routing on your simulator. |
|
Answer» As iOS developers we know, BUNDLE ID is a string that UNIQUELY identifies an application in the APP store. So while developing the app we have to choose unique bundle-id for our app. Apple recommends reverse DOMAIN name for this, like com.yourCompany.yourApp For the above SCENARIO. As apple guidelines say, once your application successfully uploaded to store, you can’t change the bundle ID later. So always make sure that you choose a bundle identifier that makes sense for the project and the owner. |
|
| 2. |
class LogInViewController: UIViewController { var NetworkManager = NetworkManager() } func loginAction(){ let loginVC = myStoryboard.instantiateViewController(withIdentifier: "LogInViewController") as! LogInViewController loginVC.networkManager.doSomeAction() } |
|
Answer» NO - They are similar but have a slightly different purpose. Swift DOCUMENT SAYS, “If you assign a closure to a property of a class instance, and the closure captures that instance by referring to the instance or its MEMBERS, you will create a strong reference cycle between the closure and the instance.” See below example. class Device { var closure: (() -> ())? var name = "iPad" init() { self.closure = { print("inventory has \(self.name)") } } }var iPad:Device? = Device() iPad?.closure?() iPad = nilHere device instance iPad will never be released DUE to a strong reference cycle as described. So we have to use a capture list. Capture lists help us avoid memory problems. Inside the capture list, we can use weak or unowned to avoid any strong reference. weak: use weak when the captured VALUE may become nil. class Device { var closure: (() -> ())? var name = "iPad" init() { self.closure = {[weak self] in print("inventory has \(self?.name)") } } }var iPad:Device? iPad = Device() iPad?.closure?() iPad = nilunowned: If we know for sure captured value will never become nil while the closure is called. In the below code, We are creating a non-nil instance of the device. So will sure that name will be always available. class Device { var closure: (() -> ())? var name = "iPad" init() { self.closure = {[unowned self] in print("inventory has \(self.name)") } } }var iPad = Device() iPad.closure?() |
|
| 3. |
Modify below code using Dependency Injection. |
|
Answer» 35 Higher order functions are simply functions that can EITHER accept functions or closures as arguments or return a function/closure. Higher Order Functions are very useful and powerful and help us to write more elegantly and maintainable code. Those functions are Map, Filter, Reduce, Sort, CompactMap etc. let result = [listOne.compactMap({ $0}),listTwo.compactMap({ $0})].flatMap({ $0}).reduce(0) { $0 + $1 }First compact map removes NIL ELEMENT from the array. Then by using a flat map, we COMBINE these two arrays. And finally, reduce function will help to get the sum of array elements. |
|
| 4. |
Swift is a static language and Objective-C dynamic language. What does it mean? |
|
Answer» Add GPS locations in GPX file and add GPX this file to your project. You can go to HTTP://gpx-poi.com/ and simply fill the required data. So you can also select this route from Xcode by clicking Location Services icon in the jump bar of the debug AREA. And user navigation will be simulated as per given GPX points. |
|
| 5. |
Suppose you are developing an application that lists news updates for a nearby location. Like popular apps on the store, the client also wants to do it using a paginating mechanism. How would you implement this? |
|
Answer» Dependency injection is a technique/pattern help to adopt loose COUPLING between related OBJECTS. It also helps in unit testing. The above EXAMPLE demonstrates one of the types of Dependency Injection, property INJECTIONS. Here we are injecting Network manager instance when it required. class LogInViewController: UIViewController { var networkManager : NetworkManager? } func loginAction(){ LET loginVC = myStoryboard.instantiateViewController(withIdentifier: "LogInViewController") as! LogInViewController loginVC.networkManager = NetworkManager() loginVC.networkManager.doSomeAction() } |
|
| 6. |
print("Apple”) DispatchQueue.main.async { print("iPod”) DispatchQueue.main.async { print("iPhone") } DispatchQueue.global().sync { print("iPad") } print("Apple Watch") } print("Apple TV") |
Answer»
|
|
| 7. |
What will be output for below code? |
|
Answer» Fetch your API with new page number until the last page. FUNC tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if indexPath.row == list.count - 1 { self.fetchMoreItems(currentPageNumber) } }
|
|
| 8. |
What is the difference between the consumable and non-consumable type of in-app purchase? |
|
Answer» First, it will PRINT “Apple”. Next logic is contained in a 'main' async EVENT. So it's postponed. The next thing it will print is "Apple TV". Once the main thread is FREE, our first main.async task will run. So “iPod” will print. Next to its main.async task again. For this to EXECUTE, the main thread needs free. Since we're still in the middle of the control flow, It will MOVE to DispatchQueue.global().sync. Read carefully, that it's dispatching sync not async. That means that the main thread is frozen until the code dispatched to the global queue is done.Then it will print “Apple Watch”. So finally prints “iPhone”. |
|
| 9. |
What happens when the app receives a push notification? Which are delegate methods that deal with the notification? |
Answer»
|
|
| 10. |
class Student { let name: String init(name: String) { self.name = name } var scoreCard: ScoreCard?{ didSet{ print("\(name) got \(scoreCard!.marks) marks in this exam") } } } class ScoreCard { let marks: Float init(marks: Float) { self.marks = marks } var student: Student?{ didSet{ print("\(marks) for \(student!.name) in this exam") } } } var student: Student? var score:ScoreCard? student = Student(name: "Albert Einstein") score = ScoreCard(marks: 80) student!.scoreCard = score score!.student = student student = nil score = nil |
|
Answer» When the user receives a notification, there are two possible OUTCOME actions depending on the app state.
|
|
| 11. |
What will be output for below code snippet? Also, analyze the memory performance for this code. |
|
Answer» Albert Einstein GOT 80.0 marks in this EXAM 80.0 for Albert Einstein in this exam. Here we have defines two classes, Student and ScoreCard. Each student owns one scoreCard also Each scoreCard belongs to the specific student. Finally, we have created a student with the name "Albert Einstein" and ScoreCard with 80 marks. We assigned scorecard to student and student to scorecard. Now both objects are strongly REFERENCING to each other. So they will never be removed from memory. This code will cause a memory leak. To fix this, we have to use weak reference for scoreCard property of student class. class Student { let name: String init(name: String) { self.name = name } weak var scoreCard: ScoreCard?{ didSet{ print("\(name) got \(scoreCard!.marks) marks in this exam") } } } |
|
| 12. |
Almost all companies are taking the subject of data security and privacy severely. So how to protect user's sensitive data in iOS applications. |
|
Answer» APPLE provides great security MECHANISMS like Keychain, data ENCRYPTION, or App Transport Security(which FORCES developers to use SSL pinning). Keychain is the password management system developed by Apple. LOGINS, keys, and passwords should be stored in Keychain. For network request, SSL pinning is used to ensure that an application communicates with the right server. For this, we need to save the SSL certificate within the app bundle. Others security mechanisms are like using encryption methods like AES-256 encryption while saving sensitive data to persistent storage or passing data over the internet. |
|
| 13. |
For your photo-gallery iPad application, What would be your preference as senior developer, Alamofire or URLSession? |
|
Answer» With URLSession APIs AVAILABILITY, Now it becomes easier to build up network requests. The developer can build own NETWORKING layer on top of URLSession. Alamofire is POPULAR Elegant HTTP Networking library in Swift. Alamofire saves you a ton of time and simplifies the USE. It is a well-tested library. Maintained by very clever PEOPLE. Cost of including it - very small. For small projects and projects with less time-line Alamofire is a good choice. But for long term product, we can build in the house layer with NSURLSession. To avoid third-party dependency and upgrade overhead. |
|
| 14. |
Bob developed an iOS application, uploaded on store successfully. Since he is working for client XYZ, he chooses bundle-id com.xyz.appName for this music application. But the later name of the organization changed now the client wants to update bundle-id too.What are the possible ways to handle this? |
|
Answer» As iOS developers we know, BUNDLE ID is a string that UNIQUELY identifies an application in the app store. So while DEVELOPING the app we have to choose unique bundle-id for our app. Apple recommends REVERSE domain name for this, like com.yourCompany.yourApp For the above scenario. As apple guidelines say, once your application successfully uploaded to store, you can’t change the bundle ID later. So always make sure that you choose a bundle identifier that makes SENSE for the project and the owner. |
|
| 15. |
In case of capture list, do weak and unowned are substitutable to each other? |
|
Answer» NO - They are similar but have a slightly DIFFERENT purpose. Swift document says, “If you assign a closure to a property of a class instance, and the closure captures that instance by referring to the instance or its members, you will create a strong REFERENCE cycle between the closure and the instance.” See below example. class Device { var closure: (() -> ())? var name = "iPad" init() { self.closure = { print("inventory has \(self.name)") } } }var iPad:Device? = Device() iPad?.closure?() iPad = NILHere device instance iPad will never be released due to a strong reference cycle as described. So we have to use a capture list. Capture lists help us avoid memory problems. Inside the capture list, we can use weak or unowned to avoid any strong reference. weak: use weak when the captured value may become nil. class Device { var closure: (() -> ())? var name = "iPad" init() { self.closure = {[weak self] in print("inventory has \(self?.name)") } } }var iPad:Device? iPad = Device() iPad?.closure?() iPad = nilunowned: If we KNOW for sure captured value will never become nil while the closure is called. In the below code, We are creating a non-nil instance of the device. So will sure that name will be always available. class Device { var closure: (() -> ())? var name = "iPad" init() { self.closure = {[unowned self] in print("inventory has \(self.name)") } } }var iPad = Device() iPad.closure?() |
|
| 16. |
Consider two input arrays,let listOne = [3,nil,5,7]let listTwo = [4,6,2,8,nil]By using higher order functions, write code snippet to get a sum of all integers from both arrays. |
|
Answer» 35 Higher order functions are simply functions that can either ACCEPT functions or closures as arguments or return a function/closure. Higher Order Functions are very useful and powerful and HELP us to write more elegantly and maintainable code. Those functions are Map, FILTER, Reduce, Sort, CompactMap etc. let result = [listOne.compactMap({ $0}),listTwo.compactMap({ $0})].flatMap({ $0}).reduce(0) { $0 + $1 }First compact map removes nil element from the array. Then by using a flat map, we combine these two arrays. And finally, reduce function will help to get the SUM of array elements. |
|