Monday, 1 April 2013

Array - 2



package net.dharmaraj;

public class dev {
public static void main(String[] args) {
for (int i = 0; i < 1; i++)
// for (int i = 0; i < args.length;i++ )
{
System.out.print(args[i]);
}
}
}



Which of the code fragments will throw an "ArrayIndexOutOfBoundsException" ? 


A) for (int i = 0; i < args.length; i ++ ) { 
           System.out.print( i ) ; 
     } 
B) System.out.print(args.length); 
C) for ( int i = 0; i < 1; i++ ) { 
          System.out.print(args[i]); 
     } 
D) None of the above 


Ans - C

OutPut - 

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at net.dharmaraj.dev.main(dev.java:8)


No comments:

Post a Comment