Java认证考试

现有:      class Pencil  {  public void write (String content){     System.out.println( "Write",+content){     }     }class RubberPencil extends Pencil{     public void write (String content){  System.out.println("Rubber Write"+content);     }  public void

题目

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

  • A、Write Hello
  • B、Rubber Write Hello
  • C、编译失败
  • D、运行时抛出异常
如果没有搜索结果,请直接 联系老师 获取答案。
如果没有搜索结果,请直接 联系老师 获取答案。
相似问题和答案

第1题:

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 来明确。

第2题:

设有类定义如下:

class InOut{

String s= new String("Between");

public void amethod(final int iArgs){

int iam;

class Bicycle{

public void sayHello(){

//Here

}

}

}

public void another(){

int iOther;

}

}

以下哪些语句可以安排在//Here处 ?

A. System.out.println(s);

B.System.out.println(iOther);

C. System.out.println(iam);

D. System.out.println(iArgs);


正确答案:AD

第3题:

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

第4题:

class MyThread extends Thread {  public void run() { System.out.println(“AAA”); }  public void run(Runnable r) { System.out.println(“BBB”); }  public static void main(String[] args) {  new Thread(new MyThread()).start();  }  }   What is the result?()  

  • A、 AAA
  • B、 BBB
  • C、 Compilation fails.
  • D、 The code runs with no output.

正确答案:A

第5题:

阅读下列说明和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!


答案:
解析:
(1) ticket.printInvoice() (2) ticket.printInvoice() (3) ticket.printInvoice() (4) new FootDecorator(new HeadDecorator(t)) (5) new FootDecorator(new HeadDecorator(new Decorator(null)))
【解析】

试题分析
本题考查的是面向对象程序设计和设计模式。本题涉及的设计模式是装饰模式。装饰模式(Decorator) :动态地给一个对象添加一些额外的职责。它提供了用子类打展功能的一个灵活的替代,比派生一个子类更加灵活。
对于程序填空可以参照代码上下文、题干说明和设计模式综合考虑。
对于第(1) 空,是对printInvoice方法的具体调用,在Decorator是 装饰类,继承了Invoice发票类。此处需要填写的是printInvoice方法的方法体,根据Decorator类的上下文,已定义ticket对象,所以此处调用printinvoice方法的是ticket,第(1) 空填写ticket printlnvoice()。
对于第(2) (3) 空,根据类图可知,分别是HeadDecorator抬头 类、FootDecorator脚注类调用printInvoice方法的方法体,由于在这两个类中并没有定义属性,只有借助其超类的构造函数,所以这两个地方调用printlnvoice方法的是它们的超类,即(2) (3) 填写的是super.printInvoice()。
对于第(4) (5) 空,考查的是对装饰模式的调用,都是main函数中实例化的过程,根据输出结果可以看到,第(4)空实例化ticket对象,可以输出抬头、内容、脚注3个部分, 因此需要调用三者的printInvoice()方法, 前面已经实例化了一个Invoice对象t,可以利用给子类实例化,因此第(4) 空填写new HeadDecorator(new FootDecorator());而第(5)空没有输出具体内容,只有抬头和脚注部分,可以看到这里的Invoice对象应该是空,所以第(5) 空填写new HeadDecorator(new FootDecorator(nl)。

第6题:

interface Playable {

void play();

}

interface Bounceable {

void play();

}

interface Rollable extends Playable, Bounceable {

Ball ball = new Ball("PingPang");

}

class Ball implements Rollable {

private String name;

public String getName() {

return name;

}

public Ball(String name) {

this.name = name;

}

public void play() {

ball = new Ball("Football");

System.out.println(ball.getName());

}

}

这个错误不容易发现。


正确答案:

 

错。"interface Rollable extends Playable, Bounceable"没有问题。interface 可继承多个

interfaces,所以这里没错。问题出在interface Rollable 里的"Ball ball = new Ball("PingPang");"。

任何在interface 里声明的interface variable (接口变量,也可称成员变量),默认为public static

final。也就是说"Ball ball = new Ball("PingPang");"实际上是"public static final Ball ball = new

Ball("PingPang");"。在Ball 类的Play()方法中,"ball = new Ball("Football");"改变了ball 的

reference,而这里的ball 来自Rollable interface,Rollable interface 里的ball 是public static final

的,final 的object 是不能被改变reference 的。因此编译器将在"ball = new Ball("Football");"

这里显示有错。

第7题:

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

public class Try extends Thread{

public static void main(String args[]){

Threadt=new Try();

【 】;

}

public void run(){

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

}

}


正确答案:i
i

第8题:

现有: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

第9题:

阅读下列说明和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

第10题:

class Parent {     String one, two;  public Parent(String a, String b){     one = a;     two = b;    }  public void print(){ System.out.println(one); }    }  public class Child extends Parent {     public Child(String a, String b){     super(a,b);     }  public void print(){  System.out.println(one + " to " + two);     }  public static void main(String arg[]){     Parent p = new Parent("south", "north");     Parent t = new Child("east", "west");     p.print();     t.print();     }     }  Which of the following is correct?()

  • A、 Cause error during compilation. 
  • B、 south         east 
  • C、 south to north     east to west    
  • D、 south to north      east    
  • E、 south     east to west

正确答案:E

更多相关问题