下列程序的输出结果是( )。public class Test {public static void main (String[] args) {Strin

题目

下列程序的输出结果是( )。 public class Test { public static void main (String[] args) { String s="hello"; s.replace ('r','m'); System.out.println(s); } }

A.hello

B.HELLO

C.hemmo

D.HEMMO

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

第1题:

下列程序的运行结果是【 】。 include class test { private: int num; public: tes

下列程序的运行结果是【 】。

include <iostream. h>

class test

{

private:

int num;

public:

test()

int TEST() {return num+100;}

~test()

};

test::test(){num=0;}

test::~test(){cout<<"Destructor is active"<<endl;}

void main()

{

test x[3]

cout<<x[1]. TEST()<<endl;

}


正确答案:100
100 解析:本题比较简单,考查考生基本的类的定义,构造函数以及对象数组的概念。

第2题:

下列程序的运行结果是______。 include class test { private: int hum; public: tes

下列程序的运行结果是______。

include<iostream.h>

class test

{

private:

int hum;

public:

test( );

int TEST( ){return num+100;)

~test( );

};

test::test( ){num=0;)

test::~test( ){cout<<"Destructor is active"<<endl;)

void main( )

{

test x[3];

cout<<x[1].TEST( )<<endl;

}


正确答案:100 Destructor is active Destructor is active Destructor is active
100 Destructor is active Destructor is active Destructor is active 解析:本题比较简单,考查考生基本的类的定义,构造函数以及对象数组的概念。

第3题:

有如下程序:includeusing namespace std;class test{private: int a;public: test(

有如下程序:#include<iostream>using namespace std;class test{private: int a;public: test(){cout<<"constructor"<<endl;} test(int a){cout<<a<<endl;} test(const test&_test) { a=_test.a; cout<<"copy constructor"<<en+dl; } ~test(){cout<<"destructor"<<endl;}};int main(){ test A(3); rerun 0;}运行时输出的结果是

A.3

B.constructor destructor

C.copy constructor destructor

D.3 destructor


正确答案:D
解析:本题考查的知识点是:构造函数和析构函数。一个类可以有多个构造函数,但只能有一个析构函数。每一个对象在被创建的时候,都会隐含调用众多构造函数中的一个,而在被销毁的时候,又会隐含调用唯一的那个析构函数。因此,解此类题目只要找准创建时调用的是哪个构造函数,和对象何时被销毁即可。本题只有主函数中创建了一个对象A,并使用了构造参数3,因此会隐含调用test(int a)这个构造函数,输出一个3。接下来主函数结束,对象A被销毁,所以又隐含调用~test()析构函数,输出一个destructor。故本题应该选择D。

第4题:

下列程序的输出结果是______。 include using namespace std; class Test( public: Te

下列程序的输出结果是______。 #include<iostream> using namespace std; class Test( public: Test() {cnt++;} ~Test() {cnt--;} static int Count(){return cnt;} private: static int cnt; }; int Test::cnt=0; int main() { cout<<Test::Count()<<""; Test t1,t2; Test*pT3=new Test; Test*pT4=new Test; cout<<Test::Count()<<""; delete pT4; delete pT3; cout<<Test::Count()<<end1; return 0; }

A.024

B.042

C.420

D.240


正确答案:B
解析:此题考查的是类的构造函数与析构函数的调用。语句 coutTcst::Count()"";;输出“0”,因为static型变量cnt的默认值是0;“T Test t1,t2;Test*pT3=new Test;Test*pT4=new Test;”调用4次类的构造函数,使得cnt的值增加到4,并输出4;然后delete pT4;delete pT3;调用两次析构函数,cnt的值变为2,并输出2。

第5题:

下列程序的运行结果是______。include class Base { public: void f(int x){cout<<“B

下列程序的运行结果是______。

include<iostream.h>

class Base

{

public:

void f(int x){cout<<“Base:”<<x<<endl;}

);

class Derived:public Base

{

public:

void f(char*str){cout<<“Derived:”<<str<<endl;}

};

void main(void)

{

Base*pd=ne


正确答案:Base:97。
Base:97。 解析: 本题主要考查两个知识点,一是基类指针可以指向派生类对象,并可以访问派生类的所有成员。二是在函数重载中进行隐式类型转换。如pd->f(‘a’);系统到底调用哪个重载函数呢?实参既不是派生类中的形参,也不是基类中f函数的形参类型。此时系统根据就近原则和从高优先级到低优先级的规则尝试隐式转换。单字符更接近整数,故调用的是基类的f函数。

第6题:

下列程序的运行结果是【 】。 include class SomeClass { public: SomeClass(int va

下列程序的运行结果是【 】。

include <iostream. h>

class SomeClass

{

public:

SomeClass(int value) { some_value=value;};

void show_data(void) { cout<<data<<"<<~some_value<<endl; };

static void set_data(int value) {data=value; }

private:

static int data;

int some_value

};

int SomeClass::data

void main(void)

{

SomeClass my_class(1001),your_class(2002);

your_class. set_data(4004);

my_elass. show_data()

}


正确答案:4004 1001
4004 1001 解析:本题考查静态成员变量在不同对象间的共享现象。无论哪个对象修改了其静态变量的值,其他对象再访问该变量时已经发生了变化。

第7题:

如下程序的输出结果是includeusing namespace std;class Test{public:Test( ){n+=2;}

如下程序的输出结果是 #include<iostream> using namespace std; class Test{ public: Test( ){n+=2;} ~Test( ){n-=3;} static int getNum( ){return n;} private: static int n; }; int Test::n=1; int main( ){ Test*P=new Test: delete P; cout<<"n="<<Test::getNum( )<<endl; return 0; }

A.n=0

B.n=1

C.n=2

D. n=3


正确答案:A
解析:静态数据成员的初始值n=1,执行Test*p=new Test;,调用构造函数后,n= 3,deletep;调用析构函数,n-=3,所以最终n=0。

第8题:

使用VC6打开考生文件夹下的工程test17_1,此工程包含一个源程序文件test17_1.唧,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:

Vehicle message

Car message

Vehicle message

源程序文件test17_1.cpp 清单如下:

include <iostream.h>

class vehicle

{

int wheels;

float weight;

public:

/***************** found *****************/

void message(void){ cout<<"Vehicle message\n";}

};

class car:public vehicle

{

int passenger_load;

public:

/***************** found *****************/

void message (void){cout>>"Car message\n";}

};

class truck:public vehicle

{

int passenger_load;

float payload;

public:

int passengers(void){return passenger_load;}

};

void main()

{

vehicle * unicycle;

unicycle=new vehicle;

/***************** found *****************/

unicycle.message()

delete unicycle;

unicycle=new car;

unicycle->message();

delete unicycle;

unicycle=new truck;

unicycle->message();

delete unicycle;

}


正确答案:(1)错误:void message(void){cout"Vehicle message\n";} 正确:virtual void message(void){cout"Vehicle message\n";} (2)错误:void message(void){cout>>"Carmessage\n”;} 正确:void message(void){eout"Carmessage\n";} (3)错误:unicycle.message(); 正确:unicycle->message();
(1)错误:void message(void){cout"Vehicle message\n";} 正确:virtual void message(void){cout"Vehicle message\n";} (2)错误:void message(void){cout>>"Carmessage\n”;} 正确:void message(void){eout"Carmessage\n";} (3)错误:unicycle.message(); 正确:unicycle->message(); 解析:(1)主要考查考生对于虚函数使用规则的掌握,要实现对函数message的动态联编应该使用虚函数,否则产生的是静态联编,调用的函数输出结果就与要求不符;
(2)主要考查考生对于流输出操作的掌握,该处错误的使用了流输入操作;
(3)主要考查考生是否能分清对象和对象指针,对象指针使用“->”调用成员函数,而对象使用“.”调用成员函数。

第9题:

有如下程序:include using namespace std;class Test{public:Test(){n+=2; }~Test(){

有如下程序: #include <iostream> using namespace std; class Test { public: Test() {n+=2; } ~Test() {n-=3; } static int getNum() {return n; } private: static int n; }; int Test::n=1; int main() { Test* p=new Test; delete p; cout<<"n="<<Test::getNum()<<endl; return 0; } 执行该程序的输出结果是( )。

A.n=0

B.n=1

C.n=2

D.n=3


正确答案:A
解析:此题考查的是静态数据成员和静态成员函数。静态数据成员是类中所有对象共享的成员,而不是某个对象的成员。题目中的静态数据成员n的运算具有叠加性,执行“n+=2”和“n-=3”后n的值为0。

第10题:

阅读以下说明和Java程序,填写程序中的空(1)~(6),将解答写入答题纸的对应栏内。
【说明】
以下Java代码实现一个简单绘图工具,绘制不同形状以及不同颜色的图形。部分接口、类及其关系如图5-1所示。




【Java代码】
interface?DrawCircle?{? //绘制圆形 public(1) ;}class?RedCircle?implements?DrawCircle?{? ?//绘制红色圆形???????public?void?drawCircle(int?radius,intx,?int?y)??{????????????System.out.println("Drawing?Circle[red,radius:"?+?radius?+",x:"?+?x?+?",y:"?+y+?"]");???????}}class?GreenCircle?implements?DrawCircle?{????//绘制绿色圆形??????public?void?drawCircle(int?radius,?int?x,int?y)?{???????????System.out.println("Drawing?Circle[green,radius:"?+radius+",x:?"?+x+?",y:?"?+y+?"]");??????}}abstract?class?Shape?{????//形状? protected? ? (2)???;? ? public?Shape(DrawCircle?drawCircle)?{? ?this.drawCircle=?drawCircle;? ? ? public?abstract?void?draw();}class?Circle?extends?Shape?{? //圆形? ?private?int?x,y,radius;? public?Circle(int?x,int?y,intradius,DrawCircle?drawCircle)?{? ?(3)???;? this.x?=?x;? ? ? this.y?=?y;? ?this.radius?=radius;? }? ? ?public?void?draw()?{? ? drawCircle.? ?(4)? ?;? ? ? }}public?class?DrawCircleMain?{? public?static?void?main(String[]?args)?{? Shape?redCircle=new?Circle(?100,100,10,? (5) );//绘制红色圆形? Shape?greenCircle=new?Circle(200,200,10,(6) );//绘制绿色圆形? ?redCircle.draw(); greenCircle.draw();? ?}}


答案:
解析:
(1)void drawCircle (int radius,int x,int y)
(2)DrawCircle drawCircle
(3)super.drawcircle=drawcircle
(4)drawCircle(radius,x,y)
(5)new RedCircle()
(6)new GreenCircle()【解析】
第一空是填接口里面的方法,在接口的实现里面找,可以发现应该填void drawCircle (int radius,int x,int y)。
第二空可以根据后面this drawCircle=drawCircle判断,这里应该有一个drawCircle属性,因此应该填)DrawCircle drawCircle。
第三空这里用super,用super. drawcircle来引用父类的成员。
第四空调用drawCircle(radius,x,y)方法。
第五、六空分别创建一个红色圆形对象和一个绿色圆形对象作为Circle里面的实参。