计算机二级

有以下程序:include using namespace std;int f1(int x, int y){ return x>y ? x:y;}有以下程序: #include <iostream> using namespace std; int f1(int x, int y) { return x>y ? x:y; } int f2(int x, int y) { return x>y? y:x; } int main ( ) { int a = 4,b = 3,c = 5,d,e,f; d =

题目
有以下程序:include using namespace std;int f1(int x, int y){ return x>y ? x:y;}

有以下程序: #include <iostream> using namespace std; int f1(int x, int y) { return x>y ? x:y; } int f2(int x, int y) { return x>y? y:x; } int main ( ) { int a = 4,b = 3,c = 5,d,e,f; d = f1(a,b) ; d = f1(d,c) ; e = f2 (a,b) ; e = f2(e,c) ; f = a+b+c-d-e; cout<<d<<", "<<e<<", "<<f<<end1; return 0; } 执行后的输出结果是 ( )。

A.3,4,5

B.5,3,4

C.5,4,3

D.3,5,4

如果没有搜索结果,请直接 联系老师 获取答案。
如果没有搜索结果,请直接 联系老师 获取答案。
相似问题和答案

第1题:

若有以下程序段:include using namespace std;int main (){ int a[]={1,4,5}; int *p

若有以下程序段: #include <iostream> using namespace std; int main () { int a[]={1,4,5}; int *p=&a[0],x=6, y,z; for (y=0; y<3; y++) z= ( (* (p+y) <x) ? *(p+y) :x); cout<<z<<end1; return 0; } 程序运行后的输出结果是( )。

A.1

B.4

C.5

D.2


正确答案:C
解析:本题考核指针的运算。程序首先定义了一整型数组a,并赋初值1,4,5。即a[0],a[1]和a[2]的值分别为1,4和5。程序还定义了一个整型指针p,并将其初始化且指向数组元素a[0]。在for循环语句中,赋值语句“z=((*(p+y)x)?*(p+y):x);”可等价为“z=(a[y]x)?a[y]:x”。因为表达式中的“*(p+y)=p[y]=a[y]”。for语句每循环一次,就给变量z重新赋值一次,z的值其实就是数组和x相比较,将数组a中的最大值赋给z。当y=2后,退出循环,输出结果是5。

第2题:

有如下程序: include using namespace std; void f1(int& x, int& y){int z=

有如下程序:

#include<iostream>

using namespace std;

void f1(int& x, int& y){int z=x; x=y; y=z;)

void f2(int x, int y){int z=x; x=y; y=z;}

intmain(){

int x=10, y=26;

f1(x, y);

f2(x, y);

cout<<y<<end1;

return 0;

}

运行时的输出结果是( )。

A) 10

B) 16

C) 26

D) 36

A.

B.

C.

D.


正确答案:A

第3题:

如下程序的输出结果是______。 include using namespace std; int funl(int x){retu

如下程序的输出结果是______。

include<iostream>

using namespace std;

int funl(int x){return++x;}

int fun2(int&x){return++x;}

int main( ){

int x:1,y=2;

Y=funl(fun2(x));

cout<<x<<','<<y;

return 0;

}


正确答案:23
2,3

第4题:

有以下程序:include using namespace std;class myclass{private: int a,b;public: v

有以下程序: #include <iostream> using namespace std; class myclass { private: int a, b; public: void init( int i, int j ) { a = i; b = j; } friend int sum( myclass x ); }; int sum( myclass x ) { return x.a + x.b; } int main () { myclass y; y.init( 15, 20 ); cout<<sum( y )<<end1; return 0; } 执行后的输出结果是( )。

A.15

B.20

C.5

D.35


正确答案:A
解析:本题主要考察C++中友元函数的使用。程序中,首先定义一个myclass类,其中包含private类型数据成员inta和intb;并含有成员函数voidinit(),其功能为初始化数据成员a和b,使其值分别为a=i和b=i;同时声明了一个友元函数intsum(),其函数定义在类声明体外,功能为返回myclass类的对象中的两个数据成员之和。程序主函数中,首先创建一个myclass类的对象y,并调用y的初始化函数对其变量进行赋值操作,执行该语句后,应该有:y.a=15和y.b=20。程序最后调用函数sum()计算对象y中数据成员的和,由于sum()为类myclass的友元函数,因此其可以自由访问对象y中的数据成员,显然其返回值为:15+20=35。

第5题:

有如下程序:include include using namespace std;class CSum{int x,y

有如下程序: #include <iostream.h> #include <iomanip.h> using namespace std; class CSum { int x,y; public: CSum (int x0,int y0):x(x0),y(y0){} friend ostream & operator<<(ostream& os,const CSum& xa) { os<<setw(5)<<xa.x+xa.y; return os; } }; int main () { CSum y(3,5); cout<<setfill ('*')<<8; cout<<y; return 0; } 执行上面程序的输出是

A.88

B.****88

C.****8****8

D.8****8


正确答案:D
解析:本题考核知识点包括:类与对象、运算重载、I/O的格式化输出。语句 "coutsetfill('*')8;"输出8。语句"couty;"将调用运算符重载输出****8,因为前面语句中设置的填充符号现在还有效,而数据"8"的宽度不够5,所以剩下4个字符的宽度用填充符号"*"填充。

第6题:

以下程序执行后的输出结果是( )。include usingnamespacestd;void try(int,int,int,in

以下程序执行后的输出结果是( )。 #include <iostream> using namespace std; void try(int,int,int,int); int main ( ) { int x,y,z,r; x=1; y=2; try(x,y,z,r); cout<<r<<end1; return 0; } void try(int x,int y, int z,int r) { z = x+y; x = X*X; y = y*y; r = z+x+y; }

A.18

B.9

C.10

D.不确定


正确答案:D
解析:本题常见的错误解答是:把x=1,y=2代入到函数try中,逐步计算出r=8。最后得到r的输出值是8。下面是正确解答。根据程序逐步分析:①程序中定义了一个名为try的void型函数,即函数try()没有任何返回值。②而try()函数在主函数中是以一条独立语句的方式被调用的,且主函数最后输出变量r的值。③但在主函数中,并没有对变量r赋值。④在C++语言中,数据只能从实参单向传递给形参,称为按值传递。也就是说,当简单变量作为实参时,用户不能在函数中改变对应实参的值。所以,虽然在函数try()中,r的值为8,但它并不能传递给实参,当然最终的输出肯定是不确定的随机数了。

第7题:

若有以下程序:include using namespace std;void sub(int x,int y, int *z){ *z = y+

若有以下程序: #include <iostream> using namespace std; void sub(int x,int y, int *z) { *z = y+x; } int main() { int a,b, c; sub (8,4,&a) ; sub (6, a, &b) ; sub(a,b,&c) ; cout<<a<<", "<<b<<", "<<c<<end1; return 0; } 程序运行后的输出结果是( )。

A.12,18,30

B.-12,6,8

C.6,8,10

D.12,-18,16


正确答案:A
解析:本题考核对指针作为函数的参数的理解程度。分析程序:①函数sub()为void型。函数的形参中,z是一个血型的指针变量,因此它只能从实参接收一个血型变量的地址。②在函数sub()体中,语句:“*z=y+x;”的功能是把形参y与x的和值放入形参z所指的存储单元中。③在主函数中,3次调用sub()函数。第一次调用时,把8和4分别传递给形参x和y,把主函数中变量a的地址传递给形参z,这样形参就指向了主函数中的变量a,在sub函数中执行语句“*z=y+x;”后,把12放入z所指的存储单元中,即变量a被赋值12。以此类推,最后b被赋值18,c被赋值30。所以最后输出是12,18,30。

第8题:

若有以下程序: include using namespace std; int f(int x, int y) {return(y-x)*x;

若有以下程序:

include <iostream>

using namespace std;

int f(int x, int y)

{

return (y-x)*x;

}

int main()

{

int a=3,b=4,c=5,d;

d=f(f(a,b) ,f(a,c) );

cout<<d<<<end1;

return 0;

}

执行后的输出结果是【 】。


正确答案:9
9 解析:本题考核函数的嵌套调用。在主函数中执行语句“d=f(f(a,b) ,f(a,c));”调用了3次f()函数:调用f(a,b) 得到的值为3,调用f(a,c) 得到的值为6,调用f(3,6)得到的值为9。

第9题:

下面程序输出的结果是( )。 include using namespace std; void swap(int

下面程序输出的结果是( )。 #include <iostream> using namespace std; void swap(int &a,int &b){ int temp; temp=a; a=b; b=temp; } void main(){ int x=2; int y=3; swap(x,y); cout<<x<<y; }

A.23

B.32

C.ab

D.ba


正确答案:B
解析:函数的参数是引用,故能实现引用调用。

第10题:

有以下程序: include using namespace std; voidt(intx,inty, intcp,intdp) {cp=x*x+y

有以下程序:

include <iostream>

using namespace std;

void t(int x,int y, int cp,int dp)

{

cp=x*x+y+y;

dp=x*x-y*y;

}

int main()

{

int a=4,b=3,c=5,d=6;

t(a,b,c,D) ;

cout<<c<<","<<d<<end1;

return 0;


正确答案:56
5,6 解析:在主函数中定义了a、b、c、d等4个整型变量,并给他们赋初值4、 3、5、6。然后调用函数t(),把a、b、c和d的值依次传递给相应的形参x、y、cp、卸,形参和实参之间不再存在联系。进入函数t()后,执行其中的语句序列。函数t()调用返回后,输出的c和d的值依旧是5和6。

更多相关问题