Java OCA OCP Practice Question 3263

Question

What will be the result of compiling and running the following program?

public class Main {
  public static <E> void print(E e, E...src) {
    System.out.print("|");
    for (E elt : src) {
      System.out.print(elt + "|");
    }// w  ww.j  av  a  2  s. c  om
    System.out.println();
  }

  public static void main(String[] args) {
    String[] sa = {"9", "6"};
    print("9", "6");                  // (1)
    print(sa);                        // (2)
    print(sa, sa);                    // (4)
    print(sa, sa, sa);                // (5)
    print(sa, "9", "6");              // (6)
  }
}

Select the one correct answer.

(a) The program does not compile because of errors in one or more calls to the print() method.
(b) The program compiles, but throws a NullPointerException when run.
(c) The program compiles, and prints (where XXXXXXX is some hash code for the
    String class):

     |9|6|// w w  w  .j  a  v a  2 s .c  om
     |6|
     |
     |[Ljava.lang.String;@XXXXXXX|[Ljava.lang.String;@XXXXXXX|
     |9|6|
(d) The program compiles, and prints (where XXXXXXX is some hash code for the
    String class):

     |6|
     |
     |9|6|
     |[Ljava.lang.String;@XXXXXXX|[Ljava.lang.String;@XXXXXXX|
     |9|6|


(d)

Note

Note that the print() method does not print its first argument.

The vararg is passed in the method calls as follows:.

print("9", "6");                     // (1) new String[] {"6"}
print(sa);                           // (2) new String[] {}
print(sa, sa);                      // (4) sa
print(sa, sa, sa);                   // (5) new String[][] {sa, sa}
print(sa, "9", "6");                 // (6) new String[] {"9", "6"}



PreviousNext

Related