1. class Animal { Animal getOne() { return new Animal(); } }  2. class Dog extends Animal {  3. // insert code here   4. }  5.  6. class AnimalTest {  7. public static void main(String [] args) {  8. Animal [] animal = { new Animal(), new Dog() } ;  9. f

题目

1. class Animal { Animal getOne() { return new Animal(); } }  2. class Dog extends Animal {  3. // insert code here   4. }  5.  6. class AnimalTest {  7. public static void main(String [] args) {  8. Animal [] animal = { new Animal(), new Dog() } ;  9. for( Animal a : animal) {  10. Animal x = a.getOne();  11. }  12. }  13. }  和代码:  3a. Dog getOne() {  return new Dog();  }  3b. Animal getOne() {  return new Dog();  }  第 3 行中插入的哪项将编译且运行无异常?() 

  • A、3a行
  • B、3b行
  • C、3a行或3b行
  • D、既非3a,也非3b

相似考题
参考答案和解析
正确答案:D
更多“1. class Animal ”相关问题
  • 第1题:

    有如下程序:

    nclude<iostream>

    using namespace std;

    class Animal{

    public:

    virtual char*getType()const{return“Animal”;}

    virtual char*getVoice()const{return“Voice”;}

    };

    class Dog:public Animal{

    public:

    char*getType()const{rgturn“Dog”;}

    char*getVoice()const{retum“Woof”;}

    };

    void type(Animal&A){cout<<a.getType();}

    void speak(AnimalA){cout<<a.getVoice();}

    int main(){

    Dog d.type(D);tout<<“speak”;speak(D);cout<<endl;

    return 0;

    }

    运行时的输出结果是【 】


    正确答案:Dog speak Voice
    Dog speak Voice 解析:基类中有两个虚函数getType( )和getVoiee( ),在派生类中同样也有。函数type和speak的形参都是Animal类的对象,但是一个是引用调用,另一个不是。当用Animal的派生类Dog类定义的对象调用这两个函数时,type函数会转向:Dog类中的成员函数,而speak函数不会。

  • 第2题:

    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

  • 第3题:

    1. public class a {  2. public void method1() {  3. try {  4. B b=new b();  5. b.method2();  6. // more code here  7. } catch (TestException te) {  8. throw new RuntimeException(te);  9. }  10. }  11. }  1. public class b {  2. public void method2() throws TestException {  3. // more code here  4. }  5. }  1. public class TestException extends Exception {  2. }  Given:  31. public void method() {  32. A a=new a();  33. a.method1();  34. }  Which is true if a TestException is thrown on line 3 of class b?()

    • A、 Line 33 must be called within a try block.
    • B、 The exception thrown by method1 in class a is not required to be caught.
    • C、 The method declared on line 31 must be declared to throw a RuntimeException.
    • D、 On line 5 of class a, the call to method2 of class b does not need to be placed in a try/catch block.

    正确答案:B

  • 第4题:

    interface Animal {  void soundOff();  }  class Elephant implements Animal {  public void soundOff() {  System.out.println(“Trumpet”);  }  }  class Lion implements Animal {  public void soundOff() { System.out.println(“Roar”);  }  }  class Alpha1 {  static Animal get( String choice ) {  if ( choice.equalsIgnoreCase( “meat eater” )) {  return new Lion();  } else {  return new Elephant();  }  }  }  Which compiles?()  

    • A、 new Animal().soundOff();
    • B、 Elephant e = new Alpha1();
    • C、 Lion 1 = Alpha.get(“meat eater”);
    • D、 new Alpha1().get(“veggie”).soundOff();

    正确答案:D

  • 第5题:

    Which two demonstrate an “is a” relationship?()   

    • A、 public interface Person { }  public class Employee extends Person { }
    • B、 public interface Shape { }  public class Employee extends Shape { }
    • C、 public interface Color { }  public class Employee extends Color { }
    • D、 public class Species { }  public class Animal (private Species species;)
    • E、 interface Component { }  Class Container implements Component ( Private Component[ ] children;  )

    正确答案:D,E

  • 第6题:

    单选题
    11. class Animal { public String noise() { return “peep”; } }  12. class Dog extends Animal {  13. public String noise() { return “bark”; }  14. }  15. class Cat extends Animal {  16. public String noise() { return “meow”; }  17. }  .....  30. Animal animal = new Dog();  31. Cat cat = (Cat)animal;  32. System.out.printIn(cat.noise());  What is the result?()
    A

     peep

    B

     bark

    C

     meow

    D

     Compilation fails.

    E

     An exception is thrown at runtime.


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

  • 第7题:

    单选题
    在Java中,类Animal中的方法printA()定义如下:  public void printA() {    Int a=10;    Int result =10%3;    System.out.println(result); }  在类Dog中方法printA()定义如下:  public void printA() {   Int a=10;    System.out.println(a/3); }  Dog类的定义如下:  Class Dog extends Animal{…}.  Animal animal=new Dog();  animal.printA();  以上语句输出结果为()。
    A

     0

    B

     1

    C

     2

    D

    3

    E

    3.3333


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

  • 第8题:

    单选题
    现有:  1. interface Animal {  2. void eat();  3. }  4.  5. // insert code here  6.  7. public class HouseCat extends Feline {  8. public void eat() { }  9. }   和五个声明:  abstract class Feline implements Animal { }  abstract class Feline implements Animal { void eat(); }  abstract class Feline implements Animal { public void eat(); }  abstract class Feline implements Animal { public void eat() { } }  abstract class Feline implements Animal { abstract public void eat(); }  分别插入到第5行,有几个可以通过编译?()
    A

    0

    B

    1

    C

    2

    D

    3


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

  • 第9题:

    多选题
    10. interface Jumper { public void jump(); }  ......  20. class Animal {}  ......  30. class Dog extends Animal { 31. Tail tail; 32. }  ......  40. class Beagle extends Dog implements Jumper {  41. public void jump() { }  42. }  .......  50. class Cat implements Jumper {  51. public void jump() { }  52. }  Which three are true?()
    A

    Cat is-a Animal

    B

    Cat is-a Jumper

    C

    Dog is-a Animal

    D

    Dog is-a Jumper

    E

    Cat has-a Animal

    F

    Beagle has-a Tail

    G

    Beagle has-a Jumper


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

  • 第10题:

    单选题
    现有:  1.  interface Animal  f      2.    void eat();      3.    }      4.  5.  // insert code here      6.  7. public class HouseCat implements Feline  {      8.    public void eat()    {  }     9.  }  和以下三个接口声明:  interface Feline extends Animal  (  )  interface Feline extends Animal  {void eat();    }  interface Feline extends Animal  {void eat()    {  }  }   分别插入到第5行,有多少行可以编译?
    A

      0

    B

      1

    C

      2

    D

      3


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

  • 第11题:

    单选题
    1. interface Animal {   2. void eat();   3. }   4.   5. // insert code here   6.   7. public class HouseCat implements Feline {   8. public void eat() { }   9. }   和以下三个接口声明:   interface Feline extends Animal { }   interface Feline extends Animal { void eat(); }   interface Feline extends Animal { void eat() { } }   分别插入到第 5 行,有多少行可以编译?()
    A

    0

    B

    1

    C

    2

    D

    3


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

  • 第12题:

    单选题
    class Animal{ Animal getOne(){return new Animal();}}   class Dog extends Animal{   //insert code here   }   class AnimalTest{   public static void main(String[] args){   Animal[] animal={ new Animal(), new Dog()};   for(Animal a:animal){   Animal x= a.getOne();   }   }   }   和代码:   3a.Dog getOne() { return new Dog();}   3b.Animal getOne() { return new Dog();}   第3行中插入的哪项编译且运行无异常?
    A

    3a行或3b行

    B

    既非3a,也非3b

    C

    3a行

    D

    3b行


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

  • 第13题:

    下列程序片段中,能通过编译的是( )。 A.public abstract class Animal{ public void speak;}S

    下列程序片段中,能通过编译的是( )。

    A.public abstract class Animal{ public void speak;}

    B.public abstract class Animal{ public void speak{);}

    C.public class Animal{ pubilc abstract void speak;}

    D.public abstract class Animal{ pubile abstract void speak{};}


    正确答案:A
    A。【解析】Java中一个类是一个abstract类的子类,它必须具体实现父类的abstract方法。如果一个类中含有abstract方法,那么这个类必须用abstract来修饰(abstract类也可以没有abstract方法)。有abstract方法的父类只声明,由继承它的子类实现。所以选A。

  • 第14题:

    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

  • 第15题:

    现自:  1.  interface Color {  }      2. interface Weight  {  }      3.  //insert code here    和以下足六个声明:  class Boat extends Color, extends Weight  {  }     class Boat extends Color and Weight  {  }      class Boat extends Color, Weight  {  }  class Boat implements Color,  implements Weight  {  }     class Boat implements Color and Weight  {  }      class Boat implements Color, Weight  {  }    分别插入到第3行,有多少行可以编译? () 

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

    正确答案:B

  • 第16题:

    11. class Animal { public String noise() { return “peep”; } }  12. class Dog extends Animal {  13. public String noise() { return “bark”; }  14. }  15. class Cat extends Animal {  16. public String noise() { return “meow”; }  17. }  .....  30. Animal animal = new Dog();  31. Cat cat = (Cat)animal;  32. System.out.printIn(cat.noise());  What is the result?() 

    • A、 peep
    • B、 bark
    • C、 meow
    • D、 Compilation fails.
    • E、 An exception is thrown at runtime.

    正确答案:E

  • 第17题:

    多选题
    Given: 10. interface Jumper { public void jump(); } ...   20. class Animal {} ...   30. class Dog extends Animal {   31. Tail tail;   32. }   ...   40. class Beagle extends Dog implements Jumper{   41. public void jump() {}  42. }   ...   50. class Cat implements Jumper{   51. public void jump() {}   52. }. Which three are true?()
    A

    Cat is-a Jumper

    B

    Cat is-a Animal

    C

    Dog is-a Jumper

    D

    Dog is-a Animal

    E

    Beagle has-a Jumper

    F

    Cat has-a Animal

    G

    Beagle has-a Tail


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

  • 第18题:

    多选题
    Which three demonstrate an “is a” relationship?()
    A

    public class X {  }     public class Y extends X { }

    B

    public interface Shape { }     public interface Rectangle extends Shape{ }

    C

    public interface Color { }     public class Shape { private Color color; }

    D

    public interface Species { }     public class Animal { private Species species; }

    E

    public class Person { }    public class Employee {      public Employee(Person person) { }

    F

    interface Component { }     class Container implements Component {   private Component[] children; }


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

  • 第19题:

    多选题
    Which two demonstrate an “is a” relationship?()
    A

    public interface Person { }  public class Employee extends Person { }

    B

    public interface Shape { }  public class Employee extends Shape { }

    C

    public interface Color { }  public class Employee extends Color { }

    D

    public class Species { }  public class Animal (private Species species;)

    E

    interface Component { }  Class Container implements Component ( Private Component[ ] children;  )


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

  • 第20题:

    多选题
    Which the two demonstrate an “is a” relationship?()
    A

    public interface Person {}  Public class Employee extends Person {}

    B

    public interface Shape {}  public interface Rectangle extends Shape {}

    C

    public interface Color {}  public class Shape { private Color color; }

    D

    public class Species {}  public class Animal { private Species species; }

    E

    interface Component {} Class Container implements Component {private Component [] children;


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

  • 第21题:

    单选题
    interface Animal {  void soundOff();  }  class Elephant implements Animal {  public void soundOff() {  System.out.println(“Trumpet”);  }  }  class Lion implements Animal {  public void soundOff() { System.out.println(“Roar”);  }  }  class Alpha1 {  static Animal get( String choice ) {  if ( choice.equalsIgnoreCase( “meat eater” )) {  return new Lion();  } else {  return new Elephant();  }  }  }  Which compiles?()
    A

     new Animal().soundOff();

    B

     Elephant e = new Alpha1();

    C

     Lion 1 = Alpha.get(“meat eater”);

    D

     new Alpha1().get(“veggie”).soundOff();


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

  • 第22题:

    单选题
    现有:  interface Animal {       void eat () ;       }       //insert code here       public class HouseCat extends Feline {       public void eat() { }       }  和五个申明  abstract class Feline implements Animal { }  abstract  class  Feline  implements  Animal  {  void eat () ;  }  abstract class Feline implements Animal { public void eat();}  abstract class Feline implements Animal { public void eat() {}  }  abstract class Feline implements Animal { abstract public void eat();} 结果为:()
    A

    1

    B

    2

    C

    3

    D

    4


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

  • 第23题:

    单选题
    1. class Animal { Animal getOne() { return new Animal(); } }  2. class Dog extends Animal {  3. // insert code here   4. }  5.  6. class AnimalTest {  7. public static void main(String [] args) {  8. Animal [] animal = { new Animal(), new Dog() } ;  9. for( Animal a : animal) {  10. Animal x = a.getOne();  11. }  12. }  13. }  和代码:  3a. Dog getOne() {  return new Dog();  }  3b. Animal getOne() {  return new Dog();  }  第 3 行中插入的哪项将编译且运行无异常?()
    A

    3a行

    B

    3b行

    C

    3a行或3b行

    D

    既非3a,也非3b


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

  • 第24题:

    单选题
    In a class of 160 seniors, the ratio of boys to girls is 3 to 5. In the junior class, the ratio of boys to girls is 3 to 2. When the two classes are combined, the ratio of boys to girls is 1 to 1. How many students are in the junior class?
    A

    400

    B

    360

    C

    200

    D

    180

    E

    160


    正确答案: C
    解析:
    因为这个班共有160人,男生和女生的比例为3:5,所以男生和女生的人数分别为(3/8) ×160 = 60人,(5/8) ×160 = 100人。假设另一班男生女生人数共有x人,那么男生女生的人数分别为(3/5)x,(2/5)x,又因为当两班的人数相加后,男生女生的比例相等,所以可得出60 + (3/5)x = 100 + (2/5)x,经计算,x = 200。