计算机二级

下面程序段的输出结果是 class Test{ public static void main(String args[]){ MyThread t=new MyThread(); t.displayOutput("t has been created)); t.start(); } } class MyThread extends Thread{ public void displayOutput(String s){ System.out.println(s); } public void run()

题目

下面程序段的输出结果是 class Test{ public static void main(String args[]){ MyThread t=new MyThread(); t.displayOutput("t has been created)); t.start(); } } class MyThread extends Thread{ public void displayOutput(String s){ System.out.println(s); } public void run(){ displayOutput(t is running."); } }

A.t has been created.

B.t has been created. t is running.

C.t is running.

D.编译出错

参考答案和解析
正确答案:A
解析:本题考查线程的创建和调用。创建一个新的线程对象后,通过使用 start()方法就可以启动该线程,线程也就处于可运行状态Runnable。Start()方法产生了线程运行需要的系统资源,并调用线程体,也就是run()方法,使得线程可以进入运行状态。程序运行时首先创建一个新的线程对象t,并调用displayOutput(Strings)方法输出t has been created。t.start()方法调用run()方法,输出t is running,所以正确答案为选项A。
如果没有搜索结果,请直接 联系老师 获取答案。
相似问题和答案

第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题:

下面程序的输出结果是()。includeinclude"string.h"void main(){char a[]="Hello T

下面程序的输出结果是( )。 #include<iostream.h> #include"string.h" void main() {char a[]="Hello Test",b[]="Test"; strcpy(a,b); cout<<a<<end1; } A) B)

C) D)

A.Hello

B.Test

C.Hello Test

D.Hello Test HelloTest


正确答案:B

第3题:

下面程序段的输出结果是( )。 public class Test { public static void main (String[] args) { for ( int a=0;a<10;a++) { if (a==5) break; System.out.println(A); } } }

A.01234

B.6789

C.012346789

D.5


正确答案:A
解析:题目中输出语句位于循环体内,而在if语句外,所以a5时执行输出语句。当a=5时,退出循环,结束程序的执行。

第4题:

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

第5题:

下面程序段的输出结果是( )。 public class Test { public static void main (String[] args) { int j=2,i=5; while (j<i--) j++; System.out.println(j);} }

A.2

B.3

C.4

D.5


正确答案:C
解析:循环时,首先判断结束条件,25,然后i=4,j=3,继续循环,i=3,j=4,结果条件ji为假,退出循环,因此j=4。所以选C。

第6题:

下面程序段的输出结果是( )。 public class Test { public static void main (String[] args) { int n=10; do { System.out.println("n is"+n); }while(--n>10); } }

A.n is 8

B.没有输出

C.n is 10

D.n is 9


正确答案:C
解析:do-while循环至少执行一次,输出n is 10。判断结束条件时,先计算--n,n=9,才进行比较运算,所以条件为假,退出循环。因此选C。

第7题:

清在下划线处填入代码,使程序正常运行并且输出“Hello!”

classTesl______{

publicstaticvoidmain(string[]args){

Test=newTest();

t.start();

}

Pubhcvoidrun(){

System.out.println("Hello!");

}

}


正确答案:extendsThread。
extendsThread。 解析:从后面的重写了run()方法来看,这是通过继承Thread类,并重写ran()方法定义线程体,然后创建该子类的对象的方式来创建线程。

第8题:

请阅读下面的程序

classTest/{

privatestaticStringname;

static/{

name="World";

System.out.print(name);

/}

publicstaticvoidmain(String[]args)/{

System.out.print("Hello");

Testtest=newTest();

/}

/}

下列选项中,程序运行结果是()。

:A.World

B.Hello

C.World Hello

D.Hello World


参考答案:D

第9题:

下列程序的输出结果是______。 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。

第10题:

main方法是Java应用程序执行的入口点,下面main方法的方法头合法的是()

A.public static void main()

B.public static void main(String[] args)

C.public static void Main(String[] args)

D.public static int main(String[] args)


public static void main( String[] args )