Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Encapsulation and Arrays `Index Out of Range

ujz ujz
PLUS
ujz ujz
Courses Plus Student 6,664 Points

Error in quiz "Arrays"

In the 3 question part quiz, one of the questions asks "what will happen when we run this code" type of thing. The first array is called "primes" and the second one tries to call an array called "a." This is confusing.

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi! Well let's take a closer look at that question:

//What will happen when the following code runs. 
  int[] primes = {1, 3, 5, 7, 9};
  int fifthPrime = a[5];

And you're correct, that à should be primes, I believe. But of the given choices even if we made that change, there would only be one correct answer and that's a[5] will throw System.IndexOutOfRangeException. The logic here being that it will be out of bounds for the size of the array given. Because arrays are 0 indexed in C# (meaning they start with an index of 0) it means that the 9 is in the space primes[4]. There is no primes[5]. Hope this helps! :sparkles:

ujz ujz
PLUS
ujz ujz
Courses Plus Student 6,664 Points

Thank you Jennifer! What your achievements are impressive

Andrei Oprescu
Andrei Oprescu
9,547 Points

By the way, 1 is not prime, 2 is prime and 9 is not prime ;). maybe they meant odds.