Chapter 14
Practice Exam 3

Chapter 14 is the third and final practice exam chapter in the book. Make sure you have 90 minutes and scratch paper before you start. Good luck on both this chapter and the real exam!

  1. Which of the following are true about Java operators and statements? (Choose three.)
    1. Both right-hand sides of the ternary expression are evaluated at runtime.
    2. A switch statement may contain at most one default statement.
    3. The post-increment operator (++) returns the value of the variable before the addition is applied.
    4. The logical operators (|) and (||) are interchangeable, producing the same results at runtime.
    5. The complement operator (!) operator may be applied to numeric expressions.
    6. An assignment operator returns a value that is equal to the value of the expression being assigned.
  2. What is the output of the following?
    public class Legos {
       public static void main(String[] args) {
          var ok = true;
          if (ok) {
             var sb = new StringBuilder();
             sb.append("red");
             sb.deleteCharAt(0);
             sb.delete(1, 1);
          }
          System.out.print(sb);
       } }
    
    1. r
    2. e
    3. ed
    4. red
    5. The code does not compile.
    6. The code compiles but throws an exception at runtime.
  3. Which line of code belongs in a service locator?
    1. ServiceLoader<Mouse> sl = ServiceLoader.load(Mouse.class);
    2. ServiceLoader<Mouse> sl = ServiceLoader.loader(Mouse.class);
    3. ServiceLoader<Mouse> sl = ServiceLoader.lookup(Mouse.class);
    4. ServiceLocator<Mouse> sl = ServiceLoader.load(Mouse.class);
    5. ServiceLocator<Mouse> sl = ServiceLoader.loader(Mouse.class);
    6. ServiceLocator<Mouse> sl = ServiceLoader.lookup(Mouse.class);
  4. What is the result of the following?
    1: import java.util.function.*;
    2: public class Ready {
    3:     private static double getNumber() {
    4:        return .007;
    5:     }
    6:     public static void main(String[] args) {
    7:        Supplier<double> s = Ready::getNumber;
    8:        double d = s.get();
    9:        System.out.println(d);
    10:    } }
    
    1. 0
    2. 0.007
    3. The code does not compile due to line 7.
    4. The code does not compile due to line 8.
    5. The code does not compile for another reason.
  5. What is the output of executing the following code snippet?
    var e = Executors.newSingleThreadExecutor();
    Runnable r1 = () -> Stream.of(1,2,3).parallel();
    Callable r2 = () -> Stream.of(4,5,6).parallel();
     
    Future<Stream<Integer>> f1 = e.submit(r1);  // x1
    Future<Stream<Integer>> f2 = e.submit(r2);  // x2
     
    var r = Stream.of(f1.get(),f2.get())
       .flatMap(p -> p)                         // x3
       .parallelStream()                        // x4
       .collect(
          Collectors.groupingByConcurrent(i -> i%2==0));
    System.out.print(r.get(false).size()
       +" "+r.get(true).size());
    
    1. 3 3
    2. 2 4
    3. One of the marked lines (x1, x2, x3, x4) does not compile.
    4. Two of the marked lines (x1, x2, x3, x4) do not compile.
    5. Three of the marked lines (x1, x2, x3, x4) do not compile.
    6. None of the above.
  6. Which can fill in the blank so this code outputs true?
    import java.util.function.*;
    import java.util.stream.*;
    public class HideAndSeek {
       public static void main(String[] args) {
          var hide = Stream.of(true, false, true);
          Predicate<Boolean> pred = b -> b;
          var found = hide.filter(pred)._______________________(pred);
          System.out.println(found);
       } }
     
    1. Only anyMatch
    2. Only allMatch
    3. Both anyMatch and allMatch
    4. Only noneMatch
    5. Both noneMatch and anyMatch
    6. The code does not compile with any of these options.
  7. Suppose you have a consumer that calls the lion() method within a Lion service. You have four distinct modules: consumer, service locator, service provider, and service provider interface. If you add a parameter to the lion() method, how many of the modules require recompilation?
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
  8. What is the output of the following application?
    package music;
    interface DoubleBass {
       void strum();
       default int getVolume() {return 5;}
    }
    interface BassGuitar {
       void strum();
       default int getVolume() {return 10;}
    }
    abstract class ElectricBass implements DoubleBass, BassGuitar {
       @Override public void strum() {System.out.print("X");}
    }
    public class RockBand {
       public static void main(String[] strings) {
          final class MyElectricBass extends ElectricBass {
             public int getVolume() {return 30;}
             public void strum() {System.out.print("Y");}
          } } }
    
    1. X
    2. Y
    3. The application completes without printing anything.
    4. ElectricBass is the first class to not compile.
    5. RockBand is the first class to not compile.
    6. None of the above.
  9. Which condition is most likely to result in invalid data entering the system?
    1. Deadlock
    2. Livelock
    3. Resource not found
    4. Out of memory error
    5. Race condition
    6. Starvation
  10. Which statements are correct? (Choose two.)
    1. A Comparable implementation is often implemented by a lambda.
    2. A Comparable object has a compare() method.
    3. The compare() and compareTo() methods have the same contract for the return value.
    4. It is possible to sort the same List using different Comparator implementations.
    5. Two objects that return true for equals() will always return 0 when passed to compareTo().
  11. Which lines fail to compile?
    package armory;
    import java.util.function.*;
    interface Shield {
       void protect();
    }
    class Dragon {
       int addDragon(Integer count) {
         return ++count;
       } }
    public class Sword {
       public static void main(String[] knight) {
          var dragon = new Dragon();
          Function<Shield, Sword> func = Shield::protect; // line x
          UnaryOperator<Integer> op = dragon::addDragon;  // line y
       } }
    
    1. Only line x
    2. Only line y
    3. Both lines x and y
    4. The code compiles.
  12. What option names are equivalent to -p and -cp on the javac command? (Choose two.)
    1. --module-path and -classpath
    2. --module-path and -class-path
    3. --module-path and --class-path
    4. --path and -classpath
    5. --path and -class-path
    6. --path and --class-path
  13. What is the minimum number of lines that need to be changed or removed to make this method compile?
    11: public void colors() {
    12:    var yellow = "";
    13:    yellow = null;
    14:
    15:    var red = null;
    16:
    17:    var blue = "";
    18:    blue = 1;
    19:
    20:    var var = "";
    21:    var = "";
    22: 
    23:    var pink = 1;
    24: }
    
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
    6. Five
  14. What is the output of the following application?
    package ballroom;
    public class Dance {
       public static void swing(int… beats)
             throws ClassCastException {
          try {
             System.out.print("1"+beats[2]); // p1
          } catch (RuntimeException e) {
             System.out.print("2");
          } catch (Exception e) {
             System.out.print("3");
          } finally {
             System.out.print("4");
          } }
       public static void main(String[] music) {
          new Dance().swing(0,0);             // p2
          System.out.print("5");
       } }
    
    1. 145
    2. 1045
    3. 24, followed by a stack trace
    4. 245
    5. The code does not compile because of line p1.
    6. The code does not compile because of line p2.
  15. What is the output of the following application?
    1:  interface HasHue {String getHue();}
    2:  enum COLORS implements HasHue {
    3:     red {
    4:        public String getHue() {return "FF0000";}
    5:     }, green {
    6:        public String getHue() {return "00FF00";}
    7:     }, blue {
    8:        public String getHue() {return "0000FF";}
    9:     }
    10:    private COLORS() {}
    11: }
    12: class Book {
    13:    static void main(String[] pencils) {}
    14: }
    15: final public class ColoringBook extends Book {
    16:    final void paint(COLORS c) {
    17:       System.out.print("Painting: " + c.getHue());
    18:    }
    19:    final public static void main(String[] crayons) {
    20:       new ColoringBook().paint(green);
    21:    } }
    
    1. Painting: 00FF00
    2. Exactly one line of code does not compile.
    3. Exactly two lines of code do not compile.
    4. Three or more lines of code do not compile.
    5. The code compiles but prints an exception at runtime.
    6. None of the above.
  16. Which of the following methods can run without error for a SQL query that returns a count of matching rows?
    private static void choices(PreparedStatement ps,
          String sql) throws SQLException {
       try (var rs = ps.executeQuery()) {
          System.out.println(rs.getInt(1));
       } }
     
    private static void moreChoices(PreparedStatement ps, 
          String sql) throws SQLException {
       try (var rs = ps.executeQuery()) {
          rs.next();
          System.out.println(rs.getInt(1));
       } }
     
    private static void stillMoreChoices(PreparedStatement ps,
          String sql) throws SQLException {
       try (var rs = ps.executeQuery()) {
          if (rs.next())
             System.out.println(rs.getInt(1));
          }
       } }
    
    1. moreChoices()
    2. stillMoreChoices()
    3. choices() and stillMoreChoices()
    4. moreChoices() and stillMoreChoices()
    5. All three methods
    6. None of the above
  17. In most of the United States, daylight saving time ends on November 6 at 02:00 and we repeat that hour. What is the output of the following?
    var localDate = LocalDate.of(2022, Month.NOVEMBER, 6);
    var localTime = LocalTime.of(1, 0);
    var zone = ZoneId.of("America/New_York");
    var z = ZonedDateTime.of(localDate, localTime, zone);
    var offset = z.getOffset();
     
    for (int i = 0; i < 6; i++) 
       z = z.plusHours(1);
     
    System.out.print(z.getHour() + " "
       + offset.equals(z.getOffset()));
    
    1. 5 false
    2. 5 true
    3. 6 false
    4. 6 true
    5. 7 false
    6. 7 true
    7. The code does not compile.
    8. The code compiles but throws an exception at runtime.
  18. What is the output of the Light program?
    package physics;
    class Wave {
       public int size = 7;
    }
    public class Light extends Wave {
       public int size = 5;
       public static void main(String… emc2) {
          Light v1 = new Light();
          var v2 = new Light();
          Wave v3 = new Light();
          System.out.println(v1.size+","+v2.size+","+v3.size);
       } }
    
    1. 5,5,5
    2. 5,5,7
    3. 5,7,7
    4. 7,7,7
    5. The code does not compile.
    6. None of the above.
  19. Suppose we have a stored procedure named update_data that has one IN parameter named data. Which fills in the blank so the code runs without error?
    String sql = "{call update_data(?)}";
    try (Connection conn = DriverManager.getConnection(url);
       Statement cs = conn.prepareCall(sql)) {
     
       cs.____________(1, 6);
       var rs = cs.execute();
    }
     
    1. registerInParameter
    2. registerIntParameter
    3. registerInputParameter
    4. setIn
    5. setInt
    6. setInteger
    7. None of the above
  20. What are possible outputs of this code? (Choose two.)
    import java.time.LocalDate;
    public class Animal {
       private record Baby(String name, LocalDate birth) { }
       public static void main(String[] args) {
          LocalDate now = LocalDate.now();
          var b1 = new Baby("Teddy", now);
          var b2 = new Baby("Teddy", now);
          System.out.println((b1 == b2) + " " + b1.equals(b2));
          System.out.println(b1);
       } }
    
    1. false false
    2. false true
    3. true false
    4. true true
    5. Baby[name=Teddy, birth=2022-05-21]
    6. Baby@1d81eb93
  21. What is the output of calling the following method?
    public void countPets() {
       record Pet(int age) {}
       record PetSummary(long count, int sum) {}
     
       var summary = Stream.of(new Pet(2), new Pet(5), new Pet(8))
          .collect(Collectors.teeing(
             Collectors.counting(), 
             Collectors.summingInt(Pet::age)));
       System.out.println(summary);
    }
    
    1. PetSummary[count=1, sum=3]
    2. PetSummary[count=1, sum=15]
    3. PetSummary[count=3, sum=3]
    4. PetSummary[count=3, sum=15]
    5. The code does not compile due to the teeing() call.
    6. The code does not compile since records are defined inside a method.
    7. The code does not compile for another reason.
  22. Given the following, what can we infer about subtypes of First, called Chicken and Egg? (Choose two.)
    public sealed class First { }
    
    1. Chicken and Egg must be classes.
    2. Chicken and Egg can be classes, interfaces, enums, or records.
    3. Chicken and Egg must be located in a file other than First.
    4. Chicken and Egg must be located in the same file as First.
    5. Chicken and Egg must be located as nested classes of First.
    6. Chicken and Egg may be located in the same file or a different file as First.
  23. Which of the following statements about try/catch blocks are correct? (Choose two.)
    1. A catch block can never appear after a finally block.
    2. A try block must be followed by a catch block.
    3. A finally block can never appear after a catch block.
    4. A try block must be followed by a finally block.
    5. A try block can have zero or more catch blocks.
    6. A try block can have more than one finally block.
  24. What is the result of compiling and executing the following application?
     package reptile;
     public class Alligator {
        static int teeth;
        double scaleToughness;
        public Alligator() {
           this.teeth++;
        }
        public void snap(int teeth) {
           System.out.print(teeth+" ");
           teeth--;
        }
        public static void main(String[] unused) {
           new Alligator().snap(teeth);
           new Alligator().snap(teeth);
        } }
     
    1. 0 1
    2. 1 1
    3. 1 2
    4. 2 2
    5. The code does not compile.
    6. The code compiles but produces an exception at runtime.
    7. The answer cannot be determined ahead of time.
  25. Fill in the blank to make this code compile:
    String cheese = ServiceLoader.stream(Mouse.class)
       .map(__________________)
       .map(Mouse::favoriteFood)
       .findFirst()
       .orElse("");
     
    1. Mouse.get()
    2. Mouse::get
    3. Provider.get()
    4. Provider::get
    5. None of the above
  26. Which lines can fill in the blank that would allow the code to compile? (Choose two.)
    abstract public class Exam {
       boolean pass;
       protected abstract boolean passed();
       class JavaProgrammerCert extends Exam {
          private Exam part1;
          private Exam part2;
          _______________________________________
       }
    }
     

    1. boolean passed() {
         return part1.pass && part2.pass;
      }
       

    2. boolean passed() {
         return part1.passed() && part2.passed();
      }
       

    3. private boolean passed() {
         return super.passed();
      }
       

    4. public boolean passed() {
         return part1.passed() && part2.passed();
      }
       

    5. public boolean passed() {
         return part1.pass && part2.pass;
      }
       

    6. public boolean passed() {
         return super.passed();
      }
       
  27. How many of these lines print false?
    23: var smart = """
    24:    barn owl
    
    
    25:    wise
    26:    """;
    27: var clever = """
    28:      barn owl
    
    
    29:    wise
    30:    """;
    31: var sly = """
    32:    barn owl
    
    
    33:    wise""";
    34:
    35: System.out.println(smart.equals(smart.indent(0)));
    36: System.out.println(smart.equals(smart.strip()));
    37: System.out.println(smart.equals(smart.stripIndent()));
    38:
    39: System.out.println(clever.equals(clever.indent(0)));
    40: System.out.println(clever.equals(clever.strip()));
    41: System.out.println(clever.equals(clever.stripIndent()));
    42: 
    43: System.out.println(sly.equals(sly.indent(0)));
    44: System.out.println(sly.equals(sly.strip()));
    45: System.out.println(sly.equals(sly.stripIndent()));
    
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
    6. Five
    7. Six or more
    8. The code does not compile.
  28. Which are true statements about interfaces and abstract classes? (Choose three.)
    1. Abstract classes offer support for single inheritance, while interfaces offer support for multiple inheritance.
    2. All methods in abstract classes are implicitly public, while interfaces can use various access modifiers for their methods and variables, including private in some cases.
    3. Both abstract classes and interfaces can have abstract methods.
    4. Both abstract classes and interfaces can have public constructors.
    5. Interfaces can only extend other interfaces, while abstract classes can extend both abstract and concrete classes.
    6. Unlike abstract classes, interfaces can be marked final.
  29. Fill in the blanks: The operators +=, ______, ______, _______, _______, and -- are listed in increasing or the same level of operator precedence. (Choose two.)
    1. ^, *, =, ++
    2. %, *, /, &&
    3. =, +, /, *
    4. ^, *, ==, ++
    5. *, /, %, ++
    6. <=, >=, !=, !
  30. Given the following two classes in the same package, which constructors contain compiler errors? (Choose three.)
    public class Big {
       public Big(boolean stillIn) {
          super();
       } }
    public class Trouble extends Big {
       public Trouble()  {}
       public Trouble(int deep) {
          super(false);
          this();
       }
       public Trouble(String now, int… deep) {
          this(3);
       }
       public Trouble(long deep) {
          this("check", deep);
       }
       public Trouble(double test) {
          super(test>5 ? true : false);
       } }
    
    1. public Big(boolean stillIn)
    2. public Trouble()
    3. public Trouble(int deep)
    4. public Trouble(String now, int… deep)
    5. public Trouble(long deep)
    6. public Trouble(double test)
  31. What is the output of the following code snippet?
    11: Path x = Paths.get(".","song","..","/note");
    12: Path y = Paths.get("/dance/move.txt");
    13: x.normalize();
    14: System.out.println(x.resolve(y));
    15: System.out.println(y.resolve(x));
    

    1.  /./song/../note/dance/move.txt
       /dance/move.txt
       

    2.  /dance/move.txt
       /dance/move.txt/note
       

    3.  /dance/move.txt
       /dance/move.txt/./song/../note
       

    4.  /note/dance/move.txt
       ../dance/move.txt/song
       
    5. The code does not compile.
    6. The code compiles but an exception is thrown at runtime.
  32. How many lines does this code output?
    import java.util.*;
    public class PrintNegative {
      public static void main(String[] args) {
         List<Integer> list = new ArrayList<>();
         list.add(-5);
         list.add(0);
         list.add(5);
         list.removeIf(e -> e < 0);
         list.forEach(x -> System.out.println(x));
      } }
    
    1. One
    2. Two
    3. Three
    4. None. It doesn't compile.
    5. None. It throws an exception at runtime.
  33. What is the result of the following?
    public static void main(String[] args) {
       var list = Arrays.asList("0", "1", "01", "10");
     
       Collections.reverse(list);
       Collections.sort(list);
       System.out.println(list);
    }
    
    1. []
    2. [0, 01, 1, 10]
    3. [0, 01, 10, 1]
    4. [0, 1, 01, 10]
    5. The code does not compile.
    6. The code compiles but throws an exception at runtime.
  34. What is the result of the following?
    var dice = new TreeSet<Integer>();
    dice.add(6);
    dice.add(6);
    dice.add(4);
     
    dice.stream()
       .filter(n -> n != 4)
       .forEach(System.out::println)
       .count();
    
    1. It prints just one line.
    2. It prints one line and then the number 3.
    3. There is no output.
    4. The code does not compile.
    5. The code compiles but throws an exception at runtime.
  35. How many objects are eligible for garbage collection at the end of the main() method?
    package store;
    public class Shoes { 
       static String shoe1 = new String("sandal");
       static String shoe2 = new String("flip flop");
       public void shopping() {
          String shoe3 = new String("croc");
          shoe2 = shoe1;
          shoe1 = shoe3;
       }
       public static void main(String… args) {
          new Shoes().shopping();
       } }
    
    1. None
    2. One
    3. Two
    4. Three
    5. The code does not compile.
    6. None of the above.
  36. Which of the following are true? (Choose two.)
    20: int[] crossword [] = new int[10][20];
    21: for (int i = 0; i < crossword.length; i++)
    22:    for (int j = 0; j < crossword.length; j++)
    23:       crossword[i][j] = 'x'; 
    24: System.out.println(crossword.size());
    
    1. One line needs to be changed for this code to compile.
    2. Two lines need to be changed for this code to compile.
    3. Three lines need to be changed for this code to compile.
    4. If the code is fixed to compile, none of the cells in the 2D array have a value of 0.
    5. If the code is fixed to compile, half of the cells in the 2D array have a value of 0.
    6. If the code is fixed to compile, all of the cells in the 2D array have a value of 0.
  37. Which of the following can fill in the blank to output sea lion, bald eagle?
    String names = Stream.of(
       "bald eagle", "pronghorn", "puma", "sea lion")
     
       ____________________
     
       .collect(Collectors.joining(", "));
    System.out.println(names);
     

    1. .filter(s -> s.contains(" "))
      .collect(Collectors.toSet())
      .stream()
      .entrySet()
      .stream()
      .filter(e -> e.getKey())
      .map(Entry::getValue)
      .flatMap(List::stream)
      .sorted(Comparator.reverseOrder())
       

    2. .filter(s -> s.contains(" "))
      .collect(Collectors.toUnmodifiableSet())
      .stream()
      .entrySet()
      .stream()
      .filter(e -> e.getKey())
      .map(Entry::getValue)
      .flatMap(List::stream)
      .sorted(Comparator.reverseOrder())
       

    3. .collect(Collectors.toUnmodifiableSet())
      .stream()
      .collect(Collectors.groupingBy(s -> s.contains(" ")))
      .entrySet()
      .stream()
      .filter(e -> e.getKey())
      .map(Entry::getValue)
      .map(List::stream)
      .sorted(Comparator.reverseOrder())
       

    4. .collect(Collectors.toSet())
      .stream()
      .collect(Collectors.groupingBy(s -> s.contains(" ")))
      .entrySet()
      .stream()
      .filter(e -> e.getKey())
      .map(Entry::getValue)
      .flatMap(List::stream)
      .sorted(Comparator.reverseOrder())
       

    5. .filter(s -> s.contains(" "))
      .collect(Collectors.toUnmodifiableSet())
      .stream()
      .collect(Collectors.groupingBy(s -> s.contains(" ")))
      .entrySet()
      .stream()
      .filter(e -> e.getKey())
      .map(Entry::getValue)
      .map(List::stream)
      .sorted(Comparator.reverseOrder())
       

    6. .collect(Collectors.toUnmodifiableSet())
      .stream()
      .collect(Collectors.groupingBy(s -> s.contains(" ")))
      .entrySet()
      .stream()
      .filter(e -> e.getKey())
      .map(Entry::getValue)
      .map(List::stream)
      .sorted(Comparator.reverseOrder())
       
  38. What is the output of the following code snippet?
    LocalDate dogDay = LocalDate.of(2022,8,26);
     
    var x = DateTimeFormatter.ISO_DATE;
    System.out.println(x.format(dogDay));
     
    var y = DateTimeFormatter.ofPattern("Dog Day: dd/MM/yy");
    System.out.println(y.format(dogDay));
     
    var z = DateTimeFormatter.ofPattern("Dog Day: dd/MM/yy");
    System.out.println(dogDay.format(z));
    
    1. 2022-07-26 and Dog Day: 26/07/22
    2. 2022-07-25 and Dog Day: 25/07/22
    3. 2022-08-26 and Dog Day: 26/08/22
    4. The code does not compile.
    5. An exception is thrown at runtime.
    6. None of the above.
  39. How many of the following lines contain a compiler error?
    long min1 = 123.0, max1 = 987L;
    final long min2 = 1_2_3, max2 = 9 ____________ 8 ____________ 7;
    long min3 = 123, int max3 = 987;
    long min4 = 123L, max4 = 987;
    long min5 = 123_, max5 = _987;
     
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
    6. Five
  40. Fill in the blanks: Using ________ and ________ together allows a variable to be accessed from any class, without requiring an instance variable.
    1. class, static
    2. default, public
    3. final, package (no modifier)
    4. protected, instance
    5. public, static
    6. None of the above
  41. Which of the following sequences can fill in the blanks so the code prints -1 0 2?
    char[][] letters = new char[][] {
       new char[] { 'a', 'e', 'i', 'o', 'u'},
       new char[] { 'a', 'e', 'o', 'u'} };
     
    var x = Arrays.____________(letters[0], letters[0]);
    var y = Arrays.____________(letters[0], letters[0]);
    var z = Arrays.____________(letters[0], letters[1]);
     
    System.out.print(x + " " + y + " " + z);
     
    1. compare, mismatch, compare
    2. compare, mismatch, mismatch
    3. mismatch, compare, compare
    4. mismatch, compare, mismatch
    5. None of the above
  42. What is the output of the following application? Assume the file system is available and able to be written to and read from.
    package boat;
    import java.io.*;
    public class Cruise {
       private int numPassengers = 1;
       private transient String schedule = "NONE";
       { numPassengers = 2; }
       public Cruise() {
          this.numPassengers = 3;
          this.schedule = "Tropical Island";
       }
     
       public static void main(String… p) throws Exception {
          final String f = "ship.txt";
          try (var o = new ObjectOutputStream(
                new FileOutputStream(f))) {
             Cruise c = new Cruise();
             c.numPassengers = 4;
             c.schedule = "Casino";
             o.writeObject(c);
          }
          try (var i = new ObjectInputStream(
                new FileInputStream(f))) {
             Cruise c = i.readObject();
             System.out.print(c.numPassengers + "," + c.schedule);
          } } }
    
    1. 2,NONE
    2. 3,null
    3. 4,Casino
    4. 4,null
    5. One line would need to be fixed for this code to run without throwing an exception.
    6. Two lines would need to be fixed for this code to run without throwing an exception.
  43. How many lines does the following program output?
    class Coin {
       enum Side { HEADS, TAILS };
       public static void main(String[] args) {
          var sides = Side.values();
          for (var s : sides)
             for (int i=sides.length; i>0 ; i-=2)
                System.out.print(s+" "+sides[i]);
                System.out.println();
       }
    }
    
    1. One
    2. Two
    3. Four
    4. The code does not compile.
    5. The code compiles but throws an exception at runtime.
    6. The code compiles but enters an infinite loop at runtime.
  44. What is the minimum number of lines that need to be removed to make this code compile and be able to be implemented as a lambda expression?
    @FunctionalInterface
    public interface Play {
       public static void baseball() {}
       private static void soccer() {}
       default void play() {}
       void fun();
       void game();
       void toy();
    }
    
    1. 1
    2. 2
    3. 3
    4. 4
    5. The code compiles as is.
  45. What is the output of this code?
    10: var m = new TreeMap<Integer, Integer>();
    11: m.put(1, 4);
    12: m.put(2, 8);
    13:
    14: m.putIfAbsent(2, 10);
    15: m.putIfAbsent(3, 9);
    16:
    17: m.replaceAll((k, v) -> k + 1);
    18: 
    19: m.entrySet().stream()
    20:    .sorted(Comparator.comparing(Entry::getKey))
    21:    .limit(1)
    22:    .map(Entry::getValue)
    23:    .forEach(System.out::println);
    
    1. 1
    2. 2
    3. 3
    4. 4
    5. The code does not compile.
    6. The code compiles but prints something else.
  46. Given the following three property files, what does the following method output?
    toothbrush.properties
    color=purple
    type=generic
     
    toothbrush_es.properties
    color=morado
    type=lujoso
     
    toothbrush_fr.properties
    color=violette
     
    void brush() {
       Locale.setDefault(new Locale.Builder()
          .setLanguage("es")
          .setRegion("MX").build());
       var rb = ResourceBundle.getBundle("toothbrush",
          new Locale("fr"));
       var a = rb.getString("color");
       var b = rb.getString("type");
       System.out.print(a + " " + b);
    }
    
    1. morado null
    2. violette generic
    3. morado lujoso
    4. violette null
    5. The code does not compile.
    6. An exception is thrown at runtime.
  47. Which options, when inserted into the blank, allow this code to compile? (Choose three.)
    import java.io.*;
    class Music {
       void make() throws IOException {
          throw new UnsupportedOperationException();
       } }
    public class Sing extends Music {
       public void make() _____________________ {
          System.out.println("do-re-mi-fa-so-la-ti-do");
       } }
     
    1. throws FileNotFoundException
    2. throws NumberFormatException
    3. throws Exception
    4. throws SQLException
    5. throws Throwable
    6. Placing nothing in the blank
  48. Given the application shown here, which lines do not compile? (Choose three.)
    package furryfriends;
    interface Friend {
       protected String getName();                    // h1
    }
    class Cat implements Friend {
       String getName() {                             // h2
          return "Kitty";
       } }
    public class Dog implements Friend {
       String getName() throws RuntimeException {     // h3
          return "Doggy";
       }
       public static void main(String[] adoption) {
          Friend friend = new Dog();                  // h4
          System.out.print(((Cat)friend).getName());  // h5
          System.out.print(((Dog)null).getName());    // h6
       } }
    
    1. Line h1
    2. Line h2
    3. Line h3
    4. Line h4
    5. Line h5
    6. Line h6
  49. What is the output of the following?
    1: package reader;
    2: import java.util.stream.*;
    3: public class Books {
    4:    public static void main(String[] args) {
    5:       IntStream pages = IntStream.of(200, 300);
    6:       long total = pages.sum();
    7:       long count = pages.count();
    8:       System.out.println(total + "-" + count);
    9:    } }
    
    1. 2-2
    2. 200-1
    3. 500-0
    4. 500-2
    5. The code does not compile.
    6. The code compiles but throws an exception at runtime.
  50. _______________ modules are on the classpath, while ____________ modules never contain a module-info file.
    1. Automatic, named
    2. Automatic, unnamed
    3. Named, automatic
    4. Named, unnamed
    5. Unnamed, automatic
    6. Unnamed, named
..................Content has been hidden....................

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