JavaSE基础测试04及答案

Javase 基础测试04

一、填空题

1、java类或接口继承时使用     extends   关键字,类实现接口时使用       implements    关键字。

2、面向对象的三大特征:    封装、  继承   、  多态   。

3、java源程序代码是有若干        组成。

4、   class    是关键字,是用来定义类的。

5、异常处理的五大关键字:   try   、  catch   、   finally   、  throw   、  throws   

6、声明接口使用的关键字    interface    

7、抽象方法可以出现在    抽象类(abstract class)          接口(interface)    。

二、判断题

1、java编程语言中,finalize是一个关键字。(F

2、java程序中,有抽象方法的类必须声明为抽象类。(T

3、java ArrayList实现对象,下标索引总是从0开始的。(T

4、java1.8规范中,接口只能包括全局常量和全局抽象方法。(F

5、程序代码List a=List.of(1,2,3);System.out.println(a[0]);程序输出结果1。(F

6、ArrayList集合对象的元素可以是重复的。(T

7、接口声明中,声明变量可以不初始化。(F

8、接口方法全部是抽象的,不能有实现方法,可以不写abstract关键字。(F

9、抽象方法一定出现在接口中,只能出现一次。(F

10、最终类,最终方法和常量的修饰符都用finally关键声明。(F

三、简答题

1、简述ArrayList和LinkedList特点?

1.ArrayList是Array(动态数组)的数据结构,LinkedList是Link(链表)的数据结构。

2.当随机访问List(get和set操作)时,ArrayList比LinkedList的效率更高,因为LinkedList是线性的数据存储方式,所以需要移动指针从前往后依次查找。

当对数据进行增加和删除的操作(add和remove操作)时,LinkedList比ArrayList的效率更高,因为ArrayList是数组,所以在其中进行增删操作时,会对操作点之后所有数据的下标索引造成影响,需要进行数据的移动。

3.ArrayList自由性较低,因为它需要手动的设置固定大小的容量,但是它的使用比较方便,只需要创建,然后添加数据,通过调用下标进行使用;而LinkedList自由性较高,能够动态的随数据量的变化而变化,但是它不便于使用。

4.ArrayList主要控件开销在于需要在lList列表预留一定空间;而LinkList主要控件开销在于需要存储结点信息以及结点指针信息。

2、什么是序列化,什么是反序列表,为什么有时要用到序列化?

Java序列化就是指把Java对象转换为字节序列的过程

Java反序列化就是指把字节序列恢复为Java对象的过程。

序列化最重要的作用:在传递和保存对象时.保证对象的完整性和可传递性。对象转换为有序字节流,以便在网络上传输或者保存在本地文件中。

反序列化的最重要的作用:根据字节流中保存的对象状态及描述信息,通过反序列化重建对象。

总结:核心作用就是对象状态的保存和重建。(整个过程核心点就是字节流中所保存的对象状态及描述信息)

四、程序题

1、判断输入的字符串是中文,英文,数字,其它字符(使用Scanner、正则表达式)

package doy20;

import java.util.Scanner;

/**
 * @author phus [495748247@qq.com]
 * @Author: phus
 * @Date: 2023-06-29-14:45
 * @Description:
 */

public class While3 {
     public static void main(String[] args) {
          // 判断输入的字符串是中文,英文,数字,其它字符(使用Scanner、正则表达式)
          Scanner sc = new Scanner(System.in);
          System.out.print("请输入字符串:");
          String t = sc.nextLine();
          if ( t.matches("[\u4e00-\u9fa5]+") ) {
               System.out.println("中文:" + t);
          } else if ( t.matches("[a-zA-Z]+") ) {
               System.out.println("En:" + t);
          } else if ( t.matches("[0-9]+") ) {
               System.out.println("数字:" + t);
          } else {
               System.out.println("其它:" + t);
          }
     }
}

2、编程实现证明ArrayList和LinkedList优缺点?

public class Ex4 {
    public static void main(String[] args) {
        ta(100000);
        tb(100000);
    }

    static void ta(int n) {
        long s = System.currentTimeMillis();
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(0, "hello" + i);
        }
        long e = System.currentTimeMillis();
        System.out.printf("ArrayList add : %d%n",e-s);

        //删除
        s = System.currentTimeMillis();
        for(int i=0;i<n/2;i++) {
            list.remove(i);
        }
        e = System.currentTimeMillis();
        System.out.printf("ArrayList remove : %d%n",e-s);

        //随机读取
        s = System.currentTimeMillis();
        for(int i=0;i<n/2;i++) {
            String t = list.get(i);
        }
        e = System.currentTimeMillis();
        System.out.printf("ArrayList read : %d%n",e-s);

    }

    static void tb(int n) {
        long s = System.currentTimeMillis();
        LinkedList<String> list = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            list.addFirst("hello" + i);
        }
        long e = System.currentTimeMillis();
        System.out.printf("LinkedList add : %d%n",e-s);

        s = System.currentTimeMillis();
        for(int i=0;i<n/2;i++) {
            list.remove(i);
        }
        e = System.currentTimeMillis();
        System.out.printf("LinkedList remove : %d%n",e-s);

        s = System.currentTimeMillis();
        for(int i=0;i<n/2;i++) {
            String t = list.get(i);
        }
        e = System.currentTimeMillis();
        System.out.printf("LinkedList read : %d%n",e-s);

    }
}

3、编程实现猜数游戏,使用try catch处理异常。

package doy20;

import java.util.Random;
import java.util.Scanner;

/**
 * @author phus [495748247@qq.com]
 * @Author: phus
 * @Date: 2023-06-29-15:02
 * @Description:
 */

public class While5 {
     public static void main(String[] args) {
          // 编程实现猜数游戏,使用try catch处理异常。
          Random r = new Random();
          Scanner s = new Scanner(System.in);
          int t = r.nextInt(1, 101);
          while ( true ) {
               System.out.print("请输入[1-100] : ");
               int n = 0;
               try {
                    n = s.nextInt();
                    if ( n < 1 || n > 100 ) {
                         System.out.println("必须输入1-100整数");
                         continue;
                    }
               } catch (Exception e) {
                    if ( "xx".equalsIgnoreCase(s.next()) ) {
                         System.out.println("游戏结束");
                         break;
                    } else {
                         System.out.println("必须输入整数");
                         continue;
                    }
               }
               if ( n > t ) {
                    System.out.println("太大了");
               } else if ( n < t ) {
                    System.out.println("太小了");
               } else {
                    System.out.println("恭喜,你猜对了.");
                    break;
               }
          }
     }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值