计算机二级

下列代码段的执行结果是( )。 public class Test { public static void main(String args[ ]) { String s1= new String("hello"); String s2= new String("hello"); System.out.println(s1==s2); System.out.println(s1.equal(s2)); } }A.true falseB.true trueC.false trueD.false false

题目

下列代码段的执行结果是( )。 public class Test { public static void main(String args[ ]) { String s1= new String("hello"); String s2= new String("hello"); System.out.println(s1==s2); System.out.println(s1.equal(s2)); } }

A.true false

B.true true

C.false true

D.false false

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

第1题:

下列程序的运行结果是【 】。 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 解析:本题考查静态成员变量在不同对象间的共享现象。无论哪个对象修改了其静态变量的值,其他对象再访问该变量时已经发生了变化。

第2题:

下列程序的运行结果是______。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函数。

第3题:

下列程序的运行结果是【 】。 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 解析:本题比较简单,考查考生基本的类的定义,构造函数以及对象数组的概念。

第4题:

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



【C++代码】
#include?#include?using?namespace?std;class?DrawCircle?{??????//绘制圆形,抽象类? ? ? public: (1);//定义参数为?int?radius,?int?x,?inty? ?virtual~DrawCircle()?{?}};class?RedCircle:public?DrawCircle?{????//绘制红色圆形? ? ? ? public: void?drawCircle(intradius,?int?x,?int?y)?{cout?<?drawCircle?=?drawCircle;? }? ?virtual~shape()?{?}? public:? ?virtual?void?draw()?=?0;};class?Circle:public?Shape?{????//圆形? ? private:? ? ?int?x,y,radius;? ? public:? Circle(int?x,inty,int?radius,DrawCircle?*drawCircle)? (3)? {? this->x?=?x;? ?this->y?=?y;? ? this->radius?=?radius; }? ? ? public:? void?draw(){? drawCircle?-> (4); }};int?main(){Shape?*redCirclenew?Circle(100,100,10,????(5)????);//绘制红色圆形? Shape?*greenCircle=new?Circle(100,100,10, (6)??);//绘制绿色圆形redCircle >draw();? ?greenCircle?->draw();? ?return?0;}


答案:
解析:
(6)(1)void drawCircle (int radius,int x,int y)
(2)DrawCircle*drawCircle
(3)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。
第三空这里填drawcircle,用-> drawcircle来引用父类的成员。
第四空调用drawCircle(radius,x,y)方法。
第五、六空分别创建一个红色圆形对象和一个绿色圆形对象作为Circle里面的实参。

第5题:

下列代码的执行结果是( )。 class NextLetter{ public static void main(String[ ]args){ char c="a"; System.out.println("The next character is"+ + + c +"."); } }

A.a

B.b

C.c

D.a+c


正确答案:B

第6题:

下列程序的运行结果是______。 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 解析:本题比较简单,考查考生基本的类的定义,构造函数以及对象数组的概念。

第7题:

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

第8题:

以下程序的执行结果是______。 inelude class Sample { public: int x; int y; void

以下程序的执行结果是______。

inelude<iostream.h>

class Sample

{

public:

int x;

int y;

void disp( )

{

cout<<"x="<<x<<",y="<<y<<endl;

}

};

void main( )

{

int Sample::*pc;

Sample s;

pc=&Sample::x;

s.*pc=10;

pc=&Sample::y;

s.*pc=20;

s.disp( );

}


正确答案:x=10y=20
x=10,y=20 解析:本题比较特殊,考察域作用符的使用规则。其实际含义是:指针先指向x,然后指向y,并利用该指针分别为x和y赋值。在使用过程中进行了作用域的限定。

第9题:

阅读以下说明和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里面的实参。

第10题:

阅读下列代码,选出该代码段正确的文件名()。 class A{ void method1(){ System.out.println("Method1 in class A"); } } public class B{ void method2(){ System.out.println("Method2 in class B"); } public static void main(String[] args){ System.out.println("main() in class B"); } }

A.java

B.A.class

C.B.java

D.B.class


D解析:while的循环控制条件可以为“true”,run方法没有返回值,所以不能是int型,故此程