Objects and Collections

Q1: What will be the output of the program?
public class Test 
{ 
    public static void main (String[] args) 
    {
        String foo = args[1]; 
        String bar = args[2]; 
        String baz = args[3]; 
        System.out.println('baz = ' + baz); /* Line 8 */
    } 
}
And the command line invocation: > java Test red green blue

A baz =

B baz = null

C baz = blue

D Runtime Exception

ANS:A - baz =

When running the program you entered 3 arguments 'red', 'green' and 'blue'. When dealing with arrays in java you must remember ALL ARRAYS IN JAVA ARE ZERO BASED therefore args[0] becomes 'red', args[1] becomes 'green' and args[2] becomes 'blue'. When the program entcounters line 8 above at runtime it looks for args[3] which has never been created therefore you get an ArrayIndexOutOfBoundsException at runtime.



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.