Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

java homework for grad student, Assignments of Computer Science

Attendance Manager Attendance Manager Android Application Project | Android Project

Typology: Assignments

2020/2021

Uploaded on 04/04/2021

mike-usa
mike-usa 🇺🇸

2 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
JAVA Programming Language Homework III Array, Exception
Student ID: Name:
1. Given the following Java code:
1. class C {
2. public static void main(String[] args) {
3. int a=0, b=5;
4. try {
5. System.out.print(a/b+b/a);
6. } catch {
7. System.out.println(“Exceptions!!!”);
8. }
9. }
10. }
What is the result of attempting to compile the program?
A. Prints: Exceptions!!!
B. Prints Nothing
C. Compiler Error
D. Runtime Error
E. None of the above
Answer: C
題目中雖然撰寫了 catch 區塊,但並未給予例外類別名稱與建立例外實例,如:
catch(ArithmetException e),所以編譯時即會發生錯誤的情形。
2. Given the following Java code:
1. class A {
2. public static void main(String[] args) {
3. int a=0, b=5;
4. String c[] = {“A”, “B”, “C”};
5. try {
6. for(int i=1; i<4; i++) {
7. System.out.print(c[i]);
8. }
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download java homework for grad student and more Assignments Computer Science in PDF only on Docsity!

JAVA Programming Language Homework IIIArray, Exception Student ID: Name:

  1. Given the following Java code:
  2. class C {
  3. public static void main(String[] args) {
  4. int a=0, b=5;
  5. try {
  6. System.out.print(a/b+b/a);
  7. } catch {
  8. System.out.println(“Exceptions!!!”);
  9. }
  10. }
  11. }

What is the result of attempting to compile the program? A. Prints: Exceptions!!! B. Prints Nothing C. Compiler Error D. Runtime Error E. None of the above

Answer: C

題目中雖然撰寫了 catch 區塊,但並未給予例外類別名稱與建立例外實例,如: catch(ArithmetException e),所以編譯時即會發生錯誤的情形。

  1. Given the following Java code:
  2. class A {
  3. public static void main(String[] args) {
  4. int a=0, b=5;
  5. String c[] = {“A”, “B”, “C”};
  6. try {
  7. for(int i=1; i<4; i++) {
  8. System.out.print(c[i]);
  9. }
  1. System.out.print(a/b+b/a);
  2. }
  3. catch(ArithmetException e) {
  4. System.out.println(“D”);
  5. }
  6. catch(ArrayIndexOutOfBoundsException e) {
  7. System.out.println(“E”);
  8. }
  9. }
  10. }

What is the result of attempting to compile the program? A. Prints: ABC B. Prints: ABD C. Prints: BCE D. Prints: BCDE E. Compiler Error

Answer: C

c[1] = "B", c[2] = "C", c[3] = 超出陣列索引

  1. Given the following Java code:
  2. class A {
  3. public static void main(String[] args){
  4. int a=0, b=5;
  5. String c[] = {“A”, “B”, “C”};
  6. try {
  7. System.out.print(c[a/b]);
  8. try {
  9. for(int i=0; i<4; i++) {
  10. System.out.print(c[i]);
  11. }
  12. }
  13. catch (Exception e)
  14. {
  1. int res[] = {1, 2, 3, 4, 5};
  2. System.arraycopy(src, 0, res, 0, src.length);
  3. for(int i=0; i<res.length; i++) {
  4. System.out.print(res[i]);
  5. }
  6. }
  7. }

What is the result? A. 10987654321 B. 10987612345 C. 12345612345 D. Compiler error E. Runtime exception

Answer: E

呼叫 arraycopy()時,來源陣列必須比目的陣列元素個數來的少,反則會發生超 出陣列索引的情形。

  1. Given the following Java code:
  2. class A {
  3. public static void main (String[] args) {
  4. byte a[] = new byte[2];
  5. long b[] = new long[2];
  6. float c[] = new float[2];
  7. Object d[] = new Object[2];
  8. System.out.print(a[1]+”,”+b[1]+”,”+c[1]+”,”+d[1]);
  9. }
  10. }

What is the result? A. Prints: 0,0,0,null B. Prints: 0,0,0.0,null C. Prints: 0,0,0, D. Prints: null,null,null,null

E. The code runs with no output.

Answer: B

此題四個資料型態各宣告了 1 維陣列,並配置 2 個陣列元素空間,並且沒有給予 初始值,故此題目的在於測驗各資料型態的預設初始值情形。

  1. Given the following Java code:
  2. class A {
  3. public static void main(String[] args) {
  4. int[ ] var1;
  5. int[5] var2;
  6. int[] var3;
  7. int var4[];
  8. }
  9. }

What is the result? 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

Answer: B

若要指定元素個數不可在[]裡寫數值,而是以 new 的方式配置: int[] var2 = new int[5];

  1. Given the following Java code:
  2. class A {
  3. static void my() throws ArithmeticException {
  4. System.out.print(“A”);
  5. throw new ArithmeticException(“A”);
  1. a = b = c = d = x = y = 0;
  2. z = 1;
  3. try {
  4. try {
  5. switch(z) {
  6. case 1: throw new B();
  7. case 2: throw new C();
  8. case 3: throw new D();
  9. case 4: throw new Exception();
  10. }
  11. a++;
  12. }
  13. catch ( C e ) {b++;}
  14. finally{c++;}
  15. }
  16. catch ( B e ) {d++;}
  17. catch ( Exception e ) {x++;}
  18. finally {y++;}
  19. System.out.print(a+”,”+b+”,”+c+”,”+d+”,”+x+”,”+y);
  20. }
  21. }

What is the result? A. 0,0,1,1,0, B. 0,1,0,1,1, C. 0,0,1,1,0, D. 0,1,1,1,1, E. 1,1,0,1,0,

Answer: A

  • 初始化 a, b, c, d, x, y 為 0,z 為 1
  • 進入 switch,case 1,丟出例外 B
  • catch(C e)無法捕捉,進入第二層的 finally,執行 c++: c=
  • 將例外往上丟
  • 被 catch (B e)捕捉,執行 d++: d=
  • 略過 catch (Exception e),離開第一層的 try
  • 進入第一層的 finally,執行 y++: y= 故結果為: 0,0,1,1,0,
  1. Given the following Java code:
  2. class B extends Exception {
  3. public void myMethod( ) throws RuntimeException {}
  4. }
  5. class A extend B{
  6. public void myMethod( ) throws Exception {}
  7. public static void main (String[] args) {}
  8. }

Compile-time errors occur at which lines? A. 1 B. 2 C. 6 D. 7 E. None of the above

Answer: C

由於所丟出的 Exception 類別比原來方法所丟出的 RuntimeException 的涵蓋範 圍還大,所以在 A 類別裡覆寫 myMethod( )是不合法的作法。

  1. Given the following Java code:
  2. class A {
  3. public static void main (String[] args) {
  4. int a=1, b=0;
  5. int c[] = {1,2,3};
  6. try {
  7. System.out.print(c[1]);
  8. try {
  9. System.out.print(a/b+b/a);
  10. }
  11. catch (ArithmeticException e)
  12. {
  13. System.out.print(“C”);
  14. }