Java OCA OCP Practice Question 1682

Question

Which statement about the following program is true?.

public class Main {
  public static void main(String[] args) {
    String[] numbers = { "one", "two", "three", "four" };

    if (args.length == 0) {
      System.out.println("no arguments");
    } else {/*w w w  . ja v a2s. co  m*/
      System.out.println(numbers[ args.length ] + " arguments");
    }
  }
}

Select the one correct answer.

  • (a) The program will fail to compile.
  • (b) The program will throw a NullPointerException when run with no program arguments.
  • (c) The program will print "no arguments" and "two arguments" when called with zero and three program arguments, respectively.
  • (d) The program will print "no arguments" and "three arguments" when called with zero and three program arguments, respectively.
  • (e) The program will print "no arguments" and "four arguments" when called with zero and three program arguments, respectively.
  • (f) The program will print "one arguments" and "four arguments" when called with zero and three program arguments, respectively.


(e)

Note

The program will print "no arguments" and "four arguments" when called with zero and three program arguments, respectively.

When the program is called with no program arguments, the args array will be of length zero.

The program will in this case print "no arguments".

When the program is called with three arguments, the args array will have length 3.

Using the index 3 on the numbers array will retrieve the string "four", because the start index is 0.




PreviousNext

Related