Using an Array Flashcards

1
Q

Give all the ways is going to trick you !

A

The exam will test whether you are being observant by trying to access elements that are not in the array.

EXAMPLE 1 :

int[] numbers=new int[10];
for (int i = 0; i <= numbers.length; i++) numbers[i] = i + 5;//THROWS EXCEPTION

EXAMPLE 2 :

int[] numbers=new int[10];
for (int i = 0; i < numbers.length; i++) numbers[i] = i + 5;
numbers[numbers.length] = 5;
//THROWS AN EXCEPTION

EXAMPLE 3 :

int[] numbers=new int[10];

for (int i = 0; i < numbers.length; i++) numbers[i] = i + 5;

numbers[10] = 3;//THROWS AN EXCEPTION

How well did you know this?
1
Not at all
2
3
4
5
Perfectly