Question
What will be the output of the following Java code
snippet, which implements a simple ArrayList and performs an insertion and a retrieval? import java.util.*;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â public class TestArrayList { Â Â Â public static void main (String[] args) { Â Â Â Â Â Â Â ArrayList list = new ArrayList (); Â Â Â Â Â Â Â list.add( 10 ); Â Â Â Â Â Â Â list.add( 20 ); Â Â Â Â Â Â Â list.add( 30 ); Â Â Â Â Â Â Â list.add( 1 , 15 );Â // Inserting 15 at index 1 Â Â Â Â Â Â Â System.out.println(list.get( 2 )); Â Â Â }}Solution
In this code, an ArrayList is created and populated with three elements. The add() method is used to insert 15 at index 1. This insertion shifts the other elements accordingly. Let's break down why the correct output is B and explain why the other options are incorrect: ·        Explanation of Correct Option (B): Initially, the ArrayList contains [10, 20, 30] . When list.add(1, 15) is executed, it inserts the value 15 at index 1 . This causes the elements at index 1 and 2 (i.e., 20 and 30 ) to be shifted one position to the right. After the insertion, the list looks like this: [10, 15, 20, 30] . The call to list.get(2) retrieves the element at index 2 , which is now 20 . ·        Why the Other Options Are Incorrect:
- Â 15: 15 is inserted at index 1 , not index 2 . So, list.get(2) will not return 15 .
- 10: The element at index 0 is 10 , but the retrieval happens at index 2 , so it will not return 10 .
- Â 30: After the insertion, 30 is at index 3 , not 2 , so it will not be retrieved by list.get(2) .
- Â Runtime exception: There is no runtime exception in this code. All list operations are performed within valid bounds.
During his visit to Italy in May 2026, PM Narendra Modi was honoured with the Agricola Medal, which is the highest honour of which organisation?
Which new benchmark has the RBI introduced to replace MIBOR?
Who among the following has been honoured with Indian Film Personality of the Year at 53rd IFFI?
Which financial institution's rating forecast for India's GDP growth for FY25 was raised to 7%?
Rearranging the letters "AKBLU", we get the capital of ______
- How many government schools were assessed under ASER 2024?
Which logistics company  has joined ONDC (Open Network for Digital Commerce), enabling Pan India delivery services to over 20,000 pin codes?Â
...What is the theme of the 81st plenary meeting of the International Cotton Advisory Committee (ICAC)?
BimaKavach has received a direct insurance broking licence from the Insurance Regulatory and Development Authority of India (IRDAI), enabling the firm t...
As per Morgan Stanley’s projections in April 2026, what is India’s Current Account Deficit (CAD) expected to be as a percentage of GDP in FY27?