Java OCA OCP Practice Question 2891

Question

Given:

3. public class Main {  
4.   static public void main(String[] scurvy) {  
5.     System.out.print(scurvy[1] + " ");  
6.     main(scurvy[2]);  
7.   }  
8.   public static void main(String dogs) {  
9.     assert(dogs == null);  
10.     System.out.println(dogs);  
11. } } 

And, if the code compiles, the command-line invocation:

java Main -ea 1 2 3 

What is the result? (Choose all that apply.)

  • A. 1 2
  • B. 2 3
  • C. Compilation fails due to an error on line 4.
  • D. Compilation fails due to an error on line 6.
  • E. Compilation fails due to an error on line 8.
  • F. Compilation fails due to an error on line 9.
  • G. Some output is produced and then an AssertionError is thrown.


A is correct.

Note

The code is legal.

Traditionally, main()'s String[] argument is named "args", but it's not required.

The second main() is an overload.

In order to enable assertions, the -ea flag must come before the name of the.

class file to be run, so in this case -ea is treated as the first of the arguments, which are of course loaded into a zero- based array.




PreviousNext

Related