1. class A {  2. public String toString ()  {  3. return “4”

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

如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

写出程序运行的结果

Public class Base

Public virtual string Hello() {return “Base”;}

Public class Sub:Base

Public override string Hello() {return “Sub”;}

1. Base b = new Base(); b.Hello;

2. Sub s = new Sub(); s.Hello;

3. Base b = new Sub (); b.Hello;

4. Sub s = new Base(); s.Hello;


正确答案:
 

第2题:

1. public class ReturnIt {  2. return Type methodA(byte x, double y) {  3. return (long)x / y * 2;  4. }  5. }  What is the narrowest valid returnType for methodA in line2?()  

  • A、 int
  • B、 byte
  • C、 long
  • D、 short
  • E、 float
  • F、 double

正确答案:F

第3题:

阅读下列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);

}

}


正确答案:(1)private Point center (2)new Point (xValueyValue) (3)this(xValueyValuer) (4)extends Ball (5)super(xValueyValuerc)
(1)private Point center (2)new Point (xValue,yValue) (3)this(xValue,yValue,r) (4)extends Ball (5)super(xValue,yValue,r,c) 解析:(1)private Point center
Ball类以Point类的center对象作为私有成员。
(2)new Point (xValue,yValue)
用类Point中的构造方法Point构造Point类的center对象。
(3)this(xValue,yValue,r)
利用this指针调用本类的3个参数的重载构造方法。
(4)extends Ball
MovingBall类由Ball类扩展而来。
(5)super(xValue,yValue,r,c)
调用父类Ball中具有4个参数的构造方法:
Ball(double xValue, double yValue, double r,String c)

第4题:

1. public class A {  2. public void doit() {  3. }  4. public String doit() {  5. return “a”;  6. }  7. public double doit(int x) {  8. return 1.0;  9. }  10.}  What is the result?() 

  • A、 An exception is thrown at runtime.
  • B、 Compilation fails because of an error in line 7.
  • C、 Compilation fails because of an error in line 4.
  • D、 Compilation succeeds and no runtime errors with class A occur.

正确答案:C

第5题:

1. public interface A {  2. public void doSomething(String thing);  3. }  1. public class AImpl implements A {  2. public void doSomething(String msg) { }  3. }  1. public class B {  2. public A doit() {  3. // more code here  4. }  5.  6. public String execute() { 7. // more code here  8. }  9. }  1. public class C extends B {  2. public AImpl doit() {  3. // more code here  4. }  5.  6. public Object execute() {  7. // more code here  8. }  9. }  Which statement is true about the classes and interfaces in the exhibit?() 

  • A、 Compilation will succeed for all classes and interfaces.
  • B、 Compilation of class C will fail because of an error in line 2.
  • C、 Compilation of class C will fail because of an error in line 6.
  • D、 Compilation of class AImpl will fail because of an error in line 2.

正确答案:C

第6题:

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

第7题:

1. public class Target {  2. private int i = 0;  3. public int addOne() {  4. return ++i;  5. }  6. }  And:  1. public class Client {  2. public static void main(String[] args) {  3. System.out.println(new Target().addOne());  4. }  5. }  Which change can you make to Target without affecting Client?() 

  • A、 Line 4 of class Target can be changed to return i++;
  • B、 Line 2 of class Target can be changed to private int i = 1;
  • C、 Line 3 of class Target can be changed to private int addOne() {
  • D、 Line 2 of class Target can be changed to private Integer i = 0;

正确答案:D

第8题:

阅读以下函数说明和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);

}

}


正确答案:(1) Point center (2) new Point(xValueyValue) (3) this(xValueyValuer) (4) extends Ball (5) super(xValueyValuerc)
(1) Point center (2) new Point(xValue,yValue) (3) this(xValue,yValue,r) (4) extends Ball (5) super(xValue,yValue,r,c) 解析:在类Ball的有参数构造函数中,对成员变量center通过调用Point类的构造方法初始化,而center在类Ball中尚未声明。结合注释可得空(1)是将center变量声明为Point对象引用,故空(1)应填Point。空(2)是调用Point类的构造函数,根据题意,此处应将xValue和yValue作为参数调用类Point的有参数构造函数,故空(2)应填new Point(xValue,yValue)。
根据注释,空(3)是调用类Ball的有3个参数的构造方法,而其所在方法本身就是类Ball的一个构造方法,因此可用this来调用自身的构造方法,故空(3)应填this(xValue,yValue,r)。
根据题述“在MovingBall类的toString方法中,super.toString调用父类Ball的toString方法输出类Ball中声明的属性值”,可知类MovingBall是类Ball的子类,因此空(4)应填extends Ball。
根据注释,空(5)是调用父类Ball中具有4个参数的构造方法,通过super关键字实现,故空(5)应填super(xValue,yValue,r,c)。

第9题:

Class TestException  1. public class TestException extends Exception {  2. } Class a:  1. public class a {  2.  3. public String sayHello(String name) throws TestException {  4.  5. if(name == null) {  6. throw new TestException();  7. }  8.  9. return “Hello “+ name;  10. }  11.  12. }  A programmer wants to use this code in an application: 45. A a=new A();  46. System.out.println(a.sayHello(”John”));  Which two are true?()

  • A、 Class a will not compile.
  • B、 Line 46 can throw the unchecked exception TestException.
  • C、 Line 45 can throw the unchecked exception TestException.
  • D、 Line 46 will compile if the enclosing method throws a TestException.
  • E、 Line 46 will compile if enclosed in a try block, where TestException is caught.

正确答案:D,E

第10题:

现有:  1. abstract class Color  {  2.protected abstract  String getRGB();     3.  }     4.  5. public class Blue extends Color  {     6.    //insert code here      7.  }  和四个声明:  public String getRGB()  {  return "blue";  }      String getRGB()  {  return  "blue";  )  private  String getRGB()  {  return  "blue";  }      protected String getRGB()  {  return "blue";  )      分别插入到第6行,有几个可以通过编译?()    

  • A、  0
  • B、  1
  • C、  2
  • D、  3

正确答案:C

更多相关问题