Which two will compile, and can be run successfully using the command: Java fred hello walls

Which two will compile, and can be run successfully using the command:
Java fred hello walls

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.



Leave a Reply 9

Your email address will not be published. Required fields are marked *


Diego Parra

Diego Parra

Array out of bounds on D?
Are you sure?

tw

tw

B:Array out of bounds at runtime.

tw

tw

B is right.
C is right.
D is right.

Deepak

Deepak

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

Marin R

Marin R

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

Kalil Peixoto

Kalil Peixoto

Agree with Marin, C and D are corrects.

sully

sully

C and D are correct
A is missing []
B only 0 position and 1, 2 is out of bounds.