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>