Java OCA OCP Practice Question 1680

Question

Given the class.

// File name: Main.java
public class Main {
  public static void main(String[] args) {
    System.out.println(args[0] + " " + args[args.length-1]);
  }
}

what would be the result of executing the following command line?.

>java Main In movie orange is not a apple

Select the one correct answer.

  • (a) The program will throw an ArrayIndexOutOfBoundsException.
  • (b) The program will print "java apple".
  • (c) The program will print "Main apple".
  • (d) The program will print "In apple".
  • (e) The program will print "Main a".
  • (f) The program will print "In a".


(d)

Note

The length of the array passed to the main() method is equal to the number of program arguments specified in the command line.

The element at index 0 does not contain the name of the program.

The first argument given is retrieved using args[0], and the last argument given is retrieved using args[args.length-1].




PreviousNext

Related