iphone Interview Questions part-2

11-Can you explain what happens when you call autorelease on an object?

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.)

12-Whats the NSCoder class used for?

NSCoder is an abstractClass 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).

13-Whats an NSOperationQueue and how/would you use it?

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.

14-Explain the correct way to manage Outlets memory

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.

15-Is the delegate for a CAAnimation retained?

Yes it is!! This is one of the rare exceptions to memory management rules.

16-What happens when the following code executes?

Ball *ball = [[[[Ball alloc] init] autorelease] autorelease];

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

17-Explain the difference between NSOperationQueue concurrent and non-concurrent.

In the context of an NSOperation object, which runs in an NSOperationQueue, the terms concurrent and non-concurrent 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.

 

18-Implement your own synthesized methods for the property NSString *title.

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*) newTitle //Setter method

{

if (newTitle != title)

{

[title release];

title = [newTitle retain]; // Or copy, depending on your needs.

}

}

19-Implement the following methods: retain, release, autorelease.

-(id)retain

{

NSIncrementExtraRefCount(self);

return self;

}

-(void)release

{

if(NSDecrementExtraRefCountWasZero(self))

{

NSDeallocateObject(self);

}

}

-(id)autorelease

{ // Add the object to the autorelease pool

[NSAutoreleasePool addObject:self];

return self

20-What are the App states. Explain them?

  • Not running State: The app has not been launched or was running but was terminated by the system.
  • Inactive state: The app is running in the foreground but is currently not receiving events. (It may be executing other code though.) An app usually stays in this state only briefly as it transitions to a different state. The only time it stays inactive for any period of time is when the user locks the screen or the system prompts the user to respond to some event, such as an incoming phone call or SMS message.
  • Active state: The app is running in the foreground and is receiving events. This is the normal mode for foreground apps.
  • Background state: The app is in the background and executing code. Most apps enter this state briefly on their way to being suspended. However, an app that requests extra execution time may remain in this state for a period of time. In addition, an app being launched directly into the background enters this state instead of the inactive state. For information about how to execute code while in the background, see “Background Execution and Multitasking.”
  • Suspended state:The app is in the background but is not executing code. The system moves apps to this state automatically and does not notify them before doing so. While suspended, an app remains in memory but does not execute any code. When a low-memory condition occurs, the system may purge suspended apps without notice to make more space for the foreground app.

Discover more from mycodetips

Subscribe to get the latest posts sent to your email.

Discover more from mycodetips

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top