目录
题目1
- 异常的体系
- 请描述异常的继承体系
异常的根类是java.lang.Throwable类,分有两个子类:java.lang.Error与java.util.Exception.而Exception又分为编译时期异常:checked异常,运行期异常:runtime异常。 - 请描述你对错误(Error)的理解
表示不可修复的恶行的错误,只能通过修改代码规避错误的产生,通常是系统级别的。 - 请描述你对异常(Expection的理解)
表示可修复的异常,异常产生后,程序员可以通过代码的修正,使程序继续运行,是必须要处理的一个异常。 - 请描述你对运行时异常(RuntimeException)的理解
在运行期,检查异常,在编译期,运行异常不会编译器检测。
- 请描述异常的继承体系
- throw与throws
- 请描述throw的使用位置,作用是什么?
throw关键字通常用在方法体中,并且抛出一个异常对象,程序在执行到throw语句时立即停止,后面的语句都不执行。 - 请描述throws的使用位置,作用是什么?
throws关键字通常被应用在声明方法时,用来指定可能抛出的异常。多个异常可以使用逗号隔开,当在主函数中调用该方法时,如果发生异常,就会将异常对象抛给方法调用。
- 请描述throw的使用位置,作用是什么?
- 异常的处理方式
- 异常处理方式有几种,分别是什么?
有两种,分别是throws和try…catch…finally - 详细阐述每种方式对异常是如何处理的
* throws用在方法的声明上后接异常类名,是把异常抛给调用者进行处理。
* try…catch…finally是捕获异常,自己处理,处理完毕后面的程序可以继续运行
1. try代码块中是可能出现异常的代码
2. catch代码块是遇到异常,对异常进行处理的代码
3. finally代码块无论是否发生异常,都必须执行的代码,用于释放资源。
- 异常处理方式有几种,分别是什么?
- 常见异常以及产生的原因
- NullPointerException:空指针异常
当应用试图要求使用对象的地方使用null时,抛出该异常。例如:调用null对象的实例方法、访问null对象的属性、计算null对象的长度等等。 - ArrayIndexOutOfBoundsException:数组索引越界异常。
当对数组的索引值为负数或大于等于数组大小时抛出此异常。 - ArithmeticException:算术运算异常。
程序中出现了除以零这样的运算就会出这样的异常,对这种异常,大家就要好好检查一下自己程序中涉及到数学运算的地方,公式是不是有不妥了。 - NumberFormatException:数字格式异常。
当试图将一个String转换为指定的数字类型,而该字符串确不满足数字类型要求的格式时,抛出该异常。
- NullPointerException:空指针异常
题目2
- 看代码,分析结果
- 第一题
public class Test01 {
public static void main(String[] args) {
String str = null;
System.out.println(str.length());
}
}
- 第二题
public static void main(String[]args){
int arr[]={1,2};
System.out.println(arr[2]);
}
- 第三题
public static void main(String[]args){
System.out.println(1/0);
}
- 第四题
public static void main(String[]args){
System.out.println(Integer.parseInt("itcast"));
}
- 第五题
public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
try {
Date date = format.parse("2018-04-03");
System.out.println("程序正常");
} catch (ParseException e) {
System.out.println("程序异常");
}
}
题目3
- 并行、并发概念
请简单描述什么是并行,什么是并发?
- 并行指两个或多个事件在同一时刻发生,关键是有同时处理多个任务的能力
- 并发指两个或多个事件在同一个时间段发生,关键是有处理多个任务的能力
题目4
- 进程概念、线程概念、线程与进程练习
请描述什么是进程,什么是线程,进程与线程之间的关系
- 进程指正在运行的程序。确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于运行过程中的程序,并且具有一定独立功能。
- 线程是进程中的一个执行单元,负责当前进程中程序的执行,一个进程中至少有一个线程。一个进程中是可以有多个线程的,这个应用程序也可以称之为多线程程序。
- 一个程序运行后至少有一个进程,一个进程中可以包含多个线程,但一个进程中至少包含一个线程。
题目5
- 自定义异常类
请使用代码实现如下:
每一个学生(Student)都有学号,姓名和分数,分数永远不能为负数,如果老师给学生赋值一个负数,抛出一个自定异常。
代码如下:
//自定义异常类
public class ScoreException extends RuntimeException{
public ScoreException(String message) {
super(message);
}
}
public class Student {
private String name;
private String id;
private int score;
public Student() {
}
public Student(String name, String id, int score) {
this.name = name;
this.id = id;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getScore() {
return score;
}
public void setScore(int score) {
if(score < 0){
throw new ScoreException("分数不能为"+score);
}
this.score = score;
}
}
public class Test08 {
public static void main(String[] args) {
Student student = new Student();
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
String id = scanner.next();
int score = scanner.nextInt();
student.setId(id);
student.setName(name);
student.setScore(score);
}
}
题目6
- 多线程
创建多线程对象,开启多线程。在子线程中输出1-100之间的偶数,主线程输出1-100之间的奇数。
代码如下;
public class SonThread implements Runnable{
@Override
public void run() {
for(int i = 0;i < 100;i+=2){
System.out.println(i);
}
}
}
public class Test09 {
public static void main(String[] args) {
SonThread sonThread = new SonThread();
Thread son = new Thread(sonThread);
son.start();
for(int i = 1;i < 100;i+=2){
System.out.println(i);
}
}
}
版权声明:本文为qq_44612121原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。