• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mock Exam Array question

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this question on Dan Chrisholm's mock exam:
http://www.danchisholm.net/apr18/topic/section1/arrays1.html
Questions 17

What is the result of attempting to compile and run the above program?
I didn't understand why it gives a runtime-exception, and the answer posted, can anyone put it in simpler terms please
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, let me try to make this easy to understand - I'll do my best.
a3 references a 3 dimensional array. Another way to look at a a 3-D array is as a single dimensional array in which each element of the array references a 2-D array. Think of it like this (forgive the poor ASCII art):

However, in this question, in line 7, you're assigning each of the elements of a3 to a reference to a 2-D array which has dimensions 2x1. Therefore, your picture looks something like this now:

Notice that now each element of a3 references another array which has dimensions 2x1. I've also written in the index values for each of the elements indexed by a3[2]. As you can see, the array element a3[2][2][2] no longer exists! That's why you end up with the run-time exception.
I hope that helps,
Corey
 
David Attard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent Thanks a lot!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic