Saturday, January 4, 2014

What is the difference between a deep copy and a shallow copy?

There are two kinds of object copying:  shallow copies and deep copies.

The normal copy is a shallow copy that produces a new collection that shares ownership of the objects with the original.

Deep copies create new objects from the originals and add those to the new collection.

 

What is the basic difference between coredata and sqlite?

SQLite is a relational database. But CoreData is an objective-c framework  which acts as a layer between the database and the UI.

What is the maximum size of SQLite Data database on iOS?

The max size of your SQLite database is only limited by free disk space.

What happens when you invoke a method on a nil pointer?

In objective-c calling a method on nil is valid and will be like no operation.

When dealing with property declarations, what is the difference between atomic and non-atomic?

atomic -Atomic means only one thread access the variable.Atomic is thread safe,but it is slow in performance.atomic is default behavior.Atomic accessors in a non garbage collected environment (i.e. when using retain/release/autorelease) will use a lock to ensure that another thread doesn't interfere with the correct setting/getting of the value. atomic it is not actually a keyword.

Ex: @property (retain) NSString *name; 
 
nonatomic -Nonatomic means multiple thread access the variable. Nonatomic is thread unsafe,but it is fast in performance.Nonatomic is NOT default behavior,we need to add nonatomic keyword in property attribute.It may result in unexpected behavior, when two different threads access the same variable at the same time.

Ex: @property (nonatomic,retain) NSString *name; 

Explain:
Suppose there is an atomic string property called "name", and if you call [self setName:@"A"] from thread A, call [self setName:@"B"] from thread B, and call [self name] from thread C, then all operation on different thread will be performed serially which means if one thread is executing setter or getter, then other threads will wait. This makes property "name" read/write safe but if another thread D calls [name release] simultaneously then this operation might produce a crash because there is no setter/getter call involved here. Which means an object is read/write safe (ATOMIC) but not thread safe as another threads can simultaneously send any type of messages to the object. Developer should ensure thread safety for such objects.

If the property "name" was nonatomic, then all threads in above example - A,B, C and D will execute simultaneously producing any unpredictable result. In case of atomic, Either one of A, B or C will execute first but D can still execute in parallel.

List the five iOS app states?

1.not running
2.inactive
3.active
4.background
5.suspended


What happens when the following code executes? Ball *ball = [[[[Ball alloc] init] autorelease] autorelease];

The application will crash.

Reason:

when alloc is called on Ball, object will be created and its retain count will be set to one.
Because of autorlease calls the ball object will be added to autorelease pool twice.
When pool is getting released release method will be called on the ball object twice.
When first time release method is called ball objects reatin count will become zero and object will be destroyed.Now when second time release method is called on that destroyed object  application will crash.