Top IOS Interview Questions and Answers (Sept 2018)

In this article, I am going to cover interviews questions asked in current month, there will be more articles based on iOS developer…

Top IOS Interview Questions and Answers (Sept 2018)

In this article, I am going to cover interviews questions asked in current month, there will be more articles based on iOS developer interviews shared by friends (iOS developers). Lets start with these question asked in recent interview to iOS developer.

Q1. What are value types and on what basis you decide to use them?

Q2. What is protocol oriented programming and what are its benefit?

Solution: Although OOP offers a lot of advantages like encapsulation, access control and abstractions but it comes with its own set of problems which protocol oriented programming solved.

  1. You mostly start with single class inheritance but then you realize that you need some more functionality in your class from some different class. This makes you lean towards multiple inheritance which is not supported by most programming languages and leads to undesired complexity.
  2. As classes are reference types passing them around in functions can cause unexpected behavior especially if you are working in a multi threaded environment.
  3. Due to tight coupling of classes with one another it becomes difficult to write unit tests for a single class.

For getting better insight check this — Protocol Oriented Programming in Swift

Q3. What is the difference in sync and async operations, how it works?

Solution: When you invoke something synchronously, it means that the thread that initiated that operation will wait for the task to finish before continuing. Asynchronous means that it will not wait.

Having said that, when people suggest that you perform some slow or expensive process asynchronously, they are implicitly suggesting not only that you should run it asynchronously, but that you should do that on a background thread. The goal is to free the main thread so that it can continue to respond to the user interface (rather than freezing), so you are dispatching tasks to a background thread asynchronously.

Q4. kind of different queues ?

Solution:

1. DISPATCH_QUEUE_SERIAL(A dispatch queue that executes blocks serially in FIFO order.)

2. DISPATCH_QUEUE_CONCURRENT (A dispatch queue that executes blocks concurrently. Although they execute blocks concurrently, you can use barrier blocks to create synchronization points within the queue.)

Q5. What all architectures you have used to design iOS application?

Solution: MVVM vs. MVC vs. VIPER (here)

Q6. Please let me know about design patterns which you normally use during app development?

Solution:

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

Q8. What are solid principles, did you use it in your iOS application?

Solution: Yes

S.O.L.I.D. STANDS FOR:

  • S — Single responsibility principle
  • O — Open closed principle
  • L — Liskov substitution principle
  • I — Interface segregation principle
  • D — Dependency Inversion principle

# Single responsibility principle

A class should have one and only one reason to change, meaning that a class should only have one job.

# Open-closed Principle

Objects or entities should be open for extension, but closed for modification.

# Liskov substitution principle

Derived classes must be substitutable for their base classes.

# Interface segregation principle

A client should never be forced to implement an interface that it doesn’t use or clients shouldn’t be forced to depend on methods they do not use.

# Dependency inversion principle

Entities must depend on abstractions not on concretions. It states that the high level module must not depend on the low level module, but they should depend on abstractions.

For more details — here

Q10. When you process some block of code on main queue, what happen, how it works — serially or parallel , on what thread?

Solution:

The main queue does indeed run on the main thread like you say.

The global queues are concurrent queues and from the main page for dispatch_get_global_queue:

Unlike the main queue or queues allocated with dispatch_queue_create(), the global concurrent queues schedule blocks as soon as threads become available (“non-FIFO” completion order). The global concurrent queues represent three priority bands:
•   DISPATCH_QUEUE_PRIORITY_HIGH        
• DISPATCH_QUEUE_PRIORITY_DEFAULT
• DISPATCH_QUEUE_PRIORITY_LOW
Blocks submitted to the high priority global queue will be invoked before those submitted to the default or low priority global queues. Blocks submitted to the low priority global queue will only be invoked if no blocks are pending on the default or high priority queues.

So, they are queues which run on background threads as and when they become available. They’re “non-FIFO” so ordering is not guaranteed.

Q11. How to use a custom class type to be the key in Dictionary in Swift?

Solution:

Any custom type that you want to use a dictionary key must conform with the Hashable protocol.

This protocol has one property that you must implement.

var hashValue: Int { get }

Q12. How do you write unit test cases , Do you know about mocking?

Solution:

In short, mocking is creating objects that simulate the behavior of real objects.

Mocking is primarily used in unit testing. An object under test may have dependencies on other (complex) objects. To isolate the behavior of the object you want to replace the other objects by mocks that simulate the behavior of the real objects. This is useful if the real objects are impractical to incorporate into the unit test.

When it comes to testing, there’s good news, and bad news. The bad news is that there can be disadvantages to unit tests, like the following:

  • More code: In projects with high test coverage it’s possible to have more test code than functional code.
  • More to maintain: When there is more code, there is more to maintain.
  • No silver bullet: Unit tests don’t (and can’t) ensure that your code is free of bugs.
  • Takes longer: Writing tests takes time — time you could spend learning new exciting stuff on raywenderlich.com!

Although there is no silver bullet, there is a silver lining — testing has the following advantages:

  • Confidence: You can demonstrate that your code works.
  • Quick feedback: You can use unit tests to quickly validate code that is buried many layers deep in your app navigation — things that are cumbersome to test manually.
  • Modularity: Unit tests help keep you focused on writing more modular code.
  • Focus: Writing tests for micro features keep you focused on the small details.
  • Regression: Be sure that the bugs you fixed stay fixed — and aren’t broken by subsequent fixes.
  • Refactoring: Until Xcode gets smart enough to refactor your code on its own, you’ll need unit tests to validate your refactoring.
  • Documentation: Unit tests describe what you think the code should do; they serve as another way to document your code.

Bonus

At times you may want to distinguish between mocking as opposed to stubbing. There may be some disagreement about this subject but my definition of a stub is a “minimal” simulated object. The stub implements just enough behavior to allow the object under test to execute the test.

A mock is like a stub but the test will also verify that the object under test calls the mock as expected. Part of the test is verifying that the mock was used correctly.

To give an example: You can stub a database by implementing a simple in-memory structure for storing records. The object under test can then read and write records to the database stub to allow it to execute the test. This could test some behavior of the object not related to the database and the database stub would be included just to let the test run.

If you instead want to verify that the object under test writes some specific data to the database you will have to mock the database. Your test would then incorporate assertions about what was written to the database mock.

Hope this will help you are looking for good opportunities in iOS field.

References:

  1. https://docs.swift.org/swift-book/
  2. https://en.wikipedia.org/wiki/SOLID