Sie sind auf Seite 1von 10

iPhone Interview questions & Objective C Interview question

Posted by idevfans on June 4, 2012

I have sharing here my experience of iPhone interview questions. Here, you can find some common iPhone interview questions & Objective C Interview questions. It will be helpful to you for an interview in iPhone. You can go through it before interview as an iPhone developer. 1) Diffrence between atomic and nonatomic The best was to understand the difference is using following example. 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. Hope this helps. 2) Difference between notifications and delegates The two differences between delegates and notifications is that delegate is a one-to-one connection between the delegate and the delegating object. Notifications are posted application wide and can be observed by as many objects as needed creating a one-to-many connection. Think about this as a broadcast. The second main difference is that delegation can be used to transfer information back from the delegate to the delegating object. Meaning that the delegating object asks the delegate for certain information. Typical example would be the data source of an UITableView. Notifications however are a one way street. The information flows from the posting object to the observing objects. This makes sense because think about the situation where you would have more than one observer and each would give feedback to the posting objects. Which one would be the right one? In your case you would have to look up the delegate methods of the asynchronous HTTP request object and implement them accordingly.

3) What is autorelease: Every time -autorelease is sent to an object, it is added to the inner-most autorelease pool. When the pool is drained, it simply sends -release to all the objects in the pool. Autorelease pools are simply a convenience that allows you to defer sending -release until later. That later can happen in several places, but the most common in Cocoa GUI apps is at the end of the current run loop cycle. 4) Three occasions when you might use your own autorelease pools: If you are writing a program that is not based on a UI framework, such as a command-line tool. If you write a loop that creates many temporary objects.You may create an autorelease pool inside the loop to dispose of those objects before the next iteration. Using an autorelease pool in the loop helps to reduce the maximum memory footprint of the application. If you spawn a secondary thread. You must create your own autorelease pool as soon as the thread begins executing; otherwise, your application will leak objects. 5) InApp purchase product type: Consumable products must be purchased each time the user needs that item. For example, one-time services are commonly implemented as consumable products. Non-consumable products are purchased only once by a particular user. Once a non-consumable product is purchased, it is provided to all devices associated with that users iTunes account. Store Kit provides built-in support to restore non-consumable products on multiple devices. Auto-renewable subscriptions are delivered to all of a users devices in the same way as non-consumable products. However, auto-renewable subscriptions differ in other ways. When you create an auto-renewable subscription in iTunes Connect, you choose the duration of the subscription. The App Store automatically renews the subscription each time its term expires. If the user chooses to not allow the subscription to be renewed, the users access to the subscription is revoked after the subscription expires. Your application is responsible for validating whether a subscription is currently active and can also receive an updated receipt for the most recent transaction. Free subscriptions are a way for you to put free subscription content in Newsstand. Once a user signs up for a free subscription, the content is available on all devices associated with the users Apple ID. Free subscriptions do not expire and can only be offered in Newsstandenabled apps. 6) the advantages and disadvantages about synchronous versus asynchronous connections.

Thats it, pretty fast and easy, but there are a lot of caveats : The most important problem is that the thread which called this method will be blocked until the connection finish or timeout, so we surely dont want to start the connection on the main thread to avoid freezing the UI. That means we need to create a new thread to handle the connection, and all programmers know that threading is hard. Cancellation, its not possible to cancel a synchronous connection, which is bad because users like to have the choice to cancel an operation if they think it takes too much time to execute. Authentication, there is no way to deal with authentication challenges. Its impossible to parse data on the fly. So lets put it up straight, avoid using synchronous NSURLConnection, there is absolutely no benefit of using it. Its clear that asynchronous connections give us more control : You dont have to create a new thread for the connection because your main thread will not be blocked. You can easily cancel the connection just by calling the cancel method. If you need authentication just implement the required delegate methods. Parsing data on the fly is easy. So clearly we have a lot of more control with this, and the code is really not difficult. Even better, we dont have to handle the creation of a new thread, which is a good thing, because you know, threading is hard. Well, if you read me until here, you should be convinced to use asynchronous connections, and forget about synchronous ones. They clearly give us more control and possibilities and, in some case can spare us to create new thread. So I encourage you to move away from synchronous connections, just think of them as evil. 7) JSON or XML?

In general, I would prefer XML only when I need to pass around a lot of text, since XML excels at wrapping and marking up text. When passing around small data objects, where the only strings are small (ids, dates, etc.), I would tend to use JSON, as it is smaller, easier to parse, and more readable. Also, note that even if you choose XML, that does not by any means mean you need to use SOAP. SOAP is a very heavy-weight protocol, designed for interoperability between partners. As you control both the client and server here, it doesnt necessarily make sense. 8) Delegate: A delegate allows one object to send messages to another object when an event happens.A delegate is just an object that another object sends messages to when certain things happen, so that the delegate can handle app-specific details the original object wasnt designed for. Its a way of customizing behavior without subclassing. Please find some more iPhone interview questions as below. NSOperation queue. When to use NSMutableArray and when to use NSArray ? Difference between frame and bounds ? What is retain count ? and When retain count increase ? What is core data? What are the different data storage methods in iPhone ? What is the different between core data & SQLite ? Difference between copy, retain, assign ? What is the difference between frames & bound ? Memory management in iPhone application ? What is autorelease pool in iPhone application ? What is protocol and how can we define it ? What is the use of protocol What is Polymorphism ? Push notification. NSDictionary New features in latest iOS Have you ever used any analytical tools for iOS application ? Have you ever used UIAnimation ?

iOS Interview Questions


POSTED BY JEFF . FILED UNDER DEVELOPERS, IPHONE OS, OBJECTIVE-C | 64 COMMENTS

Ive been interviewing alot of people recently for iPhone and iPad developer positions. Asides from algorithm runtime problems, I ask some general iOS questions. So being the kind person I am, im posting some questions based on difficulty and expected answers. NOTE: If you want to test your iOS skills before you read the answers, heres a text file just with questions iOS Interview questions BEGINNER Q: How would you create your own custom view?A: Subclass the UIView class. Q: Whats fast enumeration?A: Fast enumeration is a language feature that allows you to enumerate over the contents of a collection. (Your code will also run faster because the internal implementation reduces message send overhead and increases pipelining potential.) Q: Whats a struct? A: A struct is a special C data type that encapsulates other pieces of data into a single cohesive unit. Like an object, but built into C.

Q: Whats the difference between a NSArray and a NSMutableArray? A: A NSArrays contents can not be modified once its been created whereas a NSMutableArray can be modified as needed, i.e items can be added/removed from it. Q: Explain retain counts.A: Retain counts are the way in which memory is managed in Objective-C. When you create an object, it has a retain count of 1. When you send an object a retain message, its retain count is incremented by 1.When you send an object a release message, its retain count is decremented by 1. When you send an object a autorelease message, its retain count is decremented by 1 at some stage in the future. If an objects retain count is reduced to 0, it is deallocated. Q: Whats the difference between frame and bounds?A: The frame of a view is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.The bounds of a view is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0). Q: Is a delegate retained?A: No, the delegate is never retained! Ever! INTERMEDIATE Q: If I call performSelector:withObject:afterDelay: is the object retained? A: Yes the object is retained. It creates a timer that calls a selector on the current threads run loop. It may not be 100% precise time-wise as it attempts to dequeue the message from the run loop and perform the selector. Q: Can you explain what happens when you call autorelease on an object? A: 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 tasks memory footprint increasing unnecessarily. You can also consider creating pools with a narrower scope or use NSOperationQueue with its own autorelease pool. (Also important You only release or autorelease objects you own.) Q: Whats the NSCoder class used for? A: 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). Q: Whats an NSOperationQueue and how/would you use it? A: 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. Q: Explain the correct way to manage Outlets memoryA: 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. ADVANCED Q: Is the delegate for a CAAnimation retained? A: Yes it is!! This is one of the rare exceptions to memory management rules. Q: What happens when the following code executes? 1 Ball *ball = [[[[Ball alloc] init] autorelease] autorelease]; A: It will crash because its added twice to the autorelease pool and when it it dequeued the autorelease pool calls

release more than once. Q: Outline the class hierarchy for a UIButton until NSObject. A: UIButton inherits from UIControl, UIControl inherits from UIView, UIView inherits from UIResponder, UIResponder inherits from the root class NSObject Q: Explain the difference between NSOperationQueue concurrent and non-concurrent. A: 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. Q: Implement your own synthesized methods for the property NSString *title. A: Well you would want to implement the getter and setter for the title object. Something like this: 1 - (NSString*) title { 2 3 } 4 - (void) setTitle: (NSString*) newTitle { 5 6 7 if (newTitle != title) { [title release]; title = [newTitle retain]; // Or copy, depending on your needs. return title;

8 9 }

Q: Implement the following methods: retain, release, autorelease.A: 01 -(id)retain { 02 03 04 05 } 06 07 -(void)release { 08 09 if(NSDecrementExtra RefCountWasZero(s elf)) { NSDeallocateObjec t(self); } return self; NSIncrementExtraRef Count(self);

10 11 12 } 13

14 -(id)autorelease {

15

// Add the object to the autorelease pool [NSAutoreleasePool addObject:self];

16 17 18 19 }

return self;

Das könnte Ihnen auch gefallen