Here’s a list of commonly asked interview questions for iOS developers focused on Objective-C, along with demonstrative and concise answers:

1. What is Objective-C?
Answer:
Objective-C is an object-oriented programming language that extends C by adding Smalltalk-style messaging. It’s primarily used for macOS and iOS development.
2. Explain the difference between id and instancetype.
Answer:
• id: A generic pointer to any Objective-C object. It doesn’t enforce type safety.
Example:
id myObject = @"Hello";
• instancetype: A compile-time type that returns the same type as the class.
Example:
+ (instancetype)sharedInstance;
3. What is a category in Objective-C?
Answer:
A category is a way to add methods to an existing class without modifying the original implementation.
Example:
@interface NSString (Reverse)
- (NSString *)reversedString;
@end
4. What are protocols in Objective-C?
Answer:
Protocols are similar to interfaces in other languages. They define methods that a class must implement.
Example:
@protocol MyProtocol
- (void)doSomething;
@end
5. What is the purpose of @synthesize and @dynamic?
Answer:
• @synthesize: Automatically generates getter and setter methods for a property.
Example:
@synthesize name = _name;
6. How does memory management work in Objective-C?
Answer:
Objective-C uses Automatic Reference Counting (ARC) to manage memory. Objects are automatically retained or released based on strong, weak, or unowned references.
Example:
@property (strong, nonatomic) NSString *name;
@property (weak, nonatomic) id delegate;
7. What is the difference between copy and assign properties?
Answer:
• copy: Creates a new copy of the object (used for immutable objects like NSString).
• assign: Directly assigns the reference (used for primitive data types like int).
8. Explain message passing in Objective-C.
Answer:
Message passing is how methods are called in Objective-C. Instead of direct calls, messages are sent to objects using square brackets.
Example:
[object performAction];
9. What is the use of NS_ASSUME_NONNULL_BEGIN and NS_ASSUME_NONNULL_END?
Answer:
These macros specify that all pointers between them are non-null by default unless marked with _Nullable.
10. How do you handle exceptions in Objective-C?
Answer:
Exceptions are handled using @try, @catch, and @finally blocks.
Example:
@try {
// Code that might throw an exception
}
@catch (NSException *exception) {
NSLog(@"Exception: %@", exception.reason);
}
@finally {
// Cleanup code
}
11. What is Key-Value Coding (KVC) in Objective-C?
Answer:
KVC allows accessing an object’s properties dynamically at runtime using keys.
Example:
[object setValue:@"value" forKey:@"key"];
NSString *value = [object valueForKey:@"key"];
12. Explain Key-Value Observing (KVO).
Answer:
KVO lets objects observe changes to specified properties of other objects.
Example:
[object addObserver:self forKeyPath:@"property" options:NSKeyValueObservingOptionNew context:nil];
13. What is the difference between nil, Nil, and NULL in Objective-C?
Answer:
• nil: Null pointer to an Objective-C object.
• Nil: Null pointer to a class.
• NULL: Null pointer for C pointers.
14. What is a block in Objective-C?
Answer:
A block is a chunk of code that can be passed around and executed later.
Example:
void (^simpleBlock)(void) = ^{
NSLog(@"This is a block!");
};
simpleBlock();
15. Explain the use of @autoreleasepool.
Answer:
@autoreleasepool is used to manage temporary objects in memory, ensuring they are released promptly.
Example:
@autoreleasepool {
NSString *temp = [NSString stringWithFormat:@"Hello"];
}
16. What is the difference between weak and assign?
Answer:
• weak: Non-owning reference to an object that is automatically set to nil when the object is deallocated.
• assign: Non-owning reference, but not set to nil automatically.
17. How does NSObject work in Objective-C?
Answer:
NSObject is the root class of most Objective-C classes. It provides fundamental methods like alloc, init, and dealloc.
18. What is the purpose of respondsToSelector:?
Answer:
It checks if an object can respond to a specific method at runtime.
Example:
if ([object respondsToSelector:@selector(methodName)]) {
[object methodName];
}
19. What is dynamic method resolution?
Answer:
Objective-C allows adding methods to classes at runtime using resolveInstanceMethod.
Example:
+ (BOOL)resolveInstanceMethod:(SEL)sel {
// Add method implementation dynamically
return YES;
}
20. What are the differences between class and instancetype?
Answer:
• class: Specifies the return type as a class type.
• instancetype: Returns an instance of the class in which it is declared.
Comments