前言
在學C++語言之前回顧一下C中的一些知識.選用的是中國大學MOOC中C++程序設計(面向對象進階)中的C語言水平評估測試題.
題目
?The keyword "unsigned" can modify the keyword [? B? ]
-
A.signed
-
B.long
-
C.long double
-
D.float
題解:unsigned是無符號的意識,通常在整數前面加
?In the following strings, the correct C identifier is [ C? ]
-
A.break
-
B.2d
-
C._256
-
D.foo-1
題解:不能是關鍵字,連接符,數字(不能寫第一個)
?The string "r\tu\r\"Okay?\"\n"? will occupy [? D ] bytes memory.
-
A.15
-
B.18
-
C.12
-
D.13
題解:轉義符算一個字節,一個字母算一個字節,/和?也算一個字節.
?In C programming language, the result of statement?5 ^ 4??is [ D? ]
-
A.4
-
B.3
-
C.2
-
D.1
題解:" ^ "這個是異或的意思.
?
For the statement: int* a[10],*b; the correct description is[? A? ]
-
A.a can only be rvalue, but b can be lvalue
-
B.both a and b can only be rvalue
-
C.a can only be lvalue, but b can be rvalue
-
D.both a and b can only be lvalue
題解:這個涉及到指針數組,這個int* a[10]就是指數組中的每個元素都是一個指針(int
*類型),即使a是一個指針數組,但是它仍是一個數組,所以a是代指整個數組的起始地址,這個地址是常量.
If compiled with a STANDARD C COMPILER (e.g. gcc), which is correct about the following function "add"? [? A? ]
?double?add(int?*,?int?*,?int?k)?
?
{ ??return?(double)?(8+k); }
int?main()?{ ??
int?x=1,?y=2,z=3; ??
add(&x,?&y,?z); ??
return?0; }
-
A.Compile error. After filling in the name of the formal parameters, the program can be compiled without errors;
-
B.Compile success.
-
C.Compile error. After changing " return (double) (8+k);" to "return 8+k", the program can be compiled without errors;
-
D.Compile error. After changing "int? k" to "double? k", the program can be compiled without errors;
題解:個人認為編譯器版本會影響這道題,就以菜鳥教程的編譯器為例要將形參寫的具體.
The correct one about pointers is: [? ? D ]?
We assume that all codes are compiled on 32-bit platform
-
A.double *p;??where p occupies 8 byte memory;
-
B.struct S{ char* m;} n;?where n occupies 1 byte memory;
-
C.char *p;??where p occupies 1 byte memory;
-
D.struct T{ double d; } *p;??where p occupies 4 bytes memory;
題解:指針的占內存空間4bytes
Given the following program, when do-while loop finishes,the value of x is[? ?B? ]?enum?{?APPLE,?LEMON=6,?ORANGE,?BANANA=2,?GRAPE};
void?f?(?)?{ ????
int?x=GRAPE; ????
do?{ ?????????
x++; ????}?
while?((x-APPLE)<=ORANGE); }
-
A.6?
-
B.8
-
C.ORANGE? ? ? ? ?
-
D.BANANA?
題解:這個題可去看我之前寫的非阻塞式按鍵-單雙擊長按的實現-CSDN博客里面有對enum一些解釋,這里APPLE默認是0,ORANGE是在LEMON上加一,類似GRAPE同理.
Which of the following statements are completely correct? [? B? ?]
-
A.int *p; scanf("%d", &p);
-
B.int k, *p=&k;? scanf("%d", p);
-
C.int k, *p;? *p= &k;? ?scanf("%d", p);
-
D.int *p; scanf("%d", p);
題解:這里主要考察野指針.野指針就是指沒有初始化指針.它所指向的內存地址可以是任何地址,可能指向的是只讀,或者從操作系統的保留地址.舉例
int k, *p;
*p = &k;
scanf("%d", p);
分析:p是一個未初始化的指針,它的值是隨機的,指向位置是未知的.
*p = &k;的意思是將k的存儲地址給p指向的位置.
你無法確定這個內存位置是安全的.
Which statement satisfies the condition: If string s1 equals to strings s2, then execute ST.? [? ?D? ]
-
A.if(strcpy(sl, s2)==1) ST;
-
B.if(sl==s2) ST;
-
C.if(sl-s2==0) ST;
-
D.if(strcmp(s2,s1)==0) ST;
題解:strcpy(sl, s2)是指把s2的內容復制到s1,用的是s1的地址,不是數值1.
s1和s2比較的是字符串的地址值,不是內容,上面的數組一個這個s1和s2屬于衰減,代表數組的起始地址.
s1-s2同樣的算的是地址差值.
strcmp(s1,s2)用于比較s1和s2的內容.
?Given the following program[ D ]
?#include??<stdio.h>
int?fun(?)
{ ??static?int?x=1; ??x+=1; ??return?x; }
int?main(?){ ??int?i,?s=1; ??for(i=1;?i<=5;i++) ????
s+=fun(?); ??
printf("%d\n",?s); ??
return?0 }
-
A.11
-
B.120
-
C.6
-
D.21
題解:
#include ?<stdio.h>int fun( ){static int x=1;x+=1;return x;
}
int main( ){int i, s=1;for(i=1; i<=5;i++){int a =fun( );s+=a;printf("fun=%d\n",a);printf("s=%d\n",s);}}
運行結果:fun=2 s=3 fun=3 s=6 fun=4 s=10 fun=5 s=15 fun=6 s=21
總結
這些題還是比較不錯和全面的,有些題還可以深入研究,比如數組指針,指針數組(不好記的話可以在中間加上"的",指針的數組,數組的指針.就很好的明白.)(數組指針聲明:int (*p)[5]; ?// p 是指向 int 類型數組的指針;指針的數組聲明int *arr[5]; ?// arr 是一個包含 5 個 int 指針的數組這兩個也很好記,[5]前面就是這是什么數組舉例:int (*p)[5];p[5],p其實代表是數組的起始地址,現在使用是(*p)則表示指向這個數組,則叫數組的指針.指針數組也是一樣:int *arr[5];[5]前面跟的是arr再前面表示類型所以這表示一個數組里面存儲的指針,叫指針的數組.為什么要從右往左讀,為了聲明的結構清晰)還有野指針,懸空指針(指向一塊已經釋放的內存就是選空指針),越界指針(指針訪問超出數組分配的內存范圍).