第三周作業,可能是例外那一章當時沒怎么聽,此前也不怎么接觸,感徑訓挺陌生的,
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題
/*
* 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 shandaiwang
*/
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 ,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/452820.html
標籤:Java
上一篇:JScrollPane做到點擊哪里,修改哪里,并且知道修改的位置
下一篇:【JavaWeb-jQuery】筆記(3)--- jQuery中給dom物件系結事件;通過jQuery實作Ajax請求的處理
