1. public class Test { 2. public static String output =””; 3.  4. public static void foo(int i) { 5. try { 6. if(i==1) { 7. throw new Exception(); 8. } 9. output += “1”; 10. } 11. catch(Exception e) { 12. output += “2”; 13. return; 14. } 15. finally { 16.

题目

1. public class Test { 2. public static String output =””; 3.  4. public static void foo(int i) { 5. try { 6. if(i==1) { 7. throw new Exception(); 8. } 9. output += “1”; 10. } 11. catch(Exception e) { 12. output += “2”; 13. return; 14. } 15. finally { 16. output += “3”;17. } 18. output += “4”; 19. } 20.  21. public static void main(String args[]) { 22. foo(0); 23. foo(1); 24.  25. }26. } What is the value of the variable output at line 23?()

参考答案和解析
正确答案:13423
如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

1. public class a {  2. public void method1() {  3. try {  4. B b=new b();  5. b.method2();  6. // more code here  7. } catch (TestException te) {  8. throw new RuntimeException(te);  9. }  10. }  11. }  1. public class b {  2. public void method2() throws TestException {  3. // more code here  4. }  5. }  1. public class TestException extends Exception {  2. }  Given:  31. public void method() {  32. A a=new a();  33. a.method1();  34. }  Which is true if a TestException is thrown on line 3 of class b?()

  • A、 Line 33 must be called within a try block.
  • B、 The exception thrown by method1 in class a is not required to be caught.
  • C、 The method declared on line 31 must be declared to throw a RuntimeException.
  • D、 On line 5 of class a, the call to method2 of class b does not need to be placed in a try/catch block.

正确答案:B

第2题:

1. public class SwitchTest {  2. public static void main (String []args)  {  3. System.out.PrintIn(“value =” +switchIt(4));  4. }  5. public static int switchIt(int x)  {  6. int j = 1;  7. switch (x) {  8. case 1: j++;  9. case 2: j++;  10. case 3: j++;  11. case 4: j++;  12. case 5: j++;  13. default:j++;  14. }  15. return j + x;  16. }  17. }     What is the output from line 3?()  

  • A、 Value = 3
  • B、 Value = 4
  • C、 Value = 5
  • D、 Value = 6
  • E、 Value = 7
  • F、 Value = 8

正确答案:F

第3题:

1. class A {  2. public int getNumber(int a) {  3.     return a + 1;  4. }  5. }  6.    7. class B extends A {  8. public int getNumber (int a) {  9. return a + 2  10. }  11.    12. public static void main (String args[])  {  13. A a = new B();  14. System.out.printIn(a.getNumber(0));  15.    } 16. }     What is the result?()  

  • A、 Compilation succeeds and 1 is printed.
  • B、 Compilation succeeds and 2 is printed.
  • C、 An error at line 8 causes compilation to fail.
  • D、 An error at line 13 causes compilation to fail.
  • E、 An error at line 14 causes compilation to fail.

正确答案:B

第4题:

public class Test {  public static void aMethod() throws Exception {  try {  throw new Exception(); } finally {  System.out.println(“finally”);  }  }  public static void main(String args[]) {  try {  aMethod();  } catch (Exception e) {  System.out.println(“exception”);  }  System.out.println(“finished”);  }  }  What is the result?()  

  • A、 finally
  • B、 exception finished
  • C、 finally exception finished
  • D、 Compilation fails.

正确答案:C

第5题:

1. class Pizza {  2. java.util.ArrayList toppings;  3. public final void addTopping(String topping) {  4. toppings.add(topping); 5. }  6. }  7. public class PepperoniPizza extends Pizza {  8. public void addTopping(String topping) {  9. System.out.println(”Cannot add Toppings”);  10. }  11. public static void main(String[] args) {  12. Pizza pizza = new PepperoniPizza();  13. pizza.addTopping(”Mushrooms”);  14. }  15. }  What is the result?() 

  • A、 Compilation fails.
  • B、 Cannot add Toppings
  • C、 The code runs with no output.
  • D、 A NullPointerException is thrown in Line 4.

正确答案:A

第6题:

1. public class A {  2. void A() {  3. System.out.println(“Class A”);  4. }  5. public static void main(String[] args) {  6. new A();  7. }  8. }  What is the result?()  

  • A、 Class A
  • B、 Compilation fails.
  • C、 An exception is thrown at line 2.
  • D、 An exception is thrown at line 6.
  • E、 The code executes with no output.

正确答案:E

第7题:

1. class A {  2. public byte getNumber ()  {  3.   return 1;  4.   }  5. }  6.    7. class B extends A {  8. public short getNumber()  {  9.  return 2;  10. }  11.    12. public static void main (String args[]) {   13.    B  b = new B ();  14.      System.out.printIn(b.getNumber())     15.   }  16. }    What is the result?()  

  • A、 Compilation succeeds and 1 is printed.
  • B、 Compilation succeeds and 2 is printed.
  • C、 An error at line 8 causes compilation to fail.
  • D、 An error at line 14 causes compilation to fail.
  • E、 Compilation succeeds but an exception is thrown at line 14.

正确答案:C

第8题:

1. public class SimpleCalc {  2. public int value;  3. public void calculate() { value += 7; }  4. } And:  1. public class MultiCalc extends SimpleCalc {  2. public void calculate() { value -= 3; }  3. public void calculate(int multiplier) {  4. calculate();  5. super.calculate();  6. value *=multiplier;  7. }  8. public static void main(String[] args) {  9. MultiCalc calculator = new MultiCalc();  10. calculator.calculate(2);  11. System.out.println(”Value is: “+ calculator.value);  12. }  13. }  What is the result?() 

  • A、 Value is: 8
  • B、 Compilation fails.
  • C、 Value is: 12
  • D、 Value is: -12
  • E、 The code runs with no output.
  • F、 An exception is thrown at runtime.

正确答案:A

第9题:

11. static class A {  12. void process() throws Exception { throw new Exception(); }  13. }  14. static class B extends A {  15. void process() { System.out.println(”B”); }  16. }  17. public static void main(String[] args) {  18. new B().process();  19. }  What is the result?() 

  • A、 B
  • B、 The code runs with no output.
  • C、 Compilation fails because of an error in line 12.
  • D、 Compilation fails because of an error in line 15.
  • E、 Compilation fails because of an error in line 18.

正确答案:A

第10题:

1. package foo;  2.    3. import java.util.Vector;  4.    5. private class MyVector extends Vector {  6. int i = 1;  7. public MyVector()  {  8. i = 2;  9.    }  10. }  11.    12. public class MyNewVector extends MyVector {  13. public MyNewVector ()  {  14. i = 4;  15. }  16. public static void main (String args [])  {  17. MyVector v = new MyNewVector();  18.   }  19. }     The file MyNewVector.java is shown in the exhibit.  What is the result?()  

  • A、 Compilation will succeed.
  • B、 Compilation will fail at line 5.
  • C、 Compilation will fail at line 6.
  • D、 Compilation will fail at line 14.
  • E、 Compilation will fail at line 17.

正确答案:B

更多相关问题