Popular Advanced iOS Interview Questions Flashcards

1
Q

Explain Objective-C in OS.

A

Since the 1990s, Objective-C has been used by Apple as an object-oriented programming language. This language combines the advantages of two earlier languages - C and Smalltalk. As a superset of C, it provides object-oriented functionality and a dynamic runtime environment.

Features of Objective-C :

  1. In Objective C, meta classes are automatically created and managed during runtime.
  2. Dynamic typing and static typing are both supported.
  3. It is easy to understand.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Name the most important data types in Objective-C?

A

Following are data types that the developers mostly use in Objective – C:

  1. BOOL: Represents a Boolean value that is either true or false. Both the _Bool or BOOL keywords is valid.

Example:
_Bool flag = 0;
BOOL secondflag = 1;

  1. NSInteger: Represents an Integer.

Example:
typedef long NSInteger;
typedef int NSInteger;

  1. NSUInteger: Represents an unsigned integer

Example:
typedef unsigned long NSUInteger;
typedef unsigned int NSUInteger;

  1. NSString: Represents a string.

Example:
NSString *greeting = @”Hello”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain Swift in iOS?

A

Swift is the fastest-growing programming language today, created by Apple. With a significant advantage pool over the well-known Objective-C, Swift holds a leading position in iOS development. It’s an entirely new language created specifically to develop software for Apple’s operating systems. Since Swift implements all the features of other modern languages, you can find type inference, optional, generics, and such higher-order functions. It is compatible with macOS, iOS, watchOS, and tvOS.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the important features of Swift?

A

Swift programming language is being designed so that developers can write correct programs and maintain them easily. It offers the following features:

  1. Safety: Swift is an efficient way to write programs. Checking code before it is used in production is very important. Apple Swift removes any unsafe code before it is used in production.
  2. Simple syntax: Swift’s syntax is simple and easy-to-use, just as developers would expect it. The syntax features of Swift enable you to write more expressive code.
  3. Readability: Swift has a simple syntax, which is easier to read and write. It is easier for developers to write Swift code since it is more similar to plain English, enabling them to spend less time looking for problematic code.
  4. Multiplatform support: Swift is fully compatible with iOS, macOS, tvOS, watchOS, Linux, and many other platforms. This means you are able to develop software that is compatible with all operating systems.
  5. Open-source: Swift is developed at swift.org, an open-source framework. For Swift to become a defining programming language, the technology had to be open to all. Swift supports all Apple platforms and makes programming easier, faster, and safer.
  6. Compatible with Objective C: Swift has full compatibility with Objective-C. Swift enables programmers to import frameworks from Objective-C using the Swift syntax. Programmers can utilize Objective-C libraries and classes inside Swift code.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain NSError in Swift.

A

Information about an error condition is encapsulated within an NSError object in an extendable and object-oriented manner. An NSError object is comprised of three basic attributes: a predefined error domain (represented as a string), a domain-specific error code, and a user info dictionary containing application-specific information.

Example: The examples below show how to create custom errors.

NSString *domain = @”com.MyCompany.MyApplication.ErrorDomain”;

NSString *desc = NSLocalizedString(@”Unable to complete the process”, @””);

NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : desc };

NSError *error = [NSError errorWithDomain:domain code:-101 userInfo:userInfo];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Enum or Enumerations in Swift?

A

The term enumeration refers to a user-defined data type consisting of a set of related values that allows you to work with those values in your code in a type-safe manner. An enumerated data type is defined by the keyword enum.

Syntax:

enum enum_name
{
// enumeration values are described here
}

Example:

enum MonthsofaYear
{
case January
case Februrary

case December
}

Values defined in an enumeration MonthsofaYear such as January, February, and so on until December are its enumeration cases. New enumeration cases can be introduced using the case keyword. You can put multiple cases on a single line, separated by commas as follows:

enum MonthsofaYear
{
case January, February,….,December
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What do you mean by lazy property in iOS?

A

Lazy properties are properties whose initial value isn’t computed until the first time they are used. Including the lazy modifier/keyword before the declaration of a stored property indicates it is lazy.This lets you delay the initialization of stored properties.

This could be a great way to streamline your code and reduce unnecessary work. When a code is expensive and unlikely to be called consistently, a lazy variable can be a great solution.

Example:

class Person {
var name: String
lazy var personalizdgreeting : String = {
return “HelloScala (self.name)!”
}()
init(name: String) {
self.name = name
}
}

As shown above, we aren’t sure what value personalizedgreeting should have. To know the correct greeting for this person, we need to wait until this object is initialized.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are generics in swift and write its usage?

A

A major feature of Swift is generics, and much of the Swift standard library is written in generic code. Swift’s ‘Array’ and ‘Dictionary’ types, for example, constitute generic collections. Generic code allows you to create flexible, reusable functions and types that work with any data type. You can create code that does not get too specific about underlying data types, resulting in cleaner code.

Example:

func Swapping(x: inout Int, y: inout Int)
{
let temp = x
x = y
y = temp
}
var num1 = 10
var num2 = 50
print(“Before Swapping: (num1) and (num2)”)
Swapping(x: &num1, y: &num2)
print(“After Swapping: (num1) and (num2)”)

Output:

Before Swapping: 10 and 50
After Swapping: 50 and 10

In the above example, we have defined a function called Swapping() to swap integers. It takes two parameters x and y of int type. As seen in the output, the values of x and y are exchanged after the swapping.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain dictionary in Swift.

A

Swift programming defines a dictionary as an unordered collection of items. It stores items in key-value pairs. A dictionary uses a unique identifier called a key to store an associated value which can later be referenced and retrieved through the same key.

Syntax:

var Dict_name = KeyType: ValueType

Example:

var examresult= [“Rahul”: “79”, “Aditya”: “86”, “Aditi”: “67”]
print(examresult)

Output:

[“Rahul”: “79”, “Aditya”: “86”, “Aditi”: “67”]

In the above example, we have created a dictionary named examresult. Here,

Keys are “Rahul”, “Aditya”, “Aditi”
Values are “79”, “86”, “67”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why design patterns are important? Name some of the popular design patterns used in iOS?

A

A design pattern is a solution to a specific problem you might encounter while designing an app’s architecture. They are templates designed to make coding easier and more reusable for you. Following are some of the design patterns that can be used in iOS:

  1. MVC (Model-View-Controller):

MVC is a design pattern for developing web applications in which three components are connected i.e., view, controller, and model. Views can be conceived as the user interface shown to the end-user at a certain point in time. The model represents the data to be shown on the view. The controller acts as a link between view and model. There is always a relationship between these three components.

  1. MVVM (Model-View-View Model):

Unlike MVM, there is a special layer in MVVM called the View Model between the View and the Model. Using a view model, model information can be transformed into values that can be displayed on the view. There is a binding link between the view and View model that allows the view model to share updates with the view.

  1. Facade:

The facade design pattern provides a simplified (but limited) interface to a complex set of classes, libraries or frameworks. As opposed to providing the user with a set of classes and their APIs, you instead provide a simple, unified API. While Facade helps to reduce overall application complexity, it also helps to move unwanted dependencies to a single location.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the JSON framework supported by iOS?

A

iOS supports the SBJson framework as the JSON framework. Humans and computers alike can easily read and write this lightweight data exchange formatter. JSON handling is simplified with SBJson’s flexible APIs and additional control.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Explain iBeacons in iOS.

A

iBeacon, Apple’s new low-energy Bluetooth wireless technology, allows iPhone and other iOS users to receive location-based information and services on smartphones. IBeacons are small, wireless transmitters that transmit signals to nearby smart devices via Bluetooth low energy technology.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

State the difference between KVC and KVO in Swift.

A

KVC (Key-Value Coding): It enables object properties to be accessed at runtime using strings rather than knowing the property names statically during development.

KVO (Key-Value Observing): In Objective-C and Swift, KVO is one of the methods for observing program state changes. If an object has instance variables, KVO enables other objects to observe the changes to those variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain TDD (Test-Driven Development).

A

Software developers can use testing-driven development (TDD) in the development of software.

In TDD, developers plan the features of the software that they want to create and then write test cases for each feature before implementing it.

Through test-driven development, we can gain insight into both the quality of the implementation (does it work) and the quality of the design (is it well structured).

1.FAIL –>2.PASS—>3. REFACTOR

At first, the test case will fail because the code is not yet implemented, and this is usually referred to as the red phase. After that, code is written to ensure that the test case passes and does not break any components or current test cases, which is called the green phase. The developer should then refactor the implementation of the code by cleaning and maintaining the codebase, as well as optimizing the efficiency. This process should then be repeated every time a new test case is added.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain the function of the completion handler.

A

Completion handlers are basically just functions passed as parameters to other functions. They are used to dealing with the response of asynchronous tasks since we do not know when they will end. Completion handlers inform an application when an operation, such as an API call, has been completed. The program is informed that the next step needs to be executed.

Example:

Let’s create a simple class called CompletionHandler that has one method called count that counts from 0 to 50. Once it reaches 25 (a random value), it will make a network request to https://scaler.com. Once the request has been completed, we will print Received response.

class CompletionHandler {
func count() {
for i in 0…50 {
if i == 25 {
if let url = URL(string: “https://scaler.com”) {
URLSession.shared.dataTask(with: url) { (data, response, error) in
print(“Received response”)
}.resume()
}
}
print(“I = “, i)
}
}
}
let newInstance = CompletionHandler()
newInstance.count()

You will see all the numbers printed out in the console as soon as the code runs, but the Received response will be printed only after all those other numbers have been printed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

State the difference between strong, weak, read only and copy.

A

The difference between strong, weak, read-only and copy is as follows:

  1. Strong: This property maintains a reference to property throughout the lifetime of an object. When you declare strong, you intend to “own” the object you are referencing. Data you assign to this property will not be destroyed as long as you or any other object references it strongly.
  2. Weak: It means that the object should be kept in memory as long as someone points to it strongly, and you don’t need control over its lifetime.
  3. Read-only: An object’s property can be defined initially, but it cannot be altered or modified.
  4. Copy: This attribute is an alternative to strong. In place of taking ownership of a current object, it creates a copy of whatever you assign to the property, then takes ownership of that copy.
17
Q

What do you mean by dynamic dispatch?

A

In simple terms, dynamic dispatch means that the program decides at runtime which implementation of a particular method or function it needs to invoke. In the case where a subclass overrides a method of its superclass, dynamic dispatch determines whether to invoke the subclass implementation of the method or the parents.

18
Q

Explain the @dynamic and @synthesize commands in Objective-C.

A
  1. @synthesize: This command generates getter and setter methods within the property and works in conjunction with the @dynamic command.

By default, @synthesize creates a variable with the same name as the target of set/get as shown in the below example.

Example1:
@property (nonatomic, retain) NSButton *someButton;

@synthesize someButton;

It is also possible to specify a member variable as its set / get target as shown below:

Example2:
@property (nonatomic, retain) NSButton *someButton;

@synthesize someButton= _homeButton;

Here, the set / get method points to the member variable _homeButton.

  1. @dynamic: This tells the compiler that the getter and setter methods are not implemented within the class itself, but elsewhere (like the superclass or that they will be available at runtime).

Example:
@property (nonatomic, retain) IBOutlet NSButton *someButton;

@dynamic someButton;

19
Q

How can you implement storage and persistence in iOS?

A

Persistence means storing data on the disk so that it can be retrieved without being altered the next time the app is opened. From simple to complex, there are the following methods for storing data:

  1. Data structures such as arrays, dictionaries, sets, and other data structures are perfect for storing data intermediately.
  2. NSUserDefaults and Keychains are both simple key-value stores. NSUserDefaults is insecure, whereas Keychains is secure.
  3. A file or disk storage is a way to store data (serialized or not) to or from a disk using NSFileManager.
  4. Relational databases, such as SQLite, are good for implementing complex querying mechanisms.
20
Q

What are synchronous and asynchronous tasks in iOS? In what way can you execute asynchronous tasks in iOS?

A

Apple’s iOS supports both synchronous and asynchronous tasks. In synchronous operations, tasks are performed one at a time. Therefore, other tasks must wait until the previous task is completed before continuing. Asynchronous tasks run simultaneously in the background. If background tasks are completed, you will be notified. Asynchronous programming allows you to process multiple requests at the same time, so you can accomplish more tasks in a shorter amount of time.

Async work can be handled using third-party libraries. A few notable examples are Promises (PromiseKit), RxSwift, and ReactiveCocoa.