class A {   public String toString () {   return “4”;   }   }   class B extends A {   8. public String toString () {   return super.toString() + “3”;   }   }   public class Test {   public static void main(Stringargs) {   System.out.printIn(new B());   }

题目

class A {   public String toString () {   return “4”;   }   }   class B extends A {   8. public String toString () {   return super.toString() + “3”;   }   }   public class Test {   public static void main(Stringargs) {   System.out.printIn(new B());   }   }   What is the result?()  

  • A、 Compilation succeeds and 4 is printed.
  • B、 Compilation succeeds and 43 is printed.
  • C、 An error on line 9 causes compilation to fail.
  • D、 An error on line 14 causes compilation to fail.
  • E、 Compilation succeeds but an exception is thrown at line 9.

相似考题

3.阅读以下函数说明和Java代码,将应填入(n)处的字句写在对应栏内。【说明】下面的程序先构造Point类,再顺序构造Ball类。由于在类Ball中不能直接存取类Point中的xCoordinate及yCoordinate属性值,Ball中的toString方法调用Point类中的toStrinS方法输出中心点的值。在MovingBsll类的toString方法中,super.toString调用父类Ball的toString方法输出类Ball中声明的属性值。【Java代码】//Point.java文件public class Point{private double xCoordinate;private double yCoordinate;public Point(){}public Point(double x,double y){xCoordinate=x;yCoordinate=y;}public String toStrthg(){return"("+Double.toString(xCoordinate)+","+Double.toString(yCoordinate)+")";}//other methods}//Ball.java文件public class Ball{private (1);//中心点private double radius;//半径private String color;//颜色public Ball(){}public Ball(double xValue, double yValue, double r){//具有中心点及其半径的构造方法center=(2);//调用类Point中的构造方法radius=r;}public Ball(double xValue, double yValue, double r, String c){//具有中心点、半径和颜色的构造方法(3);//调用3个参数的构造方法color=c;}public String toString(){return "A ball with center"+center.toString()+",radius "+Double.toString(radius)+",color"+color;}//other methods}class MovingBall (4) {private double speed;public MovingBall(){}public MoyingBall(double xValue, double yValue, double r, String c, double s){(5);//调用父类Ball中具有4个参数的构造方法speed=s;}public String toString(){return super.toString()+",speed"+Double.toString(speed);}//other methods}public class test{public static void main(String args[]){MovingBall mb=new MovingBall(10,20,40,"green",25);System.out.println(mb);}}

4.阅读下列Java程序和程序说明,将应填入(n)处的字句写在对应栏内。【说明】下面的程序先构造Point类,再顺序构造Ball类。由于在类Ball中不能直接存取类Point中的xCoordinate及yCoordinate属性值,Ball中的toString方法调用Point类中的toString方法输出中心点的值。在MovingBall类的toString方法中,super.toString调用父类Ball的toString方法输出类Ball中声明的属性值。public class Point{private double xCoordinate;private double yCoordinate;public Point 0 }public Point(ouble x, double y){xCoordinate = x;yCoordinate = y;}public String toString(){return "( + Double.toString(Coordinate)+ ","+ Double.toString(Coordinate) + ");}//other methods}public class Ball{(1); //中心点private double radius; //半径private String colour; ///颜色public Ball() { }public Ball(double xValue, double yValue, double r)// 具有中心点及半径的构造方法{center=(2);//调用类Point 中的构造方法radius = r;}public Ball(double xValue, double yValue, double r, String c)// 具有中心点、半径及颜色的构造方法{(3);//调用3个参数的构造方法colour = c;}public String toString(){return "A ball with center" + center, toString() + ", radius"+ Double.toString(radius) + ", colour" + colour;}//other methods}public class MovingBall. (4){private double speed;public MovingBall() { }public MovingBall(double xValue, double yValue, double r, String e, double s){(5);// 调用父类Ball中具有4个参数的构造方法speed = s;}public String toString( ){ return super, toString( ) + ", speed "+ Double.toString(speed); }//other methods}public class Tester{public static void main(String args[]){MovingBall mb = new MovingBall(10,20,40,"green",25);System.out.println(mb);}}

更多“class A {   public String toString () {   return “4”;   }   }   class B extends A {   8. public String toString () {   return super.toString() + “3”;   }   }   public class Test {   public static void main(Stringargs) {   System.out.printIn(new B());   } ”相关问题
  • 第1题:

    public class Something {

    public static void main(String[] args) {

    Something s = new Something();

    System.out.println("s.doSomething() returns " + doSomething());

    }

    public String doSomething() {

    return "Do something ...";

    }

    }

    看上去很完美。


    正确答案:

     

    错。看上去在main 里call doSomething 没有什么问题,毕竟两个methods 都在同一个

    class 里。但仔细看,main 是static 的。static method 不能直接call non-static methods。可改

    成"System.out.println("s.doSomething() returns " + s.doSomething());"。同理,static method 不

    能访问non-static instant variable。

  • 第2题:

    以下语句能顺利通过编译: class test { static void sayHello() { this.toString(); } public String toString() { retur。()

    此题为判断题(对,错)。


    答案:错


  • 第3题:

    class Super {  public int getLenght( ) { return 4; }  }  public class Sub extends Super {  public long getLenght( ) { return 5; }  public static void main(String[] args) {  Super sooper = new Super( );  Sub sub = new Sub( );  System.out.println(  sooper.getLenght( ) + “,” + sub.getLenght( ) );  }  } What is the output?()

    • A、 Just after line 13.
    • B、 Just after line 14.
    • C、 Just after line 15.
    • D、 Just after line 16 (that is, as the method returns).

    正确答案:C

  • 第4题:

    class super {  public int getLength()  {return 4;}  }  public class Sub extends Super {  public long getLength() {return 5;}  public static void main (String[]args)  {  super sooper = new Super ();  Sub sub = new Sub();  System.out.printIn(  sooper.getLength()+ “,” + sub.getLength()   };  }  What is the output?()  

    • A、 4, 4
    • B、 4, 5
    • C、 5, 4
    • D、 5, 5
    • E、 The code will not compile.

    正确答案:E

  • 第5题:

    Public class test (    Public static void stringReplace (String text) (    Text = text.replace („j„ , „i„);    )      public static void bufferReplace (StringBuffer text) (    text = text.append (“C”)   )      public static void main (String args ){  String textString = new String (“java”);    StringBuffer text BufferString = new StringBuffer (“java”);      stringReplace (textString);    BufferReplace (textBuffer);      System.out.printIn (textString + textBuffer);    }   )   What is the output?()


    正确答案:javajavaC

  • 第6题:

    1. class A {  2. public String toString ()  {  3. return “4”;  4. }  5. }  6. class B extends A {  7. public String toString ()   {  8. return super.toString()  + “3”;  9. }  10. }  11. public class Test {  12.   public static void main(String[]args)  {  13.      System.out.printIn(new B());  14.      }  15. }    What is the result?()  

    • A、 Compilation succeeds and 4 is printed.
    • B、 Compilation succeeds and 43 is printed.
    • C、 An error on line 9 causes compilation to fail.
    • D、 An error on line 14 causes compilation to fail.
    • E、 Compilation succeeds but an exception is thrown at line 9.

    正确答案:B

  • 第7题:

    Given the following code:     1) class Parent {     2) private String name;     3) public Parent(){}     4) }  5) public class Child extends Parent {     6) private String department;  7) public Child() {}  8) public String getValue(){ return name; }     9) public static void main(String arg[]) {     10) Parent p = new Parent();     11) }  12) }  Which line will cause error?()   

    • A、 line 3
    • B、 line 6
    • C、 line 7
    • D、 line 8
    • E、 line 10

    正确答案:D

  • 第8题:

    1. interface TestA { String toString(); }  2. public class Test {  3. public static void main(String[] args) {  4. System.out.println(new TestA() {  5. public String toString() { return “test”; }  6. }  7. }  8. }  What is the result?() 

    • A、 test
    • B、 null
    • C、 An exception is thrown at runtime.
    • D、 Compilation fails because of an error in line 1.
    • E、 Compilation fails because of an error in line 4.
    • F、 Compilation fails because of an error in line 5.

    正确答案:A

  • 第9题:

    单选题
    Given the following code:     1) class Parent {     2) private String name;     3) public Parent(){}     4) }  5) public class Child extends Parent {     6) private String department;  7) public Child() {}  8) public String getValue(){ return name; }     9) public static void main(String arg[]) {     10) Parent p = new Parent();     11) }  12) }  Which line will cause error?()
    A

     line 3

    B

     line 6

    C

     line 7

    D

     line 8

    E

     line 10


    正确答案: D
    解析: 第8行的getValue()试图访问父类的私有变量,错误。

  • 第10题:

    多选题
    public class X {  public X aMethod() { return this;}  }  public class Y extends X {  }  Which two methods can be added to the definition of class Y?()
    A

    public void aMethod() {}

    B

    private void aMethod() {}

    C

    public void aMethod(String s) {}

    D

    private Y aMethod() { return null; }

    E

    public X aMethod() { return new Y(); }


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

  • 第11题:

    单选题
    class Super { public int getLenght() { return 4; } } public class Sub extends Super { public long getLenght() { return 5; } public static void main(String[] args) {Super sooper = new Super(); Sub sub = new Sub(); System.out.println( sooper.getLenght() + “,” + sub.getLenght() ); } } What is the output? ()
    A

     4,4

    B

     4,5

    C

     5,4

    D

     5,5

    E

     Compilation fails.


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

  • 第12题:

    单选题
    class A {   public String toString () {   return “4”;   }   }   class B extends A {   8. public String toString () {   return super.toString() + “3”;   }   }   public class Test {   public static void main(Stringargs) {   System.out.printIn(new B());   }   }   What is the result?()
    A

     Compilation succeeds and 4 is printed.

    B

     Compilation succeeds and 43 is printed.

    C

     An error on line 9 causes compilation to fail.

    D

     An error on line 14 causes compilation to fail.

    E

     Compilation succeeds but an exception is thrown at line 9.


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

  • 第13题:

    以下程序能顺利通过编译: public class am_I_right { public static void main(String args[]) { this.toString(); } String toString() { retur。()

    此题为判断题(对,错)。


    答案:错

  • 第14题:

     class Super {  public Integer getLenght() { return new Integer(4); } }  public class Sub extends Super {  public Long GetLenght() { return new Long(5); }  public static void main(String[] args) { Super sooper = new Super();  Sub sub = new Sub();  System.out.println(  sooper.getLenght().toString() + “,” +  sub.getLenght().toString() ); } }  What is the output?()  

    • A、 4,4
    • B、 4,5
    • C、 5,4
    • D、 5,5
    • E、 Compilation fails.

    正确答案:A

  • 第15题:

    Public class test (  Public static void main (String args[])  (  System.out.printIn (6 ^ 3);  )  )   What is the output?()


    正确答案:5

  • 第16题:

    class Super { public int getLenght() { return 4; } } public class Sub extends Super { public long getLenght() { return 5; } public static void main(String[] args) {Super sooper = new Super(); Sub sub = new Sub(); System.out.println( sooper.getLenght() + “,” + sub.getLenght() ); } } What is the output? ()

    • A、 4,4
    • B、 4,5
    • C、 5,4
    • D、 5,5
    • E、 Compilation fails.

    正确答案:E

  • 第17题:

    public class X {  public X aMethod() { return this;}  }  public class Y extends X {  }  Which two methods can be added to the definition of class Y?()

    • A、 public void aMethod() {}
    • B、 private void aMethod() {}
    • C、 public void aMethod(String s) {}
    • D、 private Y aMethod() { return null; }
    • E、 public X aMethod() { return new Y(); }

    正确答案:C,E

  • 第18题:

    class super {   public int getLength() {return 4;}    }   public class Sub extends Super {   public long getLength() {return 5;}   public static void main (Stringargs) {   super sooper = new Super ();  Sub sub = new Sub();   System.out.printIn(   sooper.getLength()+ “,” + sub.getLength() };   }   What is the output?() 

    • A、 4, 4
    • B、 4, 5
    • C、 5, 4
    • D、 5, 5
    • E、 The code will not compile.

    正确答案:E

  • 第19题:

    1. class A {  3. public String to String() {  4. return “4”;  5. }  6. }  7. class B extends A {  8. public String toString() { 9. return super.toString() + “3”;  10. }  11. }  12. public class Test {  13. public static void main (String[] args) {  14. System.out.printIn(new B()); 15. }  16. }   What is the result( )?  

    • A、 Compilation succeeds and 4 is printed.
    • B、 Compilation …………… is printed.
    • C、 An error on line 9 cause compilation to fail.
    • D、 An error on line 14 cause compilation to fail.
    • E、 Compilation succeeds but an exception is thrown at line 9.

    正确答案:B

  • 第20题:

    填空题
    Public class test (    Public static void stringReplace (String text) (    Text = text.replace („j„ , „i„);    )      public static void bufferReplace (StringBuffer text) (    text = text.append (“C”)   )      public static void main (String args ){  String textString = new String (“java”);    StringBuffer text BufferString = new StringBuffer (“java”);      stringReplace (textString);    BufferReplace (textBuffer);      System.out.printIn (textString + textBuffer);    }   )   What is the output?()

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

  • 第21题:

    单选题
    class super {  public int getLength()  {return 4;}  }  public class Sub extends Super {  public long getLength() {return 5;}  public static void main (String[]args)  {  super sooper = new Super ();  Sub sub = new Sub();  System.out.printIn(  sooper.getLength()+ “,” + sub.getLength()   };  }  What is the output?()
    A

     4, 4

    B

     4, 5

    C

     5, 4

    D

     5, 5

    E

     The code will not compile.


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

  • 第22题:

    单选题
    class Super {  public Integer getLenght() { return new Integer(4); } }  public class Sub extends Super {  public Long GetLenght() { return new Long(5); }  public static void main(String[] args) { Super sooper = new Super();  Sub sub = new Sub();  System.out.println(  sooper.getLenght().toString() + “,” +  sub.getLenght().toString() ); } }  What is the output?()
    A

     4,4

    B

     4,5

    C

     5,4

    D

     5,5

    E

     Compilation fails.


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

  • 第23题:

    填空题
    Public class test (  Public static void main (String args[])  (  System.out.printIn (6 ^ 3);  )  )   What is the output?()

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