Correct Answer : Get Lastest Questions and Answer : Explanation: The first println prints variable a with value 1 and then increases the variable to 2.
Question : You have been given following code
public class Test { public static void main(String[] args) { int arr[] = new int[4]; arr[0] = 1; arr[1] = 2; arr[2] = 4; arr[3] = 5; int sum = 0; try { for (int pos = 0; pos <= 4; pos++) { sum = sum + arr[pos]; } } catch (Exception e) { System.out.println("Invalid index"); } System.out.println(sum); } }
Correct Answer : Get Lastest Questions and Answer : Explanation: The loop for (int pos = 0; pos <= 4; pos++) { it should be pos <= 3, causes an exception, which is caught. Then the correct sum is printed.