Chapter 9
Working with Selected Classes from the Java API

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

  • Working with Selected classes from the Java API 
    • Manipulate data using the StringBuilder class and its methods
    • Create and manipulate Strings
    • Create and manipulate calendar data using classes from java.time.LocalDateTime,  java.time.LocalDate, java.time.LocalTime, java.time.format.DateTimeFormatter, java.time.Period 
    • Declare and use an ArrayList of a given type
    • Write a simple Lambda expression that consumes a Lambda Predicate expression

  1. What is the best reason for using StringBuilder instead of String?

    1. StringBuilder adds support for multiple threads.
    2. StringBuilder can use == to compare values.
    3. StringBuilder saves memory by reducing the number of objects created.
    4. StringBuilder supports different languages and encodings.
  2. What is not true about a String?

    1. It can be created without coding a call to a constructor.
    2. It can be reused via the string pool.
    3. It is final.
    4. It is mutable.
  3. Which of the following creates a StringBuilder with a different value than the other options?

    1. new StringBuilder().append("clown")
    2. new StringBuilder("clown")
    3. new StringBuilder("cl").insert(2, "own")
    4. All of them create the same value.
  4. What is the output of the following?

    StringBuilder teams = new StringBuilder("333");
    teams.append(" 806");
    teams.append(" 1601");
    System.out.print(teams);
    1. 333
    2. 333 806 1601
    3. The code compiles but outputs something else.
    4. The code does not compile.
  5. How many of the types ArrayList, List, and Object can fill in the blank to produce code that compiles?

    List frisbees = new  ____________();
    1. None
    2. One
    3. Two
    4. Three
  6. What is the output of the following?

    List<String> tools = new ArrayList<>();
    tools.add("hammer");
    tools.add("nail");
    tools.add("hex key");
    System.out.println(tools.get(1));
    1. hammer
    2. hex key
    3. nail
    4. None of the above
  7. What is the result of the following code?

    StringBuilder sb = new StringBuilder("radical")
       .insert(sb.length(), "robots");
    System.out.println(sb);
    1. radicarobots
    2. radicalrobots
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  8. What is the output of the following?

    List<String> museums = new ArrayList<>(1);
    museums.add("Natural History");
    museums.add("Science");
    museums.add("Art");
    museums.remove(2);
    System.out.println(museums);
    1. [Natural History, Science]
    2. [Natural History, Art, Science]
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  9. What is the output of the following?

    12:  StringBuilder b = new StringBuilder("12");
    13:  b = b.append("3");
    14:  b.reverse();
    15:  System.out.println(b.toString());
    1. 12
    2. 123
    3. 321
    4. The code does not compile.
  10. What is the main benefit of a lambda expression?

    1. It allows you to convert a primitive to a wrapper class.
    2. It allows you to change the bytecode while the application is running.
    3. It allows you to inherit from multiple classes.
    4. It allows you to write code that has the execution deferred.
  11. What is the output of the following?

    5:  StringBuilder line = new StringBuilder("-");
    6:  StringBuilder anotherLine = line.append("-");
    7:  System.out.print(line == anotherLine);
    8:  System.out.print(" ");
    9:  System.out.print(line.length());
    1. false 1
    2. false 2
    3. true 1
    4. true 2
  12. The author of this method forgot to include the data type. Which of the following reference types can fill in the blank to complete this method?

    public static void secret( ____________mystery) {
      mystery.add("metal");
      String str = mystery.get(0);
      int num = mystery.length();
    }
    1. ArrayList
    2. ArrayList<String>
    3. StringBuilder
    4. None of the above
  13. Which portion of code can be removed so that this line of code continues to compile?

    Predicate<StringBuilder> p = (StringBuilder b) ‐> {return true;};
    1. Remove StringBuilder b
    2. Remove ->
    3. Remove { and ;}
    4. Remove { return and ;}
  14. What is the output of the following?

    20:  List<Character> chars = new ArrayList<>();
    21:  chars.add('a');
    22:  chars.add('b');
    23:  chars.set(1, 'c');
    24:  chars.remove(0);
    25:  System.out.print(chars.size() + " " + chars.contains('b'));
    1. 1 false
    2. 1 true
    3. 2 false
    4. 2 true
  15. What is the output of the following?

    12:  String b = "12";
    13:  b += "3";
    14:  b.reverse();
    15:  System.out.println(b.toString());
    1. 12
    2. 123
    3. 321
    4. The code does not compile.
  16. How many of these lines fail to compile?

    Predicate<String> pred1 = s ‐> false;
    Predicate<String> pred2 = (s) ‐> false;
    Predicate<String> pred3 = String s ‐> false;
    Predicate<String> pred4 = (String s) ‐> false;
    1. One
    2. Two
    3. Three
    4. Four
  17. What does the following do?

    public class Shoot {
       interface Target {
          boolean needToAim(double angle);
       }
       static void prepare(double angle, Target t) {
          boolean ready = t.needToAim(angle);  // k1
          System.out.println(ready);
       }
       public static void main(String[] args) {
          prepare(45, d -> d > 5 || d < -5);   // k2
       }
    }
    1. It prints true.
    2. It prints false.
    3. It doesn’t compile due to line k1.
    4. It doesn’t compile due to line k2.
  18. What is the output of the following?

    String teams = new String("694");
    teams.concat(" 1155");
    teams.concat(" 2265");
    teams.concat(" 2869");
    System.out.println(teams);
    1. 694
    2. 694 1155 2265 2869
    3. The code compiles but outputs something else.
    4. The code does not compile.
  19. Which of these classes are in the java.util package?

    • ArrayList
    • LocalDate
    • String

    1. I only
    2. II only
    3. I and II
    4. I, II, and III
  20. Which of the answer choices results in a different value being output than the other three choices?

    StringBuilder sb = new StringBuilder("radical ");
    sb = ________________________;
    System.out.print(sb);
    1. new StringBuilder("radical ")
      • .append("robots")
    2. new StringBuilder("radical ")
      • .delete(1, 100)
      • .append("obots")
      • .insert(1, "adical r")
    3. new StringBuilder("radical ")
      • .insert(7, "robots")
    4. new StringBuilder("radical ")
      • .insert(sb.length(), "robots")
  21. What is the output of the following?

    String[] array = {"Natural History", "Science"};
    List<String> museums = Arrays.asList(array);
    museums.set(0, "Art");
    System.out.println(museums.contains("Art"));
    1. true
    2. false
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  22. Which is a true statement?

    1. If s.contains("abc") is true, then s.equals("abc") is also true.
    2. If s.contains("abc") is true, then s.startsWith("abc") is also true.
    3. If s.startsWith("abc") is true, then s.equals("abc") is also true.
    4. If s.startsWith("abc") is true, then s.contains("abc") is also true.
  23. What is the output of the following?

    20:  List<Character> chars = new ArrayList<>();
    21:  chars.add('a');
    22:  chars.add('b');
    23:  chars.set(1, 'c');
    24:  chars.remove(0);
    25:  System.out.print(chars.length());
    1. 0
    2. 1
    3. 2
    4. None of the above
  24. The author of this method forgot to include the data type. Which of the following reference types can fill in the blank to complete this method?

    public static void secret(_____________ mystery) {
       mystery = mystery.replace("1", "8");
       mystery.startsWith("paper");
       String s = mystery.toString();
    }
    1. ArrayList
    2. String
    3. StringBuilder
    4. None of the above
  25. Which statement is true about the following figure while ensuring the code continues to compile?

    Sheet of program code shows three parts in code separated by positions P and Q by arrows, where first part has list and second part has balloons = new ArrayList and third part has ().
    1. <> can be inserted at position P without making any other changes.
    2. <> can be inserted at position Q without making any other changes.
    3. <> can be inserted at both positions P and Q.
    4. None of the above
  26. Which of the following can fill in the blank to make the code compile?

    import java.util.function.*;
    public class Card {
       public static void main(String[] s) {
          Predicate<String> pred =____________  ‐> true;
       }
    }
    1. (Integer i)
    2. (Object o)
    3. (String s)
    4. None of the above
  27. What is the output of the following?

    5:  String line = new String("-");
    6:  String anotherLine = line.concat("-");
    7:  System.out.print(line == anotherLine);
    8:  System.out.print(" ");
    9:  System.out.print(line.length());
    1. false 1
    2. false 2
    3. true 1
    4. true 2
  28. What does the following output?

    Predicate dash = c -> c.startsWith("‐");
    System.out.println(dash.test("–"));
    1. true
    2. false
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  29. Of the classes LocalDate, LocalDateTime, LocalTime, and LocalTimeStamp, how many include hours, minutes, and seconds?

    1. One
    2. Two
    3. Three
    4. Four
  30. What is the output of the following class?

    1:  package rocket;
    2:  public class Countdown {
    3:     public static void main(String[] args) {
    4:        String builder = "54321";
    5:        builder = builder.substring(4);
    6:        System.out.println(builder.charAt(2));
    7:     }
    8:  }
    1. 2
    2. 3
    3. 4
    4. None of the above
  31. Which equivalent code can replace i -> i != 0 in the following line?

    Predicate<Integer> ip = i ‐> i != 0;
    1. i -> { i != 0 }
    2. i -> { i != 0; }
    3. i -> { return i != 0 }
    4. i -> { return i != 0; }
  32. What is the output of the following?

    LocalDate xmas = LocalDate.of(2016,  12,  25);
    xmas.plusDays(-1);
    System.out.println(xmas.getDayOfMonth());
    1. 24
    2. 25
    3. 26
    4. None of the above
  33. What is the output of the following?

    1:   public class Legos {
    2:      public static void main(String[] args) {
    3:         StringBuilder sb = new StringBuilder();
    4:         sb.append("red");
    5:         sb.deleteCharAt(0);
    6:         sb.delete(1, 2);
    7:         System.out.println(sb);
    8:      }
    9:   }
    1. e
    2. d
    3. ed
    4. None of the above
  34. What does the following output?

    Predicate clear = c -> c.equals("clear");
    System.out.println(clear.test("pink"));
    1. true
    2. false
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  35. Which starts counting from one rather than zero?

    1. Array indexes
    2. The index used by charAt in a String
    3. The months in a LocalDateTime
    4. The months in a LocalTime
  36. Which statement is not true of Predicate?

    1. A boolean is returned from the method it declares.
    2. It is an interface.
    3. The method it declares accepts two parameters.
    4. The method it declares is named test.
  37. Which of these periods represents a larger amount of time?

    Period period1 = Period.ofWeeks(1).ofDays(3);
    Period period2 = Period.ofDays(10);
    1. period1
    2. period2
    3. They represent the same length of time.
    4. None of the above. This code does not compile.
  38. What is the result of the following?

    import java.time.*;
    import java.time.format.*;
     
    public class HowLong {
       public static void main(String[] args) {
          LocalDate newYears = LocalDate.of(2017, 1, 1);
          Period period = Period.ofDays(1);
          DateTimeFormatter format = DateTimeFormatter.ofPattern("MM-dd-yyyy");
          System.out.print(format.format(newYears.minus(period)));
       }
    }
    1. 01-01-2017
    2. 12-31-2016
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  39. Which of the following can fill in the blank so the following code prints true?

    String happy = " :) - (: ";
    String really = happy.trim();
    String question = ______________________;
    System.out.println(really.equals(question));
    1. happy.substring(0, happy.length() - 1)
    2. happy.substring(0, happy.length())
    3. happy.substring(1, happy.length() - 1)
    4. happy.substring(1, happy.length())
  40. Which is not a true statement about the Period class?

    1. A Period is immutable.
    2. A Period is typically used for adding or subtracting time from dates.
    3. You can create a Period representing 2 minutes.
    4. You can create a Period representing 5 years.
  41. What is the output of the following class?

    1:  package rocket;
    2:  public class Countdown {
    3:     public static void main(String[] args) {
    4:        StringBuilder builder = new StringBuilder("54321");
    5:        builder.substring(2);
    6:        System.out.println(builder.charAt(1));
    7:     }
    8:  }
    1. 1
    2. 2
    3. 3
    4. 4
  42. What does the following output?

    List<Integer> pennies = new ArrayList<>();
    pennies.add(3);
    pennies.add(2);
    pennies.add(1);
    pennies.remove(2);
    System.out.println(pennies);
    1. [3, 1]
    2. [3, 2]
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  43. The author of this method forgot to include the data type. Which of the following reference types can best fill in the blank to complete this method?

    public static void secret(_____________ mystery) {
       char ch = mystery.charAt(3);
       mystery = mystery.insert(1, "more");
       int num = mystery.length();
    }
    1. ArrayList
    2. String
    3. StringBuilder
    4. None of the above
  44. What is the smallest unit you can add to a LocalTime object?

    1. Second
    2. Millisecond
    3. Nanosecond
    4. Picosecond
  45. What is the result of the following?

    import java.time.*;
    import java.time.format.*;
     
    public class HowLong {
       public static void main(String[] args) {
          LocalDate newYears = LocalDate.of(2017, 1, 1);
          Period period = Period.ofDays(1);
          DateTimeFormatter format = DateTimeFormatter.ofPattern("mm-dd-yyyy");
          System.out.print(format.format(newYears.minus(period)));
       }
    }
    1. 01-01-2017
    2. 12-31-2016
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  46. Which of the following types can you pass as a parameter to the replace() method on the String class?

    • char
    • String
    • StringBuilder

    1. I
    2. I and II
    3. II and III
    4. I, II, and III
  47. How many lines does this code output?

    import java.util.*;
    import java.util.function.*;
     
    public class PrintNegative {
       public static void main(String[] args) {
          List<String> list = new ArrayList<>();
          list.add("-5");
          list.add("0");
          list.add("5");
          print(list, e -> e < 0);
       }
       public static void print(List<String> list, Predicate<Integer> p) {
          for (String num : list)
             if (p.test(num))
                System.out.println(num);
       }
    }
    1. One
    2. Two
    3. None. The code does not compile.
    4. None. The code throws an exception at runtime.
  48. What is the output of the following?

    12:  List<String> magazines = new ArrayList();
    13:  magazines.add("Readers Digest");
    14:  magazines.add("People");
    15:  magazines.clear();
    16:  magazines.add("The Economist");
    17:  magazines.remove(1);
    18:  System.out.println(magazines.size());
    1. 0
    2. 1
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  49. What is the output of the following?

    public class Costume {
       public static void main(String[] black) {
         String witch = 'b';
         String tail = "lack";
         witch = witch.concat(tail);
         System.out.println(witch);
       }
    }
    1. b
    2. black
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  50. What is the result of the following?

    LocalDate xmas = LocalDate.of(2016,  12,  25);
    xmas.setYear(2017);
    System.out.println(xmas.getYear());
    1. 2016
    2. 2017
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset