Java OCA OCP Practice Question 1690

Question

Given the following code:

package p3;/*from w  w w . ja v  a  2  s. c om*/
public class Util {
  public enum Format {
    Doc { public String toString() {return "Doc"; }},
    Txt  { public String toString() {return "Txt"; }},
    Java { public String toString() {return "Java"; }};
  }
  public static <T> void print(T t) {
    System.out.print("|" + t + "|");
  }
}
// (1) INSERT IMPORT STATEMENTS HERE
public class Main {
  public static void main(String[] args) {
    Util u = new Util();
    Format[] formats = {
        Txt, Java,
        Doc,
        Format.Doc,
        Util.Format.Doc,
        p3.Util.Format.Doc
    };
    for (Format fmt : formats)
      print(fmt);
  }
}

Which sequence of import statements, when inserted at (1), will result in the code compiling, and the execution of the main() method printing:.

|Txt||Java||Doc||Doc||Doc||Doc|

Select the three correct answers.

(a)  import p3.Util;
     import p3.Util.Format;
     import static p3.Util.print;
     import static p3.Util.Format.*;
(b)  import p3.Util;
     import static p3.Util.Format;
     import static p3.Util.print;
     import static p3.Util.Format.*;
(c)  import p3.*;
     import static p3.Util.*;
     import static p3.Util.Format.*;
(d)  import p3.*;
     import p3.Util.*;
     import static p3.Util.Format.*;


(a), (b), and (c)

Note

(d) does not statically import p3.Util.print.

p3.Util.Format can be imported as a type and as a static member from p3.Util, as in (a) and (b), respectively.




PreviousNext

Related