以下程序执行后的输出结果是include using namespace std;void try(int,int,int,int) 以下程序执行后的输出结果是 #include <iostream> using namespace std; void try(int,int,int,int); int main () { int x,y,z,r; x =1 ; y = 2; tr

题目
以下程序执行后的输出结果是include using namespace std;void try(int,int,int,int)

以下程序执行后的输出结果是 #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<<endl; 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.不确定

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

第1题:

若有以下程序: 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。

第2题:

以下程序执行后的输出结果是( )。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,但它并不能传递给实参,当然最终的输出肯定是不确定的随机数了。

第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; 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

第5题:

以下程序的输出结果是【】。 include using namespace std; void fun() {static int a=0

以下程序的输出结果是【 】。

include <iostream>

using namespace std;

void fun()

{

static int a=0;

a+=2;

cout<<a;

}

int main()

{

int CC;

for(CC=1;cc<4;CC++)

fun();

cout<<end1;

return 0;

}


正确答案:246
246 解析:本题考核函数调用和静态变量。在主函数中通过一个for循环调用了3次fun()函数。第1次调用fun()函数时,a的初始值为0,执行语句“a+=2;”后, a的值为2,输出2;第2次调用时,a的初始值为2,执行语句“a+=2;”后,a的值为4,最后输出4:第3次调用时,a的初始值为4,执行语句“a+=2:”后,a的值为6,最后输出6。

第6题:

若有以下程序段: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。

第7题:

以下程序的输出结果为【】。 include using namespace std; void initialize(int printNo

以下程序的输出结果为【 】。

include<iostream>

using namespace std;

void initialize(int printNo,int state=0);

void initialize(int printNo=l,int state);

int main()

{

initialize();

return 0;

}

void initialize(int printNo,int state)

{

cout<<printNo<<","<<state<<end1;

}


正确答案:10
1,0 解析:本题考核带有默认值的函数,本题中函数initialize()进行了两次函数原型的说明,使本来不带默认值的形参带上默认值。由于主函数中调用initialize()时没有给定实参,所以函数自动调用其参数默认值,输出1和0。

第8题:

程序的输出结果是【 】。 include using namespace std; class A{ int x; public: A(int

程序的输出结果是【 】。

include <iostream>

using namespace std;

class A{

int x;

public:

A(int x=1):x(x){cout<<x;}

};

void main(){

A a,b(2),c(3);

}


正确答案:123
123 解析:a对象使用和默认的构造函数,b对象使用2来初始化对象c对象使用3来初始化对象,输出相应的值后,结果变为123。

第9题:

下列程序的输出结果是______。 include using namespace std; void fun(int &rf) {

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

include<iostream>

using namespace std;

void fun(int &rf)

{

rf*=2;

}

int main()

{

int num=500;

fun(num);

cout<<num<<endl;

return 0;

}


正确答案:1000
1000 解析:本题考核引用作为函数参数的使用。引用作为形参时,它实际上就是实参,函数对形参的访问和修改就是对实参的访问和修改,题中函数fun对形参的操作是自增2倍,所以经过函数调用后,实参的值自增2倍,即输出1000。

第10题:

若有以下程序: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。

更多相关问题