![](https://lstatic.shangxueba.com/sxbzda/h5/images/m_q_title.png)
计算机执行下列C++语句:int x = 1; { cout << x; int x = 2; { int x = 3; cout << x; } cout << x; } 执行结束后,显示器将显示()。
A.123
B.321
C.1, 3, 2
D.132
![](https://lstatic.shangxueba.com/sxbzda/h5/images/tips_org.png)
A.123
B.321
C.1, 3, 2
D.132
第1题
有如下程序:
#jnClude<iostream>
using namespaCe std; Class Part{
publiC:
Part(int x=0):val(X){Cout<<val;} ~Part{Cout<<val;}
private:
int val; t };
Class Whole{ publiC:
Whole(int x,int Y,int z=0):p2(x),pl(y),val(z){Cout<<val;} ~Whole{eout<<val;}
private:
Part pl,p2; int val;
};
int main {
Whole obj(1,2,3); return 0;
}
执行这个程序的输出结果是( )。 A.123321
B.213312
C.213
D.123123
【我提交的答案】: C |
【参考答案与解析】: 正确答案:B |
本题考查带参数的构造函数。本题中定义了一个对象obj,那么执行其构造函数,初始化p1,p2的顺序是由声明这两个成员的顺序决定的,而不是由参数化列表顺序决定,所以本题中,会先执行p1,输出2,然后执行p2输出1,然后输出3,最后一次输出312。所以本题答案为8。
哪来最后一次输出????
第2题
有如下程序:
#inClude<iostream> using namespaCe std; Class Base{
publiC:
Base(int x=0){Cout<<x;} };
Class Derived:publiC Base{ publiC:
Derived(int X=0){Cout<<x;} private:
Base val; };
int main {
Derived d(1); return 0;
}
执行这个程序的输出结果是( )。 A.0
B.1
C.01
D.001
【我提交的答案】: B |
【参考答案与解析】: 正确答案:D |
本题考查派生类的构造函数和析构函数,在定义一个派生类的对象时,先调用基类的构造函数,然后再执行派生类的构造函数,对象释放时,先执行派生类的析构函数,再执行基类的析构函数。所以本题中定义了一个对象d,先执行基类的构造函数输出0,因为派生类中的私有数据成员为Base,所以还会执行一次基类的构造函数输出0,最后执行派生类的构造函数输出1,所以本题答案为D。
能把题弄全一点吗?表示完全看不懂哪儿是哪儿啊
第3题
下面程序的输出结果是( )。
#include<iostream>
Usingnamespacestd;
ClassBase{
public:
Base(intx=O){cout<<x;
}
};
ClassDerived:publicBase{
public:Derved(intx=0{cout<<x;
}
private;
Baseval;
};
int main(){
Derivedd(1);
retrun0;
}
A.0
B.1
C.01
D.001
【我提交的答案】: D |
【参考答案与解析】: 正确答案:C |
C。【解析】++是右结合的,所以先赋值为3,最后输出3+1;常成员函数只有常对象才能调用,所以输出2。
第4题
有以下两个程序,分析它们的执行结果有什么不同。
程序1:
#include
class Point
{
int x,y;
public:
Point(){x=y=0;}
Point(int i,int j){x=i;y=j;}
Point operator+(Point);
void disp() ( cout<<”(”<
}
Point Point::operator+(Point P)
{
this->x+=P.x; this->y+=p.y;
return *this;
}
void main()
{
Point pl(2,3),p2(3,4),p3;
cout<<”p1:”;p1.disp();
cout<<”p2:”;p2.disp();
p3=pl+p2;
cout<<”执行p3=p1+p2后”<
cout<<”p1:”,p1.disp();
cout<<”p2:”;p2.disp();
cout<<”p3:”;p3.disp();
}
程序2:
#include
class Point{
int x,Y;
public:
Point(){x=y=O;}
Point(int i,int j){x=i,y=j;}
Point operator+(Point);
void disp f){cout<< ”(”<
}
Point Point::operator+(Point P)
{
Point s;
s.x=x+p.x; s.y=y+p.y;
return s;
}
void main()
{
Point pl(2,3),p2(3,4),p3;
cout<<”p1:”;p1.disp();
cout<<”p2:”;p2.disp();
p3=pl+p2;
cout<<”执行p3=p1+p2后”<
cout<<”p1:”;p1.disp();
cout<<”p2:”;p2.disp();
cout<<”p3:”;p3.disp();
}
第5题
使用VC++6.0打开考生文件夹下的源程序文件1.cpp,该程序运行时有错,请改正其中的错误,使程序正常运行,即使程序在屏幕输出以下类似内容:
0012FF6810012FF3C5
0012FF6C20012FF444
0012FF7030012FF4C3
0023FF7440012FF542
0012FF7850012FF5C1
006AFDCC~006AFDEF数据可能因为机器的不同,程序输出的内容会有差异。只要相差为4的连续地址即可。
注意:不要改动main函数,不能增加或删除行,也不能更改程序的结构,错误的语句在//******error******的下面。
试题程序:
#include
usingnamespacestd;
intmain()
{
int*P,a[5]={1,2,3,4,5};
double*q,b[5]={5.0,4.0,3.0,2.0,1.0);
intx;
P=a;
q=b;
//********error********
for(x=0;x<=5;X++)
//********error********
cout<;< p="">
(q+x)<;< p="">
return0;
}
第6题
下列程序的输出结果是( )。
public class Test{
public static void main(String[]args){
int[]array=(2,4,6,8,lO);
int size=6;
int result =-1:
try{
for(int i=0;i(size 8L&result= = -1;)
if(array[i]= =20)result=i:
}
catch(ArithmeticException e){
System.out.println("Catch- - -l");
}
catch(ArraylndexOutOfBoundsException e){
System.out.println("Catch- - -2");
}
catch(Exception e){
System.out.println("Catch- - -3");)
}
}
A.Catch- - -1
B.Catch- - -2
C.Catch- - -3
D.以上都不对
【我提交的答案】: B |
【参考答案与解析】: 正确答案:B |
B。【解析】由题可知先判断i<sizeresuh= =-1,结果为真,则执行if语句array数组中的任何数都不等于20,并且i从0开始一直到i=5时发生越界,则输出Cateh= =-2,结果为B。
第7题
有以下程序:
#include
void fun(int a,int b,int C.
{ a=456,b=567,c=678;}
void main()
{
int X=10,Y=20,Z=30;
fun(X,Y,z);
aout<
}
输出结果是()。
A.30,20,10
B.10,20,30
C.456,567,678
D.678,567,456
第8题
有以下程序:
#include <stdio.h>
main( )
{ struct node{int n; struct node*next;} *P;
struct node x[3]={{2,x+1),{4,x+2},{6,NULL}};
P= X;
printf("%d,",p->n);
printf("%d\n",p->next->n);
}
程序运行后的输出结果是( )。 A.2,3
B.2,4
C.3,4
D.4, 6
【我提交的答案】: C |
【参考答案与解析】: 正确答案:B |
略。
第9题
有以下程序:
#include <stdio.h>
main( )
{ struct node{int n; struct node*next;} *P;
struct node x[3]={{2,x+1),{4,x+2},{6,NULL}};
P= X;
printf("%d,",p->n);
printf("%d\n",p->next->n);
}
程序运行后的输出结果是( )。 A.2,3
B.2,4
C.3,4
D.4, 6
【我提交的答案】: B |
【参考答案与解析】: 正确答案:B |
略。
第10题
(37)有以下程序
#include <stdio.h>
struct ord
{ int x,y;}dt[2]={1,2,3,4};
main()
{
struct ord *p=dt;
printf("%d,",++(p->x)); printf("%d\n",++(p->y));
}
程序运行后的输出结果是
A)1,2
B)4,1
C)3,4
D)2,3
【我提交的答案】: C |
【参考答案与解析】: 正确答案:D |
【我的疑问】(如下,请求专家帮助解答)
请分析
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!