Top IOS Interview Questions and Answers (Oct 2018)

Hope you are loving these articles and its helping you, Let’s start with October months part 1.

Top IOS Interview Questions and Answers (Oct 2018)

Hope you are loving these articles and its helping you, Let’s start with October months part 1.

Q1: What is the concurrency?. How many ways you know to achieve concurrency?

Ans: Its a condition when two or more tasks can work independently, even if other task is also executing at same time.

  1. NSOperation & NSOperationQueue
  2. Grand Central Dispatch (GCD)

Queues Types

Defining the type of queue is important in achieving the results you want, and will help prioritize the way your blocks of code are executed.

Synchronous: The current thread will be blocked while execution is happening

Asynchronous: Tasks will be run on a background thread, keeping the current thread free for other tasks

Serial: Serial queues execute the blocks one at a time, in the order of which they were entered (FIFO — First In First Out).

You can create more than one serial queue; if you had four serial queues, they could be running all four tasks at the same time asynchronously, the advantage being that you can control the order(FIFO) in which the blocks of code execute, and they are thread-safe on each Serial Queue.

Concurrent: Concurrent queues have the potential to execute the blocks together, (with a default of 2 blocks at a time), allowing processes to be run in parallel. The disadvantage being that you might not have perfect control over which tasks finish first.

Dispatch Queues

Below are the 5 global queues, and their corresponding QoS class, which provides more specific definitions of their functionality.

From Apple

Q2: What are differences between Manual and Automatic Reference Counting?

Ans: WE WILL COVER EXPLANATION OF: RETAIN, RELEASE, AUTORELEASE, ALLOC, DEALLOC, COPY, AND NSAUTORELEASEPOOL.

Manual Reference Counting

Memory management in Objective-C involves four basic rules. If you follow these rules, then you will not leak memory or cause dangling pointers.

RULE 1. IF YOU CREATE AN OBJECT USING A METHOD THAT STARTS WITH “ALLOC”, “COPY” OR “NEW”, THEN YOU OWN IT.

This is where you have created a new object with a retain count of one, which automatically makes you the owner.

RULE 2. IF YOU RETAIN AN OBJECT, THEN YOU OWN IT.

This is where you call retain on an object, which increases the retain count by one.

RULE 3. IF YOU OWN IT, YOU MUST RELEASE IT.

This is where you call the release method on an object, which decreases the retain count by one. When you call release and the retain count reaches zero, the object will deallocate itself by calling dealloc. This means you should never call dealloc directly. Just release the object correctly, and it will handle everything itself.

RULE 4. IF YOU KEEP A POINTER TO AN OBJECT, THEN YOU MUST OWN THE OBJECT (WITH SOME RARE EXCEPTIONS).

Basically, if you own an object, then you know it is definitely safe to use. If you don’t own it, then it is sometimes safe to use temporarily (discussed in the autorelease section). If you want to keep an object to use later, such as storing it in an ivar or a global, you must retain it. Otherwise, it might be deallocated and you will be left with a dangling pointer.

One exception is the use of strings you type directly into the code (string literals). String literals are never deallocated, and retain and release don’t do anything to them.

Automatic Reference Counting(ARC)

Apple has used the LLVM compiler toolset to implement ARC.

— It is not part of the runtime

— Instead the compiler injects reference tracking and clean-up code, similar to what a developer would do

— Deterministic reclamation of memory, at the time when the object goes out of scope

— Since there is no background or runtime processing, it requires less CPU and RAM, making it efficient on mobile devices

— Cannot cope with retain cycles/object graphs — gets stuck with an object graph, whereas a GC would look for an external reference to an object graph, and if not found would clear up the entire object graph

— Is more prone to memory leaks based on the quality of code written

Q3: What are the benefits of Swift over Objective-C?

Ans:

Q4: What are delegates? Why you need to use?

Ans: Delegates are a design pattern that allows one object to send messages to another object when a specific event happens.

Delegates are useful to offer to the user of your objects some ability to customize their behavior.

Q5: How you explain dependency Injection, how you use?

Ans: Dependency Injection is often used with the intention of writing code that is loosely coupled, and thus, easier to test. When we use dependency injection in our code, we are essentially giving an object its instance variables. What this means is that instead of giving our object the responsibility of creating it’s own dependencies, we inject the dependencies to the object instead. There are three ways to achieve :

  1. Property based injection
  2. Initializer based injection
  3. Method based injection

References:

  1. https://swift007blog.wordpress.com/2017/01/14/what-is-arc-in-ios/
  2. https://metova.com/concurrency-in-ios/
  3. https://mlsdev.com/blog/51-7-advantages-of-using-swift-over-objective-c

Hope this is going to help you. Please like and share with your colleagues!!!