venerdì 29 marzo 2013

Q89

Given the code fragment:
ArrayList<Integer> list = new ArrayList<>(1);
list.add(1001);
list.add(1002);
System.out.println(list.get(list.size()));
What is the result?
A.
Compilation fails due to an error on line 1.
B.
An exception is thrown at run time due to error on line 3
C.
An exception is thrown at run time due to error on line 4
D.
1002

Risposta : ci ha una eccezione perchè si cerca di recuperare l'elemento di indice list.size (in questo caso 2). Però gli elementi partono da 0, quindi si scatena una java.lang.IndexOutOfBoundsException .

Riferimento : dichiarazione di arraylist
 

Nessun commento:

Posta un commento