Java OCA OCP Practice Question 1686

Question

Given the following code:.

// (1) INSERT ONE IMPORT STATEMENT HERE
public class Main {
  public static void main(String[] args) {
    System.out.println(Locale.UK);     // Locale string for UK is "en_GB".
  }
}

Which statements, when inserted at (1), will result in a program that prints en_GB, when compiled and run?.

Select the two correct answers.

  • (a) import java.util.*;
  • (b) import java.util.Locale;
  • (c) import java.util.Locale.UK;
  • (d) import java.util.Locale.*;
  • (e) import static java.util.*;
  • (f) import static java.util.Locale;
  • (g) import static java.util.Locale.UK;
  • (h) import static java.util.Locale.*;


(a) and (b)

Note

(a) imports all types from the package java.util, including the type java.util.Locale. (b) explicitly imports the type java.util.Locale, which is what is needed.

(c) is syntactically incorrect, as java.util.Locale.UK is not a type. (d) imports types from the package java.util.Locale, but not the type java.util.Locale.

In (e), the static import is specified from a package (java.util), and not from a type, as required.

In (f), the static import is incorrectly specified for a type (java.util.Locale) and not for a static member.

Both (g) and (h) with static import do not work, because we are referring to the constant UK using the simple name of the class ( Locale) in the main() method.




PreviousNext

Related