测试地址:http://www.eosgarden.com/en/articles/objc-quizz/take/
这是前几天好友共享的Obj-C测试题,共57题。自以为精通OC了的本人去做了下测试题,结果受到了较为严重的精神打击,考点非常细,有些甚至非常底层。准备分2次讲解这些题目,下面逐个讲解这些考题。其中有一些题目笔者自身也有一些疑问,欢迎探讨。
1.What is "Nil" in Objective-C? “Nil”在OC中是什么?
答案:(void *)0
说明:'NULL','nil'以及'Nil'是指向0地址的空指针。'nil'和'Nil'在OC中被定义为"DARWIN_NULL",也就是(void *)0
2.What will happen when the following program is executed?以下代码运行后会怎样?
- #include <stdlib.h>
- int main( void )
- {
- char * ptr = NULL;
- free( ptr );
- return 0;
- }
答案:不会产生任何问题。
说明:C标准库定义free()空指针是安全的。
3.What method is called by the NSLog function when the "%@" sequence is present?调用NSLog方法时"%@"会调用什么方法?
答案:description
说明:OC基础
4.Which is the correct syntax to declare a function pointer name "foo" returning an integer and having an integer as argument?
声明一个名为“foo”的函数指针,该函数返回一个整型,并接受一个整型参数
答案:int(* foo)(int)
说明:基础的函数指针问题
5.How many bytes are used to store a "long long" data type?long long类型占几个字节?
答案:Implementation defined 根据(编译器的)实现而定义
说明:这个问题笔者当时回答错了,笔者选了8个字节,虽然大多数编译器上long long的确是8个字节,但是这种说法是很值得商榷的。而实际上C标准中并没有具体给出规定那个基本类型应该是多少字节数,而且这个也与机器、OS、编译器有关。
6.What is the difference between the "unsigned int" and the "NSUInteger" data types?"unsigned int"和"NSInteger"的区别?
答案:It depends on the processor.取决于处理器
说明:基础题,看过NSUInteger的定义就应该知道,32位和64位的定义有区别。定义如下:
- #if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
- typedef long NSInteger;
- typedef unsigned long NSUInteger;
- #else
- typedef int NSInteger;
- typedef unsigned int NSUInteger;
- #endif
7.What will be printed by the following program?以下程序的输出结果?
- #include <stdio.h>
- typedef union
- {
- short s;
- char c;
- }
- sc;
- int main( void )
- {
- sc u;
- u.s = 0x602;
- printf( "%d\n", u.c );
- return 0;
- }
答案:Machine dependant.根据机器不同结果不同。
说明:涉及大小端问题,而且参照第5题的话short的字节长度也是不定的。
8.What can you assume when calling the "stringWithString:" method on a "NSString" object?
使用NSString的stringWithString时我们可以得知:
答案:The returned object is auto-released. 返回的NSString对象是auto-released(自释放的)。
说明:基础
9.Is it possible to have multiple instances of the "NSAutoreleasePool" class in the same program?
是否可以在一个程序中使用多个NSAutoreleasePool?
答案:显然可以。
说明:多线程编程的时候特别有用。
10.Which line of the following code will be reported as an error by the compiler?
下面哪一行代码编译器会报错?
- int main( void )
- {
- const char * s = "Hello world!"; /* Line 3 */
- s = "Hello universe!"; /* Line 4 */
- s[ 0 ] = 'A'; /* Line 5 */
- return 0;
- }
答案:第五行
说明:可能有人不知道const char *类型的意义。从右往左读,将*读作pointer to,也就是a pointer to const char,指向const char的指针,所以指针可变,指向的char不可变。
11.Which is true about the following statement, placed outside of any function or method?
对于放在任何方法以外的以下声明,正确的说法是?
- static int foo;
答案:The variable cannot be accessed directly from other files. The variable has a default initial value 0.变量不能直接从其他文件直接访问,变量默认值为0。
说明:这题的标准答案如上,但本人对答案持质疑态度。不能从别的文件直接访问这种说法欠妥,将其声明在头文件中,只要导入该头文件,就可以访问。
12.Is garbage collection available on iPhone OS?
iOS有垃圾回收机制吗?
答案:没有
说明:基础。但要注意,Mac OS是有垃圾回收机制的。
13.What can you say about the memory addresses that will be printed by the following program?
对于以下代码打印出的内存地址,你怎么看?
- #import <Cocoa/Cocoa.h>
- int main( void )
- {
- NSAutoreleasePool * pool;
- NSString * s1;
- NSString * s2;
- NSString * s3;
- NSString * s4;
- pool = [ [ NSAutoreleasePool alloc ] init ];
- s1 = @"Hello world!";
- s2 = @"Hello world!";
- s3 = [ NSString stringWithString: @"Hello world!" ];
- s4 = [ [ NSString alloc ] initWithString: @"Hello world!" ];
- printf( "%p\n%p\n%p\n%p\n", s1, s2, s3, s4 );
- [ s4 release ];
- [ pool release ];
- return 0;
- }
答案:All addresses will be the same. 所有地址相同
说明:不可变字符串,编译器自动优化的结果。
14.Which line can be used to compile an Objective-C executable named "test" from a "test.m" file?
下面哪一条命令可将test.m编译为可执行文件?
答案:gcc -Wall -framework Cocoa -o test test.m
说明:这题不太清楚,因为平时不太会去手动编译吧,不过凑巧蒙对了。
15.Does the Objective-C compiler treats the identifiers of an enumeration as integer constants?
OC的编译器将枚举型当作整型来处理么?
答案:显然是的
说明:基础
16.What will be printed by the following program?下面程序的输出结果?
- #include <stdio.h>
- int main( void )
- {
- unsigned int array[ 2 ][ 2 ] = { { 0, 1 }, { 2, 3 } };
- unsigned int i = 0;
- unsigned int sum = 0;
- int x = 0;
- int y = 0;
- for( i = 0; i < 4; ++i )
- {
- x = i % 2;
- y = ( x ) ? 0 : 1;
- sum += array[ x ][ y ];
- }
- printf( "%d\n", sum );
- return 0;
- }
答案:6
说明:基础
17.What happen when a floating point value is assigned to an integer variable?
当一个浮点型数据赋值给整型变量时会发生?
答案:取整,浮点型缩短
说明:基础
18.In theory, is it safe to call a function of the standard C library from a signal handler?
理论上,在信号处理函数中调用标准库函数是安全的吗?
答案:不安全
说明:Functions from the C Standard Library may not be reentrant.
由于标准库函数中可能用到静态或全局变量(可能被主过程和信号处理函数同时操作),这种情况属于不可重入函数,所以在信号处理函数中调用标准库函数是不安全的。具体参考APUE中相关章节。
PS:这题要感谢Unix大牛 @delo 在微博中的解答,太专业了。Unix相关知识咱还是有所欠缺。
19.What will be printed by the following program?
以下程序的输出结果是?
- #include <stdlib.h>
- #include <stdio.h>
- int main( void )
- {
- int * ptr;
- int i;
- ptr = ( int * )malloc( 4 * sizeof( int ) );
- for( i = 0; i < 4; i++ )
- {
- ptr[ i ] = i;
- }
- printf( "%d, ", *ptr++ );
- printf( "%d, ", ++( *ptr ) );
- printf( "%d, ", *++ptr );
- printf( "%d\n", ++*ptr );
- return 0;
- }
答案:0,2,2,3
说明:1:打印ptr[0],指针右移;2:ptr[1]自增,打印ptr[1];3:指针右移,打印ptr[2];4:ptr[2]自增,打印ptr[2]
20. Is it possible to use dynamic libraries when developing for iPhone OS?
iOS开发中可以使用动态链接库吗?
答案:不能
说明:iOS开发只能使用静态库
21.Which of the following creates a class that conforms to a protocol?
如何继承协议?
答案:@interface ClassName < ProtocolName >
说明:基础
22.What is the default visibility for instance variables?
实例变量的默认访问权限是?
答案:@protected
说明:和其他语言类似,虽然在iOS中也许不常用。
23.Is it possible to use C++ when developing for iPhone OS?
iOS开发中可以使用C++吗?
答案:可以
说明:.mm文件可进行混编,cocos2D带的Box2D物理引擎就是常见的C++编写。
24.What can you say about the code below?元方,你怎么看?
- - (void)foo: (NSString *)s
- {
- s = @"Hello world!";
- }
答案:大人,此代码完全没有问题
说明:虽然代码可以运行,但要当心,该方法并不能改变外部传入的字符串的值,形参和实参的关系。
25.Consider the following code:
- double x = 5 / 10 - 2 / 2 * 4;
Please write the value of x below:
答案:-4
说明:超级基础,注意整除
26.After the execution of the following code, what is the retain count of the "s1" and "s2" objects?
执行以下代码后,s1与s2的引用计数是多少?
- NSMutableString * s1;
- NSMutableString * s2;
- s1 = [[[NSMutableString alloc] initWithString:@"Hello world!"] autorelease];
- [[[[s1 retain] retain] retain] release];
- s2 = [s1 copy];
- [s1 release];
答案:s1:2,s2:1
说明:s1:alloc+1,retain三次+3,release两次-2,引用计数为2;s2:copy将产生新的对象,+1,引用计数1。
27.What can you say about the code below?元方,你怎么看?
- [Foo bar: 1];
- [Foo bar: 1 y: 2];
答案:调用了不同的方法。
说明:OC中冒号前的部分视为方法名的一部分,比如第一个方法名为bar:,第二个方法名为bar: y:,完全不同的两个方法,不是重载。
28.Is it true that the "initialize" message is sent to a class before any other messages, even class methods calls?
"initialize"(初始化)消息是不是首先被发送到类的消息,甚至比调用类方法更早?
答案:是的
说明:这题其实笔者当时其实不太确定,查阅了相关的官方文档,官方文档对NSObject的类方法initialize有如下说明:
initialize
Initializes the receiver before it’s used (before it receives its first message).
Discussion
The runtime sends initialize
to each class in a program exactly one time just before the class, or any class that inherits from it, is sent its first message from within the program. (Thus the method may never be invoked if the class is not used.) The runtime sends the initialize
message to classes in a thread-safe manner. Superclasses receive this message before their subclasses.
大意:运行时只发送一次“initailize”到每个类或其继承类,是程序内第一个被发送的消息。(因此如果类没有被使用,则“initialize”不会被调用。)运行时以一个线程安全的方式发送“initailize”消息给类。父类在子类之前接收到该消息。
29.Is there a difference between these two statements?
以下2段声明语句是否不同?
- const char * foo;
- char * const bar;
答案:是的,意义不同
说明:参考第10题的方法,第一句读作a pointer to const char,第二句读作a const pointer to char,一个是指向字符常量的指针变量,一个是指向字符变量的指针常量,意义完全不同。
30.In theory, which function is faster: "strcpy" or "memcpy"?
理论上,“strcpy”和“memcpy”方法哪个效率更高?
答案:memcpy
说明:strcpy比memcpy多一个搜寻字符串结尾符“\0”的操作,比memcpy慢。
原文链接:http://blog.csdn.net/xiemotongye/article/details/8915039