2014 Computer Test Questions, 2014 Computer Level Two C++ Computer Test Questions and Answers 1

1. Program error correction

Use VC++6.0 to open the source program file 1.cpp in the candidate's folder, but there is a problem with the program. Please correct the error in the main() function to make the output of the program correct.

Program output:

8

8

Note: The wrong statement is under /********error********/. You can modify this statement, other statements cannot be modified.

Exam procedure:

#include(iostream.h>

classCO

{

public:

/********error********/

staticintn = 0 :

CO()

{

n++:

}

~CO()

{

n--:

}

}:

/********error********/

intCO::n;

voidmain ()

{

COobj;

COobj2[7];

CO*C=NULL:

c=&obj:

/********error********/

eout<

cost<

}

2. Simple application questions

Use VC++6.0 to open the source program file 2.epp in the candidates folder. Read the following function description and code, and fill in the blank code. The function fun(doubleA[5][5]) returns the sum of all numbers in a two-dimensional array that are greater than the mean of this row.

Note: Part of the code has been implemented, please do not change the main function code.

Exam procedure:

#include

doublefun(doubleA[5][5])

{

}

voidmain ()

{

doubleA[S][5]=

{4431.3,45.56,5697.65,768.678,122.45),

{444.4,34.545,5667.656,2548.678,1562.45},

{537.3,655.5456,5677.65,7168.678,1242,45},

{134.3,454.5486,5637.65,7687.678,1322.45),

{189.3,445.5786,5677.65,7528.678,142.45}

};

cost<

return;

}

3. Comprehensive application questions

Use VC++6.0 to open the source program file 3.cpp in the candidate's folder, which defines classes A, A1 and A2, where class A1 is publicly derived from class A, and class A2 is publicly derived from class A1.

(1)定义类A的构造函数,该构造函数有一个整型的参数x,在构造函数中请将x赋值给数据成员a。请在注释“//********1********”之后添加适当的语句。

(2)定义类A1的构造函数,该构造函数有两个整型的参数x和y,在构造函数中请将x赋值给数据成员b,将y作为基类A构造函数的参数值传人。请在注释“//********2********”之后添加适当的语句。

(3)定义类A2的构造函数,该构造函数有3个整型的参数x,y和z,在构造函数中请将x赋值给数据成员C,将y和z分别赋值给基类A1构造函数的参数x和y。请在注释“//********3********”之后添加适当的语句。

(4)完成类A2的成员函数show的定义,该函数调用基类成员函数,输出基类数据成员a和b及类A2自身的数据成员C的值,上述3个值在输出时以空格隔开。请在注释“//********4********”之后添加适当的语句。

程序输出结果如下:

7

3

738

注意:除在指定的位置添加语句之外,请不要改动程序的其他部分。

试题程序:

#include

classA

{

inta;

public:

//********1********

intgeta(){returna;}

};

classA1:publicA

{

intb;

public:

//********2********

intgetb(){returnb;}

};

classA2:publicA1

{

intC;

public:

//****,****3********

voidshow()

{

//********4********

}

};

voidmain()

{

A2a(8.3.7):

cout<

cout<

a.show();

}

上机考试试题答案与解析

一、程序改错题

(1)“staticintn=O”应改为“staticintn;”。

(2)“intCO::n;”应改为“intCO::n=0;”。

(3)“cout<

【解析】本题要在屏幕上输出两个字符,首先要静态变量初始化,第1处应改为“staticintn;”,第2处要给n赋值,第3处输出时引用c的对象,应改为“cout

二、简单应用题

doublefun(doubleA[S][5])

{

doublesum2=0.0;

for(inti=0;i<5;i++)

{

doublesuml=0.0;

intj=0;

for(j=0;j<5;j++)

{

suml+=A[i][j];

}

sum1/=5;

for(j=0;j<5;j++)

{

if(A[i][j]>suml)

}

}

returnsum2;

}

【解析】本题要实现的功能是返回二维数组中大于本行平均数的所有数之和,数组的下标是从0开始的,所以数组中有5行5列的数,先求出每行的平均值,然后查找本行中大于平均值的数,然后求和,最后返回和值。

三、综合应用题

(1)“A(intx){a=x;}”。

(2)“A1(intx,inty):A(y){b=x;}”。

(3)“A2(intx,inty,intz):A1(y,z){c=x;}”。

(4)“cout<

【解析】本题第1处要求定义类A的构造函数,该构造函数有一个整型的参数x,在构造函数中请将x赋值给数据成员a,构造函数没有返回值类型,所以应填“A(intx){a=x;}”。第2处第3处根据继承中对象的初始化语法规则可知。第4处要求调用基类成员函数,由于都是公有继承,ge-ta()、getb()都是自身类的公有函数,所以根据继承规则,在对象A2中,依然是公有的可以通过对象A2直接调用。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324083716&siteId=291194637