What is the result?

Given:

1. public class Barn {
2. public static void main(String[] args) {
3. new Barn().go(“hi”, 1);
4. new Barn().go(“hi”, “world”, 2);
5. }
6. public void go(String… y, int x) {
7. System.out.print(y[y.length – 1] + ” “);
8. }
9. }

What is the result?

Given:

1. public class Barn {
2. public static void main(String[] args) {
3. new Barn().go(“hi”, 1);
4. new Barn().go(“hi”, “world”, 2);
5. }
6. public void go(String… y, int x) {
7. System.out.print(y[y.length – 1] + ” “);
8. }
9. }

What is the result?

A.
hi hi

B.
hi world

C.
world world

D.
Compilation fails.

E.
An exception is thrown at runtime.

Explanation:
Main.java:6: ‘)’ expected
public void go(String… y, int x) {
^
Main.java:6: ‘;’ expected
public void go(String… y, int x) {
^
2 errors

The variable argument type String of the method go must be the last parameter and it isn’t.



Leave a Reply 0

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