Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Need some help with multi dimensional array (K&B7)

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given:

What is the result? (Choose all that apply.)
A. 2
B. 4
C. An exception is thrown at runtime
D. Compilation fails due to an error on line 4
E. Compilation fails due to an error on line 5
F. Compilation fails due to an error on line 6
G. Compilation fails due to an error on line 7

can any one tell me what the above code is actually doing. i am having trouble from line 4  
 
Marshal
Posts: 65002
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please start by posting the code correctly indented without line numbers; the code tags supply their own line numbers. You can confuse yourself badly by posting incorrectly indented code. Also always say where such code comes from.

What do you think the result would be?
Remember that if the code compiles at all, you can get 2 or 4 or an Exception. You cannot get more than one of those results.
Now work out carefully what the types of all the variables in lines 4‑7 are and whether the casts can be accepted at compile time.
 
fahmida tasnim prema
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am sorry for that .i will make sure it wont happen again . can you give me some links or book reference where i can know more about array casting? its new to me
 
fahmida tasnim prema
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code is from Kathy Sierra oca study guide book
 
Ranch Hand
Posts: 451
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code say:

a[0] = {1,2}
a[1]= {3,4}

b is one dimensional array so b= {3,4}

a2 is two dimensional array

b2 is  one dimensional array , o1 is two dimensional array .

My opinion is the corect answer is C.

The line 7 is the trouble because you can't cast a two dimensional array to one dimensional array.

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fahmida tasnim prema wrote:can any one tell me what the above code is actually doing. i am having trouble from line 4


Here you'll find a very useful topic about multidimensional arrays with lots of info, examples and drawings.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fahmida tasnim prema wrote:can any one tell me what the above code is actually doing. i am having trouble from line 4


In this topic you'll find an explanation about the same question. and using the search engine of these forums (using search term "dims"), you'll find plenty of topics about this question.
 
fahmida tasnim prema
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!