Objective-C语法面试题

jopen 11年前

1.    为什么Foundation框架中的类名都是以NS开头?(Just for fun)

 

2.    以下方法中哪一个返回的不是autorelease对象?

A. [NSData data];             B.[NSString stringWithString:];

C. [NSNumber numberWithInt:];

D. [NSTimer scheduledTimeInterval:target:selector:userInfo:repeats:];

 

3.    #import与#include的区别是?

 

4.    @class的作用是?

 

5.    obj-c有多重继承么?不是的话有什么替代方法?

 

6.    一下哪个方法名是合法的?

A. initWithFileID:filename:filePath::          B. startDownload

C. finishDownload::::::                                        D.以上都是合法的

 

7.    一个工程中存在group类(group.m, group.h)和people类(people.m, people.h),在group.m文件中的test方法如下:

-(void)test{

            People*people = [[People alloc] init];

            [NSTimerscheduledTimeInterval: 1.0f target:people selector:@selector(onCall) userInfo:nil repeats:YES];

}

请问onCall方法应该写在哪个文件中?

A. group.m                    B. group.h

C. people.m                  D.people.h

 

8.    一个成员变量的声明如下:

<p>@interface Test : NSObject{</p>

            NSString*str1;

            NSString*str2;

}

<p>@property (nonatomic, assign) NSString str1;</p> <p>@property (nonatomic, retain) NSString str2;</p> <p>@end</p>

 

<p>@implementation Test</p>

 

<p>@synthesize str1;</p> <p>@synthesize str2;</p>

 

-(id)init{

            self.str2= [NSString stringWithString:@”hello world”];

            [str2retain];

            self.str1= [[NSString alloc] initWithString:str2];

            [str2release];

            请问此时str1和str2的retainCount是多少?(不考虑系统对常量字符串的优化)

}

<p>@end</p>

 

9.    某个类中有一个test方法如下:

-(void)test{

            NSString*str1 = [[NSString stringWithString:@”hello world”] retain];

            NSString*str2 = [[[NSString alloc] init] autorelease];

(1)   请问此时str1与str2的retainCount是多少?(不考虑系统对常量字符串的优化)

str1 =[[[NSString alloc] initWithString:@”hello world”] autorelease];

str2 =[[NSString alloc] initWithString:str1];

[str1 release];

(2)   请问此时将str1和str2输出的结果是什么?(不考虑系统对常量字符串的优化)

}

 

10.  请描述Objective-C的动态特性。

 

11.  这段代码有什么问题,如何修改?

for (int i = 0; i < someLargeNumber;i++)

{

NSString *string= @”Abc”;

string = [stringlowercaseString];

string = [stringstringByAppendingString:@"xyz"];

NSLog(@“%@”,string);

}

 

12.  请写出以下类成员变量自动生成的Getter和Setter方法。

<p>@property (nonatomic, retain) NSString    str1;</p> <p>@property (nonatomic, assign) NSString   str2;</p> <p>@property (nonatomic, assign) int              num;</p>

 

<p>@synthesize str1 = _str1;</p> <p>@synthesize str2 = _str2;</p> @synthesize num = _num;