文档库 最新最全的文档下载
当前位置:文档库 › 2013期中参考答案A

2013期中参考答案A

上海交通大学试卷( A 卷)

(2012 至2013 学年第2学期期中考试)班级号_________________ 学号______________ 姓名

课程名称C++(A)成绩

一、选择填空:(每题1分,共20分)

1.在函数声明中,是不必要的

A、形式参数的类型;

B、形式参数名;

C、函数的返回类型;

D、函数名;

答案:B

2. int a=10, b=11, c=12,x=(a+b)

A、11

B、0

C、7

D、1

答案:A

3. 一个函数分别进行了声明和定义,则形式参数的默认值应该中给出。

A、只在函数定义

B、只在函数声明

C、在函数的定义和声明

D、在函数调用

答案:B

4. 字符串常量“SJTU”的字符串长度和所占字节数分别为________________。

A、4,5

B、4,4

C、5,4

D、5,5

答案:A

5.以下哪个说法是正确的

A、一个函数可以同时有多个返回值,类型必须相同

B、一个函数可以同时有多个返回值,类型可以不同

C、函数返回类型必须和某个参数的类型一致

D、一个函数可以有多条return 语句

答案:D

6.在c++中,0x001101是。

A、二进制数

B、八进制数

C、十六进制数

D、十进制数

答案:C

7. 在以下运算符中,优先级最低的是。

A、!

B、>=

C、*

D、||

8. 一个文件中的全局变量,如果不允许其他的文件引用,则需要在声明时加上 关键词。

A 、auto

B 、register

C 、extern

D 、static 答案: D

9.在循环单链表中,head 指向头结点,设P 为指向结点的指针,则判断P 为尾结点的条件

是 。

A 、P==NULL

B 、p->next==NULL

C 、p->next==head

D 、p==head 答案: C

10.在以下定义的struct Date{int year; int month; int day}; Date d, *p=&d, 对d 中月份的输入 是正确的。

A 、cin>>month;

B 、cin>>d->month;

C 、cin>>p->month;

D 、cin>>p.month;

答案: C

11.结构类型Date 作为函数的参数,如果该参数在函数中只读不写,则以下参数说明中最好的是

A 、const Date &d

B 、const Date d

C 、Date &d

D 、Date d

答案:A

12.char a[20]=”hello! sjtu ”; char *p; 以下 语句组输出结果为sjtu 。

A 、p=a+7; cout<<*p ;

B 、p=a+7; cout<

C 、a=a+7; cout<<*a ;

D 、p=a; cout<<*(p+7);

答案: B

13.double *p=new double(10); 则在VC6.0中sizeof(p)的结果为 。

A 、4

B 、8

C 、80

D 、40 答案: A

14.int a[4][5]; 则和a[3][2]的值等价的表达式为 。

A 、*(*a[3]+2)

B 、*(a+17)

C 、*((a+3)+2)

D 、*(a[3]+2) 答案: D

15.char *string[10] = {"aaa", "bbb", "ctc", "ddd", "eee", "fff", "ggg", "hhh", "iii","jjj"}; 以下 输出为t 。

A 、cout<

B 、cout<

C 、cout<<*(&string[2]+2);

D 、cout<

我承诺,我将严格遵守考试纪律。

承诺人:

题号 一 二 三 四 得分 批阅人(流水阅 卷教师签名处)

16.char one_char,不正确的操作是_____________。

A、one_char=”\n”;

B、one_char=97;

C、one_char=’\t’;

D、one_char=’*’;

答案:A

17. 表达式(x||y)&&!y在时结果为真

A、x和y均为真

B、x和y均为假

C、x为真,y为假

D、x为假,y为真

答案: C

18. char s, *p=&s, &ch=s; 则等价于s=’#’的语句为。

A、p=’#’

B、*p=’#’

C、*ch=’#’

D、&ch=’#’

答案: B

19. 在下列程序段中有误的是。

A、template

B、return (a>b)? a:b;

C、func(T a, T b)

D、T a, T b;

答案: D

20. 如果以下函数声明并存,则函数调用max(3, 4.5)执行函数

A、int max(int, int);

B、double max(double, double);

C、template T max(T, T);

D、template T max(T, G);

答案: D

二、写出程序段的运行结果:(每题3分,共30分)

1. //枚举类型

#include

using namespace std;

void main()

{

enum color{RED, GREEN=5, BLUE, YELLOW=10};

enum color c1, c2;

c1=RED;

c2=BLUE;

cout<<"GREEN="<

cout<<"c1="<

cout<<"c2="<

}

结果:

GREEN=5

C1=0

2. //递归,整除

#include

using namespace std;

const int P = 100;

int calc(int);

int main()

{

cout << calc(3) << endl;

}

int calc(int n)

{

if (n == 0) return 0;

return calc(n - 1) + P / n / (n + 1); }

结果:

74

3. //赋值和判等

#include

using namespace std;

int main()

{

int x = 3, y = 6;

if ((x = 0) && (y = 4))

y = x;

cout << x << "" << y <

return 0;

}

结果:

0 6

4. //枚举

#include

using namespace std;

int main()

{

int a=012,b=0x12;

int e=(a++)*3,f=(++b)*3;

int g;

int h=(g=10)+1;

cout<

return 0;

}

答案:

138

5. //引用,指针

#include

using namespace std;

void f1(int &x, int &y) {

x = x + y;

y = x - y;

}

void f2(int &x, int &y){

int *ipx = &x;

int *ipy = &y;

while (ipx < ipy)

{

f1(*ipx, *ipy);

ipx++;

ipy--;

}

}

const int maxn = 5;

int main()

{

int a[maxn] = {3,8,1,6};

for (int i = 0; i < maxn/2; i++)

{

f2(a[0], a[i]);

f2(a[i+1], a[maxn-1]);

f2(a[0], a[maxn-1]);

}

for (i = 0; i < maxn; i++)

cout<

return 0;

}

结果:

27 19 10 11 20

6. //switch case

#include

using namespace std;

int main(){

int a=2, b=7, c=5;

switch (int(a>0)) {

case 1:

switch(int(b<0)) {

case 1: cout << "2"; break;

case 2: cout << "3";

default: cout << "1";

}

case 0:

switch(int(c==5)) {

case 0: cout << "5"; break;

case 1: cout << "6";

default: cout << "4";

}

default: cout << "7";

}

cout<

return 0;

}

结果:

1647

7. //if语句

#include

using namespace std;

int main(){

for (int b = 0; b < 10; ++b)

if (b = 2) { cout << 1; break; }

else cout << 0;

cout<

return 0;

}

结果:

8. //全局变量,静态局部变量#include

using namespace std;

int var;

void f1()

{ static int a = 0;

a++;

var++;

cout<

}

void f2(int var)

{

static int a = 10;

a--;

var--;

cout<

}

void main()

{

int var=2;

::var=3;

if (var>1)

{

int var =5;

cout<

}

f1();

f2(var);

f1();

f2(::var);

cout<

cout<<::var<

}

结果:

514

91

25

84

25

#include

using namespace std;

void iochar(int);

void main()

{

iochar(5);

cout<

}

void iochar(int n)

{

char c;

if (n<=1)

{

cin>>c;

cout<

}

else

{

cin>>c;

iochar(n-1);

cout<

}

}

当输入为abcdefg时结果为:edcba

10. //单链表

#include

using namespace std;

struct Node

{ int data;

Node *next;

};

void main()

{

Node *head, *temp, *prior, *p;

int data[9]={1,2,3,4,5,6,7,8,9};

int i=0, sum=0;

head = new Node;

head->next=NULL;

prior = head;

while (i<6)

{

temp = new Node;

//cout<

temp->data = data[i++];

temp->next = NULL;

prior ->next = temp;

prior = temp;

}

p=head->next;

while (p)

{

p=p->next;

if (!p) break;

sum+=p->data;

p=p->next;

}

cout<<"sum="<

}

结果:

sum=12

三、程序填空(每空2分,共20分)

1.

#include

#include //包含伪随机数生成函数

#include //包含取系统时间的函数

#include

using namespace std;

int solveQuadratic(int a, int b, int c, ) {

double delt, tmp;

if (a==0) return 0;

delt = b*b - 4*a*c;

if (delt<0)

if (delt<0.000001) {x1=-b/(2*a); return 1;}

tmp=sqrt(delt);

x1=(-b+tmp)/(2*a);

x2=(-b-tmp)/(2*a);

return 2;

}

void sortRoots( ) //使得两个根x1

{

double t;

if ( )

{

t=*px1;

*px1=*px2;

*px2=t;

}

}

void main()

{

int a, b, c;

int result;

double x1, x2;

cout<<"a,b: ";

cin>>a>>b;

srand(time(0));

c= //取一个0-4之间的随机整数

result = solveQuadratic(a, b, c, x1, x2);

switch (result) {

case 0: cout<<"a is zero! or no root"<

case 1: cout<<"There is only one root, it is: "<

case 2: sortRoots( );

cout<<"There are two different roots, they are: "

<

break;

}

}

答案:

1. double &x1, double &x2

2. return 0;

3. double *px1, double *px2

4. *px1<*px2

5. rand()*4/RAND_MAX+1;

6. &x1, &x2

2.//申请一个动态二维整型数组,并赋值.

#include

using namespace std;

{

int **a;

int n,m,i,j;

cout<<"Input n, m: ";

cin>>n>>m;

a =

for (i=0; i

a[i]=

for (i=0; i

for (j=0; j

{

a[i][j]=i+j;

cout<

}

cout<

for (i=0; i

}

答案:

1.new int*[n];

2. new int[m];

3. delete []a[i];

4. delete []a;

四.编程题(共30分)

1、(5分)写一个函数int ask(int a[],int l,int r)返回数组a从第l个元素到第r个元素中的

最大值,不允许使用for、while、do…while等循环体。

答案:

int ask(int a[],int l,int r)

{ int tmp;

if ((l<0)||(r<0)||(l>r)) return -999;

if (l==r) return a[l];

tmp=ask(a, l+1, r);

if (a[l]>=tmp)

return a[l];

else

return tmp;

}

递归调用2分

获取最大值方法 1 分

每个分支的返回 1分

2、(10分)已知如下程序,maxStudent函数找到学生中年龄最大的,并在main函数中输出该学生信息。

#include

using namespace std;

struct student

{ char name[20];

int age;

};

//maxStudent函数定义

const int N=3;

void main()

{

student s[N];

int i;

for (i=0; i

{ cout<<"name, age: ";

cin>>s[i].name>>s[i].age;

}

maxStudent(s, N);

cout<<"The oldest student is: "<

}

答案:

void maxStudent(student *p, int n)

{

student ss;

for (int i=0; i

if (p[i].age>p[i+1].age)

{ strcpy(https://www.wendangku.net/doc/f113109157.html,, p[i].name);

ss.age = p[i].age;

strcpy(p[i].name,p[i+1].name);

p[i].age=p[i+1].age;

strcpy(p[i+1].name,https://www.wendangku.net/doc/f113109157.html,);

p[i+1].age=ss.age;

}

}

评分标准:函数头的正确定义2分

逻辑正确5分

学生信息的交换 2分

3、(15分)从键盘读入一个字符串(不多于80个字符)。统计其中26个英文字母各自的个数(大小写不计)。同时,将A和a转换成B,B和b转换成C,……Y和y换成Z,Z和z换成A,输出该字符串以及各字母的出现次数,出现次数为0的略去不输出。

参考答案:

#include

#include

using namespace std;

int main()

{

char ch0[80];

char ch1[80];

char ch;

int count[26]={0};

int i,j;

cout<<"Input a sentence: ";

cin>>ch0;

i=j=0;

while (ch0[i]!='\0')

{

ch=toupper(ch0[i]);

if ((ch>='A') && (ch<'Z'))

{ count[ch-'A']++;

ch1[j++]=ch+1;

i++;

continue;

}

if (ch=='Z')

{ count[25]++;

ch1[j++]='A';

i++;

continue;

}

ch1[j++]=ch0[i++]; //非字母不变

}

ch1[j]='\0';

cout<<"The string is: "<

for (i=0; i<26; i++)

if (count[i]!=0)

cout<

return 0;

}

评分标准:字符串变量声明1分

计数器初始化1分

循环条件正确 1 分

转换正确5分

计数正确3分

末尾’\0’处理1分

字符串输出正确1分

统计数据输出正确2分

相关文档
相关文档 最新文档