Java面向对象程序设计5接口与抽象类

简介: Java面向对象程序设计5接口与抽象类

选择题

R1-1

分数 1

作者 王从银

单位 吉首大学

为了克服单继承的缺点,Java使用了接口,一个类可以实现多个接口。

T

F


R1-2

分数 1

作者 王从银

单位 吉首大学

abstract可以与final同时修饰同一个+类。

T

F


R1-3

分数 1

作者 王从银

单位 吉首大学

抽象类中不能有private的成员,所有的抽象方法必须存在于抽象类中。

T

F


R1-4

分数 1

作者 翁恺

单位 浙江大学

When a class implements an interface, it can define some methods of the interface as needed, and leaves some untouched.

当一个类实现一个接口时,它可以根据需要定义该接口的一些方法,并保留一些不变的方法。

T

F


R1-5

分数 1

作者 翁恺

单位 浙江大学

All abstract methods must be implemented in the first concrete subclasses in the inheritance tree.

所有抽象方法都必须在继承树的第一个具体子类中实现。

T

F


R1-6

分数 1

作者 王从银

单位 吉首大学

类在实现接口方法时必须给出方法体,并且一定要用public来修饰。

T

F


R1-7

分数 1

作者 王从银

单位 吉首大学

一个Java源文件就是由类和接口组成的。

T

F


R1-8

分数 1

作者 王从银

单位 吉首大学

抽象类必须有抽象方法。

T

F


R1-9

分数 1

作者 王从银

单位 吉首大学

一个类只能有一个父类,但一个接口可以有一个以上的父接口。

T

F


R1-10

分数 1

作者 王从银

单位 吉首大学

抽象方法必须在抽象类中,所以抽象类中的方法都必须是抽象方法。。

T

F


R1-11

分数 1

作者 王从银

单位 吉首大学

abstract类中定义的方法只能是abstract方法。

T

F


R1-12

分数 1

作者 王从银

单位 吉首大学

类在实现接口的方法时,必须显式地使用public修饰符。

T

F


R1-13

分数 1

作者 王从银

单位 吉首大学

如果一个类声明实现一个接口,但没有实现接口中的所有方法,那么这个类必须是abstract类。

T

F


R1-14

分数 1

作者 王从银

单位 吉首大学

一个类可以实现多个接口。

T

F


R1-15

分数 1

作者 王从银

单位 吉首大学

接口中的方法默认是public abstract方法。

T

F


R1-16

分数 1

作者 王从银

单位 吉首大学

接口中的属性,都是静态常量。

T

F


R1-17

分数 1

作者 翁恺

单位 浙江大学

All methods in an abstract superclass must be declared abstract in its derived class.

T

F

R2-1

分数 2

作者 强彦

单位 太原理工大学

下列选项中,用于实现接口的关键字是 ( )。

A.

implements

B.

abstract

C.

interface

D.

class


R2-2

分数 2

作者 翁恺

单位 浙江大学

Which one is to define an abstract method? ( )

A.

abstract void method()

B.

public void abstract method()

C.

public abstract void Method() {}

D.

public void abstract Method() { System.out.println("Hello"); }


R2-3

分数 2

作者 王从银

单位 吉首大学

在使用interface声明一个接口时,只可以使用(    )修饰符修饰该接口。

A.

private  protected

B.

private

C.

public

D.

protected


R2-4

分数 2

作者 殷伟凤

单位 浙江传媒学院

运行如下程序输出为:( )。

public class Test {
  public static void main(String[] args) {
    new Circle9();
  }
}
 
public abstract class GeometricObject {
  protected GeometricObject() {
    System.out.print("A");
  }
 
  protected GeometricObject(String color, boolean filled) {
    System.out.print("B");
  }
}
 
public class Circle9 extends GeometricObject {
  /** No-arg constructor */
  public Circle9() {
    this(1.0);
    System.out.print("C");
  }
 
  /** Construct circle with a specified radius */
  public Circle9(double radius) {
    this(radius, "white", false);
    System.out.print("D");
  }
 
  /** Construct a circle with specified radius, filled, and color */
  public Circle9(double radius, String color, boolean filled) {
    super(color, filled);
    System.out.print("E");
  }
}

A.

BACD

B.

AEDC

C.

ABCD

D.

BEDC


R2-5

分数 2

作者 殷伟凤

单位 浙江传媒学院

如下在一个Java抽象类中对抽象方法的声明哪个是正确的?

A.

public abstract method();

B.

public abstract void method();

C.

public abstract void method() {}

D.

public void abstract method();


R2-6

分数 2

作者 强彦

单位 太原理工大学

下列选项中,用于定义接口的关键字是( )。

A.

class

B.

abstract

C.

interface

D.

implements


R2-7

分数 2

作者 王从银

单位 吉首大学

下面哪个对类的声明是错误的?

A.

class MyClass extends MySuperClass1, MySupperClass2 {}

B.

private class MyClass {}

C.

public class MyClass{}

D.

abstract class MyClass implements YourInterface1, Youriterface2 {}

E.

class MyClass extends MySuperClass implements YourInterface {}


R2-8

分数 2

作者 殷伟凤

单位 浙江传媒学院

下面说法正确的是?

A.

定义抽象方法需有方法的返回类型、名称、参数列表和方法体。

B.

final可修饰类、变量、方法。

C.

用final修饰的变量,在程序中可对这个变量的值进行更改。

D.

abstract可修饰类、变量、方法。


R2-9

分数 2

作者 王从银

单位 吉首大学

欲构造ArrayList类的一个实例,此类继承了List接口,下列哪个方法是正确的?(   )

A.

List myList=new List();

B.

ArrayList myList=new List();

C.

List myList=new ArrayList();

D.

ArrayList myList=new Object();


R2-10

分数 2

作者 翁恺

单位 浙江大学

What will happen when you attempt to compile and run this code?

abstract class Base{
        abstract public void myfunc();
        public void another(){
            System.out.println("Another method");
        }
}
 
public class Abs extends Base{
        public static void main(String argv[]){
            Abs a = new Abs();
            a.amethod();
        }
        public void myfunc(){
                System.out.println("My Func");
        }
        public void amethod(){
            myfunc();
        }
}

A.

The compiler will complain that the Base class has non abstract methods

B.

The compiler will complain that the method myfunc in the base class has no body, nobody at all to looove it

C.

The code will compile and run, printing out the words "My Func"

D.

The code will compile but complain at run time that the Base class has non abstract methods


R2-11

分数 2

作者 殷伟凤

单位 浙江传媒学院

下面类的定义,哪个是合法的抽象类定义。

A.

class A { abstract void unfinished(); }

B.

public class abstract A { abstract void unfinished(); }

C.

abstract class A { abstract void unfinished(); }

D.

class A { abstract void unfinished() { } }


R2-12

分数 2

作者 王从银

单位 吉首大学

若A1、A2为已定义的接口 ,以下接口定义中没有语法错误的是(        ) 。

A.

interface  B {  void print()  {  } }

B.

abstract  interface  B { void print() }

C.

abstract  interface  B  extends  A1,A2  { abstract  void  print(){  };}

D.

interface  B  {  void  print();}


R2-13

分数 2

作者 强彦

单位 太原理工大学

在Java中,能实现多重继承效果的方式是( )。

A.

继承

B.

接口

C.

内部类

D.

适配器

填空题

R4-1

设计一个Flyable接口,一个实现Flyable接口的Duck类和Duck的子类RedheadDuck。

@@[interface](1) Flyable {
    void fly();
}
@@[abstract](1) class Duck @@[implements](1) Flyable {
    public @@[void](1) quack() {
    System.out.println("我会呱呱呱");
}
public void @@[swim](1)() {
    System.out.println("我会游泳");
}
public @@[abstract](1) void display()@@[;](1)
    @Override
    @@[public](1) void fly() {
    System.out.println("我会飞");
    }
}
class RedheadDuck @@[extends](1) Duck {
    public void @@[display](1)() {
    System.out.println("我是一只红头鸭");
    }
}
public class Main {
    
    public @@[static](1) void @@[main](1)(String[] args) {
        Duck @@[rduck](1) = new @@[RedheadDuck](1)();
        rduck.display();
        @@[rduck](1).quack();
        rduck.swim();
        rduck.fly();
    }
}

R4-2

接口中的方法的默认的访问权限是@@[public](2)。

R4-3

设计一个动物声音“模拟器”,希望模拟器可以模拟许多动物的叫声,要求如下:

(1)编写接口Animal

Animal接口有2个抽象方法.

(2)编写实现Animal接口的Dog类。

(3)编写主类SimulateTest,在主类SimulateTest的main方法中编写程序进行测试,输出以下结果:

我的名字叫Bob

汪汪

@@[interface](1) Animal   {
      void @@[cry()](1);
      String @@[ getName()](1);
}
class Dog @@[implements](1) Animal  {
       private @@[String](1) name;
       Dog(String name) {
              @@[this.name](1) = name;
      }
      @Override
      @@[public](1) void cry() {
              System.out.println("汪汪");
      }
      @Override
      public String getName() {
               @@[return](1) "我的名字叫"+name;
     }
}
public class @@[SimulateTest](1) {
       public @@[static](1) void @@[main](1)(String[] args) {
           Animal @@[dog](1) = new Dog("@@[Bob](1)");
           System.out.println(dog.@@[getName()](1));
           @@[dog](1).cry();
       }
}

 

R4-3

运行以下程序:

public class Main {
  public static void main(String[] args) {
    new Circle9();
 
  }
}
 
abstract class GeometricObject {
  protected GeometricObject() {
    System.out.print("A");
  }
 
  protected GeometricObject(String color, boolean filled) {
    System.out.print("B");
  }
}
 
class Circle9 extends GeometricObject {
  /** No-arg constructor */
  public Circle9() {
    this(1.0);
    System.out.print("C");
  }
 
  /** Construct circle with a specified radius */
  public Circle9(double radius) {
    this(radius, "white");
    System.out.print("D");
  }
 
  /** Construct circle with a specified radius and color */
  public Circle9(double radius, String color) {
    this(radius, "white", false);
    System.out.print("E");
  }
 
  /** Construct a circle with specified radius, filled, and color */
  public Circle9(double radius, String color, boolean filled) {
    super(color, filled);
    System.out.print("F");
  }
}

BFEDC

R4-5

以下程序输出四个图形面积,请在空格处填上合适的内容。

public class Main
{
public @@[static](2) void main(String args[])
{
@@[Shape](2) ss[] = @@[new](2) Shape[4];
ss[0] = new Rect(10,10);
ss[1] = new Rect(8, 12);
ss[2] = new Circle(5);
ss[3] = new Circle(6);
for(int i = 0; i < ss.@@[length](2); i++)
{
System.out.println( @@[ss[i]](2).getArea());
}
}
}
@@[abstract](2) class Shape
{
@@[abstract](2) int getArea();
}
class Rect @@[extends](2) Shape
{
int width;
int height;
@@[Rect](2)(int width, int height)
{
this.width = width;
this.height = height;
}
int getArea()
{
@@[return](2) width * height;
}
}
class Circle @@[extends](2) Shape
{
int radius;
Circle(@@[int](2) radius)
{
@@[this.radius](2) = radius;
}
int getArea()
{
@@[return](2) @@[(int)](2)(3.14 * radius * radius + 0.5);
}
}

R4-6

设计一个动物声音“模拟器”,希望模拟器可以模拟许多动物的叫声,要求如下:

(1)编写接口Animal

Animal接口有2个抽象方法.

(2)编写实现Animal接口的Dog类。

(3)编写主类SimulateTest,在主类SimulateTest的main方法中编写程序进行测试,输出以下结果:

我的名字叫Bob

汪汪

@@[interface](1) Animal   {
      void @@[cry()](1);
      String @@[ getName()](1);
}
class Dog @@[implements](1) Animal  {
       private @@[String](1) name;
       Dog(String name) {
              @@[this.name](1) = name;
      }
      @Override
      @@[public](1) void cry() {
              System.out.println("汪汪");
      }
      @Override
      public String getName() {
               @@[return](1) "我的名字叫"+name;
     }
}
public class @@[SimulateTest](1) {
       public @@[static](1) void @@[main](1)(String[] args) {
           Animal @@[dog](1) = new Dog("@@[Bob](1)");
           System.out.println(dog.@@[getName()](1));
           @@[dog](1).cry();
       }
}

R4-7

分数 4

作者 翁恺

单位 浙江大学

请写出以下程序运行结果:

package test;
public class Test {
public static void main(String[] args) {
Test t = new Test();
t.show(new Car(){
public void run(){
System.out.println("test run");
}
});
}
public void show(Car c){
c.run();
c.show();
}}
abstract class Car{
public void run(){
System.out.println("car run");
}
public void show(){
System.out.println(getClass().getName());
}}

R4-7

请写出以下程序运行结果:

package test;
public class Test {
public static void main(String[] args) {
Test t = new Test();
t.show(new Car(){
public void run(){
System.out.println("test run");
}
});
}
public void show(Car c){
c.run();
c.show();
}}
abstract class Car{
public void run(){
System.out.println("car run");
}
public void show(){
System.out.println(getClass().getName());
}}
@@[test run](2)
@@[test.Test$1](2)


目录
相关文章
|
3天前
|
存储 Java 编译器
Java中的抽象类与接口,在阿里工作5年了
Java中的抽象类与接口,在阿里工作5年了
|
20小时前
|
并行计算 Java API
Java 8中的接口默认方法和静态方法以及并行数组
【5月更文挑战第19天】Java 8引入了许多新特性,其中包括接口的默认方法和静态方法,以及并行数组的能力。这些特性增强了Java的面向对象编程模型和数组处理能力。让我们深入了解它们的概念和实践。
18 2
|
2天前
|
存储 Java
Java一分钟之-高级集合框架:Queue与Deque接口
【5月更文挑战第18天】本文探讨Java集合框架中的`Queue`和`Deque`接口,两者都是元素序列的数据结构。`Queue`遵循FIFO原则,主要操作有`add/remove/element/peek`,空队列操作会抛出`NoSuchElementException`。`Deque`扩展`Queue`,支持首尾插入删除,同样需注意空`Deque`操作。理解并正确使用这两个接口,结合具体需求选择合适数据结构,能提升代码效率和可维护性。
22 4
|
5天前
|
Java API 容器
Java8函数式编程接口:Consumer、Supplier、Function、Predicate
Java8函数式编程接口:Consumer、Supplier、Function、Predicate
8 1
|
5天前
|
Java ice
【Java开发指南 | 第二十九篇】Java接口
【Java开发指南 | 第二十九篇】Java接口
9 0
|
5天前
|
Java
【Java开发指南 | 第二十七篇】Java抽象类
【Java开发指南 | 第二十七篇】Java抽象类
13 0
|
5天前
|
Java
【Java开发指南 | 第九篇】访问实例变量和方法、继承、接口
【Java开发指南 | 第九篇】访问实例变量和方法、继承、接口
14 4
|
5天前
|
搜索推荐 Java
Java的面向对象特性主要包括封装、继承和多态
【4月更文挑战第5天】Java的面向对象特性主要包括封装、继承和多态
21 3
|
5天前
|
Java 程序员 编译器
【详识JAVA语言】面向对象程序三大特性之二:继承
【详识JAVA语言】面向对象程序三大特性之二:继承
48 2
|
5天前
|
Java
java面向对象——包+继承+多态(一)-2
java面向对象——包+继承+多态(一)
18 3
http://www.vxiaotou.com