• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Arrays

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A.0
B.null
C.compilation error at line 5
D.compilation error at line 6.
E.compilation error at line 7.
F.An exception is thrown at runtime.

Answer Given:
Dis correct It is legal to create array of four integers named 'array'

Did'nt understand the logic here.

Thanks in Advance!!
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Am sorry, I do not understand the answer given and especially the explanation for that.

There can not be any compilation errors here!
Actually, given code does not have appropriate main method to execute this class. Even if main exist, it would throw NullPointerException since array is not initialized.
So, F could be the only possible answer from the options provided for the given code.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
It will throw a NullPointerException while printing the value of a1[1][0].
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


There can not be any compilation errors here!
Actually, given code does not have appropriate main method to execute this class. Even if main exist, it would throw NullPointerException since array is not initialized.
So, F could be the only possible answer from the options provided for the given code.



I absolutely agree with ArulKumar's explanation.
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Smitha,

I agree that it is a bit tricky question.But it is simple if u look at the code carefully.

  • Both array a1 and a2 are declared of the same type(long) & same

  • dimension - no probs here
  • Coming to line 5 array a2 is created using new operator.But only no of rows is specified. - this is also permissible
  • In line 6 ... hmm..here the pblm starts - array a2 is assigned to a1 - no pblm at all - but where did u define the array a1 or create it using the new operator?.
  • Do you think java compiler would create the one by itself?.No u've to create or define the array using new operator!!
  • So obviously compilation would fail at line 6 itself , then no chance for runtime exception at line 7.
  • So the last point u need to keep in mind is "it is not only enough if you declare an array u also need to define it before using it".



  • Sherry & Arul,

    Here is the explanation for ur doubt.

  • There is no necessity / restriction for any class to have one such main method as u would expect.
  • Only thing is we cant execute the pgm using java command.First of all this pgm wont get compiled.Then how it'll ever run even if it has main method?.
  • In the above pgm no harm in having that main method.Only thing it'll not be recognized when JVM invokes.
  • JVM will always look for "public static void main(String[] <name can be anything> ".U can also note that in the above method no parameters are passed.



  • Hope all ur doubts are clear!!

    If any more revert to me..

    Regards,
    Priya.
     
    Ranch Hand
    Posts: 88
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello Prija,
    in the above code, u said, the compiler will fail at line 6. But, that is wrong i think. Because, you are creating memory with new operator for variable a2, and you are assaigning it to a1 means,

    a2=new long[3][];
    a1=a2;
    here in the above case, we are assigning the a2 reference to a1. so that a1 now also point to the memory allocated by a2.
    here the problem is due to, we have created memory with a2=new long[3][]; means we did not specified the second parameter. the compiler wont take any default value. so if u try to access the argument like a1[1][0] or a2[1][0], we will get the null pointer exception.

    pls let me know if u would have any questions.
    sri.
     
    Priya Jothi
    Ranch Hand
    Posts: 168
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Sri,

    Thanks for ur reply!!.I've just now verified it.Yes u r right!!.

    Sorry guys for my post.

    If this method is called by some other method it'll give null pointer exception.

    Smitha,

    This question is wrong.You can post this question to errata.

    Regards,
    Priya.
     
    Arulkumar Gopalan
    Ranch Hand
    Posts: 104
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Priya,

    Feedback on your postings/interpretation of questions will be provided in a private message, if and only if you are interested (Definitely not a compulsion though).

    Unfortunately, I could not send private message to you since you have not selected that option in Javaranch. Please kindly let me know if you would like to receive the same.

    My e-mail id: [email protected]

    Regards,
    Arul
     
    Smitha Ballikar
    Ranch Hand
    Posts: 99
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Priya/Arul,


    Thanks!! I understood the concept here.I also wanted to know which are the good mock exams in javaranch mock exam list which i should take up before giving the exam.I have scheduled my eam end of this month,i am not scoring good so a bit tensed..

    Please let me know your views..

    Cheers!!
     
    Ranch Hand
    Posts: 287
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I changed code to this



    instead of printing a1[1][0], I let it print a1[1], then there is no runtime error, the output is null. Can someone explain this? Thanks.
     
    Arulkumar Gopalan
    Ranch Hand
    Posts: 104
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Reference for the rows of the array is already created and compiler is aware of the same. You are printing the second row which does not have any value. So, null is getting printed.

    But, incase if the array declaration is complete as below, you would not get null.
    int i[][] = new int[2][2];
     
    reubin haz
    Ranch Hand
    Posts: 287
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Arulkumar Gopalan:
    You are printing the second row which does not have any value.


    Im wondering what exact action is done at here. If only part of dimension length is given, then does it mean null is assigned to each inner array object?

    So, null is getting printed.


    why printing null, why its not getting null pointer exception?
    I think:
    In case its a2=new long[3][];
    and ----- if we print a2[1], it prints null because a2[1] returns null object.
    but ----- if we print a2[1][0], its trying to access the member associated with that null array object, thus a null pointer exception occurs.

    Hope I'm right
    [ August 12, 2005: Message edited by: reubin yi ]
     
    Priya Jothi
    Ranch Hand
    Posts: 168
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Smitha,

    Be cool..Need not get tensed.Mock exam is basically to identify ur weaker areas & strengthen it up.So nothing to worry..U r allowed to make mistakes but not to repeat it.This is what expected in the mock exam.

    U can refer to mock exams given in K&B CD & also the following links.

    http://danchisholm.net/
    http://www.javaprepare.com/links.html

    Hope u'll find it useful!!

    Good luck!!!

    Regards,
    Priya.
     
    Smitha Ballikar
    Ranch Hand
    Posts: 99
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Priya,

    Thanks for your advice !!

    Cheers Smitha
     
    reply
      Bookmark Topic Watch Topic
    • New Topic