Use our engineer-created questions to interview and hire the most qualified Objective-C developers for your organization.
Objective-C is still popular even though Swift has become the language of choice for macOS and iOS apps. This is because it works with both C and C, has a dynamic runtime, and has a mature ecosystem.
Our team has made functional programming tasks and discussion questions that are specifically designed to test developers’ Objective-C skills during coding tests. We’ve also put together a list of the best ways to make sure that your interview questions are accurate when testing candidates’ Objective-C knowledge.
Objective-C is one of the core programming languages used for developing applications on Apple platforms like iOS and macOS. Even with the rise of Swift, Objective-C still remains relevant due to its seamless interoperability with C and C++. As an Objective-C developer you need to demonstrate proficiency in Objective-C programming concepts memory management, frameworks like Cocoa/Cocoa Touch, and design patterns.
Preparing for an Objective-C job interview? Here are the top 10 commonly asked Objective-C interview questions that you should review:
1. What is the difference between strong, weak and assign property attributes in Objective-C?
-
strong
means that the reference to the object is retained. This is the default attribute for object properties under ARC. The object will not be deallocated until all strong references are removed. -
weak
means the reference does not retain the object. It is used to avoid strong reference cycles. The object can be deallocated even when a weak reference exists. -
assign is used for primitive data types It assigns a value directly without retaining it The object pointed to can be deallocated.
Using the right attributes avoids crashes from dangling pointers. strong
is appropriate in most cases. weak
prevents retain cycles and assign
should be used for primitive types.
2. Explain ARC (Automatic Reference Counting) in Objective-C.
ARC handles memory management in Objective-C by automatically inserting retain, release and autorelease messages. The compiler determines where these are needed based on the ownership semantics of objects in code.
Benefits of ARC:
-
Eliminates manual reference counting, simplifying code.
-
Avoid crashes caused by incorrect manual memory management.
-
Code remains portable between ARC and non-ARC files.
ARC works by analyzing lifetime semantics of objects. Rules are enforced at compile time to ensure objects are not prematurely deallocated or over-retained.
3. What are blocks in Objective-C? Give an example.
Blocks or closures are self-contained chunks of code that can be passed around and executed asynchronously. The code block encapsulates the context in which it was created.
Syntax:
returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};
Example:
void (^completionBlock)(BOOL) = ^(BOOL finished) { if (finished) { NSLog(@"Task completed!"); }};
Blocks are useful for:
- Passing behavior/code as arguments to methods
- Callback methods and asynchronous tasks
- Delaying execution of code snippets
4. What is a delegate in Objective-C?
A delegate is an object that acts on behalf of another object. It is used to handle events or perform tasks on behalf of the original object.
The delegating object keeps a reference to the delegate object. It sends messages to the delegate object when specific events occur.
For example, UITableView
uses delegate objects to manage row selection, accessory views and reordering of table rows. Delegation avoids subclassing UITableView
for minor customizations.
5. How is memory management handled in Objective-C?
There are two approaches:
Manual memory management
The developer is responsible for allocating and freeing memory explicitly. Methods like alloc
, new
, retain
, release
are used directly.
Automatic Reference Counting (ARC)
The compiler automatically inserts retain/release messages based on ownership semantics. Manual reference counting is no longer needed.
ARC simplifies memory management. Manual approach is still useful for some advanced scenarios or older projects. Proper usage of retain
, release
and autorelease
is needed to avoid crashes due to bad memory access.
6. What are categories in Objective-C?
Categories allow you to add methods to existing classes in Objective-C without subclassing. This is useful for:
-
Organizing code by grouping methods into logical categories.
-
Extending classes that you don’t have access to the source code.
-
Distributing class extensions without modifying original code.
Syntax:
@interface ClassName (CategoryName)// category methods@end
Categories can’t override existing methods. New methods can call original class methods using method swizzling.
7. What are protocols in Objective-C?
Protocols declare methods that implementing classes need to provide. They formalize what classes should be capable of doing.
Syntax:
@protocol ProtocolName @required // required methods @optional // optional methods @end
Classes adopt protocols using <ProtocolName>
syntax. Multiple protocols can be adopted, enabling protocols to require other protocols.
Protocols enable polymorphism. The type can be defined using the protocol instead of a specific class. Subclasses can share the same protocol conformance.
8. What is fast enumeration in Objective-C?
Fast enumeration provides a simple way to iterate through a collection using the for-in
loop:
for (Element element in collection) { // element represents current object}
This avoids manually iterating through the collection by index and handling the internal storage. Fast enumeration works for any class that conforms to the NSFastEnumeration
protocol.
Benefits:
- Simplified iteration syntax more like other languages
- Avoid manual iteration of indexes & storage
- Improved performance through specialized enumeration
9. What are declared properties in Objective-C?
Declared properties expose class attributes for external use. They provide accessor methods for setting and getting values directly.
Syntax:
@property attribute1 attribute2 type name;
Defining properties in the class interface declares the accessor methods automatically. Attributes like readonly
can customize access control.
Benefits:
- Encapsulate access to class attributes
- Declare accessor methods concisely
- Customize access control logic in one place
- Support key-value coding and observation
10. What is difference between retain and copy for Objective-C properties?
-
retain
simply retains the object passed to the setter method. The same instance is referenced from the property. -
copy
creates a new copy of the object and stores it. This ensures the property has an immutable copy even if the original object changes later.
Use copy
for NSString
and other immutable objects to guarantee immutability. Avoid unexpected changes from mutating the original objects passed to the property.
Retain is suitable for object references that do not need to be immutable. It avoids the performance cost of copying.
By mastering these key Objective-C concepts, you will demonstrate comprehensive knowledge of the language to prospective employers. Prepare sufficiently for the coding interview by practicing questions similar to these. Your hard work will help you land your ideal iOS or macOS development role!
Help us design a parking lot
Hey candidate! Welcome to your interview. Boilerplate is provided. Feel free to change the code as you see fit. To run the code at any time, please hit the run button located in the top left corner.
Goals: Design a parking lot using object-oriented principles
Here are a few methods that you should be able to run:
- Tell us how many spots are remaining
- As a whole, how many parking spots are there?
- Tell us when the parking lot is full
- Tell us when the parking lot is empty
- Tell us when certain spots are full e. g. when all motorcycle spots are taken.
- Tell us how many spots vans are taking up
Assumptions:
- The parking lot can hold motorcycles, cars and vans
- Some spots are for motorcycles, some are for cars, and some are big.
- A motorcycle can park in any spot
- A car can park in either a regular spot or a single tight spot.
- There is room for a van, but it will take up three space
- These are just a few assumptions. If you need to, you can ask your interviewer about more assumptions.
Senior Objective-C interview questions
Question:Explain the concept of blocks in Objective-C. How do blocks let you grab and use variables from the surrounding scope? Give an example of how blocks are used.
Answer:Blocks in Objective-C are self-contained, anonymous segments of code that can be passed around and executed later. They are similar to closures in other programming languages. Blocks hold variables and constants from the scope around them, enclosing both the code and the environment in which it was written.
Here’s an example demonstrating the usage of blocks:
In this example, a block type CompletionBlock is defined using the typedef keyword. The performTaskWithCompletion: method takes a block as a parameter and executes it after performing a task. The block takes a BOOL parameter indicating the success status of the task.
Blocks are commonly used for asynchronous operations, callback mechanisms, and implementing higher-order functions. They let variables from the surrounding scope be captured, so the block can access and change them even after the surrounding scope has ended.
Question:Explain the concept of method swizzling in Objective-C. How does it work, and what are the potential use cases?.
Answer:Method swizzling in Objective-C is a technique that allows developers to exchange the implementations of methods at runtime. It involves swapping the implementations of two methods, typically belonging to the same class or different classes.
Method swizzling works by manipulating the Objective-C runtime and is primarily accomplished using two functions: class_getInstanceMethod and method_exchangeImplementations. These functions retrieve the method implementations and then swap them.
Potential use cases of method swizzling include:
- Adding Features: Method swizzling lets you add new features to existing methods without changing how they were originally implemented. This can be useful for logging, profiling, or debugging purposes.
- Method Overriding: You can use method swizzling to replace existing methods with a different way to do things than the original method. This can be used to change how third-party libraries or system frameworks work or fix bugs in them.
- A/B Testing: Method swizzling lets you switch between different versions of a method on the fly, so you can do A/B testing or feature toggling without having to change the codebase.
- Fixing Bugs: You can use method swizzling to fix bugs or change how system classes or frameworks work that you don’t control.
It’s important to use method swizzling judiciously and with caution. It can add complexity, make code harder to read and fix, and cause strange behavior or problems when used with other swizzled methods. Care should be taken to ensure that swizzled methods are properly documented, tested, and comply with Apple’s guidelines.
Question:Explain the concept of Core Data in iOS development. What is Core Data, and how does it make data persistent? Give an example of how Core Data can be used.
Answer: Core Data is a framework from Apple that lets developers control model layer objects and how long they stay in iOS and macOS apps. It lets you work with a data model through an object-oriented interface and supports different storage types, such as SQLite, XML, and in-memory stores.
Core Data enables data persistence by providing an abstraction layer between the data model and the underlying storage. It takes care of loading, saving, querying, undoing, and redoing data objects, so developers can focus on working with objects instead of low-level data storage tasks.
Here’s an example demonstrating the usage of Core Data:
In this example, a Core Data entity named “Person” is defined using the NSManagedObject subclass. The entity has two properties, “name” and “age”. To make a person object and save it, you need to get an instance of the managed object context and add a new person object to it. The properties of the person object are set, and the context is saved to persist the changes.
Core Data is the best way to manage persistent data in iOS apps because it has powerful features like data modeling, relationships, versioning, and faulting.
Question:Explain the concept of GCD (Grand Central Dispatch) in iOS development. What is GCD, and how does it enable concurrent programming? Provide an example demonstrating the usage of GCD.
Answer: Grand Central Dispatch (GCD) is a technology that Apple provides for iOS and macOS apps to do tasks at the same time or at different times. It is a low-level API for managing work across multiple threads or queues.
GCD enables concurrent programming by providing a simple and efficient way to execute tasks concurrently. As a result, it hides the details of managing threads and gives you a simple way to send work to different queues.
Here’s an example demonstrating the usage of GCD:
In this example, a concurrent queue named “com. example. concurrent” is created using dispatch_queue_create. Multiple tasks are dispatched asynchronously to the concurrent queue using dispatch_async. These tasks can execute concurrently and may complete in any order.
You can schedule a barrier task with the dispatch_barrier_async function. This makes sure that all tasks that were already in the queue finish running before the barrier task starts. After the barrier task, more tasks are dispatched asynchronously.
GCD is a powerful and efficient way to handle synchronization, manage multiple threads of execution, and make iOS apps run faster.
Question:Explain the concept of memory management in Objective-C. What are the memory management mechanisms in Objective-C, and how do they work?.
Answer: In Objective-C, memory management means keeping track of how objects are allocated and freed up so that memory is used efficiently and memory leaks don’t happen.
Objective-C has two primary memory management mechanisms:
- MRC stands for “manual reference counting.” In MRC, developers control the number of references to objects by calling the retain, release, and autorelease methods. The reference count of an object goes up every time it is retained and down every time it is released. When the reference count reaches zero, the object is deallocated. To avoid retain cycles and dangling pointers, MRC needs careful memory management.
- ARC stands for Automatic Reference Counting. ARC automates the process of managing memory by adding retain, release, and autorelease calls automatically during compilation. It keeps track of how many times an object has been referenced and adds memory management code based on that information. ARC makes it easier to manage memory without having to do it by hand and helps stop common memory bugs like over-releasing or memory leaks.
Reference counting is what both MRC and ARC are based on. Objects keep track of how many references they have. When an object’s reference count reaches zero, it is deallocated.
ARC is the best way to manage memory in modern Objective-C development because it makes managing memory easier and lowers the chance of memory-related bugs. Even so, developers should still know about memory management ideas like strong and weak references, retain cycles, and how to use strong, weak, and unsafe_unretained attributes.
Question:Explain the concept of Core Animation in iOS development. What is Core Animation, and how does it make it possible to make smooth animations? Give an example of how Core Animation is used.
Answer: Core Animation is a framework that Apple provides that lets developers make and manage animations in iOS and macOS apps. It is a high-performance animation framework built on top of Core Graphics and Core .
Core Animation enables the creation of smooth animations by leveraging hardware acceleration and rendering techniques. It utilizes the graphics processing unit (GPU) to offload animation computations, resulting in faster and more efficient rendering.
Here’s an example demonstrating the usage of Core Animation:
In this example, a CALayer object is created and configured with a size, position, and background color. A CABasicAnimation is created to animate the opacity property of the layer from 1. 0 to 0. 0 over a duration of 1. 0 second. The animation is set to repeat indefinitely. The animation is added to the layer using the addAnimation:forKey: method.
The layer is then added to the view’s layer hierarchy, and the animation is applied automatically, making the layer fade out.
Core Animation provides a wide range of animation capabilities, including keyframe animations, transitions, and custom animations. It simplifies the creation of smooth and visually appealing animations in iOS applications.
Question:Explain the concept of Autolayout in iOS development. What is Autolayout, and how does it help you make adaptive user interfaces? Give an example of how Autolayout is used.
Answer: Apple’s Autolayout is a layout system for iOS development that lets you make user interfaces that are adaptable and flexible. It lets developers set the rules and relationships between UI elements so that the layout can be used on devices with different screen sizes, orientations, and languages.
Autolayout enables building adaptive user interfaces by using a system of constraints. Constraints tell UI elements where to be, how big they should be, and how they should be aligned with each other or with the parent view. Autolayout changes the layout automatically based on these limits, making sure that the UI looks the same on all devices and screen orientations.
Here’s an example demonstrating the usage of Autolayout:
In this example, a red-colored UIView is created and added as a subview to the main view. Autolayout constraints are then created and activated to define the position and size of the red view. The red view can’t be more than 20 points from the main view’s top and bottom edges, and it can’t be wider or taller than 100 points.
Autolayout changes the red view’s position and size on the fly based on the constraints, making sure it stays the same on screens of all sizes and orientations.
Developers can make layouts that automatically adjust to different devices and localization needs with Autolayout, which is a powerful and flexible way to make adaptive user interfaces.
Question:Explain the concept of NSURLSession in iOS development. What is NSURLSession, and how does it enable network communication? Provide an example demonstrating the usage of NSURLSession.
Answer:NSURLSession is a networking API provided by Apple for performing network communication in iOS and macOS applications. It gives you a powerful and adaptable way to make HTTP and HTTPS requests, handle authentication, control downloads and uploads in the background, and more.
NSURLSession enables network communication by managing the entire lifecycle of network tasks. It handles the creation of requests, sending the requests, receiving responses, and managing the session and connection states. It supports various types of tasks, including data tasks, download tasks, and upload tasks.
Here’s an example demonstrating the usage of NSURLSession:
In this example, a URL and a URL request are created. A data task is then created using the shared session’s dataTaskWithRequest:completionHandler: method. When the data task is done, the completion handler is called and given the received data, the response, and any errors that happened. Inside the completion handler, the received data is processed or logged.
There are many features and options in NSURLSession that let you change network requests, handle authentication, manage cookies, and work with background transfers. It is a versatile and powerful framework for network communication in iOS applications.
Question:Explain the concept of Core Graphics in iOS development. What is Core Graphics, and how does it make drawing and rendering possible? Give an example of how Core Graphics is used.
Answer:Core Graphics is a powerful framework provided by Apple in iOS development for drawing and rendering 2D graphics. It gives you a set of low-level C APIs that you can use to make and change graphics contexts, paths, shapes, gradients, and more.
Core Graphics makes drawing and rendering possible by giving you a wide range of tools for making and changing graphic elements. It allows developers to create custom views, generate PDF documents, perform manipulation, apply transformations.
, and perform complex rendering operations.
Here’s an example demonstrating the usage of Core Graphics to draw a rectangle:
In this example, the drawRect: method is overridden in a custom UIView subclass. The current graphics context is obtained using UIGraphicsGetCurrentContext(), and the fill color is set to red using CGContextSetFillColorWithColor(). Finally, a rectangle is drawn on the graphics context using CGContextFillRect().
Core Graphics provides a wide range of functions and capabilities for creating and manipulating 2D graphics. Custom drawing, making charts, rendering UI elements, and making complex graphics for iOS apps are all common uses for it.
Question:Explain the concept of Core Data in iOS development. What is Core Data, and how does it make data persistent? Give an example of how Core Data can be used.
Answer: Core Data is a framework from Apple that lets developers control model layer objects and how long they stay in iOS and macOS apps. It lets you work with a data model through an object-oriented interface and supports different storage types, such as SQLite, XML, and in-memory stores.
Core Data enables data persistence by providing an abstraction layer between the data model and the underlying storage. It takes care of loading, saving, querying, undoing, and redoing data objects, so developers can focus on working with objects instead of low-level data storage tasks.
Here’s an example demonstrating the usage of Core Data:
In this example, a Core Data entity named “Person” is defined using the NSManagedObject subclass. The entity has two properties, “name” and “age”. To make a person object and save it, you need to get an instance of the managed object context and add a new person object to it. The properties of the person object are set, and the context is saved to persist the changes.
Core Data is the best way to manage persistent data in iOS apps because it has powerful features like data modeling, relationships, versioning, and faulting.
These answers provide an overview of the concepts and explanations for each question. For further understanding, it is recommended to refer to the Objective-C and iOS documentation and additional resources.
Interview questions. Test for Objective-C knowledge. 50% programmers fail it.
FAQ
What is C language basic interview questions?
What is Objective-C used for?
What is the difference between Swift and Objective-C?