Interview Questions and Answers in iOS — Part 2
Interview questions and answers in iOS in very simple language.
Q. What is File Owner and First Responder?
A. File Owner: The file owner represents the object that is set to the class that is responsible for managing the content of the xib file.
First Responder: First Responder represents the object that the user is currently interacting with.
Q. What is the Responder Chain?
A. A Responder Chain is a hierarchy of objects that have the opportunity to respond to events received. The first object in the responder chain is called the first responder.
Q. Subdirectories in each of the application folder?
A. Document, Library, Temp
Q. What is a framework?
A. Apple delivers most of its system interface in special packages called a framework.
A framework is a directory that contains a dynamic shared library and the resources needed to support that library.
Q. What is the major purposes of Frameworks?
A. Frameworks have three major purposes:
Code encapsulation
Code modularity
Code reuse
Q. How do we obtain a C string from NSString?
A. By using the UTF8String method of the NSString class.
Q. What is the cache?
A. A cache is a temporary storage area. Caching (pronounced “cashing”) is the process of storing data in a cache.
Caching is the duplicity of original data which is stored in another location that is much easier and faster for retrieval then original data.
In Image caching, all images are stored in a temporary folder. If we ever visit that view again, the application will check whether that has been modified since we last downloaded it. If the image has not been modified since the app will display the image from the temporary folder instead of downloading it.
Q. Explain NSCache?
A. NSCache is an NSDictionary that automatically clears itself out when our app receives a memory warning. NSCache is a thread-safe.
Q. Foundation and Core Foundation Framework?
A. Foundation framework is an Objective C level API, which provides NSString, NSDictionary, and like that.
Core Foundation is a C level API, which provides CFString, CFDictionary and like that.
Q. What is toll-free bridging?
A. There is a number of data types in the Core Foundation framework and the Foundation framework that are used interchangeably. This capability called toll free bridging. It means we can use the same data type as the parameter to a Core Foundation function call or as the receiver of an objective c message. Example: NSLocale, CFLocale
Q. What is Regular expressions?
A. Regular expressions are special string patterns that describe how to search through a string.
Q. Action and Outlet?
A. Action: An action is a method that can handle events raised by view in the view controller.
Outlet: An outlet allows our code to programmatically reference a view on the view window.
Q. How could we set up Live Rendering?
A. The attribute @IBDesignable lets Interface Builder perform live updates on a particular view.
Q. Multitasking support is available from which version?
A. iOS 4.0
Q. Difference between Nil and nil?
A. Nil is meant for class pointer and nil is meant for an object pointer.
Q. UIButton class hierarchy?
A. NSObject → UIResponder → UIView → UIControl → UIButton
UITextField, UISlider, UIDatePicker, UIPageControl, UISegmentedControl, UIStepper, UISwitch etc are all inheriting from UIControl.
Q. Superclass of UIViewController?
A. UIResponder → UIViewController
Q. Superclass of UIWindow?
A. UIView → UIWindow
Q. How many windows are there for an iOS app?
A. An iOS app can only have one active key window at a time.
Apple doc says: Every application has at least one window that displays the application’s user interface on a device’s main screen. If an external display is connected to the device, applications can create a second window to present content on that screen as well.
Q. Interface and Implementation in Objective C?
A. An interface declares the behavior of class and an implementation defines the behavior of a class.
Q. What is an interface in Objective C?
A. An interface declares the behavior of the class. In Interface, we declare the attributes and operations of the class.
Q. What is method swizzling?
A. Method swizzling is the process of changing the implementation of an existing selector at runtime. This is an Objective-C runtime feature. We can achieve this feature in Swift as well.
Q. Why don’t we use strong for enum property in Objective-C?
A. Because enums aren’t objects, so we don’t specify strong or weak here.
Q. Callback mechanism?
A. Selectors, Delegates, Notifications, Blocks/Closure/CompletionHandler
Q. How to pass data between view controllers?
A. There are 3 ways to pass data between view controllers.
1) Segue, in prepareForSegue method (Forward)
2) Delegate (Backward)
3) Setting variable directly (Forward)
Q. Is it possible to create segues between different storyboards within the same project?
A. Yes. Using storyboard references.
Q. Explain Selectors in ObjC?
A. Selectors are Objective-C’s internal representation of a method name.
Q. What is Deep Linking?
A. Deep linking is a way to pass data to our application from any platform like a website or any other application. By tapping once on the link, we can pass the necessary data to our application.
Q. What is Keychain?
A. Keychain is an API for persisting data securely in iOS App.
Q. Explain the super keyword?
A. We use the super keyword to call the parent class initializer.
Q. What’s the difference between a xib and a storyboard?
A. Both are used in Xcode to layout screens (view controllers). A xib defines a single View or View Controller screen, while a storyboard shows many view controllers and also shows the relationship between them.
Q. What is the purpose of the reuseIdentifier?
A. Reusability of an already allocated object.
Q. What is dequeueReusableCellWithIdentifier?
A. dequeueReusableCellWithIdentifier is a method of UITableView class that returns a reusable table view cell class.
Q. What is ReuseIdentifier?
A. ReuseIdentifier is used to group together similar row in an UITableView i.e. rows that differ only in their content but otherwise have similar layouts.
If reuseIdentifier is set to a non-nil value then when the table view is scrolled, UITableView will first attempt to reuse an already allocated UITableViewCell with the same reuseIdentifier. If reuseIdentifier has not been set, the UITableView will be forced to allocate a new UITableViewCell object for each new item that scrolls into view. It will raise laggy animation.
Q. How many UITableViewCell are allocated when you first load a UITableView? How many additional ones are allocated as you scroll through the table?
A. There’s no single shot answer to this question, It depends upon device height, device orientation, cell height, and the number of different cell identifiers.
In the case of a table view with a single identifier, it’s very likely that the cache will be hit as soon as the first cell disappears from the screen (and therefore is inserted in cache). So if we are currently displaying 12 cells, and we start scrolling, the 13th will be probably allocated from scratch as the 1st is still visible, but the 14th will probably reuse the 1st one, which in the meanwhile has gone offscreen.
So a UITableView will normally allocate just enough UITableViewCell objects to display the content visible in the table. Because of the reuseIdentifier, the UITableView will not allocate new UITableViewCell objects for each new item that scrolls into view, avoiding laggy animations.
Q. Can we use one table view with two different data sources? How you will achieve this?
A. Yes, We can conditionally bind table view with two different data sources.
Q. Subclass, Category and Extensions in Objective C?
A.
1. Subclass: Subclassing in simple words is changing the behavior of properties or methods of an existing class or in other words, subclassing is inheriting a class and modifying the methods or properties of the superclass however we want.
Suppose for example consider a UITextField class, by default the placeholder text of UITextField will be of light gray color with the default system font. If we want to change this style just subclass UITextField and override the drawPlaceholderInRect method.
In addition to this look and feel the default delegate methods and properties of UITextField will remain the same.
2. Categories: An Objective C category allows us to add our own methods to an existing class. Categories are also called as “informal protocols”.
Suppose take an example, since Foundation Framework classes such as NSString, NSArray, NSDate, etc… don’t have any access to modify, we can add our own methods into these classes with the help of a category.
Note: Usually naming convention for category file is like OriginalClassName+CategoryName
Note that in a category we can’t add an instance variable since methods within a category are added to a class at runtime.
NSString+NSString_ReverseString.h#import <Foundation/Foundation.h>@interface NSString (NSString_ReverseString)- (NSString *)reverseString:(NSString *)yourString;@end
NSString+NSString_ReverseString.m#import "NSString+NSString_ReverseString.h"@implementation NSString (NSString_ReverseString)- (NSString *)reverseString:(NSString *)yourString
{
}@end
3. Extensions: — Extensions are similar to categories but the need of the extension is different.
- Class extensions are often used to extend the public interface with additional private methods or properties for use within the implementation of the class.
- Extensions can only be added to a class for which we have the source code at compile time (the class is compiled at the same time as the class extension).
- Extensions will be local to a class file.
The syntax to declare class extension looks like,
@interface ClassName()
@end
Since no name is given in the parentheses, class extensions are often referred to as anonymous or unnamed categories.
Note Extensions can add instance variables.
The compiler will automatically synthesize the relevant accessor methods, as well as an instance variable, inside the primary class implementation.
If we add any methods in a class extension, these must be implemented in the primary implementation for the class.
Usually, people will use extensions to hide private information of a class without exposing them to access from any other class.
Finally, a few simple points to remember are:
- Subclassing is a better option if you want to customize existing stuff or functionalities, and
- A category is the best option if you want to add additional functionalities to an existing class.
Q. Inheritance?
A. Inheritance generates a new class on its own right in which we can add new instance variable & override behavior from a parent class by polymorphism.
All class in objective C is inherited from the NSObject class.
iOS(Objective C & Swift) supports Single(Example Class A : B { } ) and Multilevel(Example Class A { } then Class B : A { } and then Class C : B { } ) Inheritance.
Q. Why category is better than inheritance?
A. If the category is used, we can use the same class, no need to remember a new class name.
Category created on a base class is available on the subclass.
Q. Difference between category and Inheritance?
A.
- Category allows adding methods only as in inheritance both the instance variable and methods can be added.
- Category’s scope is in full application whereas Inheritance’s scope is in a particular file only.
Q. What is Single Inheritance in Objective C?
A. Objective C subclass can only be derived from a single direct parent class. This concept is called Single Inheritance.
iOS(Objective C & Swift) supports Single(Example Class A : B { } ) and Multilevel(Example Class A { } then Class B : A { } and then Class C : B { } ) Inheritance.
Q. How would we create our own custom view?
A. Subclass the UIView class.
Q. Does Objective C contain private methods?
A. No, there is nothing called a private method in Objective C. If a method is defined in .m class only then it becomes protected. If in .h file, it is public. If we really want a private method then we need to add a local or unnamed category or class extension on the class.
Q. What is the difference CollectionViews & TableViews?
A. TableViews display a list of items, in a single column, a vertical fashion, and limited to vertical scrolling only.
CollectionViews also display a list of items, however, they can have multiple rows and columns with vertical & horizontal scrolling.
Q. Explain blocks in Objective C?
A. Blocks are a language-level feature added to C, Objective-C, and C++, which allow us to create distinct segments of code that can be passed around to methods or functions. They are anonymous functions.
- (void)callMyBlock:(void (^)(void))callbackBlock;
- (void)callMyBlock:(void (^)(double, double))block {
…
block(3.0, 2.0);
}
Blocks have two great features:
1. They can be executed at a later time, and not when the code of the scope they have been implemented is being executed.
2. Blocks offer a nice solution for creating callbacks instead of delegates.
Blocks are objects, so they can be stored to NSArray or NSDictionary data structures, as well as to be returned from methods, even to be assigned to variables.
Q. How many different annotations available in Objective-C?
A. _Null_unspecified, which bridges to a Swift implicitly unwrapped optional. This is the default.
_Nonnull, the value won’t be nil it bridges to a regular reference.
_Nullable the value can be nil, it bridges to an optional.
_Null_resettable the value can never be nil when reading but you can set it to know to reset it. This only applies to the property.
Q. How many APIs are there for battery-efficient location tracking?
A. There are 3 APIs.
Significant location changes — the location is delivered approximately every 500 meters (usually up to 1 km)
Region monitoring — track enter/exit events from circular regions with a radius equal to 100m or more. Region monitoring is the most precise API after GPS.
Visit events — monitor place Visit events which are enters/exits from a place (home/office).
Q. Explain throw?
A. We tell the compiler that it can throw errors by using the throws keyword. Before we can throw an error, we need to make a list of all the possible errors we want to throw.
Q. Why do we use availability attributes?
A. Apple wants to support one system version back. Availability Attributes lets us support previous version iOS.
More Details
Apple’s Documentation
Q. Explain Compilation Conditions?
A. A conditional compilation block allows code to be conditionally compiled depending on the value of one or more compilation conditions.
Every conditional compilation block begins with the #if compilation directive and ends with the #endif compilation directive. A simple conditional compilation block has the following form:
#if Compilation Condition
statements
#endif#if DEBUG
statements
#endif