星空网 > 软件开发 > 操作系统

C以及Objective

测试地址: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?以下代码运行后会怎样?

 

[cpp] view plaincopy 


  1. #include <stdlib.h>  
  2. int main( void )  
  3. {  
  4.     char * ptr = NULL;  
  5.     free( ptr );  
  6.     return 0;  
  7. }  


答案:不会产生任何问题。

 

说明: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位的定义有区别。定义如下:

[cpp] view plaincopy 


  1. #if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64  
  2. typedef long NSInteger;  
  3. typedef unsigned long NSUInteger;  
  4. #else  
  5. typedef int NSInteger;  
  6. typedef unsigned int NSUInteger;  
  7. #endif  

 

 

7.What will be printed by the following program?以下程序的输出结果?

 

[cpp] view plaincopy 


  1. #include <stdio.h>  
  2. typedef union  
  3. {  
  4.     short s;  
  5.     char  c;  
  6. }  
  7. sc;  
  8. int main( void )  
  9. {  
  10.     sc u;  
  11.     u.s = 0x602;  
  12.     printf( "%d\n", u.c );  
  13.     return 0;  
  14. }  

答案: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?

下面哪一行代码编译器会报错?

 

[cpp] view plaincopy 


  1. int main( void )  
  2. {  
  3.     const char * s = "Hello world!";      /* Line 3 */  
  4.     s              = "Hello universe!";   /* Line 4 */  
  5.     s[ 0 ]         = 'A';                 /* Line 5 */  
  6.     return 0;  
  7. }  

答案:第五行

 

说明:可能有人不知道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?

对于放在任何方法以外的以下声明,正确的说法是?

 

[cpp] view plaincopy 


  1. 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?

对于以下代码打印出的内存地址,你怎么看?

 

[cpp] view plaincopy 


  1. #import <Cocoa/Cocoa.h>  
  2. int main( void )  
  3. {  
  4.     NSAutoreleasePool * pool;  
  5.     NSString          * s1;  
  6.     NSString          * s2;  
  7.     NSString          * s3;  
  8.     NSString          * s4;  
  9.     pool = [ [ NSAutoreleasePool alloc ] init ];  
  10.     s1   = @"Hello world!";  
  11.     s2   = @"Hello world!";  
  12.     s3   = [ NSString stringWithString: @"Hello world!" ];  
  13.     s4   = [ [ NSString alloc ] initWithString: @"Hello world!" ];  
  14.     printf( "%p\n%p\n%p\n%p\n", s1, s2, s3, s4 );  
  15.     [ s4 release ];  
  16.     [ pool release ];  
  17.     return 0;  
  18. }  

答案: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?下面程序的输出结果?

 

[cpp] view plaincopy 


  1. #include <stdio.h>  
  2. int main( void )  
  3. {  
  4.     unsigned int array[ 2 ][ 2 ] = { { 0, 1 }, { 2, 3 } };  
  5.     unsigned int i               = 0;  
  6.     unsigned int sum             = 0;  
  7.     int x                        = 0;  
  8.     int y                        = 0;  
  9.     for( i = 0; i < 4; ++i )  
  10.     {  
  11.         x    = i % 2;  
  12.         y    = ( x ) ? 0 : 1;  
  13.         sum += array[ x ][ y ];  
  14.     }  
  15.     printf( "%d\n", sum );  
  16.     return 0;  
  17. }  

答案: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相关知识咱还是有所欠缺C以及Objectiveimages/loading.gif' data-original="http://static.blog.csdn.net/xheditor/xheditor_emot/default/wronged.gif" />。

 

19.What will be printed by the following program?

以下程序的输出结果是?

 

[cpp] view plaincopy 


  1. #include <stdlib.h>  
  2. #include <stdio.h>  
  3. int main( void )  
  4. {  
  5.     int * ptr;  
  6.     int i;  
  7.     ptr = ( int * )malloc( 4 * sizeof( int ) );  
  8.     for( i = 0; i < 4; i++ )  
  9.     {  
  10.         ptr[ i ] = i;  
  11.     }  
  12.     printf( "%d, ", *ptr++ );  
  13.     printf( "%d, ", ++( *ptr ) );  
  14.     printf( "%d, ", *++ptr );  
  15.     printf( "%d\n", ++*ptr );  
  16.     return 0;  
  17. }  

答案: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?元方,你怎么看?

 

[cpp] view plaincopy 


  1. - (void)foo: (NSString *)s  
  2. {  
  3.     s = @"Hello world!";  
  4. }  

答案:大人,此代码完全没有问题

 

说明:虽然代码可以运行,但要当心,该方法并不能改变外部传入的字符串的值,形参和实参的关系。

 

25.Consider the following code:

 

[cpp] view plaincopy 


  1. 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的引用计数是多少?

 

[cpp] view plaincopy 


  1. NSMutableString * s1;  
  2. NSMutableString * s2;  
  3. s1 = [[[NSMutableString alloc] initWithString:@"Hello world!"] autorelease];  
  4. [[[[s1 retain] retain] retain] release];  
  5. s2 = [s1 copy];  
  6. [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?元方,你怎么看?

[cpp] view plaincopy 


  1. [Foo bar: 1];  
  2. [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).

+ (void)initialize
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段声明语句是否不同?

 

[cpp] view plaincopy 


  1. const char * foo;  
  2. 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




原标题:C以及Objective

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

新建外贸独立站没有流量?外贸网站seo如何缩短“沙盒期”?:https://www.kjdsnews.com/a/1452213.html
SOHO九问?代理九答?了解一下?:https://www.kjdsnews.com/a/1452214.html
fb海外户一般多少起充?:https://www.kjdsnews.com/a/1452215.html
信息流广告创意文案!:https://www.kjdsnews.com/a/1452216.html
跨境ChatGPT,永久免费使用!抓紧注册!:https://www.kjdsnews.com/a/1452217.html
菜鸟集团启动上市计划;华贸物流拟成立新合资公司:https://www.kjdsnews.com/a/1452218.html
出境旅游预订须知:https://www.vstour.cn/a/365175.html
九寨沟景区地图(详细指南和攻略):https://www.vstour.cn/a/365176.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流