[计算机类试卷]国家二级C++机试(操作题)模拟试卷46及答案与解析.doc

上传人:inwarn120 文档编号:497565 上传时间:2018-11-28 格式:DOC 页数:6 大小:33.50KB
下载 相关 举报
[计算机类试卷]国家二级C++机试(操作题)模拟试卷46及答案与解析.doc_第1页
第1页 / 共6页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷46及答案与解析.doc_第2页
第2页 / 共6页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷46及答案与解析.doc_第3页
第3页 / 共6页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷46及答案与解析.doc_第4页
第4页 / 共6页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷46及答案与解析.doc_第5页
第5页 / 共6页
点击查看更多>>
资源描述

1、国家二级 C+机试(操作题)模拟试卷 46及答案与解析 一、基本操作题 1 使用 VC6打开考生文件夹下的源程序文件 modi 1 cpp,该程序运行时有错,请改正其中的错误,使得程序正常运行,并使程序输出的结果为: sizeof(strl)=5 sizeof(str2)=10 sizeof(str3)=l 注意:不要改动 main函数,不能增行或删行,也不能更改程序的结构,错误的语句在 *error*的下面。 (1)不能删除 assert()语句; (2)只能修改后面的数字。 提示: assert()函数如果为假,则会产生一个中断异常。 1 #include 2 #include 3 voi

2、d main() 4 5 char*strl=”abc”; 6 *error* 7 assert(sizeof(strl)=3); 8 cout 2 int num(char*str) 3 4 5 6 int main() 7 8 char str1024; 9 cout 2 US ing namespace std ; 3 class CBase 4 5 public: 6 CBase(int i)b=i; ) 7 *1* 8 9 protected: 10 int b ; 11 ; 12 class CDerive: public CBase 13 14 public: 15 *2* 16

3、 17 void display() 18 19 coutdisplay(); 32 33 void main() 34 35 *4* 36 37 func(d1); 38 func(d2); 39 国家二级 C+机试(操作题)模拟试卷 46答案与解析 一、基本操作题 1 【正确答案】 (1)assert(Si zeof(strl)=4); (2)assert(Sizeof(str2)=10); (3)assert(Sizeof(str3)=1); 【试题解析】 本题的功能是利用 assert()函数,则会产生一个中断异常。从已定源程序的 main主函数开始入手,依次执行程序中的语句,输出题目

4、中要求的输出值。本题在调试的时候并不会产生错误,它的错误是逻辑错误。 解题思路 (1)assert()函数如果为假,则会产生一个中断异常。所以要让它正常执行输出结果,那么 assert()函数就要为真,才不会产生中断异常。 “assert(sizeof(strl)=3); ”语句中 “sizeof(strl)=3”为假,这个会使程序产生异常,不能正常执行。凶为 strl的值为 abc但是它还有一个尾符,所以它的长度应该是 4,而不是 3,所以第 1个标识下修改为 “assert(sizeof(str 1)=4); ”。 (2)同 (1), str210说明, str2的空间为 10,第 2个标

5、识下修改为“assert(sizeof(str2)-lO); ”。 (3)同 (1), char str3=23;语句说明 23是字符,占空间为 l,所以修改为“assert(sizeof(str3)=1); ”。 二、简单应用题 2 【正确答案】 1 int number=0; 2 for(int i: 0 ; stri!=0 ; i+) 3 if(stri9 stri9 stri()=0,因此第 1个标识下应添加 “virtual void display()=0; ”。 (2)类 CDerive的构造函数用来完成类成员变量的初始化,派生类没有自己的成员变量,可直接采用基类的构造函数完成初

6、始化,因此第 2个标识下应添加“CDerive(inti): CBase(i)”。 (3)第 3个标识下是完成类 CDerive2成员函数 diaplay()的定义和实现,根据要求的输出结果 “In CDerive2Sdisplay() b=2”可知 display()函数除输出字符串“InCDerive2s display() ”还输出变量 b的值,故第 3个标识下应添加 “void display()cout“In CDerive2s display() “b=“bendl; ”。 (4)在第 4个标识下应定义变量 d1和 d2, funcO函数的参数为 CBase对象指针,因此 d1和 d2均为 CBase对象指针,最终的输出结果 b分别为 1和 2,这两个对象指针分别指向 CDerive对象和 CDerive2对象,且初始化值分别为 1和 2,故 第 4个标识下应添加 “CDerive*d1=new CDerive(1); CDerive2*d2=new CDerive2(2); ”,在 C+中,一个基类指针 (或引用 )可以用于指向它的派生类,而且通过这个指针 f或引用 )调用虚函数,被调用的是该指针 (或引用 )实际指向的对象类的重定义版本,即 CDerive和 CDerive2均有成员函数 display(),在屏幕上打印结果。

展开阅读全文
相关资源
猜你喜欢
  • BS PD IEC TS 62763-2013_5284 Pilot function through a control pilot circuit using PWM (pulse width modulation) and a control pilot wire《通过控制导向线使用PWM (脉冲宽度调制) 的导向功能和控制导向线》.pdf BS PD IEC TS 62763-2013_5284 Pilot function through a control pilot circuit using PWM (pulse width modulation) and a control pilot wire《通过控制导向线使用PWM (脉冲宽度调制) 的导向功能和控制导向线》.pdf
  • BS ISO 8070-2007 Milk and milk products - Determination of calcium sodium potassium and magnesium contents - Atomic absorption spectrometric method《牛奶和奶制品 钙、钠、钾和镁含量的测定 原子吸.pdf BS ISO 8070-2007 Milk and milk products - Determination of calcium sodium potassium and magnesium contents - Atomic absorption spectrometric method《牛奶和奶制品 钙、钠、钾和镁含量的测定 原子吸.pdf
  • BS ISO 8082-1-2009 Self-propelled machinery for forestry - Laboratory tests and performance requirements for roll-over protective structures - General machines《林业用自推进机械 防倾.pdf BS ISO 8082-1-2009 Self-propelled machinery for forestry - Laboratory tests and performance requirements for roll-over protective structures - General machines《林业用自推进机械 防倾.pdf
  • BS ISO 8082-2-2011 Self-propelled machinery for forestry Laboratory tests and performance requirements for roll-over protective structures Machines having a rotating platf.pdf BS ISO 8082-2-2011 Self-propelled machinery for forestry Laboratory tests and performance requirements for roll-over protective structures Machines having a rotating platf.pdf
  • BS ISO 8083-2006 Machinery for forestry - Falling-object protective structures (FOPS) - Laboratory tests and performance requirements《林业机械 落体防护装置(FOPS) 实验室试验和性能要求》.pdf BS ISO 8083-2006 Machinery for forestry - Falling-object protective structures (FOPS) - Laboratory tests and performance requirements《林业机械 落体防护装置(FOPS) 实验室试验和性能要求》.pdf
  • BS ISO 8086-2004 Dairy plant - Hygiene conditions - General guidance on inspection and sampling procedures《乳品厂 卫生条件 检验和取样程序通用指南》.pdf BS ISO 8086-2004 Dairy plant - Hygiene conditions - General guidance on inspection and sampling procedures《乳品厂 卫生条件 检验和取样程序通用指南》.pdf
  • BS ISO 8096-2005 Rubber- or plastics-coated fabrics for water resistant clothing - Specification《雨衣用橡胶或塑料涂覆织物 规范》.pdf BS ISO 8096-2005 Rubber- or plastics-coated fabrics for water resistant clothing - Specification《雨衣用橡胶或塑料涂覆织物 规范》.pdf
  • BS ISO 8097-2001 Aircraft Minimum airworthiness requirements and test conditions for certified air cargo unit load devices《航空器 经认证的航空货运集装单元装置最低适航性要求和试验条件》.pdf BS ISO 8097-2001 Aircraft Minimum airworthiness requirements and test conditions for certified air cargo unit load devices《航空器 经认证的航空货运集装单元装置最低适航性要求和试验条件》.pdf
  • BS ISO 8114-1993 Textile machinery and accessories - Spindles for ring-spinning and doubling machines - List of equivalent terms《纺织机械和附件 环锭纺纱机和并线机用锭子 同义术语表》.pdf BS ISO 8114-1993 Textile machinery and accessories - Spindles for ring-spinning and doubling machines - List of equivalent terms《纺织机械和附件 环锭纺纱机和并线机用锭子 同义术语表》.pdf
  • 相关搜索

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

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