• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

just a problem....H E L P ! ! ! !

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which code fragments will print the last argument given on the command line to the standard output, and exit without any output and exceptions if no arguments are given?
1.
public static void main(String args[ ])
{
if (args.length != 0) System.out.println(args[args.length-1]);
}

2.
public static void main(String args[ ])
{
try { System.out.println(args[args.length-1]); }
catch (ArrayIndexOutOfBoundsException e) { }
}
3.
public static void main(String args[ ])
{
int i = args.length;
if (i != 0) System.out.println(args[i-1]);
}

4.
public static void main(String args[ ])
{
int i = args.length-1;
if (i > 0) System.out.println(args[i]);
}

5.
public static void main(String args[ ])
{
try { System.out.println(args[args.length-1]); }
catch (NullPointerException e) {}
}
Answer is given :
code 1
code 2
code 3

BUT ..how ??? code 1 and 3 r not creating any exception if args value is 0. I am not understading the overall concept of this question.
pls...any1 explain this 2 me.
THANKS IN ADVANCE.
<marquee> ratul banerjee </marquee>
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.
public static void main(String args[ ])
{
if (args.length != 0) System.out.println(args[args.length-1]);
}
3.
public static void main(String args[ ])
{
int i = args.length;
if (i != 0) System.out.println(args[i-1]);
}
If args[] is empty, args.length returns 0. args.length can not throw an exception since at the very least args[] is an empty array.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ratul banji:
Which code fragments will print the last argument given on the command line to the standard output, and exit without any output and exceptions if no arguments are given?
1.
public static void main(String args[ ])
{
if (args.length != 0) System.out.println(args[args.length-1]);
}

2.
public static void main(String args[ ])
{
try { System.out.println(args[args.length-1]); }
catch (ArrayIndexOutOfBoundsException e) { }
}
3.
public static void main(String args[ ])
{
int i = args.length;
if (i != 0) System.out.println(args[i-1]);
}

4.
public static void main(String args[ ])
{
int i = args.length-1;
if (i > 0) System.out.println(args[i]);
}

5.
public static void main(String args[ ])
{
try { System.out.println(args[args.length-1]); }
catch (NullPointerException e) {}
}
Answer is given :
code 1
code 2
code 3

BUT ..how ??? code 1 and 3 r not creating any exception if args value is 0. I am not understading the overall concept of this question.
pls...any1 explain this 2 me.
THANKS IN ADVANCE.
<marquee> ratul banerjee </marquee>


The explanation for ist and 3rd is given above
There is nothing wrong with the second option for u are catching the AIOB exception for it would have occured for no arguements but since u are not printing anything when u are catching the exception i:e is in yr catch handler u are not printing anything so no output.A good advice do read the question properly.U get a lot of time to solve it inactual test.
sandeep
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whats wrong with 4th option???
Tanveer
 
sandeep bagati
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tanveer Mehmood:
Whats wrong with 4th option???
Tanveer


in that case for length=0 it will be a[-1] AIOB exception
sandeep
 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Yes..I understood now.
sorry ..folks..I mis-understand the 1st line of the q.
yes..sandeep..u r right... better to read the question properly
thanks.
ratul banerjee
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic