(完整版)C语言2
- 格式:doc
- 大小:51.51 KB
- 文档页数:7
计算机二级c语言试题及答案解析一、选择题1. 在C语言中,以下哪个关键字用于定义一个结构体?A. structB. unionC. enumD. typedef答案:A解析:关键字`struct`用于定义一个结构体,它允许将多个不同类型的数据项组合成一个单一的数据结构。
2. 下列哪个选项不是C语言中的运算符?A. %B. &&C. ::D. ||答案:C解析:`%`是取模运算符,`&&`是逻辑与运算符,`||`是逻辑或运算符,而`::`不是C语言中的运算符。
二、填空题1. 在C语言中,`________`关键字用于声明一个函数。
答案:int解析:在C语言中,`int`是用于声明一个函数的返回类型,表示该函数返回一个整数值。
2. 以下代码片段中,`________`用于定义一个名为`data`的数组,包含10个整数。
```cint data[10];```答案:int data[10];解析:`int data[10];`定义了一个名为`data`的数组,它包含10个整数。
三、编程题1. 编写一个C语言函数,计算并返回两个整数的和。
```cint add(int a, int b) {return a + b;}```答案:```cint add(int a, int b) {return a + b;}```解析:该函数接受两个整数参数`a`和`b`,计算它们的和,并返回结果。
2. 编写一个C语言程序,实现将一个字符串反转。
```c#include <stdio.h>#include <string.h>void reverse(char *str) {int len = strlen(str);for (int i = 0; i < len / 2; i++) { char temp = str[i];str[i] = str[len - i - 1];str[len - i - 1] = temp;}}int main() {char str[] = "Hello, World!";reverse(str);printf("Reversed string: %s\n", str); return 0;}```答案:```c#include <stdio.h>#include <string.h>void reverse(char *str) {int len = strlen(str);for (int i = 0; i < len / 2; i++) { char temp = str[i];str[i] = str[len - i - 1];str[len - i - 1] = temp;}}int main() {char str[] = "Hello, World!";reverse(str);printf("Reversed string: %s\n", str);return 0;}```解析:该程序定义了一个`reverse`函数,它接受一个字符串作为参数,并通过交换字符的方式将字符串反转。
《C语言程序设计能力教程(第二版)》课后作业及实训题参考答案第1章进入C语言程序世界二、1. I love China!printf("we are students.\n")2. 6项目实训题参考答案1.编写一个C程序,输出以下信息:* * * * * * * * * * * * * * * * * * * *I am a student!* * * * * * * * * * * * * * * * * * * *main(){ printf("********************\n");printf(" I am a student!\n ");printf("********************\n");}2.已知立方体的长、宽、高分别是10cm、20cm、15cm,编写程序,求立方体体积。
解:main(){int a,b,c,v;a=10;b=20;c=15;v=a*b*c;printf("v=%d",v);}本程序运行结果为:v=3000第2章编制C程序的基础知识一选择题C B A B A C C二操作题3.,2.,-8.2. ABC DEFGHwhy is 21+35 equal 523.3 14 32 31 24. aa bb cc abcA N项目实训题1.定义一个符号常量M为5和一个变量n值为2,把它们的乘积输出。
#define M 5main(){ int n,c;n=2; c=M*n;printf("%d\n",c); }2.编程求下面算术表达式的值。
(1)x+a%3*(int)(x+y)%2/4,设x=2.5,a=7,y=4.7;(2)(float)(a+b)/2+(int)x%(int)y,设a=2,b=3,x=3.5,y=2.5。
(1)main(){ int a=7;float x=2.5,y=4.7;printf("%f\n",x+a%3*(int)(x+y)%2/4); }(2)main(){ int a=2,b=3;float x=3.5,y=2.5;printf("%f\n",(float)(a+b)/2+(int)x%(int)y); }第三章顺序结构程序设计一选择题A C D C C二操作题1. x=3,a=2,b=32. z=12.3. 1 2 1a2 1 2三.编程题编程题1. 某工种按小时计算工资,每月劳动时间(小时)×每小时工资=总工资,总工资中扣除10%公积金,剩余的为应发工资。
编写一程序P219.C实现以下功能从键盘读入一行字符(约定:字符数≤127字节),将其中的数字字符以及这些数字字符的数量在屏幕上显示,注意:要求先显示这些数字字符的数量。
编程可用素材:printf("Please input string:")...、printf("\nshu zi ge shu wei:...、printf("\nshu zi wei:...。
程序的运行效果应类似地如图1所示,图1中的gfaskl45623cvsac,53dwafl faf32535as3bf0是从键盘输入的内容。
#include <stdio.h>#include <string.h>int main(void){int num=0, i;char str[128], jieguo[128];printf("Please input string: ");gets(str);for (i=0; str[i]!='\0'; i++){if (str[i]>='0' && str[i]<='9'){jieguo[num] = str[i];num++;}}jieguo[num] = '\0';printf("\nshu zi ge shu wei:%d", num);printf("\nshu zi wei:%s", jieguo);return 0;}编写一程序P220.C实现以下功能从键盘读入一行字符(约定:字符数≤127字节),统计及输出其中的字母、数字、空格和其他符号的个数。
编程可用素材:printf("Please input string:")...、printf("\nzimu=...,shuzi=...,kongge=...,qita=..."...。
电脑二级C复习资料公共基础知识资料第一章数据结构与算法【考点1】算法的基本概念算法:是指一组有穷的指令集,是解题方案的准确而完整的描述。
算法不等于程序,也不等于计算方法。
算法的基本特征:确定性,算法中每一步骤都必须有明确定义,不允许有多义性;有穷性,算法必须能在有限的时间内做完,即能在执行有限个步骤后终止;可行性,算法原则上能够精确地执行;拥有足够的情报。
算法的组成要素:一个算法由数据对象的运算和操作以及其控制结构这两部分组成。
算法的基本运算和操作:算术运算,逻辑运算,关系运算,数据传输。
算法的基本控制结构:顺序,选择,循环。
算法基本设计方法:列举法、归纳法、递推、递归、减半递推技术。
【考点2】算法的复杂度算法效率的度量——算法的复杂度:时间复杂度和空间复杂度。
算法时间复杂度:指执行算法所需要的计算工作量。
通常,一个算法所用的时间包括编译时间和运行时间。
算法空间复杂度:指执行这个算法所需要的内存空间。
包括算法程序所占的空间,输入的初始数据所占的空间,算法执行过程中所需的额外空间。
空间复杂度和时间复杂度并不相关。
【考点3】数据结构的基本概念数据:数据是客观事物的符号表示,是能输入到电脑中并被计算程序识别和处理的符号的总称,如文档,声音,视频等。
数据元素:数据元素是数据的基本单位。
数据对象:数据对象是性质相同的数据元素的集合。
数据结构:是指由某一数据对象中所有数据成员之间的关系组成的集合。
【考点4】逻辑结构和存储结构数据结构可分为数据的逻辑结构和存储结构。
数据的逻辑结构是对数据元素之间的逻辑关系的描述,与数据的存储无关,是面向问题的,是独立于电脑的。
它包括数据对象和数据对象之间的关系。
数据的存储结构也称为数据的物理结构,是数据在电脑中的存放的方式,是面向电脑的,它包括数据元素的存储方式和关系的存储方式。
数据结构和逻辑结构的关系:一种数据的逻辑结构可以表示成多种存储结构即数据的逻辑结构和存储结构不一定一一对应。
一.填空1、 are used to document a program and improve its readability.2、What, if anything, prints when each of the following C statements is performed?If nothing prints, then answer “nothing.”Assume x = 2 and y = 3.a)printf( "%d = %d", x + y, y + x );b) z = x + y;A location in the computer's memory that may contain different values at varioustimes throughout the execution of a program is calledaWrite a single pseudocode statement that indicates:Assign the sum of variablesx, y, and z to variable p.The solution to any problem involves performing a series of actions in a specificA synonym for procedure is7、State which values of the control variable x are printed by each of the followingfor statements:a)for ( x = 2; x <= 13; x += 2 )printf( "%d\n", x );b) for ( x = 3; x <= 15; x += 3 )printf( "%d\n", x );8、Show the value of x after each of the following statements is performed:x=7.5a) x = fabs( x);b) x = floor( x);C stores lists of values in10、 The contents of a particular element of an array is called the ofthat element.11、The name of the element in row 3 and column 5 of array d is .12、Write statements to accomplish each of the following:a) Display the value of the seventh element of character array f.b) Copy array a into the first portion of array b. Assume double a[ 11 ], b[ 34 ];13、Consider a 2-by-5 integer array t.a) Write a definition for t.b) How many elements does t have?Write for statements that print the following sequences of values:1, 2, 3, 4, 5, 6, 7Write statements that assign random integers to the variable n in the followingranges:–3 ≤ n ≤ 11选择1. The function inputs values from the keyboard is ( ).A.scanfB.printfC.whileD.main2.Input three integer values from the keyboard and place these values in integervariables a, b and c.A.scanf(“%d%d%d”, a, b, c )B.printf(“%d%d%d”, a, b, c)C.scanf(“%d%d%d”, &a, &b, &c ) C.printf( “%d%d%d”, &a, &b, &c )3.Which contents will display on the screen while you write the statement printf("a= 5;");A. printf("a = 5;")B.5C.a = 5D.a = 5 ;4. Which one is invalid variable names?A.3gB.whileC.cut.outD.EATING5.Which, if any, of the following C statements contain variables involved indestructive read-in?a) scanf( "%d%d%d%d%d", &b, &c, &d, &e, &f );b) p = i + j + k + 7;c) printf( "Destructive read-in" );d) printf( "a = 5" );6.Given the equation y = ax3 + 7, which of the following, if any, are correctC statements for this equation?A) y = a * x * x * ( x + 7 );B) y = ( a * x ) * x * ( x + 7 );C) y = ( a * x ) * x * x + 7;D) y = a * x * ( x * x + 7 );7. Show the value of equator x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) ); afterC statement is performed.A.324B.338C.108D.124The item written inside a decision symbol is called a ( )A.flowlineB.conditionC. algorithmD.assignmentWhich C statement is true?printf(“%d\n”, x ); B. While ( counter <<= 100 );C.for ( x = =3; x <= 15; x += 3 )D. counter ++2;Which values of the control variable x are printed?for ( x = 5; x <= 22; x += 7 )printf( "%d\n", x );A.5, 9,12B.6,9,12C.6,12,19D.5,12,19Write for statements that print the following sequences of values:1, 2, 3, 4,5, 6, 7for ( i = 1; i <= 7; i++ )printf( “%d ”, i );B.for ( i = 0; i <= 7; i++ )printf( “%d ”, i );C.for ( i = 1; i < 7; i++ )printf( “%d ”, i );D.for ( i = 0; i < 7; i++ )printf( “%d ”, i );How many @ will be printed by follow statement?x=3;y=3;for ( i = 0; i <= y; i++ ) {for ( j = 1; j < x; j++ ) {printf( "@" ); } } A.6 B.4 C.9 D.8Which statement is used to shift to another line after print a string.printf( “The product of %d and %d is %d”\n, x, y, z );B. printf( “The product of %d and %d is %d\n”, x, y, z );C. printf( “The product of %d and %d is %d\n”, &x, &y,&z );D. printf( “The product of %d and %d is %d”\n, &x, &y, &z );14.The function used to display information on the screen is ( )A.scanfB.printfC.whileD.mainCalculations are normally performed by statements.condition B.assignment C.judgementD.algorithmWhich statement is true to assign the product of variables b and c to variablea.A.a* = b +c;B.a == b * c;C.a *= =b+c;D.a =b * c;17.Which contents will be printed by printf( "%d = %d", x + y, y + x );A.%5=%5B.5 5C. 5 = 5D.5==518.Which contents will be printed by /*printf( "%d = %d", x + y, y + x ); */A.%5=%5B.5 5C. 5 = 5D.(NOTHING)19. Rectangle symbols correspond to input/output operations that are normallyperformed by calls to the______and______standardlibrary functions.ANS: assignment, printf, scanf.assignment, printf B. assignment, scanf.C. printf, scanf.D. scanf, assignment,20.how many space would be used by follow statement char array [ ]="China";A.4 bytesB.5 bytesC.6 bytesD.7 bytes21、Given the equation y = ax3 + 7, which of the following, if any, are correctC statements for this equation?A) y = a * x * x * ( x + 7 );B) y = ( a * x ) * x * ( x + 7 );C) y = ( a * x ) * x * x + 7;D) y = a * x * ( x * x + 7 );22、Which, if any, of the following C statements contain variables involved indestructive read-in?A、 scanf( "%d%d%d%d%d", &b, &c, &d, &e, &f );B、 p = i + j + k + 7;C、 printf( "Destructive read-in" );D、 printf( "a = 5" );Show the value of x after the following statements is performedx = ceil( -fabs( -8 + floor( -5.5 ) ) );A、14.0B、-13.0C、 -13.5 D -14.0The name of the element in row 3 and column 5 of array d isA 、 d[3][4] B、 d[3][5] C、 d[2][4] D 、 d[2][4]25、Write the function header for function zero, which takes a long integer arrayparameter a and does not return a value.A void zero( long int *a);B int zero( long int *a);C void zero( int long*a);D void zero( int long a[]);An m-by-n array contains _____ rows,_______ columns and ______ elements.A、n,m,m*nB、 m,n,(m-1)*(n-1)C、 m,n m*nD、 n,m,(m-1)*(n_1)27、Consider a 2-by-5 integer array t, write a statement that totals the elementsof the fourth column of t.A、 sum = t[ 0 ][ 3 ] + t[ 1 ][ 3 ];B、sum = t[4 ][ 0] + t[ 4 ][ 1]+t[4][2]+t[4][3];C、sum = t[3][ 0] + t[ 3 ][ 1]+t[3][2]+t[3][3];D、sum = t[ 0 ][ 4 ] + t[ 1 ][ 4 ];Display the value of the seventh element of character array f.printf( “%d\n”, f[ 6 ] ); B、 printf( “%d\n”, f[ 7] );C、 printf( “%c\n”, f[ 7 ] );D、 printf( “%c\n”, f[ 6 ] );What's the way when a function is called and the data transfer between theargument variables and parameter variables ()A 、pass by address B、 pass by values C、User specify thetransfer modeD、passing argument variables to parameter variables and return fromparameter variables to argument variablesWhat does the following program printmain(){int i=1,j=2,k=3;if(i++==1&&(++j==3||k++==3))printf("%d %d %d\n",i,j,k);}A、1 2 3B、2 3 4C、2 2 3D、2 3 331、Assuming the following term int a[4][3]={1,2,3,4,5,6,7,8,9,10,11,12};int (*ptr)[3]=a, *p=a[0]; the name of the element a[1][2]()A) *((ptr+1)[2]) B) *(*(p+5))C) (*ptr+1)+2 D) *(*(a+1)+2 )32、Given int i=0,j=1,*p=&i,*q=&j;which of the following statement is wrong?()A) i=*&j; B) p=&*&i; C)j=*p++; D)i=*&q;33、An m-by-n array has ____elements before a[i][j]。