What will be the value of arr[2] in the following code? String[] arr = new…
2024
What will be the value of arr[2] in the following code? String[] arr = new String[3]; arr[0] = "Maths"; arr[1] = "Science";
- A.
Null
- B.
"Science"
- C.
"Maths"
- D.
" "
- E.
Question not attempted
Attempted by 990 students.
Show answer & explanation
Correct answer: A
Step 1: The array arr is declared as String[] arr = new String[3];, which creates an array of size 3 with indices 0, 1, and 2.
Step 2: arr[0] is assigned "Maths" and arr[1] is assigned "Science". However, arr[2] is not assigned any value.
Step 3: In Java, when a String array is created, all elements are initialized to null by default if not explicitly assigned.
Step 4: Since arr[2] is not assigned, it retains its default value, which is null.