首页 > 试题广场 >

What is the output of the foll

[问答题]

What is the output of the following program? And explain why.

public class Testing {
      public static void main(String[] args) {
        	int a [] = { 0, 0, 0 };
         	update(a, a[2]);
         	try {   System.out.println(a[2]/a[1]); }
catch(Exception e) { System.out.println("Exception!"); }		 		finally {  System.out.println("Finally!"); }
      }
      static int update(int x[], int y) {
          for (int i = 1; i < x.length; i++)
               if (y < x.length)  x[i] = x[i - 1] + 1;
             return x[1];
      }
}

调用update方法,传回一个数组x,此时a[2]=x[2]=2,a[1]=x[1]=1,即输出1,finally内的语句无论如何都要输出的,即输出1和Finally!
发表于 2021-07-15 13:20:50 回复(0)
首先调用update方法输出,返回值是1。然后下面0/0抛异常,输出catch内容,最后输出finally内容。
发表于 2021-07-13 17:37:28 回复(0)