Lists of Opportunities
A List<Opportunity> keeps records in order: add puts one on the end, [i] reads position i starting at 0, and size() counts them.
Your task
Put two Opportunities in a List, change one through the List, and read items only at safe positions.
List<Opportunity> opportunities = new List<Opportunity>();
Opportunity renewal = new Opportunity(
Name = 'Renewal', StageName = 'Prospecting', CloseDate = Date.newInstance(2027, 6, 30)
);
Opportunity expansion = new Opportunity(
Name = 'Expansion', StageName = 'Prospecting', CloseDate = Date.newInstance(2027, 9, 30)
);
// Pick at 1
Integer requestedIndex = 1;
String picked = null;
// Pick at 2
requestedIndex = 2;
picked = null;
// Pick at -1
requestedIndex = -1;
picked = null;Requirements
First name: Renewalshould be printedList size: 2should be printedRenewal stage: Qualificationshould be printedPick at 1: Expansionshould be printedPick at 2: noneshould be printedPick at -1: noneshould be printed