Accenture Accenture Primer Practice Question
Predict the output What will be the output for the given code snippet startprogram Public class Main{ public static void main(String args[]) { int arr[] = new int[] {0,1,2,3,4,5,6,7,8,9}; int n=6; n = arr[arr[n] / 2]; System.out.println(arr[n] / 2); } } endprogramAnswer options
A
2
B
1
C
6
D
0
Correct answer: 1
Explanation
arr={0..9}, n=6. Step 1: n = arr[arr[6]/2] = arr[6/2] = arr[3] = 3. Step 2: System.out.println(arr[3]/2) = 3/2 = 1 (integer division).