C语言错题分析

编码规范的基本原则是 高内聚,低耦合
设计原则:SOLID原则
单一职责原则 SRP 某个代码的功能,保证只有单一明确的执行任务。
开放封闭原则 OCP 对扩展(继承)开放,对修改封闭
里氏替换原则 LSP 在继承中,子类必须实现父类的抽象方法,但不能覆盖父类的非抽象方法。
接口隔离原则 ISP 接口要尽量小在不违反单一职责原则前提下
依赖倒置原则 DIP 面向接口编程,接口不依赖于实现类,但实现类依赖接口。模块间和实现类之间的依赖关系通过抽象类或接口产生。

正交四原则:
消除重复 消除调用和回调的重复。
分离变化方向 将不同的变化方向进行分离,意味着各个变化方向职责的单一化。
减少依赖 模块间不存在耦合,他们都共同耦合在API上,API应包含尽可能少的知识;高内聚
向稳定方向依赖 如果两个模块需要协作,他们之间必然存在耦合点,此时应考虑API向着稳定的方向依赖。

第5题 单选题(2分)
请识别下面获取占地面积函数所违反正交四原则中的主要原则

typedef struct Building { 
    int long; 
    int width; 
    int height; 
    int volume; 
    int cost; 
    int outsideColor; 
    int insideColor; 
}Building; 
int GetBuildingArea(Building *building) 
{ 
    return building->long * building->width; 
}

1) 消除重复

2) 分离关注点
3) 缩小依赖范围
4) 向稳定方向依赖

参考答案:
3
考生答案:
1

第8题 单选题(2分)

What is the output of the following program? 
#include<stdio.h> 
main() 
{ 
  fprintf(stdout,"Hello, World!");    //fprintf是格式化输出到一个流 / 文件中(格式化控制符可省略)
}
1) Hello, World!
2) No output
3) Compile error
4) Runtime error

参考答案:
1
考生答案:
3

第9题 单选题(2分)
Function fopen with the mode "r+" tries to open the file for __
1) reading and writing
2) reading and adding new content
3) only for reading
4) it works only for directories

参考答案:
1
C string containing a file access mode. It can be:

"r" read: Open file for input operations. The file must exist.
"w" write: Create an empty file for output operations. If a file with the same name already exists, its contents are discarded and the file is treated as a new empty file.
"a" append: Open file for output at the end of a file. Output operations always write data at the end of the file, expanding it. Repositioning operations (fseekfsetposrewind) are ignored. The file is created if it does not exist.
"r+" read/update: Open a file for update (both for input and output). The file must exist.
"w+" write/update: Create an empty file and open it for update (both for input and output). If a file with the same name already exists its contents are discarded and the file is treated as a new empty file.
"a+" append/update: Open a file for update (both for input and output) with all output operations writing data at the end of the file. Repositioning operations (fseekfsetposrewind) affects the next input operations, but output operations move the position back to the end of file. The file is created if it does not exist.

第10题 单选题(2分)

sizeof是运算符,表示编译时分配的空间大小,即数组定义的大小,char t[20] = "sfa"。sizeof: 20; strlen: 3.
在未定义数组大小时:

sizeof根据实际位数确定,注意字符串隐藏的也算一位。

//strlen遇到结束,不计

What is the output of the following program?    
#include<stdio.h> 

main() 
{ 
    char s[20] = "HelloHi"; 
    printf("%d %d", strlen(s), sizeof(s)); 
}
1) 5 9
2) 7 20
3) 5 20
4) 8 20

参考答案:
3
考生答案:
1

第14题 单选题(2分)

What is the output of the following program? 
#include<stdio.h> 
main() 
{ 
  register int x = 5; 
  int * p; 
  p=&x;         //寄存器变量在硬件CPU内,无法通过取&获得地址
  x++; 
  printf("%d",*p); 
}
1) Compile error
2) 5
3) 6
4) Garbage value

第19题 单选题(2分)
Choose the invalid identifier from the below //自定义不可使用保留关键字
1) Int
2) volatile
3) DOUBLE
4) __0__

参考答案:
2
考生答案:
4

第22题 单选题(2分)

What is the output of the following code snippet? 
#include<stdio.h> 
main() 
{ 
  int x = 5; 
  if(x=5) 
  { 
    if(x=5) 
    break; 
    printf("Hello"); 
  } 
  printf("Hi"); 
}
1) Compile error
2) Hi
3) HelloHi
4) Compiler warning

参考答案:
1
考生答案:
3

1.break

用break语句可以使流程跳出switch语句体,也可以用break语句在循环结构终止本层循环体,从而提前结束本层循环。

使用说明:

(1只能循环体内switch语句体内使用break;    // 其他地方使用会编译错误2)当break出现在循环体中的switch语句体内时,起作用只是跳出该switch语句体,并不能终止循环体的执行。若想强行终止循环体的执行,可以在循环体中,但并不在switch语句中设置break语句,满足某种条件则跳出本层循环体。

2.continue

continue语句的作用是跳过本次循环体中余下尚未执行的语句,立即进行下一次的循环条件判定,可以理解为仅结束本次循环。

注意:continue语句并没有使整个循环终止。

第25题 单选题(2分)

在64位机器上,以下代码test(val)的返回值是多少()? 数组名做函数参数退化为指针,在64位机上占8字节

char val[10]; 
int test(int var[]) 
{ 
    return sizeof(var); 
}
1) 10
2) 1
3) 8
4) 4

参考答案:
3
考生答案:
4

第30题 单选题(2分)
关于算法,以下叙述中错误的是
1) 任何算法都能转换成计算机高级语言的程序,并在有限时间内运行完毕
2) 同一个算法对于相同的输入必能得出相同的结果
3) 一个算法对于某个输入的循环次数是可以事先估计出来的
4) 某个算法可能会没有输入

参考答案:
1
考生答案:
2

第31题 单选题(2分)
已知大写字母A的ASCII码值是65,小写字母a的ASCII码是97,则用八进制表示的字符常量'101'是
1) 字符A
2) 字符a
3) 字符e
4) 非法的常量

参考答案:
1
考生答案:
3

第32题 单选题(2分)

What is the output of the following program? 
#include<stdio.h> 
main() 
{ 
    int x = 3; 
    x += 2; 
    x =+ 2; 
printf("%d", x); 
}
1) 2
2) 5
3) 7
4) Compile error

参考答案:
1
考生答案:
4

第33题 单选题(2分)

What is the output of the following code snippet? 
#include<stdio.h> main() 
{ 
    int x = 5; 
    if(x==5) 
    { 
       if(x==5) break; 
       printf("Hello"); 
    } 
    printf("Hi"); 
}
1) Compile error //break只能在循环语句和switch语句中使用,否则会编译错误
2) Hi
3) HelloHi
4) Hello

参考答案:
1
考生答案:
3

第37题 单选题(2分)

int main(void) 
{ 
    int a = 1; 
    long b = 10; 
    float c = 5.8; 
    double d = 31.95; 
    char e = a + b + c + d; //不同类型赋值操作会进行数据类型转换,eg: 47.85 -> 47
    return 0; 
} 
e被赋值后,e的值为:
1) 47
2) '0'
3) 'a'
4) 48.75

参考答案:
2
考生答案:
4

第41题 单选题(2分)
请识别下面结构体所违反SOLID原则中的主要原则

typedef struct Building 
{ 
    int long; 
    int width; 
    int height; 
    int volume; 
    int cost; 
    int outsideColor; 
    int insideColor; 
}Building; 
int GetBuildingArea(Building *building) 
{ 
    return building->long * building->width; 
}
1) 单一职责
2) 开放封闭
3) 接口隔离
4) 依赖倒置
5) 里氏替换

参考答案:
3
考生答案:
5

第42题 单选题(2分)

What is the output of the following program? 
#include<stdio.h> 
main() 
{ 
    int i = 1; 
    while(++i <= 5) 
    printf("%d ",i++); 
}
1) 1 3 5
2) 2 4
3) 2 4 6
4) 2

参考答案:
2
考生答案:
4

第46题 单选题(2分)
What is the size of ‘int’?
1) 2
2) 4
3) 8
4) Compiler dependent //在64位机子上是8字节,32位机子是4字节

参考答案:
4
考生答案:
2

第47题 单选题(2分)

What is the output of the following code snippet? 
#include<stdio.h> 
main() 
{ 
    const int a = 5; 
    a++; 
    printf("%d", a); 
}
1) 5
2) 6
3) Runtime error
4) Compile error

参考答案:
4
考生答案:
3

第48题 单选题(2分)

What is the output of the following program? 
#include<stdio.h> 
main() 
{ 
    char * s = "Fine"; 
    * s = 'N'; 
    printf("%s", s); 
}
1) Fine
2) Nine
3) Compile error
4) Runtime error

参考答案:
4
考生答案:

第49题 单选题(2分)

What is the output of the following program? 
#include<stdio.h> 
main() 
{ 
    int r, x = 2; 
    float y = 5; 
    r = y % x; %运算只能用于整数与整数之间
    printf("%d", r); 
}
1) 1
2) 0
3) 2
4) Compile error

参考答案:
4
考生答案:

第50题 单选题(2分)

What is the output of the following program? 
#include<stdio.h> 
main() 
{ 
    int x = 1; 
    do{
    printf("%d ", x); 
    }while(x++<=1); 
}
1) 1
2) 1 2
3) No output
4) Compile error

参考答案:
2

原文地址:https://www.cnblogs.com/Lunais/p/9974516.html