QuestionQ159

Creating and Using Arrays

Given the code snippet from a compiled Java source file:

Question Image

Which command-line arguments should be passed to the program to produce the following output?

Arg is 2 -

  • A java MyFile 1 3 2 2
  • B java MyFile 2 2 2
  • C java MyFile 1 2 2 3 4
  • D java MyFile 0 1 2 3
Explanation

Java passes arguments after the class name to main as elements of the String[] args array. Because array indexing is zero-based, args[3] is the fourth supplied argument. java MyFile 1 3 2 2 therefore assigns 2 to arg3 and prints Arg is 2.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!