3. What is the output of the program below, state if there will be an exception?
ID: 3719218 • Letter: 3
Question
3. What is the output of the program below, state if there will be an exception? If so, state
what type is it, and show how to handle it.
class Class1 {
public static void main(String args[]) {
int total = 0;
int[] i = new int[3];
for(int j=1;j<= i.length; j++)
total += (i[j] = j);
System.out.println(total);
}
}
4. What is the output of the program below:
try {
int values[] = {1,2,3,4,3,2,1};
for (int i = values.length-1; i >= 0; i++)
System.out.print( values[i] + " " );
} catch (Exception e) {
System.out.print("2" + " ");
} finally {
System.out.print("3" + " ");
}
Explanation / Answer
Question 3
Answer:
The above code will throw an ArrayIndexOutOfBoundsException. we can handle like below
public class Class1 {
public static void main(String[] args) {
int total = 0;
int[] i = new int[3];
for(int j=0;j< i.length; j++)
total += (i[j] = j);
System.out.println(total);
}
}
Output:
3
Question 4
Answer:
Output is
1 2 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.