C语言简单测试总结
前言
在学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再前面表示类型所以这表示一个数组里面存储的指针,叫指针的数组.为什么要从右往左读,为了声明的结构清晰)还有野指针,悬空指针(指向一块已经释放的内存就是选空指针),越界指针(指针访问超出数组分配的内存范围).