程序语言与编程实践7-> Java实操4 | 第三周作业及思路讲解 | 异常处理考察
第三周作业,可能是异常那一章当时没怎么听,此前也不怎么接触,感觉还挺陌生的。
00 第1题
00-1 题目
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package homework_week3; /** * * @author zzrs123 */ public class Homework_week3 { /** * @param args the command line arguments */ public static void main(String[] args) { int a=0, b=5; try{ System.out.print(a/b+b/a); } catch { System.out.println("Exceptions!!!"); } } }
编译以上代码,会出现什么错误?
A. Prints: Exceptions!!! B. Prints Nothing C. Syntax error D. Runtime Error E. None of the above
00-2 解答
Answer: D
catch处语法出错,导致不能编译: Uncompilable source code - 非法的类型开始;
如果改为:
catch (ArithmeticException e){ System.out.println("Exceptions!!!"); }
那么就会输出
Exceptions!!!。
考察的是
try + catch + finally异常捕获机制的语法:如果
try{}抛出的对象属于
catch()括号内欲捕获的异常类,则
catch()会捕捉此异常,然后进到
catch()的块里继续运行。
01 第2题
01-1 题目
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package homework_week3; /** * * @author zzrs123 */ public class Homework_week3 { /** * @param args the command line arguments */ public static void main(String[] args) { int a=0, b=5; String c[] = {"A","B","C"}; try{ for(int i = 1;i < 4; i++){ System.out.print(c[i]); } System.out.print(a/b+b/a); } catch (ArithmeticException e) { System.out.println("D"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("E"); } } }
编译并执行代码,会出现哪种结果?
A. Prints: ABC B. Prints: ABD C. Prints: BCE D. Prints: BCDEE. Compiler Error
01-2 解答
**Answer: **C
这道题就考察了
try + catch + finally的运行机制,检测到
for循环里出现数组下标溢出,就抛出异常,产生中断,接着匹配到第二个catch语句块
catch(ArrayIndexOutOfBoundsException),输出
E。
考察的是
try + catch + finally异常捕获机制的捕获到异常立刻中断,进入异常处理。
02 第3题
02-1 题目
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package homework_week3; /** * * @author zzrs123 */ public class Homework_week3 { /** * @param args the command line arguments */ public static void main(String[] args) { int a=0, b=5; String c[] = {"A","B","C"}; try{ System.out.print(c[a/b]); try{ for(int i = 1;i < 4; i++){ System.out.print(c[i]); } } catch (Exception e) { System.out.println("D"); } finally{ System.out.println("E"); } } catch(Exception e) { System.out.println("F"); } finally{ System.out.println("G"); } } }
编译并执行代码,会出现哪种结果?
A. Prints: AABCG B. Prints: ABCDG C. Prints: AABCDG D. Prints: AABCDEG E. Prints: AABCDEFG
02-2 解答
Answer: D
两个
try + catch + finally的嵌套,考察的知识是:
" 无论 try 程序块是否有捕捉到异常,或者捕捉到的异常是否与 catch()括号里的异常相同,最后一定会运行 finally 块里的程序代码。finally 的程序代码块运行结束后,程序再回到 try-catch-finally 块之后继续执行。"
03 第4题
03-1 题目
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package homework_week3; /** * * @author zzrs123 */ public class Homework_week3 { /** * @param args the command line arguments */ public static void main( String[] args ) { int src[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; int res[] = {1, 2, 3, 4, 5}; System.arraycopy(src, 0, res, 0, src.length); for(int i=0; i<res.length; i++) { System.out.print(res[i]); } } }
What is the result?
A. 10987654321 B. 10987612345 C. 12345612345 D. Compiler error E. Runtime exception
03-2 解答
Answer: E
很明显,这里的语句都符合语法,所以Compiler error可以排除;
而
arraycopy()函数的语法:
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
参数说明:
- Object src : 原数组
- int srcPos : 拷贝到原数组的起始位置
- Object dest : 目标数组
- int destPos : 目标数组的开始起始位置
- int length : 要copy的数组的长度
题目中,要向长度为5的数组放入10个元素,所以运行到这个函数的时候,会发生越界错误,采取java默认的异常处理机制,直接抛出异常中断:
ArrayIndexOutOfBoundsException。
04 第5题
04-1 题目
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package homework_week3; /** * * @author zzrs123 */ public class Homework_week3 { /** * @param args the command line arguments */ public static void main( String[] args ) { byte a[] = new byte[2]; long b[] = new long[2]; float c[] = new float[2]; Object d[] = new Object[2]; System.out.print(a[1]+","+b[1]+","+c[1]+","+d[1]); } }
编译并执行代码,会出现哪种结果?
A. Prints: 0,0,0,null B. Prints: 0,0,0.0,null C. Prints: 0,0,0,0 D. Prints: null,null,null,null E. The code runs with no output.
04-2 解答
Answer: B
在new一个数组的时候,默认初始化为0,当然,这种初始化契合于数据类型,比如
float要初始化为0.0,
Object初始化为
null。
05 第6题
05-1 题目
1. class A { 2. public static void main(String[] args) { 3. int[ ] var1; 4. int[5] var2; 5. int[] var3; 6. int var4[]; 7. } 8. }
编译并执行代码,会出现哪种结果?
A. compile-time errors occur at line 3 B. compile-time errors occur at line 4 C. compile-time errors occur at line 5 D. compile-time errors occur at line 6 E. None of the above
05-2 解答
Answer: B
这两道题考察的都是数组的声明、分配内存和初始化,
一维数组的声明与分配内存:
数据类型 数组名[ ] ; // 声明一维数组 数组名 = new 数据类型[个数] ; // 分配内存给数组声明数组的同时分配内存:
数据类型 数组名[] = new 数据类型[个数]
06 第7题
06-1 题目
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package homework_week3; /** * * @author zzrs123 */ public class Homework_week3 { /** * @param args the command line arguments */ static void my() throws ArithmeticException { System.out.print("A"); throw new ArithmeticException("A"); } public static void main (String args []) { try { my(); } catch (Exception e) { System.out.print("B"); } finally { System.out.print("C"); } } }
编译并执行代码,会出现哪种结果?
A. Prints: A B. Prints: AC C. Prints: ABC D. Prints: AABC E. Prints: C
06-2 解答
Answer: C
考察的是
throw指定方法抛出异常的知识:
用处:
如果方法内的程序代码可能会发生异常,且方法内又没有使用任何的代码块来捕捉这些异常时,则必须在声明方法时一并指明所有可能发生的异常,以便让调用此方法的程序得以做好准备来捕捉异常。也就是说,如果方法会抛出异常,则可将处理这个异常的
try-catch-finally块写在调用此方法的程序代码内。语法:
方法名称(参数…) throws 异常类 1,异常类 2,…具体执行:
throws在指定方法中不处理异常,在调用此方法的地方处理,如果指定方法中异常,则在调用处进行处理(进入catch())
所以这道题的运行过程为:
main函数-> try()块-> my函数-> 输出A -> new ArithmeticException(“A”)-> catch()-> 输出B-> finally()-> 输出C
07 第8题
07-1 题目
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package homework_week3; /** * * @author zzrs123 */ class B extends Exception {} class C extends B {} class D extends C {} public class Homework_week3 { /** * @param args the command line arguments */ public static void main(String args[]) { int a,b,c,d,x,y,z; a = b = c = d = x = y = 0; z = 1; try { try { switch(z) { case 1: throw new B(); case 2: throw new C(); case 3: throw new D(); case 4: throw new Exception(); } a++;//a=0 } catch ( C e ) {b++;}//b=0 finally{c++;}//c=1 } catch ( B e ) {d++;}//d=1 catch ( Exception e ) {x++;}//x=0 finally {y++;}//y=1 System.out.print(a); System.out.print(b); System.out.print(c); System.out.print(d); System.out.print(x); System.out.print(y); } }
编译并执行代码,会出现哪种结果?
A. 0,0,1,1,0,1 B. 0,1,0,1,1,0 C. 0,0,1,1,0,1 D. 0,1,1,1,1,1 E. 1,1,0,1,0,0
07-2 解答
Answer: AC
这道题的亮点是异常类的层层继承,人脑运行的关键在于:如果报了一个类的错误,那么catch其父类和子类的块会不会响应。
答案是不会,
catch只能识别错误本身。
此外就是运行到
case 1: throw new B();时,程序就中断,直接进入
catch()区,不会运行
a++。
08 第9题
08-1 题目
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package homework_week3; /** * * @author zzrs123 */ 1.class B extends Exception { 2. public void myMethod( ) throws RuntimeException {} 3.} 4. 5.public class Homework_week3 extends B { /** * @param args the command line arguments */ 6. public void myMethod( ) throws Exception {} 7. public static void main (String[] args) {} 8.}
编译错误会出现在哪一行?
A. 1 B. 2 C. 6 D. 7 E. None of the above
08-2 解答
Answer: C
0330 这个问题不是很明白,还是编译错误,所以也很难从IDE得到点什么,查了查,有两点:
- 子类中覆盖方法的访问权限不能比父类小,比如父类的
myMethod( )
是public类型,子类的不能是private。 - 子类中覆盖方法抛出的异常只能比父类中原方法抛出的异常低级,比如父类抛出
Exception
,而子类的覆盖方法只能抛出RuntimeException
之类的子异常。
09 第10题
09-1 题目
class A { public static void main (String[] args) { int a=1, b=0; int c[] = {1,2,3}; try { System.out.print(c[1]); try { System.out.print(a/b+b/a); } catch (ArithmeticException e){ System.out.print(“C”); }} catch (ArrayIndexOutOfBoundsException e) { System.out.print(“A”); } finally { System.out.print(“B”); } } }
编译并执行代码,会出现哪种结果?
(A) 1BC (B) 1CB (C) 2BC (D) 2CB (E) 2AC
09-2 解答
Answer: D
这道题相对常规,是一个
try-catch-finally的嵌套问题,
System.out.print(c[1]); try { System.out.print(a/b+b/a); } catch (ArithmeticException e){ System.out.print(“C”); }
正常输出c[1] 为2,然后进入内层的 try,发现异常,捕获输出 C,接着出来进入外层 try 的catch,未捕获异常,进入外层的 finally ,输出 B 。
- 程序设计入门—Java语言 . 翁恺-第四周编程作业-2.念整数
- 用java语言编程统计程序的行数
- java第三周编程作业
- 对于java程序语言的单例设计模式讲解
- 程序设计入门—Java语言 . 翁恺-第四周编程作业-1.素数和
- Java语言编程 - Java第一个程序HelloWorld
- Java -->多语言混合编程正成为主流
- Java程序语言设计基础篇原书第十版第三章答案编程练习题
- 基于Java语言的安卓程序编程之一环境搭建2
- 《程序设计入门—Java语言.翁恺》第七周编程作业(2)-完数
- 编程代写/编程代做/编程作业代写/编程作业代做/程序开发/软件开发Python代写Python代做java代写java代做C语言代写C++代做C++代写服务器环境运维
- <Java 并发编程实践>读书笔记 --- 重排序
- Java程序语言(基础篇)第2章 基本程序设计 编程练习题解答
- 基于Java语言的安卓程序编程之二HelloWorld程序的运行
- 基于Java语言的安卓程序编程之一环境搭建1
- java并发编程实践 part 01 --> 线程创建方式
- JAVA第三周编程作业 奇偶个数 数字特征值
- Java程序语言(基础篇)第2章 基本程序设计 编程练习题解答
- JAVA技术支持课--第三次java语言技术支持授课思路 (课件及作业)
- Java程序语言(基础篇)第2章 基本程序设计 编程练习题解答