Java is the future !: Quiz of the Day | November 5, 2010 [2]

Friday, November 5, 2010

Quiz of the Day | November 5, 2010 [2]

What will be printed on standard output if the following class is executed using the command "java Test 1 two 3" ?

a. 1 

b. two 
c. NumberFormatException 
d. ArrayIndexOutOfBoundsException 
e. Code does not compile


Solution : Choice C is the correct answer. 

In Java, command line arguments are stored in the args array which is an argument to the main method. The index of the args array starts with 0 and the first command line argument is the word after the classname. Thus, args[0] will be 1 and args[1] will be "two".

The parseInt() method of the Integer class parses the String argument to the int type. The String passed to the parseInt() method should have only digits, with an exception that it can start with '-' to indicate a negative number. Otherwise, NumberFormatException will be thrown. Thus, when "two" is passed to the parseInt() method, NumberFormatException will be thrown.

Please note that there is nothing wrong with the definition of main, the static word can come after or before public keyword. 


Source : "Facebook community"

No comments:

Post a Comment