单选题现有:     class Pencil  {  public void write (String content){     System.out.println ("Write"+content);     }     }  class RubberPencil extends Pencil{     public void erase (String content){     System.out.println ("Erase"+content);     }     }  执行下列代码

题目
单选题
现有:     class Pencil  {  public void write (String content){     System.out.println ("Write"+content);     }     }  class RubberPencil extends Pencil{     public void erase (String content){     System.out.println ("Erase"+content);     }     }  执行下列代码的结果是哪项?()      Pencil pen=new RubberPencil();      pen.write ("Hello");      pen.erase ("Hello");
A

 Write Hello        Erase Hello

B

 Erase Hello        Write Hello

C

编译错误

D

运行时抛出异常

参考答案和解析
正确答案: B
解析: 暂无解析
如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

阅读下列代码段,选出该代码段的正确文件名( )。 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.A.java

B.A.class

C.B.class

D.B.java


正确答案:D
解析:Java源程序文件是以Java为后缀的,Java字节码文件以.class为后缀,而且 Java源文件中只能有一个public类,该类的名字为源文件名,程序段中类B是以public修饰的,因此源程序文件名为Biava。

第2题:

现有:classPencil{publicvoidwrite(Stringcontent){System.out.println("Write",+content){}}classRubberPencilextendsPencil{publicvoidwrite(Stringcontent){System.out.println("RubberWrite"+content);}publicvoiderase(Stringcontent)}}执行下列代码的结果是哪项?()Pencilpen=newPencil();((RubberPencil)pen).write("Hello");

A.WriteHello

B.RubberWriteHello

C.编译失败

D.运行时抛出异常


参考答案:D

第3题:

下列代码的执行结果是( )。 public class Test { public static void main(String args[]) { System.out.println(7/2); } }

A.3.5

B.3

C.3.0

D.3.50


正确答案:B
解析:整数除以整数其结果是舍去小数点后面的数,不进行四舍五入。

第4题:

下列程序创建了一个线程并运行,请在下划线处填入正确代码。

public class Try extends Thread{

public static void main(String args[]){

Threadt=new Try();

【 】;

}

public void run(){

System.out.println(“Try!”);

}

}


正确答案:i
i

第5题:

interface A{

int x = 0;

}

class B{

int x =1;

}

class C extends B implements A {

public void pX(){

System.out.println(x);

}

public static void main(String[] args) {

new C().pX();

}

}


正确答案:

 

错误。在编译时会发生错误(错误描述不同的JVM 有不同的信息,意思就是未明确的

x 调用,两个x 都匹配(就象在同时import java.util 和java.sql 两个包时直接声明Date 一样)。

对于父类的变量,可以用super.x 来明确,而接口的属性默认隐含为 public static final.所以可

以通过A.x 来明确。

第6题:

( 8 )阅读下列代码

public class Test2{

public static void main(String args[]){

System.out.println(5/2);}}

其执行结果是 【 8 】 。


正确答案:

第7题:

现有:classPencil{publicvoidwrite(Stringcontent){System.out.println("Write"+content);}}classRubberPencilextendsPencil{publicvoidwrite(Stringcontent){System.out.println("RubberWrite"+content);}publicvoiderase(Stringcontent){System.out.println("Erase"+content);}}执行下列代码的结果是哪项?()Pencilpen=newRubberPencil();pen.write("Hello");

A.WriteHello

B.RubberWriteHello

C.编译错误

D.运行时抛出异常


参考答案:B

第8题:

阅读下列代码段,选出该代码段的正确文件名( )。 class A { void methodl() { System.out.println("methodl 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.A.java

B.A.class

C.B.class

D.B.java


正确答案:D
解析:Java源程序文件是以.java为后缀的,Java字节码文件以.class为后缀,而且 Java源文件中只能有一个public类,该类的名字为源文件名,程序段中类B是以public修饰的,因此源程序文件名为B.java。

第9题:

试题六(共15分)

阅读下列说明和Java代码,将应填入 (n) 处的字句写在答题纸的对应栏内。

【说明】

某发票(lnvoice)由抬头(Head)部分、正文部分和脚注(Foot)部分构成。现采用装饰( Decorator)模式实现打印发票的功能,得到如图6-1所示的类图。

【java代码】

class invoice{

public void printInvoice(){:

System.out.println ( "This is the content of the invoice!");

}

}

class Decorator : extends Invoice {

protected Invoice ticket;

public Decorator(lnvoice t){

ticket = t;

}

public void printinvoice(){

if(ticket != NULL)

(1);

}

}

class FootDecorator extends Decorator{

public FootDecorator(lnvoice t){

super(t);

}

public void printinvoice (){

Systent.out.println( "This is the header of the invoice! ");

(2) ;

}

}

class FootDecorator extends Decorator {

public FootDecorator(invoice t):{

super(t);

}

public void printlnvoice(){

(3) ;

Systent.out.println( "This is the header of the invoice! ");

}

}

Class test {

public static void main(string[] args){

Invoice t =new invioce();

Invoice ticket;

Ticket= (4) ;

Ticket. Printinvoice();

Systent.out.println(“--------------“)

Ticket= (5) ;

Ticket. Printinvoice();

}

}

程序的输出结果为:

This is the header of the invoice!

This is the content of the invoice!

This is the footnote of the invoice!

----------------------------

This is the header of the invoice!

This is the footnote of the invoice!


正确答案:

第10题:

阅读下列说明和java代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
某发票(lnvoice)由抬头(Head)部分、正文部分和脚注(Foot)部分构成。现采用装饰(Decorator)模式实现打印发票的功能,得到如图6-1所示的类图。

【java代码】
class invoice{
public void printInvoice( ){

System.out.println ( "This is the content of the invoice!");
}
}
class Decorator extends Invoice {

protected Invoice ticket;

public Decorator(lnvoice t){

ticket = t;
}
public
void printInvoice( ){

if(ticket != null)
(1) ;

}
}
class HeadDecorator extends Decorator{

public HeadDecorator(lnvoice t){

super(t);
}

public void printInvoice ( ){

Systent.out.println( "This is the header of the invoice! ");
(2) ;
}
}
class FootDecorator extends Decorator {

public FootDecorator(Invoice t){

super(t);
}

public void printlnvoice( ){

( 3) ;

Systent.out.println( "This is the footnote of the invoice! ");
}
}
Class test {

public static void main(String[] args){

Invoice t =new Invioce( );

Invoice ticket;

ticket= (4) ;

ticket.printInvoice( );

Systent.out.println(“------------------“);

ticket= (5) ;

ticket.printInvoice( );
}
}
程序的输出结果为:

This is the header of the invoice!

This is the content of the invoice!

This is the footnote of the invoice!

----------------------------

This is the header of the invoice!

This is the footnote of the invoice!


答案:
解析:
(1) ticket.printInvoice()
(2) ticket.printInvoice()

(3) ticket.printInvoice()

(4) new FootDecorator(new

更多相关问题