Impressive Top 10 Objective C Interview Questions

84 / 100 SEO Score

Objective C Interview Questions

Objective C Interview QuestionsObjective C Interview Questions: ObjectiveC is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. … It was later selected as the main language used by NeXT for its NeXTSTEP operating system, from which macOS and iOS are derived.

 

 

Interface

@interface classname : superclassname { // instance variables } + classMethod1; + (return_type)classMethod2; + (return_type)classMethod3:(param1_type)param1_varName; - (return_type)instanceMethod1With1Parameter:(param1_type)param1_varName; - (return_type)instanceMethod2With2Parameters:(param1_type)param1_varName param2_callName:(param2_type)param2_varName; @end

Implementation

The interface only declares the class interface and not the methods themselves: the actual code is written in the implementation file.

@implementation classname
+ (return_type)classMethod
{
 // implementation
}
- (return_type)instanceMethod
{
 // implementation
}
@end

Methods are written using their interface declarations. Comparing Objective-C and C:

- (int)method:(int)i
{
 return [self square_root:i];
}
int function (int i)
{
 return square_root(i);
}

The syntax allows pseudo-naming of arguments.

- (void)changeColorToRed:(float)red green:(float)green blue:(float)blue
{
    //... Implementation ...
}

//Called like so:
[myColor changeColorToRed:5.0 green:2.0 blue:6.0];

Protocols

@protocol NSLocking
- (void)lock;
- (void)unlock;
@end

denotes that there is the abstract idea of locking. By stating in the class definition that the protocol is implemented,

@interface NSLock : NSObject <NSLocking>
//...
@end

Forwarder.h

#import <objc/Object.h>

@interface Forwarder : Object {
    id recipient; //The object we want to forward the message to.
}

//Accessor methods.
- (id)recipient;
- (id)setRecipient:(id)_recipient;
@end

Forwarder.m

# import "Forwarder.h"

@implementation Forwarder

- (retval_t)forward:(SEL)sel args:(arglist_t) args {
 /*
 * Check whether the recipient actually responds to the message.
 * This may or may not be desirable, for example, if a recipient
 * in turn does not respond to the message, it might do forwarding
 * itself.
 */
 if ([recipient respondsToSelector:sel]) {
  return [recipient performv:sel args:args];
 } else {
  return [self error:"Recipient does not respond"];
 }
}

- (id)setRecipient:(id)_recipient {
 [recipient autorelease];
 recipient = [_recipient retain];
 return self;
}

- (id) recipient {
 return recipient;
}
@end

Recipient.h

# import <objc/Object.h>

// A simple Recipient object.
@interface Recipient : Object
- (id)hello;
@end

Recipient.m

# import "Recipient.h"

@implementation Recipient

- (id)hello {
 printf("Recipient says hello!\n");

 return self;
}

@end

main.m

# import "Forwarder.h"
# import "Recipient.h"

int main(void) {
 Forwarder *forwarder = [Forwarder new];
 Recipient *recipient = [Recipient new];

 [forwarder setRecipient:recipient]; //Set the recipient.
 /*
 * Observe forwarder does not respond to a hello message! It will
 * be forwarded. All unrecognized methods will be forwarded to
 * the recipient
 * (if the recipient responds to them, as written in the Forwarder)
 */
 [forwarder hello];

 [recipient release];
 [forwarder release];

 return 0;
}

What is Objective C and what is it used for?

This is an ANSI-based version of C. It is used by app developers who develop apps for the OS X operating system and the mobile version of iOS. It is derived from the C language with the major difference being the addition of small talk features that makes it an Object Oriented Language.

What is a protocol in Objective C?

It announces a programmatic interface that will be implemented by a class. Protocols allow two classes that inherit properties from each other to communicate so that they can achieve a common goal.

Name two types of protocols in Objective C and what they are used for.

Formal protocols are an extension to the Objective-C language that declare a list of methods that client classes are expected to implement. They have their own declaration, adoption, and type-checking syntax.

Informal protocols are a category on NSO objects that implicitly makes all objects their adopters. Therefore, implementation of methods in informal protocols are optional.

What is the function of categories?

Categories are language features that allow you to add methods to a class without sub classing it. They are used to distribute the implementation of their classes in different source files.

How is the #import directive different from #include?

In Objective-C, the #import directive is almost identical to #include that is inherited from C. The only difference is that the former ensures that a file is never included more than once.

State the syntax for calling a function?

[className methodName]
For announcing methods in same class, use this:
[self methodName]  

 Top 10 Data Structures Interview Questions

What is the difference between atomic and non atomic synthesized properties?

Atomic is the default behavior and ensures that the present process is completed by the CPU. Non-atomic is not the default behavior and is most likely to result in unexpected behavior.

What are blocks and how are they used?

They are language level features that allow you to create segments of code that can be passed to functions and methods as values. A block is initiated by the ^ (caret) symbol.

State some benefits of using Objective C?

  • It has documented frameworks
  • Reliable support from online sources
  • Can be integrated with C and C++
  • It is well tested and mature

Is there function overloading in objective C?

No. The language does not support overloading which makes it necessary to use a different method.

Objective C Interview Questions

Was this post helpful?

Leave a Reply

Your email address will not be published.