面试题分享【不断更新】
问题1:求以下程序段的输出
int test(int x, int y) { x = x + y; return x * y; } int main(int argc, const char * argv[]) { @autoreleasepool { int x = 3, y = 10, z = test(x, y); NSLog(@"%d%d", x++, ++z); } return 0; }
答案:3131
问题2:求以下程序段的输出
int func(int x) { int countx = 0; while (x) { countx++; x = x&(x-1); } return countx; } int main(int argc, const char * argv[]) { @autoreleasepool { NSLog(@"%d", func(2013)); } return 0; }
答案:9
问题3:简述两个for循环的优缺点
for (i = 0; i < N; i++) { if (condition) { doSomeThing(); } else { doOtherThing(); } } if (condition) { for (i = 0; i < N; i++) { doSomeThing(); } } else { for (i = 0; i < N; i++) { doOtherThing(); } }
答案:无
问题4:请指出下面代码的问题
UIView *tmpView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.view addSubview:tmpView];
答案:无
问题5:你所了解的单例有哪些?请写了一个单例的示例代码。
答案:
@interface Manager : NSObject + (id)sharedManager; @end
static Manager *instance = nil; @implementation Manager + (id)sharedManager { if (!instance) { instance = [[super allocWithZone:NULL] init]; } return instance; } // 复写allocWithZone和copyWithZone的目的是在外部多次调用alloc的时候,内部能够确保对象只创建了一次 + (id)allocWithZone:(NSZone *)zone { return [[self sharedManager] retain]; } - (id)copyWithZone:(NSZone *)zone { return self; } // 复写retain、retainCount、release、autorelease避免单件对象被外部释放 - (id)retain { return self; } - (NSUInteger)retainCount { return NSUIntegerMax; } - (void)release { } - (id)autorelease { return self; }
问题6:在你的日常开发过程中,是否遇到过崩溃问题?怎么解决?
答案:无
问题7:请写出使用委托(代理)的示例代码。
答案:
A委托B
A.h
@protocol ADelegate; @interface A : NSObject @property (nonatomic, assign) id<ADelegate> delegate; - (void)go; @end @protocol ADelegate <NSObject> - (void)doSomeThing; @end
A.m
@implementation A - (void)go { if ([_delegate respondsToSelector:@selector(doSomeThing)]) { [_delegate doSomeThing]; } } @end
B.h
#import "A.h" @interface B : NSObject<ADelegate> @end
B.m
@implementation B - (void)doSomeThing { NSLog(@"do something..."); } @end
man.m
#import "A.h" #import "B.h" int main(int argc, const char * argv[]) { @autoreleasepool { A *a = [[A alloc] init]; B *b = [[B alloc] init]; a.delegate = b; [a go]; [a release]; [b release]; return 0; } }
// 输出
do something...
问题8:写出控制器加载视图的常规调用方法流程。
答案:
loadView
viewDidLoad
viewWillAppear
viewWillLayoutSubviews
viewDidLayoutSubviews
viewDidAppear
问题9:Objective-C有多重继承么?不是的话,请以示例代码说明。
答案:无
问题10:请写出下面代码的打印结果(默认在32位机器下运行)。
char a[] = "hello world"; char *p = a;
NSLog(@"%ld", sizeof(a)); NSLog(@"%ld", sizeof(p));
答案:
12
4
如果在64位机器上运行
12
8
问题11: 请完成下面的函数
@interface NSCustomArray : NSArray @property (nonatomic, retain) NSArray *targets; - (void)updateTargetsByArray:(NSArray *)newArray; // 将newArray赋值给targets @end
@implementation NSCustomArray - (void)updateTargetsByArray:(NSArray *)newArray { NSArray *tmpArray = [[NSArray alloc] initWithArray:newArray]; // 将tmpArray赋值给targets } @end
答案:无
问题12:检查如下代码是否有问题,如果有问题,正确的写法是?
- (void)setTarget:(NSObject *)target { self.target = target; }
答案:无
问题13:指出下面代码的问题
- (void)willEndStepOne:(NSObject *)userInfo { [NSThread detachNewThreadSelector:@selector(doSomeThing:) toTarget:self withObject:userInfo]; } - (void)doSomeThing:(NSObject *)userInfo { int max_loop_times = 999999; for (int i=0; i<max_loop_times; i++) { int randNum = rand(); NSString *tmpString = [NSString stringWithFormat:@"%d", randNum]; NSLog(@"%@", tmpString); } }
答案:无