Which two will compile, and can be run successfully using the command:
Java fred hello walls
A.
class Fred1{
public static void main (String args) {
System.out.println(args[1]);
}
B.
class Fred1{
public static void main (String [] args) {
System.out.println(args[2]);
}
C.
class Fred1 {
public static void main (String [] args) {
System.out.println (args);
}
D.
class Fred1 {
public static void main (String [] args) {
System.out.println (args [1]);
}
Explanation:
Incorrect answers:
A: Will not compile. array is required, not a string.
D: Array out of bounds at runtime.
Array out of bounds on D?
Are you sure?
B:Array out of bounds at runtime.
B is right.
C is right.
D is right.
A and C is correct i think. B is out of bound and option D has space between args and bracket.
However this is wrong question as java is executing Fred and class names are Fred1
assuming the command line is
Java Fred1 hello walls
Correct is C and D
A. invalid arguments for main, runtime exception
B. runtime exception ArrayIndexOutOfBounds
space is allowed when declaring arrays:
int[] i
int []i
int [] i
int[ ] i
int [ ]i
int[ ]i
…
Agree with Marin, C and D are corrects.
C and D are correct
A is missing []
B only 0 position and 1, 2 is out of bounds.
??
c