iOS For Beginner

Interview Questions – Objective c

Question: If I call performSelector:withObject:afterDelay: – is the object retained?
Answer: Yes, the object is retained. It creates a timer that calls a selector on the current thread’s run loop. It may not be 100% precise timewise as it attempts to dequeue the message from the run loop and perform the selector.

Question: Can you explain what happens when you call autorelease on an object?
Answer: When you send an object a autorelease message, its retain count is decremented by 1 at some stage in the future. The object is added to an autorelease pool on the current thread. The main thread loop creates an autorelease pool at the beginning of the function, and release it at the end. This establishes a pool for the lifetime of the task. However, this also means that any autoreleased objects created during the lifetime of the task are not disposed of until the task completes. This may lead to the taskʼs memory footprint increasing unnecessarily. You can also consider creating pools with a narrower scope or use NSOperationQueue with itʼs own autorelease pool. (Also important – You only release or autorelease objects you own.)

Question: What’s the NSCoder class used for?
Answer: NSCoder is an abstract Class which represents a stream of data. They are used in Archiving and Unarchiving objects. NSCoder objects are usually used in a method that is being implemented so that the class conforms to the protocol. (which has something like encodeObject and decodeObject methods in them).

Question: What’s an NSOperationQueue and how/would you use it?
Answer: The NSOperationQueue class regulates the execution of a set of NSOperation objects. An operation queue is generally used to perform some asynchronous operations on a background thread so as not to block the main thread.

Question: Explain the correct way to manage Outlets memory.
Answer: Create them as properties in the header that are retained. In the viewDidUnload set the outlets to nil(i.e self.outlet = nil). Finally in dealloc make sure to release the outlet.

Question: Is the delegate for a CAAnimation retained?
Answer: Yes it is!! This is one of the rare exceptions to memory management rules.

Question: What happens when the following code executes?
Ball *ball = [[[[Ball alloc] init] autorelease] autorelease]; (For previous xcode ,iOS versions)

It will crash because itʼs added twice to the autorelease pool and when it is dequeued the autorelease pool calls release more than once.

Question: Explain the difference between NSOperationQueue concurrent and nonconcurrent.
Answer: In the context of an NSOperation object, which runs in an NSOperationQueue, the terms concurrent and nonconcurrent do not necessarily refer to the side by side execution of threads. Instead, a non concurrent operation is one that executes using the environment that is provided for it while a concurrent operation is responsible for setting up its own execution environment.

Question: Implement your own synthesized methods for the property NSString *title.
Answer: Well you would want to implement the getter and setter for the title object. Something like this: view source print?

– (NSString*) title // Getter method { return title;
– (void) setTitle: (NSString*) new Title //Setter method { if (new Title != title)
title = [new Title retain]; // Or copy, depending on your needs. }

Question: How to find the memory leaks in MRC?
Answer: Memory leaks can be find by using

1. Static analyzer.
2. Instrument

Question: Why do we need to use @Synthesize?
Answer: We can use generated code like nonatomic, atomic, retain without writing any lines of code. We also have getter and setter methods. To use this, you have 2 other ways:
1. @synthesize: compiler will generate the getter and setter automatically for you
2. @dynamic: you have to write them ourself.

@property is really good for memory management, for example: retain. How can you do retain without @property?

if (_variable != object)
[_variable release]; _variable = nil; _variable = [object retain]; }

How can you use it with @property?self.variable = object; When we are calling the above line, we actually call the setter like [self setVariable:object] and then the generated setter will do its job.

Question: What are all the difference between categories and subclasses?Why should we go to subclasses?
Answer: Category is a feature of the Objective C language that enables you to add methods (interface and implementation) to a class without having to make a subclass. There is no runtime difference—within the scope of your program—between the original methods of the class and the methods added by the category. The methods in the category become part of the class type and are inherited by all the class’s subclasses.As with delegation, categories are not a strict adaptation of the Decorator pattern, fulfilling the intent but taking a different path to implementing that intent. The behavior added by categories is a compile time artifact, and is not something dynamically acquired.

Question: What is notification in iOS?
Answer: The notification mechanism of Cocoa implements one to many broadcast of messages based on the Observer pattern. Objects in a program add themselves or other objects to a list of observers of one or more notifications, each of which is identified by a global string (the notification name). The object that wants to notify other objects—the observed object—creates a notification object and posts it to a notification center. The notification center determines the observers of a particular notification and sends the notification to them via a message.

The methods invoked by the notification message must conform to a certain single parameter signature. The parameter of the method is the notification object, which contains the notification name, the observed object, and a dictionary containing any supplemental information.Posting a notification is a synchronous procedure. The posting object doesn’t regain control until the notification center has broadcast the notification to all observers.

For asynchronous behavior, you can put the notification in a notification queue; control returns immediately to the posting object and the notification center broadcasts the notification when it reaches the top of the queue.Regular notifications—that is, those broadcast by the notification center—are interprocess only. If you want to broadcast notifications to other processes, you can use the distributed notification centre and its related API.

Question: What is the difference between delegates and notifications?
Answer: We can use notifications for a variety of reasons. For example, you could broadcast a notification to change how user interface elements display information based on a certain event elsewhere in the program. Or you could use notifications as a way to ensure that objects in a document save their state before the document window is closed. The general purpose of notifications is to inform other objects of program events so they can respond appropriately.But objects receiving notifications can react only after the event has occurred. This is a significant difference from delegation. The delegate is given a chance to reject or modify the operation proposed by the delegating object. Observing objects, on the other hand, cannot directly affect an impending operation.

Question: What is posing in iOS?
Answer: Objective C permits a class to entirely replace another class within an application. The replacing class is said to “pose as” the target class. All messages sent to the target class are then instead received by the posing class. There are some restrictions on which classes can pose:

● A class may only pose as one of its direct or indirect superclasses
● The posing class must not define any new instance variables which are absent from the target class (though it may define or override methods).
● No messages must have been sent to the target class prior to the posing.
● Posing, similarly to categories, allows globally augmenting existing classes. Posing permits two features absent from categories
● A posing class can call overridden methods through super, thus incorporating the implementation of the target class.
● A posing class can override methods defined in categories.

Question: What is atomic and nonatomic? Which one is safer? Which one is default?
Answer: You can use this attribute to specify that accessor methods are not atomic. (There is no keyword to denote atomic.)

Specifies that assessors are nonatomic. By default, accessors are atomic. Properties are atomic by default so that synthesized accessors provide robust access to properties in a multithreaded environment—that is, the value returned from the getter or set via the setter is always fully retrieved or set regardless of what other threads are executing concurrently.

Question: Where can you test Apple iPhone apps if you don’t have the device?
Answer: iOS Simulator can be used to test mobile applications. Xcode tool that comes along with iOS SDK includes Xcode IDE as well as the iOS Simulator. Xcode also includes all required tools and frameworks for building iOS apps. However, it is strongly recommended to test the app on the real device before publishing it.

Question: What is the purpose of UIWindow object?
Answer: The presentation of one or more views on a screen is coordinated by UIWindow object.

Question: How do you change the content of your app in order to change the views displayed in the corresponding window?
Answer: To change the content of your app, you use a view controller to change the views displayed in the corresponding window. Remember, window itself is never replaced.

Question: Define view object.
Answer: Views along with controls are used to provide visual representation of the app content. View is an object that draws content in a designated rectangular area and it responds to events within that area.

Question: Apart from incorporating views and controls, what else an app can incorporate?
Answer: Apart from incorporating views and controls, an app can also incorporate Core Animation layers into its view and control hierarchies.

Question: What are layer objects and what do they represent?
Answer: Layer objects are data objects which represent visual content. Layer objects are used by views to render their content. Custom layer objects can also be added to the interface to implement complex animations and other types of sophisticated visual effects.

Question: Define property?
Answer: It is used to access instance variables outside of class.

Question: Why synthesized is used?
Answer: After declaring property we will have to tell compiler instantly by using synthesize directive. This tells the compiler to generate setter and getter methods.

Question: What is retaining?
Answer: It is reference count for an object.

Question: What is tab bar controller?
Answer: It is used to display the data on the view.

Question: Which are the protocols used in table view?
Answer: Tableview contain two delegate protocols

(1) Uitableview data source
(2) Uitableviewdelegate.

In uiview tableview data source three method namely

(1)No of sections.
(2)No of rows in sections.
(3)Cell for row indexpath row.

In uitableview delegate contain

(1)Did select row at indexpath row

Question: What is single inheritance in objective c?
Answer: Objective c subclass can derived from a single parent class.It is called “single inheritance”.

Question: What is MVC architecture?
Answer: MVC stands for Model, V­iew, C­ontroller

Main advantage of MVC architecture is to provide “reusability and security” by separating the layer by using MVC architecture.

Model: it is a class model is interact with database.

Controller: controller is used for by getting the data from model and controls the views.

View : Display the information in views.

Question: What is the instance methods?
Answer: Instance methods are essentially code routines that perform tasks so instances of classes we create methods to get and set the instance variables and to display the current values of these variables.

Declaration of instance method :

– (void)clickme: (id)sender;

Void is return type which does not giving anything here. Click me is method name. Id is data type which returns any type of object.

Question: What is the class method?
Answer: Class methods work at the class level and are common to all instance of a class these methods are specific to the class overall as opposed to working on different instance data encapsulated in each class instance.

@interface classname :nsobject {


+(class name *)new alloc: (int)total open

Question: What is data encapsulation?
Answer: Data is contained within objects and is not accessible by any other than via methods defined on the class is called data encapsulation.

Question: What is accessor methods?
Answer: Accessor methods are methods belonging to a class that allow to get and set the values of instance valuables contained within the class.

Question: What is synthesized accessor methods?
Answer: Objective C provides a mechanism that automates the creation of accessor methods that are called synthesized accessor methods that are implemented through use of the @property and @synthesized.

Question: How to access the encapsulated data in objective c?
(a)Data encapsulation encourages the use of methods to +get and set the values of instance variables in a class.
(b)But the developer to want to directly access an instance variable without having to go through an accessor method.
(c) In objectivec syntax for an instance variable is as follow [class instance variable name]

Question: What is dot notation?
Answer: Dot notation features introduced into version 2.0 of objective c. Dot notation involves accessing an instance variable by specifying a class “instance” followed by a “dot” followed in turn by the name of instance variable or property to be accessed.

Question: Difference between shallow copy and deep copy?
Answer: Shallow copy is also known as address copy. In this process, in which only the address of the object i.e references are copied. Since the object being copied may have primitive data stored or any another object within it.In shallow copy completely copies primitive data but for object i.e pointing reference toward object, Only reference is copied whereas in deep copy copy of object where reference is pointing is also created/copied .

Question: Can we use two table view controllers on one view controller?
Answer: Yes, we can use two table views on the same view controllers and you can differentiate between two by assigning them tags…or you can also check them by comparing their memory addresses.

Question: Swap the two variable values without taking third variable?

int x=10;i
=x+y;NSLog(@”x==> %d”,x);

SLog(@”Y Value==> %d”,y);

SLog(@”x Value==> %d”,x);

Question: What is push notification?
Answer: Imagine, you are looking for a job. You go to software company daily and ask sir “is there any job for me” and they keep on saying no. Your time and money is wasted on each trip.(Pull Request mechanism).

So, one day owner says, if there is any suitable job for you, I will let you know. In this mechanism, your time and money is not wasted. (Push Mechanism)

How it works?

This service is provided by Apple in which rather than pinging server after specific interval for data which is also called pull mechanism, server will send notification to your device that there is new piece of information for you. Request is initiated by server not the device or client.

Flow of push notification

Your web server sends message (device token + payload) to Apple push notification service (APNS) , the APNS routes this message to device whose device token specified in notification.

Question: What is polymorphism?
Answer: This is very famous question and every interviewer asks this. Few people say polymorphism means multiple forms and they start giving example of draw function which is right to some extent but interviewer is looking for more detailed answer.

Ability of base class pointer to call function from derived class at runtime is called polymorphism.

Question: What is responder chain?



Suppose you have a hierarchy of views such like there is superview A which have subview B and B has a subview C. Now you touch on innermost view C. The system will send touch event to subview C for handling this event. If C View does not want to handle this event, this event will be passed to its superview B (next responder). If B also does not want to handle this touch event it will pass on to superview A. All the view which can respond to touch events are called responder chain. A view can also pass its events to uiviewcontroller. If view controller also does not want to respond to touch event, it is passed to application object which discards this event.

Question: Can we use a uitableview with two different data sources? How you will achieve this?
Answer: Yes. We can conditionally bind tableviews with two different data sources.

Question: What is a protocol?
Answer: A protocol is a language feature in objective C which provides multiple inheritance in a single inheritance language. Objective C supports two types of protocols:

● Ad hoc protocols called informal protocol
● Compiler protocols called formal protocol

Question: What is the navigation controller?
Answer: Navigation controller contains the stack of controllers every navigation controller must be having root view controller by default these controllers contain 2 method

(a) push view (b) pop view

By default navigation controller contain “table view”.

Question: What is the split view controller
Answer: This control is used for ipad application and it contain proper controllers by default split view controller contain root view controller and detail view controller.

Question: What is Objective C ?
Answer: Objective C is a very dynamic language. Its dynamism frees a program from compile time and link time constraints and shifts much of the responsibility for symbol resolution to runtime, when the user is in control. Objective C is more dynamic than other programming languages because its dynamism springs from three sources:

Dynamic typing—determining the class of an object at runtime Dynamic binding—determining the method to invoke at runtime Dynamic loading—adding new modules to a program at runtime

Question: Objective C vs C/C++?
1. The Objective C class allows a method and a variable with the exact same name. In C++, they must be different.
2.Objective C does not have a constructor or destructor. Instead it has init and dealloc methods, which must be called explicitly.
3.Objective C uses + and – to differentiate between factory and instance methods, C++ uses static to specify a factory method.
4.Multiple inheritance is not allowed in ObjC, however we can use protocol to some extent.
5.ObjC has runtime binding leading to dynamic linking.
6.ObjC has got categories.
7.ObjectiveC also does not allow stack based objects. Each object must be a pointer to a block of memory

Question: Retain cycle or Retain loop.
Answer: When object A retains object B, and object B retains A. Then Retain cycle happens. To overcome this use “close” method.

Objective C garbage collector (when enabled) can also delete retain loop groups but this is not relevant on the iPhone, where Objective C garbage collection is not supported.

Please login to get access to the quiz
Interview Questions- iOS Basics (Prev Lesson)
(Next Lesson) Interview Question-Web Service
Back to iOS For Beginner
Inquire Now
close slider