Java面向对象程序设计综合练习2(填空题)(下)

简介: Java面向对象程序设计综合练习2(填空题)(下)

R4-18

请说出A类中System.out.println的输出结果。


 class B{
    int x=100,y=200;
    public void setX(int x){ x=x; }
    public void setY(int y){ this.y=y; } 
    public int getXYSum(){ return x+y; } 
 } 
 public class A { 
    public static void main(String args[]){
        B b=newB(); 
        b.setX(-100); 
        b.setY(-200); 
        System.out.println("sum="+b.getXYSum()); 
    } 
 } 


程序输出结果为:sum=

-100

2 分


R4-19

阅读下列程序:

public class Main{ 
    public static void main(String[]args){ 
        System.out.println("创建一个Faculty对象"); 
        new Faculty(); 
    } 
} 
class Faculty extends Employee { 
    public Faculty(){ 
        System.out.println("完成Faculty的任务"); 
    } 
} 
class Employee extends Person{ 
    public Employee(){ 
        this("调用Employee的重载构造方法"); 
        System.out.println("完成Employee的任务"); 
    } 
    public Employee(String s){ 
        System.out.println(s); 
    } 
} 
class Person { 
    public Person() { 
        System.out.println("完成Person的任务"); 
    } 
}


从下面选项中进行选择,将相应序号(1、2、3、4、5中选其一)填入空格中:


(1)完成Person的任务

(2)完成Employee的任务

(3)完成Faculty的任务

(4)调用Employee的重载构造方法

(5)创建一个Faculty对象


按程序执行顺序输出结果:


答案:

第1空:5 ||

第2空:1 ||

第3空:4 ||

第4空:2 ||

第5空:3 ||


R4-20

下列代码的运行结果是什么?


public class Main{
  static{
     System.out.print("Hi here,");
  }
  public void print(){
     System.out.print("Hello");
  }
  public static void main(String args[]){
     Main m1=new Main();
     m1.print();
     Main m2=new Main();
     m2.print();
  }
}


上述代码的运行结果为:

答案:

第1空:Hi here,HelloHello ,

2 分


R4-21


给出以下代码:


public class Test {
    public int t=4;
    public static void main(String[] args) {
        new Test().NumberPlay();
    }
    public void NumberPlay() {
        int t=2;
        t = t+5;
        this.t = this.t-2;
        t = t-this.t;
        System.out.println(t+this.t+"ok");
    }
}


程序运行后输出结果为:


7ok


2 分


R4-22


给出以下代码:


class Number {
    public int i;
    public Number(int ii) {i=ii;};
}
public class Main {
    static void f(Number n) {
        n.i = 9;
    }
    static void g(Integer n) {
        n=9;
    }
    public static void main(String[] args) {
        Number k = new Number(10);
        f(k);
        Integer m = new Integer(10);
        g(m);
        System.out.println(k.i+":"+m);
    }
}



程序运行后输出结果是:


9:10


2 分


R4-23


补全程序使其能够运行,结果为打印四个图形对象的面积。

e114c4c266854ce38ca8de425a9c1a4a.png

第1空:new Shape[4] ,

第2空:ss[i].getArea() ,

第3空:abstract class Shape ,

第4空:this.radius = radius; ,

第5空:(int)(3.14 * radius * radius + 0.5); ,


R4-24


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


593b4088b8fd4130938bb1f21b868bdf.png

答案:


第1空:static ,

第2空:Shape ,

第3空:new ,

第4空:length ,

第5空:ss[i] ,

第6空:abstract ,

第7空:abstract ,

第8空:extends ,

第9空:Rect ,

第10空:return ,

第11空:extends ,

第12空:int ,

第13空:this.radius ,

第14空:return ,

第15空:(int) ,



R4-25

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


R4-26

已知代码:


class A{``
  String name="A";
  String getName(){
     return name;
  }
  String greeting(){
     return "class A"; 
  }
}
class B extends A{
  String name="B";
  String greeting(){
     return "class B";
  }
}
public class Main{
  public static void main(String args[]){
     A a=new A();
     A b=new B();
     System.out.println(a.greeting()+" has name "+a.getName());
     System.out.println(b.greeting()+" has name "+b.getName());
  }
}


在下列运行结果的空格中选择填写A或B:


class

A

2 分

has name A

2 分

class

B

2 分

has name A

2 分


R4-27


一个类如果实现一个接口,那么它就需要实现接口中定义的全部( ),否则该类就必须定义成( )。


答案:

第1空:方法 ||


第2空:抽象类 ||


R4-28

接口中的方法的默认的访问权限是


答案:


第1空:public ||


R4-29

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

接口


2 分


R4-30

阅读下列程序:

class Shape {
     private String strColor;
     public Shape() {
           System.out.println("我是图形");
       }
 public Shape(String strColor) {
        System.out.println("我是"+strColor+"的图形");
 }
 public void printType() {
       System.out.println("我是"+strColor+"的图形");
 }
 public void printName(){
       System.out.println("我不知道我叫什么");
 }
}
class Circle extends Shape{
     private String strColor,strName;
      public Circle() {
            System.out.println("我是圆");
      }
 public Circle(String strColor,String strName) {
      super(strColor);
      this.strName = strName;
       System.out.println("我是"+strColor+"的圆");
 }
 @Override
 public void printName(){
       System.out.println("我叫"+strName);
 }
}
public class Test {
     public static void main(String[] args)  {
         String strColor = "红色";
         String strName = "圆圆";
         Shape shape1 = new Shape();
         shape1.printName();
         shape1 = new Circle(strColor,strName);
         shape1.printName();
     }
}


按执行顺序在空格处填上如下选项的序号:


(1)我是图形
(2)我是红色的图形
(3)我是圆
(4)我是红色的圆
(5)我不知道我叫什么
(6)我叫圆圆


在下面空格处按顺序填上1、2、3、4、5、6中的其中一个数字。


答案:


第1空:1 ||

第2空:5 ||

第3空:2 ||

第4空:4 ||

第5空:6 ||


R4-31


25-4: The outour of the code below:


class A {
    public int data=5;
    public void print() { 
        System.out.println(data);
    }
}
class B extends A {
    public int data=2;
    public void print() {
        System.out.println(data);
    }
}
public class Main {
    public static int data=3;
    public static void main(String[] args) {
        A a = new A();
        a.print();
        System.out.println(a.data);
        A b = new B();
        b.print();
        System.out.println(b.data);
        System.out.println(Main.data);
    }
}


answer:

Empty 1: 5||

Empty 2: 5||

Empty 3: 2||

Empty 4: 5||

Empty 5: 3||


R4-32


用final关键字修饰的类不能被继承。


R4-33

如果一个方法只有方法的声明,而没有方法的实现,则成为  抽象方法|abstract 方法。


R4-34

阅读以下程序:


public class Test extends TT{ 
    public static void main(String args[]){ 
        Test t=new Test("Tom"); 
    } 
    public Test(String s){ 
        super(s); 
        System.out.println("How do you do?"); 
    } 
    public Test(){ 
        this("I am Tom"); 
    } 
} 
class TT { 
    public TT(){ 
        System.out.println("What a pleasure!"); 
    } 
    public TT(String s){ 
        this(); 
        System.out.println("I am "+s); 
    } 
} 


程序运行结果为:


答案:

第1空:What a pleasure! ||

第2空:I am Tom ||

第3空:How do you do? ||


R4-35


下面是People和Child类的定义和构造方法,每个构造方法都输出编号。在执行new Child("mike")的时候都有哪些构造方法被顺序调用?输出结果是


123


2 分


class People {
String name;
public People() {
System.out.print(1);
}
public People(String name) {
System.out.print(2);
this.name = name;
}
}
class Child extends People {
People father;
public Child(String name) {
System.out.print(3);
this.name = name;
father = new People(name + ":F");
}
public Child(){
System.out.print(4);
}
}

R4-36


阅读下列程序,根据运行输出的结果,在空格处填上适当的内容。

075090c50ccc4cbd812593f894911144.png


R4-37


Java中成员方法可分成两种:类方法和


成员方法


2 分


R4-38

运行以下程序:

public class Test {
  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");
  }
}


从以下选项中进行选择,按程序运行顺序填入空格中(填入序号1、2、3、4、5或6中之一):

(1)A (2)B (3) C (4)D (5)E (6)F


答案:

第1空:2 ||


第2空:6 ||


第3空:5 ||


第4空:4 ||


第5空:3 ||


R4-39

7a10705a773641b7babbd76480b8437a.png


答案:

第1空:interface ||


第2空:cry() ||


第3空: getName() ||


第4空:implements ||


第5空:String ||


第6空:this.name ||


第7空:public ||


第8空:return ||


第9空:SimulateTest ||


第10空:static ||


第11空:main ||


第12空:dog ||


第13空:Bob ||


第14空:getName() ||


第15空:dog ||


R4-40


阅读下列程序:


class A{ 
    int i=7; 
    public A(){ 
        setI(20); 
        System.out.println("i="+i); 
    } 
    public void setI(int i){
        this.i=2*i;
    } 
}
class B extends A{ 
    public B(){ 
        System.out.println("i="+i); 
    } 
    public void setI(int i){
        this.i=3*i;
    } 
} 
public class Main{ 
    public static void main(String[] args){ 
        new A(); 
        new B(); 
    } 
} 


程序运行后依次输出:


答案:

第1空:i=40 ||


第2空:i=60 ||


第3空:i=60 ||

86190d15581d41eb9aceac6961355604.png




目录
相关文章
|
23天前
|
搜索推荐 Java
Java的面向对象特性主要包括封装、继承和多态
【4月更文挑战第5天】Java的面向对象特性主要包括封装、继承和多态
15 3
|
3天前
|
安全 Java
Java基础&面向对象&继承&抽象类
Java基础&面向对象&继承&抽象类
|
3天前
|
Java
【Java基础】详解面向对象特性(诸如继承、重载、重写等等)
【Java基础】详解面向对象特性(诸如继承、重载、重写等等)
8 0
|
8天前
|
安全 Java 机器人
《Java 简易速速上手小册》第2章:面向对象的 Java(2024 最新版)
《Java 简易速速上手小册》第2章:面向对象的 Java(2024 最新版)
19 0
|
11天前
|
存储 Java
Java基础教程(7)-Java中的面向对象和类
【4月更文挑战第7天】Java是面向对象编程(OOP)语言,强调将事务抽象成对象。面向对象与面向过程的区别在于,前者通过对象间的交互解决问题,后者按步骤顺序执行。类是对象的模板,对象是类的实例。创建类使用`class`关键字,对象通过`new`运算符动态分配内存。方法包括构造函数和一般方法,构造函数用于对象初始化,一般方法处理逻辑。方法可以有0个或多个参数,可变参数用`类型...`定义。`this`关键字用于访问当前对象的属性。
|
15天前
|
存储 Java 编译器
对象的交响曲:深入理解Java面向对象的绝妙之处
对象的交响曲:深入理解Java面向对象的绝妙之处
47 0
对象的交响曲:深入理解Java面向对象的绝妙之处
|
1月前
|
算法 IDE Java
【软件设计师备考 专题 】面向对象程序设计语言:C++、Java、Visual Basic和Visual C++
【软件设计师备考 专题 】面向对象程序设计语言:C++、Java、Visual Basic和Visual C++
41 0
|
1月前
|
Java 程序员 编译器
【详识JAVA语言】面向对象程序三大特性之二:继承
【详识JAVA语言】面向对象程序三大特性之二:继承
47 2
|
2月前
|
安全 Java C++
【Java SE语法篇】8.面向对象三大特征——封装、继承和多态
【Java SE语法篇】8.面向对象三大特征——封装、继承和多态
|
9月前
|
Java
【Java面向对象】继承的认识与实现(2) 关键字 this 与 super 区别
【Java面向对象】继承的认识与实现(2) 关键字 this 与 super 区别
53 0