Menu
Topics Index
...
`

Mains and more mains


If you need explanation Read this topic

If you need Answer Take test on this topic

Mains and more mains
At which line will the below program throw an exception, if run with arguments "A B C D D C B A"?
import java.util.Arrays;
public class MainsAndMoreMains {

    public static void main(String[] args) {
        String[] args2 = Arrays.copyOfRange(args, 3, 6); // LINE A
        M.main(args2);
        main(args[0]);
        main(args[7]);
        N.main(Arrays.copyOfRange(args2, 2, 4)); // LINE B
    }

    public static int main(String two) {
        System.out.print(two);
        return two.length();
    }
}
class M {
    public static void main(String[] args) {
        MainsAndMoreMains.main(args[2]); // LINE C
    }
}
class N extends M {
    public static void main(String[] args) {
        System.out.print(args[2]); // LINE D
    }
}

A. LINE A
B. LINE B
C. LINE C
D. LINE D
E. The program compiles properly and runs with out any exception
Topic: Unknown

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

C

Posted by Kick Bhasha    2014-01-24 12:42:58


B.is answer.program compiles fine. but  at N.main ArrayOutof Boudns Exception happens to arise.

Posted by Shabaaz Shaikz    2014-01-24 12:44:35


Ans is B...in copyofrange method the indices result in array index out of bounds exception....

Posted by Santosh Munugota    2014-01-24 13:19:26


At LINE A, we are copying 'D D C' into args2, and when we do copyOfRange at LINE B, 'C and null' will be passed to N.main method. Since we are accessing args[2] in the N.main method, it throws ArrayIndexOutOfBoundsException.

Posted by Merit Campus    2014-02-03 12:11:55


So the correct answer is 'D'. Hence no winner for this dose. :-(

Posted by Merit Campus    2014-02-03 12:12:30


© meritcampus 2019

All Rights Reserved.

Open In App