• 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

ArrayOutofbound

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

One of the question from a mock test of examguide online is ..........

class C {

public static void main (String[] a1) {

System.out.print(a1[1] + a1[2] + a1[3]);

}}

What is the result of attempting to compile and run the program?


java command A B C

1.Prints: ABC

2.Prints BC and Runtime Exception

3.Prints: BCD

4.Runtime Exception

5.None of the above


The given answer for this Answer 2(array index outof bounds exception only till a1[2] is allowed.)
but I think the answer for this 4.runtime exception. when i tried to execute array out of bound exception has arrivied, with no output as BC. So how can one justify the answer to be no.2? please explain.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Usha,

The answer given is wrong. It should be 4. You may want to inform the author of examguide about this error.

Joyce
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
While executing this code as java c a b c
i am getting runtime exception ... Idealy the first system.out.print should print b , c rite .. Pls explain me ...why it is not printing ....




Thanks and Regards
Prasanna
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a hint:

 
newgen pras
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tony ...

Thanks for ur reply...but still i am not clear ...
will flush the stream , so now those parameters will get printed ..

My doubt is if that program is runned as

it will execute rite .. in that case when will be that System.out.flush() will get called ?? I am bit confused ..pls explain me

Thanks and Regards
G.Prasanna
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic