測試地址: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?以下代碼運行后會怎樣?
答案:不會產生任何問題。
說明: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位的定義有區別。定義如下:
7.What will be printed by the following program?以下程序的輸出結果?
答案: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?
下面哪一行代碼編譯器會報錯?
答案:第五行
說明:可能有人不知道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?
對于放在任何方法以外的以下聲明,正確的說法是?
答案: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?
對于以下代碼打印出的內存地址,你怎么看?
答案: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?下面程序的輸出結果?
答案: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?
以下程序的輸出結果是?
答案: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?元方,你怎么看?
答案:大人,此代碼完全沒有問題
說明:雖然代碼可以運行,但要當心,該方法并不能改變外部傳入的字符串的值,形參和實參的關系。
25.Consider the following code:
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的引用計數是多少?
答案: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?元方,你怎么看?
答案:調用了不同的方法。
說明: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有如下說明:
Initializes the receiver before it’s used (before it receives its first message).
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段聲明語句是否不同?
答案:是的,意義不同
說明:參考第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
新聞熱點
疑難解答