• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Problem with arrays

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
Found this question at the site: http://www.examsonline.com and I find it hard to understand why answer D is correct since answer A is wrong. Can anyone please explain to me?
QWhat are valid ways of declaring and initializing arrays? Select all that apply.
A int [ ] arr = new int [5] {1, 2, 3, 4, 5};
Bint arr [5] = new int [ ] {1, 2, 3, 4, 5};
Cint arr [ ] [ ] = new int [5] [ ] {1, 2, 3, 4, 5};
Dint arr [ ] [ ] = new int [5] [5] {{1, 2, 3, 4, 5},
{1, 2, 3, 4, 5}};
Eint [ ] arr [ ] = new int [25];
The correct answers is C, D, E according to the site I mentioned above.
And by the way, I�ve got one more question to you:
I assume that the declaration
int arr [][] = //some code;
means the same as
int[] arr[]= //some code;
Is that correct??
/Linda
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Linda,
you bring up some really good points, I am sure you will get more answers. I didnt realize answer d was correct. and i am real curious about answer e. I think both are probably ok.
your assumption is absolutely correct:
int[][]b = int[]b[] = int b[][]
and you can add as many spaces as you want anywhere.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think in the above case only option E is fine. When you
initialize during declaration, the compiler accepts only when the number of elements in the RHS is blank.
So it would be right to say
int [ ] arr = new int[] {1, 2, 3, 4, 5} but not
int [ ] arr = new int[5] {1, 2, 3, 4, 5};
Same holds good for D also. Pls correct me if I am wrong.
I tried the above with JDK1.2.2
Thanks
MKBala...
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have tried all the examples given above and the only one that works is A. According to my JDK1.2 compiler your can never specify a size when initializing using anonymous arrays. Therefore, B, C, and D are not correct. JDK1.2 gives error messages for each of those cases.
Answer E is not correct because you are trying to assign an int[] to an int[][]. JDK1.2 compiler will not let that happen!
Make sure the test you have is for JDK1.2 and not JDK1.1. I don't think it will make a difference, but you never know ...
Regards,
Manfred.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Linda,
None of them above were correct
A int [ ] arr = new int [5] {1, 2, 3, 4, 5}; --> Incorrect
Reason: {1, 2, 3, 4, 5} creates an array and initializes its elements to the specified values.
Corrected form int [ ] arr = new int [] {1, 2, 3, 4, 5};
B int arr [5] = new int [ ] {1, 2, 3, 4, 5}; --> Incorrect
Reason: arr [5] should not have dimention in it.
Corrected form int [ ] arr = new int [] {1, 2, 3, 4, 5};
C int arr [ ] [ ] = new int [5] [ ] {1, 2, 3, 4, 5}; --> Incorrect
Reason: This is a two dimensional array. Initialization should be different
Corrected form int arr [ ] [ ] = new int [ ] [ ] {{1, 2, 3, 4, 5},{1,2,3,4,5}};
D int arr [ ] [ ] = new int [5] [5] {{1, 2, 3, 4, 5},
{1, 2, 3, 4, 5}}; -->Incorrect
Reason: If you define size don't initialize with {{1, 2, 3, 4, 5},
{1, 2, 3, 4, 5}};
Corrected form : Either remove size or initialization
int arr [ ] [ ] = new int [] []{{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}};
or
int arr [ ] [ ] = new int [5] [5];
E int [ ] arr [ ] = new int [25]; --> Incorrect
Reason: In compatible declaration
Corrected form:
int [ ] arr = new int [25];
or
int arr [ ] = new int [25]

[This message has been edited by venu gopal (edited January 19, 2001).]
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks !
I am with venu Gopal. None of the answers are correct
 
Ranch Hand
Posts: 1874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi , Linda , Welcome to Javaranch.
Proper Names are Now Required. Linda javaranch has got naming policy which everybody has to strictly follow. Please Re register yourself with proper last name & enjoy sharing wealth of knowledge.
Your Friendly Bartender
Shailesh.

[This message has been edited by shailesh sonavadekar (edited January 19, 2001).]
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic