【计算机类职业资格】初级程序员下午试题-64及答案解析.doc

上传人:dealItalian200 文档编号:1330124 上传时间:2019-10-17 格式:DOC 页数:13 大小:51KB
下载 相关 举报
【计算机类职业资格】初级程序员下午试题-64及答案解析.doc_第1页
第1页 / 共13页
【计算机类职业资格】初级程序员下午试题-64及答案解析.doc_第2页
第2页 / 共13页
【计算机类职业资格】初级程序员下午试题-64及答案解析.doc_第3页
第3页 / 共13页
【计算机类职业资格】初级程序员下午试题-64及答案解析.doc_第4页
第4页 / 共13页
【计算机类职业资格】初级程序员下午试题-64及答案解析.doc_第5页
第5页 / 共13页
点击查看更多>>
资源描述

1、初级程序员下午试题-64 及答案解析(总分:18.00,做题时间:90 分钟)一、试题一(总题数:1,分数:3.00)1.阅读以下说明和流程图回答问题,将解答填入对应栏。 说明 “直接插入法”排序是一种 N2运算量的例程,只能用在 N较小的时候,其方法是:挑出第二个数将它按与第一个数大小的顺序插入,然后挑出第三个数将它按大小顺序插入到前两个数中,如此下去,一直到最后一个也插入。 注:流程中循环开始的说明按照“循环变量:循环初值,循环终值,增量”格式描述。 * 问题 将流程图的(1)(5)处补充完整。(分数:3.00)填空项 1:_二、试题二(总题数:1,分数:3.00)2.阅读以下函数说明和

2、C语言函数,将应填入 (n) 处的字句写在对应栏内。 说明 1 函数 int factors(int n)的功能是判断整数 n(n=2)是否为完全数。如果 n是完全数,则函数返回 0,否则返回-1。 所谓“完全数”是指整数 n的所有因子(不包括 n)之和等于 n自身。例如:28 的因子为1,2,4,7,14,而 28=1+2+4+7+14,因此 28是“完全数”。 C函数 1 int factors(int n) int i,S; for(i=l,s=0;i=n/2;i+) if(n%i=O) (1) ; if( (2) )return 0; rerurn -1; 说明 2 函数 int ma

3、xint(int a,int k)的功能是用递归方法求指定数组中前 k个元素的最大值,并作为函数值返回。 C函数 2 int maxint(int a,int k) int t; if( (3) )return (4) ; t=maxint(a+1, (5) )j return(a0t) ? a0 :t; (分数:3.00)填空项 1:_三、试题三(总题数:1,分数:3.00)3.阅读以下函数说明和 C语言函数,将应填入 (n) 处的字句写在对应栏内。 说明 该程序从正文文件 test.txt中读入一批整数,并将它们按照递增的顺序存放在一个链表中。其中,函数struct Link*insert

4、Chain(struct Link*head,structLink*k)用来寻找结点 k在链表 head中的插入位置,并插入该结点。 C程序 #include stdio.h#include stdlib.h struct Link int number; struct Link *next; struct Link *insertChain(struct Link *head,struct Link *k); VOid main() struct Link *ptr, *head; FILE *fd; int hum; if(fd=fopen(“test.txt“,“r“)=NULL) pr

5、int(“Cannot open this file!/n“); return; head=NULL; while(fscanf(fd,“%d“,hum)=1) ptr= (1) ; if(!ptr) return; ptr-number=hum; ptr-next=NULL; head=insertChain(head,ptr); (2) ; return; struct Link *insertChain(struct Link *head,struct Link *k) struct Link *ptr,*u; ptr=head; while(ptr k k-numberptr-numb

6、er) u=ptr; (3) if(ptr = head) head=k; else (4) ; (5) ; return head; (分数:3.00)填空项 1:_四、试题四(总题数:1,分数:3.00)4.阅读以下函数说明和 C语言函数,将应填入 (n) 处的字句写在对应栏内。 说明 这是一个模拟渡口管理的算法。某汽车轮渡口,过江渡船每次能载 10辆车过江。过江车辆分为客车类和火车类,上船有如下规定:同类车先到先上船,客车先于货车上渡船,且每上 4辆客车,才允许上一辆货车;若等待客车不足 4辆,则以货车代替,若无货车等待则允许客车都上船。 程序中用到的函数有 enqueue(queue*

7、sq,elemtype *x)在队列 sq中入队一个元素x;outqueue(queue *sq,elemtype *x)在队列 sq中出队一个元素,并将其值赋给 x;empty(queue *sq)判断队列 sq是否为空队,若为空,返回 1;否则返回 0。 C程序 #includestdio.h void pass() queue bus,truct; /*bus表示客车队列,truck 表示货车队列*/ char ch; int n,tag; / * n为车号,tag 为标志,tag=0 表示客车,tag=1 表示货车 */ intcount=0,countbus=0,counttruck

8、=0; / *分别表示上渡船汽车数、客车数、货车数 */ while(1) printf(“输入命令: /n“); Scanf(“%c“, switch(ch) casee: caseE: printf(“车号: /n“); Scanf(“%d“, printf(“客车/货车(0/1): /n“); scanf(“%d“, if( (1) ) enqueue( else enqueue( break; casei: caseI: while(count10) if( (2) printf(“上船的车号为: /n“); count+; (3) ; eise if( (4) ) /*货车出队 */

9、 countbus=0; outqueue( printf(“上船的车号为: /n“); count+; counttruck+; else if(empty( outqueue( printf(“没有 10辆车排队轮渡/n“); count+; countbus+; else printf(“没有 10辆车排队轮渡/n“); retUrn; break; caseq: caseQ:break; if(ch=q | ch=Q) break; (分数:3.00)填空项 1:_五、试题五(总题数:1,分数:3.00)5.阅读以下说明和 C+程序,将应填入 (n) 处的字句写在对应栏内。 说明 下面

10、程序输出一个矩形面积,以及矩形区域上的假想的作物产量。 C+程序 #include iostream.h class crop_assessment int actual_crop; int ideal_crop; public: void set(int in_actual,int in_ideal) actual crop=in_actual; ideal_crop=in_ideal; int get_actual_crop(void) (1) ; int get_ideal_crop(void) (2) ;) ; Class lot_size int length; int width;

11、 (3) crop; public: void set(int 1,int w,int a,int i) length=1; width=w; crop.set(a,i); int get_area(void)return length*width; int get_data(void)return (4) ; int get_data2(void)freturn (5) ; int main() Los_size small,medium; small.set(5,5,5,25); medium.set(10,10,10,50); cout“For a small lot of area“s

12、mallget_area()“/n”;cout“the actual crops are$“small.get_data2()“/n“;cout“and ideal crops are$”small.get_data()“/n“;cout“For a medium Lot of area“medium.get area():/n”;cout“the actual crops are$“medium.get_data2()“/n“;cout“and ideal crops are$“medium.get_data()“/n“;return 0;(分数:3.00)填空项 1:_六、试题六(总题数:

13、1,分数:3.00)6.阅读以下说明和 Java程序,将应填入 (n) 处的字句写在对应栏内。 说明 下面程序输出一个矩形面积,以及矩形区域上的假想的作物产量。 Java程序 public class MainJava public static void main(String args) Lot_size small=new Lot_size(); Lot_size medium=new Lot_size(); small.set(5,5,5,25); medium.set(10,10,10,50); System.out.println(“For a small lot of area“

14、 +small.get_area()+“/n“); System.out.println(“the actual crops are $“ +small.get_data2()+“/n“); System.out.println(“and ideal crops are $“ +small.get data()+“/n“); System.out.println(“For a medium lot of area“ +medium.get_area()+“/n”); System.out.println(“the actual crops are $“ +medium.get_data2()+

15、“/n“); System.out.println (“and ideal crops are $“ +medium.get_data()+“/n“); class Crop_assessment private int actual_crop; private int ideal_crop; public void set(int in_actual,int in ideal) actual_crop=in_actual; ideal_crop=in_ideal; public int get_actual_crop()return (1) ; public int get_ideal_cr

16、op()(return (2) ;class Lot_size private int length; private int width; private Crop_assessment crop= (3) ; public void set(int 1,int W,int a,int i) length=1; width=W; crop.set(a,i); public int get_area()return length*width; public int get_data()freturn (4) ; public int get_data2()(return (5) ;(分数:3.

17、00)填空项 1:_初级程序员下午试题-64 答案解析(总分:18.00,做题时间:90 分钟)一、试题一(总题数:1,分数:3.00)1.阅读以下说明和流程图回答问题,将解答填入对应栏。 说明 “直接插入法”排序是一种 N2运算量的例程,只能用在 N较小的时候,其方法是:挑出第二个数将它按与第一个数大小的顺序插入,然后挑出第三个数将它按大小顺序插入到前两个数中,如此下去,一直到最后一个也插入。 注:流程中循环开始的说明按照“循环变量:循环初值,循环终值,增量”格式描述。 * 问题 将流程图的(1)(5)处补充完整。(分数:3.00)填空项 1:_ (正确答案:1,n-1,1; (2) aj;

18、 (3) ajai; (4) ai+1=ai; (5) ai+1=a;)解析:解析 本题目考查流程图。 题目中已经给出了直接插入法排序的算法,由于该算法是从数组中第二个数起,取出并与前面的数进行排序,直到数组中最后一个数排序完成,所以,循环变量初值为 1,终值为 N-1,增量为 1,按照题目中要求的格式,则为“1,N-1,1”。 由题目中的算法可知,我们要将取出来的数同排在其前面的数做比较,并插入,所以,首先要把取出来的数赋给一个变量,即(2)填入“aj”。然后从第 j-1个数开始,如果 aiaj,那么我们就将 aiN移一位,以便空出一个位置来插入 aj,所以(4)填入“ai+1=ai”这样一

19、直进行到 aiaj或者i0,这时,我们就找到了 aj要插入的位置,可以将 aj插入,即(3)填入“ajai”,(5)填入“ai+1=a”。二、试题二(总题数:1,分数:3.00)2.阅读以下函数说明和 C语言函数,将应填入 (n) 处的字句写在对应栏内。 说明 1 函数 int factors(int n)的功能是判断整数 n(n=2)是否为完全数。如果 n是完全数,则函数返回 0,否则返回-1。 所谓“完全数”是指整数 n的所有因子(不包括 n)之和等于 n自身。例如:28 的因子为1,2,4,7,14,而 28=1+2+4+7+14,因此 28是“完全数”。 C函数 1 int facto

20、rs(int n) int i,S; for(i=l,s=0;i=n/2;i+) if(n%i=O) (1) ; if( (2) )return 0; rerurn -1; 说明 2 函数 int maxint(int a,int k)的功能是用递归方法求指定数组中前 k个元素的最大值,并作为函数值返回。 C函数 2 int maxint(int a,int k) int t; if( (3) )return (4) ; t=maxint(a+1, (5) )j return(a0t) ? a0 :t; (分数:3.00)填空项 1:_ (正确答案:s+=i (2) n=s (3) k-1 或

21、 k-1=0 (4) a0或 *a或 ak-1 (5) k-1或-k)解析:解析 对于函数 1,是判断整数 n(n=2)是否为完全数。首先用 for循环求该整数的所有因子之和,所以(1)填“s+=i”若其和等于整数本身,则为完全数,返回值为 0,则(2)填“n=s”;否则返回值为-1。 对于函数 2,是用递归方法找出数组中的最大元素。该递归的出口条件为 k=1,即(3)填“k=1”或“k-1=0”:只有一个数时,它本身就是最大的,(4)填“a0”或“ *a”或“ak-1”;对于多个数的情况,在剩下的 k-1个元素中找到最大的,并与首元素值比较,返回最大的一个,所以(5)填“k-1”或“-k”。

22、三、试题三(总题数:1,分数:3.00)3.阅读以下函数说明和 C语言函数,将应填入 (n) 处的字句写在对应栏内。 说明 该程序从正文文件 test.txt中读入一批整数,并将它们按照递增的顺序存放在一个链表中。其中,函数struct Link*insertChain(struct Link*head,structLink*k)用来寻找结点 k在链表 head中的插入位置,并插入该结点。 C程序 #include stdio.h#include stdlib.h struct Link int number; struct Link *next; struct Link *insertCha

23、in(struct Link *head,struct Link *k); VOid main() struct Link *ptr, *head; FILE *fd; int hum; if(fd=fopen(“test.txt“,“r“)=NULL) print(“Cannot open this file!/n“); return; head=NULL; while(fscanf(fd,“%d“,hum)=1) ptr= (1) ; if(!ptr) return; ptr-number=hum; ptr-next=NULL; head=insertChain(head,ptr); (2

24、) ; return; struct Link *insertChain(struct Link *head,struct Link *k) struct Link *ptr,*u; ptr=head; while(ptr k k-numberptr-number) u=ptr; (3) if(ptr = head) head=k; else (4) ; (5) ; return head; (分数:3.00)填空项 1:_ (正确答案:(struct Link *)malloc(sizeof (struct Link) (2) fclose(fd) (3) ptr=ptr-next 或 pt

25、r=u-next (4) u-next=k (5) k-next=ptr)解析:解析 本题采用文件的输入方式,其主要思路:打开文件;从文件读入一个整数,动态申请一个结点;将结点插入到以 head为头指针的链表中;反复执行和直到文件结束;关闭文件。 通过上述分析,不难得到(1)为申请结点,应该填“(struct Link *)malloc(sizeof(structLink)”;(2)为关闭文件,即 fclose(fd)。在插入结点时,让 u指向插入结点 ptr之前,因此(3)为 u的下一个结点,填“ptr=ptr-next”或“ptr=u-next”。插入结点时分为在头结点插入和 u之后插入两

26、种情况。(4)为在 u之后插入的情况,应该填“u-next=k”,(5)填“k-next=ptr”。四、试题四(总题数:1,分数:3.00)4.阅读以下函数说明和 C语言函数,将应填入 (n) 处的字句写在对应栏内。 说明 这是一个模拟渡口管理的算法。某汽车轮渡口,过江渡船每次能载 10辆车过江。过江车辆分为客车类和火车类,上船有如下规定:同类车先到先上船,客车先于货车上渡船,且每上 4辆客车,才允许上一辆货车;若等待客车不足 4辆,则以货车代替,若无货车等待则允许客车都上船。 程序中用到的函数有 enqueue(queue*sq,elemtype *x)在队列 sq中入队一个元素x;outq

27、ueue(queue *sq,elemtype *x)在队列 sq中出队一个元素,并将其值赋给 x;empty(queue *sq)判断队列 sq是否为空队,若为空,返回 1;否则返回 0。 C程序 #includestdio.h void pass() queue bus,truct; /*bus表示客车队列,truck 表示货车队列*/ char ch; int n,tag; / * n为车号,tag 为标志,tag=0 表示客车,tag=1 表示货车 */ intcount=0,countbus=0,counttruck=0; / *分别表示上渡船汽车数、客车数、货车数 */ while

28、(1) printf(“输入命令: /n“); Scanf(“%c“, switch(ch) casee: caseE: printf(“车号: /n“); Scanf(“%d“, printf(“客车/货车(0/1): /n“); scanf(“%d“, if( (1) ) enqueue( else enqueue( break; casei: caseI: while(count10) if( (2) printf(“上船的车号为: /n“); count+; (3) ; eise if( (4) ) /*货车出队 */ countbus=0; outqueue( printf(“上船的

29、车号为: /n“); count+; counttruck+; else if(empty( outqueue( printf(“没有 10辆车排队轮渡/n“); count+; countbus+; else printf(“没有 10辆车排队轮渡/n“); retUrn; break; caseq: caseQ:break; if(ch=q | ch=Q) break; (分数:3.00)填空项 1:_ (正确答案:tag=O (2) count4 (3) countbus+ (4)empty( int ideal_crop; public: void set(int in_actual,

30、int in_ideal) actual crop=in_actual; ideal_crop=in_ideal; int get_actual_crop(void) (1) ; int get_ideal_crop(void) (2) ;) ; Class lot_size int length; int width; (3) crop; public: void set(int 1,int w,int a,int i) length=1; width=w; crop.set(a,i); int get_area(void)return length*width; int get_data(

31、void)return (4) ; int get_data2(void)freturn (5) ; int main() Los_size small,medium; small.set(5,5,5,25); medium.set(10,10,10,50); cout“For a small lot of area“smallget_area()“/n”;cout“the actual crops are$“small.get_data2()“/n“;cout“and ideal crops are$”small.get_data()“/n“;cout“For a medium Lot of

32、 area“medium.get area():/n”;cout“the actual crops are$“medium.get_data2()“/n“;cout“and ideal crops are$“medium.get_data()“/n“;return 0;(分数:3.00)填空项 1:_ (正确答案:return actual_crop (2) return ideal_crop (3) Crop_assessment (4) cropget_ideal_crop() (5) crop.get_actual_crop())解析:解析 本题以 C+语言为载体,考查面向对象程序设计中

33、的几个重要概念类的嵌套及函数的使用。 本题的功能是通过已定义的粮食收成类定义了一个计算特定矩形域上粮食收成的类,在主函数中定义了两个对象,并调用了相应的函数,来输出理想和实际的粮食产量。 首先,由于 Crop_assessment定义的成员数据缺省为私有的,所以想要获得实际和理想的粮食产量,要通过两个公有成员函数,所以(1)处应填入“return actual_crop”,(2)处应填入“return ideal_crop”。其次,在类 Lot_size中,由于我们要反映矩形域上的粮食产量,所以我们在类中嵌套定义了一个类,(3)应填入“Crop_assessment”。 最后,由于我们想通过

34、Lot_size类中的 getdata和 get_data2函数得到粮食产量,但由于这两个成员数据是私有函数,所以我们必须通过定义的对象调用它,所以(4)应填入“crop.get ideal_crop()”,(5)应填入“crop.get actual_crop()”。六、试题六(总题数:1,分数:3.00)6.阅读以下说明和 Java程序,将应填入 (n) 处的字句写在对应栏内。 说明 下面程序输出一个矩形面积,以及矩形区域上的假想的作物产量。 Java程序 public class MainJava public static void main(String args) Lot_size

35、 small=new Lot_size(); Lot_size medium=new Lot_size(); small.set(5,5,5,25); medium.set(10,10,10,50); System.out.println(“For a small lot of area“ +small.get_area()+“/n“); System.out.println(“the actual crops are $“ +small.get_data2()+“/n“); System.out.println(“and ideal crops are $“ +small.get data(

36、)+“/n“); System.out.println(“For a medium lot of area“ +medium.get_area()+“/n”); System.out.println(“the actual crops are $“ +medium.get_data2()+“/n“); System.out.println (“and ideal crops are $“ +medium.get_data()+“/n“); class Crop_assessment private int actual_crop; private int ideal_crop; public

37、void set(int in_actual,int in ideal) actual_crop=in_actual; ideal_crop=in_ideal; public int get_actual_crop()return (1) ; public int get_ideal_crop()(return (2) ;class Lot_size private int length; private int width; private Crop_assessment crop= (3) ; public void set(int 1,int W,int a,int i) length=

38、1; width=W; crop.set(a,i); public int get_area()return length*width; public int get_data()freturn (4) ; public int get_data2()(return (5) ;(分数:3.00)填空项 1:_ (正确答案:return actual_crop (2) return ideal_crop (3) new Crop_assessment() (4) crop.get_ideal_crop (5) crop.get_actual_crop())解析:本题以 Java语言为载体,考查面

39、向对象程序设计中的几个重要概念类的嵌套及函数的使用。本题的功能是通过已定义的粮食收成类定义了一个计算特定矩形域上粮食收成的类,在主函数中定义了两个对象,并调用了相应的函数,来输出理想和实际的粮食产量。首先,由于 Crop assessment定义的成员数据缺省为私有的,所以想要获得实际和理想的粮食产量,要通过两个公有成员函数,所以(1)处应填入“return actual_crop”,(2)处应填入“return ideal_crop”。其次,在类 Lot size中,由于我们要反映矩形域上的粮食产量,所以我们在类中嵌套定义了一个类,Java中对象需要实例化,故(3)应填入“new Crop_assessment()”。最后,由于我们想通过 Lot_size类中的 get_data和 get_data2函数得到粮食产量,但由于这两个成员数据是私有函数,所以我们必须通过定义的对象调用它,所以(4)应填入“crop.get_ideal_crop()”,(5)应填入“crop.get_actual_crop()”。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试资料 > 职业资格

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1