標籤:
測試地址: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地址的null 指標。‘nil‘和‘Nil‘在OC中被定義為"DARWIN_NULL",也就是(void *)0
2.What will happen when the following program is executed?以下代碼運行後會怎樣?
[cpp] view plaincopy
- #include <stdlib.h>
- int main( void )
- {
- char * ptr = NULL;
- free( ptr );
- return 0;
- }
答案:不會產生任何問題。
說明:C標準庫定義free()null 指標是安全的。
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
- #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?以下程式的輸出結果?
[cpp] view plaincopy
- #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?
下面哪一行代碼編譯器會報錯?
[cpp] view plaincopy
- 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?
對於放在任何方法以外的以下聲明,正確的說法是?
[cpp] view plaincopy
- 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
- #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?下面程式的輸出結果?
[cpp] view plaincopy
- #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?
以下程式的輸出結果是?
[cpp] view plaincopy
- #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?元方,你怎麼看?
[cpp] view plaincopy
- - (void)foo: (NSString *)s
- {
- s = @"Hello world!";
- }
答案:大人,此代碼完全沒有問題
說明:雖然代碼可以運行,但要當心,該方法並不能改變外部傳入的字串的值,形參和實參的關係。
25.Consider the following code:
[cpp] view plaincopy
- 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
- 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?元方,你怎麼看?
[cpp] view plaincopy
- [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).
+ (void)initializeDiscussion
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
- 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
C以及Objective-C測試題講解(上)