Chapter 22
Localization

THE OCP EXAM TOPICS COVERED IN THIS PRACTICE TEST INCLUDE THE FOLLOWING:

  • Localization
    • Read and set the locale by using the Locale object
    • Create and read a Properties file
    • Build a resource bundle for each locale and load a resource bundle in an application

  1. Which is not a locale?

    1. Cultural region
    2. Geographical region
    3. Political region
    4. Time zone region
  2. When localizing an application, which type of data varies in presentation depending on locale?

    1. Currencies
    2. Dates
    3. Both
    4. Neither
  3. How do you find out the locale of the running program?

    1. Locale.get("default")
    2. Locale.get(Locale.DEFAULT)
    3. Locale.getDefault()
    4. None of the above
  4. If your application has all town names in a single file named strings.properties, what is the most specific process that has been implemented?

    1. Internationalization
    2. Localization
    3. Specialization
    4. Translation
  5. Which interfaces does Properties implement?

    • Hashtable
    • HashMap
    • Map

     

    1. III
    2. I and II
    3. I and III
    4. II and III
  6. Which filename extension can hold a String property value in a resource bundle?

    1. .java
    2. .properties
    3. Both of the above
    4. Neither of the above
  7. How long will the effects of calling Locale.setDefault() be active assuming no other calls to that method are made?

    1. Until the end of the method
    2. Until the program exits
    3. Until the next reboot of the computer
    4. None of the above. It persists even past a reboot.
  8. Given this properties file used to load the Properties object props and this code snippet, what is the output?

    mystery=bag
    type=paper
    18:  System.out.print(props.getProperty("mystery"));
    19:  System.out.print(" ");
    20:  System.out.print(props.getProperty("more"));
    1. bag
    2. bag null
    3. bag ?
    4. This code throws a runtime exception on line 20.
  9. Fill in the blanks to complete the following code for a Java resource bundle.

    public class Flights_en extends __________ {
       protected Object[][] __________() {
          return new Object[0][0];
       }
    }
    1. JavaResourceBundle, getContents
    2. JavaResourceBundle, getProperties
    3. ListResourceBundle, getContents
    4. ListResourceBundle, getProperties
  10. Which of the following shows a valid Locale format for the language Hindi and the country India?

    1. hi_IN
    2. HI_in
    3. in_HI
    4. IN_hi
  11. If the key purple is in all four of these files, which file will the following code use for the resource bundle?

    Locale.setDefault(new Locale("en", "US"));
    ResourceBundle rb = ResourceBundle.getBundle("Colors");
    rb.getString("purple");
    1. Colors.class
    2. Colors.properties
    3. Colors_en_US.class
    4. Colors_en_US.properties
  12. What is the output of the following?

    package counter;
    import java.util.*;
     
    public class CountResource extends ListResourceBundle {
       private int count = 0;
     
       @Override
       protected Object[][] getContents() {
          return new Object[][] { { "count", count++ } };
       }
       public static void main(String[] args) {
          ResourceBundle rb = ResourceBundle.getBundle("counter.CountResource");
          System.out.println(rb.getObject("count") + " " + rb.getObject("count"));
       }
    }
    1. 0 0
    2. 0 1
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  13. Which of the following shows a valid Locale format?

    1. en
    2. US
    3. US_en
    4. None of the above
  14. Which filename extension can define a LocalDateTime property value in a resource bundle?

    1. .java
    2. .properties
    3. Both of the above
    4. Neither of the above
  15. What happens if you run this code with no resource bundles available?

    ResourceBundle rb = ResourceBundle.getBundle("ghost.None");
    1. It creates a resource bundle with no key/value pairs.
    2. It runs without issue because the resource bundle is only searched for when getting the first key.
    3. It throws a MissingResourceException.
    4. None of the above
  16. What is the result of the following?

    Map<String, String> map = new TreeMap<>();
    map.put("tool", "hammer");
    map.put("problem", "nail");
     
    Properties props = new Properties();      // p1
    map.forEach((k,v) -> props.put(k,  v));   // p2
     
    String t = props.get("tool");             // p3
    String n = props.get("nail");
    System.out.println(t + " " + n);
    1. hammer nail
    2. The code does not compile due to line p1.
    3. The code does not compile due to line p2.
    4. The code does not compile due to line p3.
  17. Which filename extension can hold a LocalDateTime property key in a resource bundle?

    1. .java
    2. .properties
    3. Both of the above
    4. Neither of the above
  18. If the key purple is in all four of these files, which file will the following code use for the resource bundle?

    Locale.setDefault(new Locale("en", "US"));
    ResourceBundle rb = ResourceBundle.getBundle("Colors");
    rb.getString("purple");
    1. Colors_en.class
    2. Colors_en.properties
    3. Colors_US.class
    4. Colors_US.properties
  19. Fill in the blank so the code correctly compiles and creates a Locale.

    Locale loc = Locale.____________________;
    1. get("Italian")
    2. get(Locale.ITALIAN)
    3. getLocale("Italian")
    4. None of the above
  20. What is the result of the following?

    Map<Object, Object> map = new TreeMap<>();
    map.put("tool", "hammer");
    map.put("problem", "nail");
     
    Properties props = new Properties();      // p1
    map.forEach((k,v) -> props.put(k,  v));   // p2
     
    String t = props.getProperty("tool");     // p3
    String n = props.getProperty("problem");
    System.out.println(t + " " + n);
    1. hammer nail
    2. The code does not compile due to line p1.
    3. The code does not compile due to line p2.
    4. The code does not compile due to line p3.
  21. What is the output of the following?

    1:   package keyboard;
    2:   import java.util.*;
    3:
    4:   public class Type {
    5:      protected Object[][] getContents() {
    6:         return new Object[][] { { "keys", new ArrayList<String>() }};
    7:      }
    8:      public static void main(String[] args) {
    9:         ResourceBundle rb = ResourceBundle.getBundle("keyboard.Type");
    10:        List<String> keys = (List) rb.getObject("keys");
    11:        keys.add("q");
    12:        keys.add("w");
    13:        keys.add("e");
    14:        keys.add("r");
    15:        keys.add("t");
    16:        keys.add("y");
    17:        System.out.println(((List) rb.getObject("keys")).size());
    18:     }
    19:  }
    1. 0
    2. 6
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  22. How many lines does the following print out?

    3:   Locale.setDefault(Locale.KOREAN);
    4:   System.out.println(Locale.getDefault());
    5:   Locale.setDefault(new Locale("en", "AU"));
    6:   System.out.println(Locale.getDefault());
    7:   Locale.setDefault(new Locale("EN"));
    8:   System.out.println(Locale.getDefault());
    1. None; it does not compile.
    2. One followed by an exception
    3. Two followed by an exception
    4. All three
  23. Given this properties file used to load the Properties object props and this code snippet, what is the output?

    mystery=bag
    type=paper
     
    18:  System.out.print(props.getProperty("mystery", "?"));
    19:  System.out.print(" ");
    20:  System.out.print(props.getProperty("more", "?"));
    1. bag
    2. bag null
    3. bag ?
    4. This code throws a runtime exception on line 20.
  24. Given the following four properties files, what does this code print?

    Diagram shows four properties of files that are Cars_en.properties, Cars_fr.properties, Cars_en_US.properties, and Cars_fr_FR.properties with each having separate codes.
    Locale.setDefault(new Locale("en", "US"));
    ResourceBundle rb = ResourceBundle.getBundle(
       "Cars", new Locale("fr", "FR"));
    System.out.println(rb.getString("country") + " "
       + rb.getString("engine"));
    1. France engine
    2. France moteur
    3. France null
    4. The code throws an exception at runtime.
  25. Given the four properties files in question 24, what does this code print?

    Locale.setDefault(new Locale("en", "US"));
    ResourceBundle rb = ResourceBundle.getBundle(
        "Cars", new Locale("fr", "CA"));
    System.out.println(rb.getString("engine") + " "
        + rb.getString("horses"));
    1. engine 241
    2. moteur 241
    3. moteur null
    4. The code throws an exception at runtime.
  26. Given the four properties files in question 24, what does this code print?

    Locale.setDefault(new Locale("fr", "CA"));
    ResourceBundle rb = ResourceBundle.getBundle(
        "Cars", new Locale("en", "CA"));
    System.out.println(rb.getString("engine") + " "
        + rb.getString("horses"));
    1. engine 241
    2. moteur 241
    3. moteur null
    4. The code throws an exception at runtime.
  27. Which statement about ListResourceBundle is true?

    1. A disadvantage over properties files is that you cannot create the value at runtime.
    2. Names-fr.java is a bad implementation class name.
    3. Values of type Integer are not allowed.
    4. None of the above
  28. If the key red is in all four of these files, which file will the following code use for the resource bundle?

    Locale loc = new Locale("zh", "CN");
    Locale.setDefault(new Locale("en", "US"));
    ResourceBundle rb = ResourceBundle.getBundle("Colors", loc);
    rb.getString("red");
    1. Colors_CN.properties
    2. Colors_en.properties
    3. Colors_US.properties
    4. Colors_zh.properties
  29. What is the output of the following?

    package counter;
    import java.util.*;
     
    public class PropertyCounter extends ListResourceBundle {
       private int count = 0;
       @Override
       protected Object[][] getContents() {
          return new Object[][] {{ "count", new PropertyCounter() }};
       }
       public int getCount() {
          return count++;
       }
       public static void main(String[] args) {
          ResourceBundle rb = ResourceBundle.getBundle("counter.PropertyCounter");
          PropertyCounter obj = (PropertyCounter) rb.getObject("count");
          System.out.println(obj.getCount() + " " + obj.getCount());
       }
    }
    1. 0 0
    2. 0 1
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  30. What is the output of the following?

    1:   package keyboard;
    2:   import java.util.*;
    3:
    4:   public class Type extends ListResourceBundle {
    5:      protected Object[][] getContents() {
    6:         return new Object[][] { { "keys", new ArrayList<String>() }};
    7:      }
    8:      public static void main(String[] args) {
    9:         ResourceBundle rb = ResourceBundle.getBundle("Type");
    10:        List<String> keys = (List) rb.getObject("keys");
    11:        keys.add("q");
    12:        keys.add("w");
    13:        keys.add("e");
    14:        keys.add("r");
    15:        keys.add("t");
    16:        keys.add("y");
    17:        System.out.println(((List) rb.getObject("keys")).size());
    18:     }
    19:  }
    1. 0
    2. 6
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  31. Given the following snippets of resource bundles from when we compiled the application, what is the result of the following?

    Diagram shows snippets of resource bundles that are Buggy.java, Buggy.properties, Buggy_en.java, and Buggy_en.properties with each having separate codes when application is complied.
    Locale.setDefault(new Locale("en", "US"));
    ResourceBundle rb = ResourceBundle.getBundle("Buggy");
    System.out.println(rb.getString("wheels"));
    1. null
    2. 4
    3. 6
    4. The code throws an exception at runtime.
  32. Given the snippets of resource bundles in question 31 from when we compiled the application, what is the result of the following?

    Locale.setDefault(new Locale("en"));
    ResourceBundle rb = ResourceBundle.getBundle("Buggy");
    System.out.println(rb.getString("color"));
    1. null
    2. black
    3. white
    4. The code throws an exception at runtime.
  33. Given the snippets of resource bundles in question 31 from when we compiled the application, what is the result of the following?

    Locale.setDefault(new Locale("zh"));
    ResourceBundle rb = ResourceBundle.getBundle("Buggy");
    System.out.println(rb.getString("wheels"));
    1. null
    2. 4
    3. 6
    4. The code throws an exception at runtime.
  34. If the key red is in all three of these files, which file will the following code use for the resource bundle?

    Locale loc = new Locale("zh", "CN");
    Locale.setDefault(new Locale("en", "US"));
    ResourceBundle rb = ResourceBundle.getBundle("Colors", loc);
    rb.getString("red");
    1. Colors_en.properties
    2. Colors.properties
    3. Red_en.properties
    4. None of the above. It will throw a MissingResourceException.
  35. Which can fill in the blank in this code?

    Properties props = new Properties();
    props.__________("x");
    1. get
    2. getProperty
    3. Both of the above
    4. Neither of the above
  36. What is the output of the following?

    1:   package keyboard;
    2:   import java.util.*;
    3:
    4:   public class Type extends ListResourceBundle {
    5:      protected Object[][] getContents() {
    6:         return new Object[][] { { "keys", new ArrayList<String>() }};
    7:      }
    8:      public static void main(String[] args) {
    9:         ResourceBundle rb = ResourceBundle.getBundle("keyboard.Type");
    10:        List<String> keys = (List) rb.getObject("keys");
    11:        keys.add("q");
    12:        keys.add("w");
    13:        keys.add("e");
    14:        keys.add("r");
    15:        keys.add("t");
    16:        keys.add("y");
    17:        System.out.println(((List) rb.getObject("keys")).size());
    18:     }
    19:  }
    1. 0
    2. 6
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  37. What is the result of the following?

    Map<String, String> map = new TreeMap<>();
    map.put("tool", "hammer");
    map.put("problem", "nail");
     
    Property props = new Property();          // p1
    map.forEach((k,v) -> props.put(k, v));    // p2
     
    String t = props.getProperty("tool");     // p3
    String n = props.getProperty("nail");
    System.out.println(t + " " + n);
    1. hammer nail
    2. The code does not compile due to line p1.
    3. The code does not compile due to line p2.
    4. The code does not compile due to line p3.
  38. What is the output of the following?

    package counter;
    import java.util.*;
     
    public class CountResource extends ListResourceBundle {
       private int count = 0;
     
       @Override
       protected Object[][] getContents() {
          return new Object[][] { { "count", count++ } };
       }
       public static void main(String[] args) {
          ResourceBundle rb = new ResourceBundle("counter.CountResource");
          System.out.println(rb.getObject("count") + " " + rb.getObject("count"));
       }
    }
    1. 0 0
    2. 0 1
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  39. Given the following two properties files in the pod package, what does the following class output?

    pod.container.properties
    name=generic
    number=2
     
    pod.container_en.properties
    name=Docker
    type=container
     
    package pod;
    import java.util.*;
    public class WhatKind {
       public static void main(String[] args) {
          Locale.setDefault(new Locale("en"));
          ResourceBundle rb = ResourceBundle.getBundle("pod.container");
          String name = rb.getString("name");
          String type = rb.getString("type");
          System.out.println(name + " " + type);   }
    }
    1. Docker container
    2. generic container
    3. generic null
    4. None of the above
  40. If the key red is in all three of these files, which file will the following code use for the resource bundle?

    Locale loc = new Locale("zh", "CN");
    Locale.setDefault(new Locale("en", "US"));
    ResourceBundle rb = ResourceBundle.getBundle("Colors", loc);
    rb.getString("red");
    1. Colors_EN.properties
    2. Colors_ZH.properties
    3. Red_EN.properties
    4. None of the above. It will throw a MissingResourceException.