niusouti.com

单选题现有:     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

运行时抛出异常


相似考题
更多“单选题现有:     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);     }     }  执行下列代码”相关问题
  • 第1题:

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

  • 第2题:

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

  • 第3题:

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

    public class Try extends Thread{

    public static void main(String args[]){

    Threadt=new Try();

    【 】;

    }

    public void run(){

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

    }

    }


    正确答案:i
    i

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

    现有:      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){     System.out.println ("Erase "+content);     }     } 执行下列代码的结果是哪项?()  Pencil pen=new RubberPencil();      pen.write("Hello");     

    • A、  Write Hello
    • B、  Rubber Write Hello
    • C、编译错误
    • D、运行时抛出异常

    正确答案:B

  • 第6题:

    class TestA {  public void start() { System.out.println(”TestA”); }  }  public class TestB extends TestA {  public void start() { System.out.println(”TestB”); } public static void main(String[] args) {  ((TestA)new TestB()).start();  }  }  What is the result?() 

    • A、 TestA
    • B、 TestB
    • C、 Compilation fails.
    • D、 An exception is thrown at runtime.

    正确答案:B

  • 第7题:

    现有:      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、运行时抛出异常

    正确答案:D

  • 第8题:

    现有:      class TestA  {  public void start()  {  System.out.println("TestA");  }     }  public class TestB extends TestA  {  public void start()  {  System.out.println("TestB");  }     public static void main (string[]  args)  (     ((TestA)new TestB()).start();     )     }  运行结果是哪项?()     

    • A、  TeStA
    • B、  TeStB
    • C、编译失败
    • D、运行时抛出异常

    正确答案:B

  • 第9题:

    单选题
    现有:      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

    运行时抛出异常


    正确答案: D
    解析: 暂无解析

  • 第10题:

    单选题
    现有:      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){     System.out.println ("Erase "+content);     }     } 执行下列代码的结果是哪项?()  Pencil pen=new RubberPencil();      pen.write("Hello");
    A

      Write Hello

    B

      Rubber Write Hello

    C

    编译错误

    D

    运行时抛出异常


    正确答案: C
    解析: 暂无解析

  • 第11题:

    单选题
    class TestA {  public void start() { System.out.println(”TestA”); }  }  public class TestB extends TestA {  public void start() { System.out.println(”TestB”); } public static void main(String[] args) {  ((TestA)new TestB()).start();  }  }  What is the result?()
    A

     TestA

    B

     TestB

    C

     Compilation fails.

    D

     An exception is thrown at runtime.


    正确答案: D
    解析: 暂无解析

  • 第12题:

    单选题
    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


    正确答案: C
    解析: 这个题目涉及继承时的多态性问题,在前面的问题中已经有讲述,要注意的是语句t.print();在运行时t实际指向的是一个Child对象,即java在运行时决定变量的实际类型,而在编译时t是一个Parent对象,因此,如果子类Child中有父类中没有的方法,例如printAll(),那么不能使用t.printAll()。

  • 第13题:

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

  • 第14题:

    试题六(共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!


    正确答案:

  • 第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 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

  • 第16题:

    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

  • 第17题:

    public class Employee{       private String name;  public Employee(String name){           this.name = name;      }  public void display(){         System.out.print(name);      } }  public class Manager extends Employee{       private String department;  public Manager(String name,String department){          super(name);  this.department = department;  }  public void display(){  System.out.println(super.display()+”,”+department);      } }  执行语句new Manager(“smith”,”SALES”)后程序的输出是哪项?() 

    • A、 smith,SALES
    • B、 null,SALES
    • C、 smith,null
    • D、 null,null
    • E、 编译错误

    正确答案:E

  • 第18题:

    public class Employee{   private String name;   public Employee(String name){   this.name = name;  }   public void display(){   System.out.print(name);  }  }   public class Manager extends Employee{   private String department;   public Manager(String name,String department){   super(name);   this.department = department;  }   public void display(){   System.out.println( super.display()+”,”+department);  }   }   执行语句new Manager(“smith”,”SALES”)后程序的输出是哪项?() 

    • A、 smith,SALES
    • B、 null,SALES
    • C、 smith,null
    • D、 null,null

    正确答案:A

  • 第19题:

    现有:     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、运行时抛出异常

    正确答案:C

  • 第20题:

    单选题
    现有:     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

    运行时抛出异常


    正确答案: C
    解析: 暂无解析

  • 第21题:

    填空题
    Given the following code, write a line of code that, when inserted at the indicated location, will make the overriding method in Extension invoke the overridden method in class Base on the current object.   class Base {   public void print( ) {   System.out.println("base");   }   }   class Extention extends Base {   public void print( ) {   System.out.println("extension");   // insert line of implementation here   }   }   public class Q294d {   public static void main(String args[]) {   Extention ext = new Extention( );   ext.print( );   }   }   Fill in a single line of implementation.()

    正确答案: super.print();
    解析: 暂无解析

  • 第22题:

    单选题
    现有:      class TestA  {  public void start()  {  System.out.println("TestA");  }     }  public class TestB extends TestA  {  public void start()  {  System.out.println("TestB");  }     public static void main (string[]  args)  (     ((TestA)new TestB()).start();     )     }  运行结果是哪项?()
    A

      TeStA

    B

      TeStB

    C

    编译失败

    D

    运行时抛出异常


    正确答案: D
    解析: 暂无解析

  • 第23题:

    单选题
    public class Employee{   private String name;   public Employee(String name){   this.name = name;  }   public void display(){   System.out.print(name);  }  }   public class Manager extends Employee{   private String department;   public Manager(String name,String department){   super(name);   this.department = department;  }   public void display(){   System.out.println( super.display()+”,”+department);  }   }   执行语句new Manager(“smith”,”SALES”)后程序的输出是哪项?()
    A

     smith,SALES

    B

     null,SALES

    C

     smith,null

    D

     null,null


    正确答案: D
    解析: 暂无解析

  • 第24题:

    单选题
    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
    解析: 暂无解析