Chapter 2
Controlling Program Flow

  1. Which statements are true of the following code? (Choose two.)
    public class Penguins {
        public static void main(String[] args) {
           var pen = new Penguins();
           pen.length("penguins");
           pen.length(5);
           pen.length(new Object());
       }
       public void length(Object obj) {
         if (obj instanceof String x) 
            System.out.println(x.length());
       } }
    
    1. The code compiles as is.
    2. One line causes compiler errors.
    3. Two lines cause compiler errors.
    4. If any lines that do not compile are removed, this code does not print anything.
    5. If any lines that do not compile are removed, this code prints one line.
    6. If any lines that do not compile are removed, this code prints two lines.
  2. Variables declared as which of the following are never permitted in a switch statement? (Choose two.)
    1. var
    2. double
    3. int
    4. String
    5. char
    6. Object
  3. When can you omit the default condition in a switch expression? (Choose two.)
    1. When all the values of an enum are covered
    2. When no value is returned
    3. When the type is a Boolean
    4. When the type is a Byte
    5. When the type is a Boolean or Byte
  4. What happens when running the following code snippet?
     3: var gas = true;
     4: do (
     5:    System.out.println("helium");
     6:    gas = gas ^ gas;
     7:    gas = !gas;
     8: ) while (!gas);
    
    1. It completes successfully without output.
    2. It outputs helium once.
    3. It outputs helium repeatedly.
    4. Line 6 does not compile.
    5. None of the above.
  5. What is output by the following?
     10: int m = 0, n = 0;
     11: while (m < 5) {
     12:    n++;
     13:    if (m == 3) 
     14:       continue;
     15:   
     16:    switch (m) {
     17:       case 0:
     18:       case 1:
     19:          n++;
     20:       default:
     21:          n++;
     22:    }
     23:    m++;
     24: }
     25: System.out.println(m + " " + n);
     
    1. 3 10
    2. 3 12
    3. 5 10
    4. 5 12
    5. The code does not compile.
    6. None of the above.
  6. What is true of the following program?
     enum Admission { ADULT, SENIOR, CHILD}
     public class Movie {
        public static void main(String[] args) {
           var price = switch (Admission.CHILD) {
              case ADULT -> 12.50;
               case SENIOR, CHILD -> 10;
           };
           System.out.println(price);
        } }
     
    1. The code does not compile because the return types of the case branches are different.
    2. The code does not compile because one of the case branches has two values.
    3. The code does not compile because the value being evaluated in the switch is hard coded.
    4. The code does not compile because there are too many semicolons.
    5. The code compiles and prints 10.
    6. The code compiles and prints 10.0.
  7. Given the following, which can fill in the blank and allow the code to compile? (Choose three.)
     var quest = _________________;
     for(var zelda : quest) {
        System.out.print(zelda);
     }
     
    1. 3
    2. new int[] {3}
    3. new StringBuilder("3")
    4. List.of(3)
    5. new String[3]
    6. "Link"
  8. Which of the following rules about adding a default branch to this switch statement are correct? (Choose two.)
     switch (numPenguins) {
        case 0 : System.out.println("no penguins");
        case 1 : System.out.println("one penguin");
     }
     
    1. This switch statement is required to declare a default statement.
    2. A default statement must be placed after all case statements.
    3. A default statement can be placed between any case statements.
    4. Unlike a case statement, a default statement does not take a parameter value.
    5. This switch statement can contain more than one default statement.
    6. A default statement can be used only when at least one case statement is present.
  9. What does the following method output?
     void dance() {
        var singer = 0;
        while (singer)
           System.out.print(singer++);
     }
     
    1. The method does not compile.
    2. The method completes with no output.
    3. The method prints 0 and then terminates.
    4. The method enters an infinite loop.
    5. None of the above.
  10. How many lines contain compiler errors?
     22: int magicNumber = 7;
     23:    var ok = switch (magicNumber) {
     24:       case 7 -> true;  break;
     25:       case 9 -> { yield true }
     26:       case 11 -> yield true;
     27:       case 13 : {yield true;}
     28:       default -> false;
     29: }
     
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
    6. Five
  11. Which are true statements comparing for-each and traditional for loops? (Choose two.)
    1. Both can iterate through an array starting with the first element.
    2. Only the for-each loop can iterate through an array starting with the first element.
    3. Only the traditional for loop can iterate through an array starting with the first element.
    4. Both can iterate through an array starting from the end.
    5. Only the for-each loop can iterate through an array starting from the end.
    6. Only the traditional for loop can iterate through an array starting from the end.
  12. What statements are true about filling in the blank and calling with zero(0)? (Choose two.)
     public void zero(Object number) {
        if (number instanceof _________________ Math.abs(n) == 0)
           System.out.println("zero");
        else
           System.out.println("non-zero");
     }
     
    1. When filling in the blank with Integer n ||, the code does not compile.
    2. When filling in the blank with Integer n ||, the output is zero.
    3. When filling in the blank with Integer n ||, the output is non-zero.
    4. When filling in the blank with int n &&, the code does not compile.
    5. When filling in the blank with int n &&, the output is zero.
    6. When filling in the blank with int n &&, the output is non-zero.
  13. What is the output of the following application?
     package planning;
     public class ThePlan {
        public static void main(String[] input) {
           var plan = 1;
           plan = plan++ + --plan;
           if(plan==1) {
              System.out.print("Plan A");
           } else { if(plan==2) System.out.print("Plan B"); }
           } else System.out.print("Plan C");
           }
        }
     }
     
    1. Plan A
    2. Plan B
    3. Plan C
    4. The class does not compile.
    5. None of the above.
  14. What is true about the following code? (Choose two.)
     23: var race = "";
     24: loop:
     25: do {
     26:    race += "x";
     27:    break loop;
     28: } while (true);
     29: System.out.println(race);
     
    1. It outputs x.
    2. It does not compile.
    3. It is an infinite loop.
    4. With lines 25 and 28 removed, it outputs x.
    5. With lines 25 and 28 removed, it does not compile.
    6. With lines 25 and 28 removed, it is an infinite loop.
  15. What does the following code output?
     int count = 0;
     char letter = 'A';
     switch (letter) {
        case 'A' -> count++;
        case 'B' -> count++;
     }
     System.out.println(count);
     
    1. 0
    2. 1
    3. 2
    4. The code does not compile.
  16. Which of the following can replace the body of the perform() method to produce the same output on any nonempty input? (Choose two.)
     public void perform(String[] circus) {
        for (int i=circus.length-1; i>=0; i--)
           System.out.print(circus[i]);
     }
     

    1.  for (int i=circus.length; i>0; i--)
          System.out.print(circus[i-1]);
       

    2.  for-reversed (String c = circus)
          System.out.print(c);
       

    3.  for (var c : circus)
          System.out.print(c);
       

    4.  for (var i=0; i<circus.length; i++) 
          System.out.print(circus[circus.length-i-1]);
       

    5.  for (int i=circus.length; i>0; i--)
          System.out.print(circus[i+1]);
       

    6.  for-each (String c circus)
          System.out.print(c);
       
  17. What does the following code snippet output?
     var bottles = List.of("glass", "plastic", "can");
     for (int type = 1; type < bottles.size();) {
        System.out.print(bottles.get(type) + "-");
        if(type < bottles.size()) break;
     }
     System.out.print("end");
     
    1. glass-end
    2. glass-plastic-can-end
    3. plastic-end
    4. plastic-can-end
    5. The code does not compile.
    6. None of the above.
  18. What is the result of executing the following code snippet?
     final var GOOD = 100;
     var score = 10;
     switch (score) {
        default:
        1 : System.out.print("1-");
        -1 : System.out.print("2-"); break;
        4,5 : System.out.print("3-");
        6 : System.out.print("4-");
        9 : System.out.print("5-");
     }
     
    1. 1-
    2. 1-2-
    3. 2-
    4. 3-
    5. 4-
    6. None of the above
  19. What is the output of the following application?
     package dinosaur;
     public class Park {
        public final static void main(String… arguments) {
           int pterodactyl = 8;
           long triceratops = 3;
           if(pterodactyl % 3> 1 + 1)
              triceratops++;
              triceratops--;
           System.out.print(triceratops);
        } }
     
    1. 2
    2. 3
    3. 4
    4. The code does not compile.
    5. The code compiles but throws an exception at runtime.
  20. What variable type of red allows the following application to compile?
     package tornado;
     public class Kansas {
        public static void main(String[] args) {
           int colorOfRainbow = 10;
           _________________ red = 5;
           switch(colorOfRainbow) {
              default:
                 System.out.print("Home");
                 break;
              case red:
                 System.out.print("Away");
           } } }
     
    1. long
    2. double
    3. int
    4. var
    5. String
    6. None of the above
  21. What is true about the following method when calling with an empty ArrayList? (Choose two.)
     public void meow(Collection<String> kitties) {
        if (kitties instanceof List c) {
           System.out.println("L " + c.size());
        } else if (kitties instanceof Map c) {
           c = new TreeMap<>();                  // x1
           System.out.println("M " + c.size());
        } else {
           System.out.println("E " + c.size());
        }
     }
     
    1. The code compiles.
    2. The code does not compile due to line x1.
    3. The code does not compile for another reason.
    4. If any lines that do not compile are removed, the output is L0.
    5. If any lines that do not compile are removed, the output is E0.
    6. If any lines that do not compile are removed, the output is another value.
  22. How many lines of the magic() method contain compilation errors?
     10: public void magic() {
     11:    do {
     12:       int trick = 0;
     13:       LOOP: do {
     14:          trick++;
     15:       } while (trick < 2--);
     16:       continue LOOP;
     17:    } while (1> 2);
     18:    System.out.println(trick);
     19: }
     
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
  23. How many of these statements can be inserted after the println to have the code flow follow the arrow in this diagram?
     break;
     break letters;
     break numbers;
     continue;
     continue letters;
     continue numbers;
     
    An illustration of the code flow. An arrow pointing the first line.
    1. One
    2. Two
    3. Three
    4. Four
    5. Five
    6. None of above
  24. What is the output of the following application?
     package dessert;
     public class IceCream {
        public final static void main(String… args) {
           var flavors = 30;
           int eaten = 0;
           switch(flavors) {
              case 30: eaten++;
              case 40: eaten+=2;
               default: eaten--;
           }
           System.out.print(eaten);
        } }
     
    1. 1
    2. 2
    3. 3
    4. The code does not compile because var cannot be used in a switch statement.
    5. The code does not compile for another reason.
    6. None of the above.
  25. Which of the following statements compile and create infinite loops at runtime? (Choose three.)
    1. while (!false) {}
    2. do {}
    3. for( : ) {}
    4. do {} while (true);
    5. while {}
    6. for( ; ; ) {}
  26. How many of these methods compile?
     public void m(Object obj) {
        if (obj instanceof LocalDate date)
           System.out.println(date);
        else
           System.out.println(date);
     }
     public void n(Object obj) {
        if (obj instanceof LocalDate date)
           return;
        else
          System.out.println(date);
     }
     public void o(Object obj) {
        if (!obj instanceof LocalDate date)
           return;
        else
           System.out.println(date);
     }
     public void p(Object obj) {
        if (!(obj instanceof LocalDate date))
           return;
        else
           System.out.println(date);
     }
     public void q(Object obj) {
        if (!obj instanceof LocalDate date)
           return;
        System.out.println(date);
     }
     public void r(Object obj) {
        if (!(obj instanceof LocalDate date))
           return;
        System.out.println(date);
     }
     
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
    6. Five
    7. Six
  27. Which of the following iterates a different number of times than the others?
    1. for (int k=0; k < 5; k++) {}
    2. for (int k=1; k <= 5; k++) {}
    3. int k=0; do { } while(k++ < 5);
    4. int k=0; while (k++ < 5) {}
    5. All of these iterate the same number of times.
  28. What is the output of the following code snippet?
     int count = 0;
     var stops = new String[] { "Washington", "Monroe",
        "Jackson", "LaSalle" };
     while (count < stops.length)
        if (stops[++count].length() < 8)
           break;
        else continue;
     System.out.println(count);
     
    1. 0
    2. 1
    3. 2
    4. 3
    5. The code does not compile.
    6. None of the above.
  29. What is the output of the following code snippet?
     int hops = 0;
     int jumps = 0;
     jumps = hops++;
     if(jumps)
        System.out.print("Jump!");
     else
        System.out.print("Hop!");
     
    1. Jump!
    2. Hop!
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
    5. None of the above.
  30. Which of the following best describes the flow of execution in this for loop if beta always returns false?
     for (alpha; beta; gamma) {
       delta;
     }
     
    1. alpha
    2. alpha, beta
    3. alpha, beta, gamma
    4. alpha, gamma
    5. alpha, gamma, beta
    6. None of the above
  31. What is the output of the following code snippet?
     boolean balloonInflated = false;
     do {
        if (!balloonInflated) {
           balloonInflated = true;
           System.out.print("inflate-");
        }
     } while (!balloonInflated);
     System.out.println("done");
     
    1. done
    2. inflate-done
    3. The code does not compile.
    4. This is an infinite loop.
    5. None of the above.
  32. Which are true about switch expressions and switch statements? (Choose three.)
    1. Both allow assigning the result to a variable.
    2. Both allow multiple values in the same case.
    3. Only a switch expression supports break.
    4. Only a switch statement supports break.
    5. A switch expression is more compact.
    6. A switch statement is more compact.
  33. Which of these code snippets behaves differently from the others?

    1.  if (numChipmunks == 1)
          System.out.println("One chipmunk");
       if (numChipmunks == 2)
          System.out.println("Two chipmunks");
       if (numChipmunks == 3)
          System.out.println("Three chipmunks");
       

    2.  switch (numChipmunks) {
          case 1:  System.out.println("One chipmunk");
          case 2:  System.out.println("Two chipmunks");
          case 3:  System.out.println("Three chipmunks");
       }
       

    3.  if (numChipmunks == 1)
          System.out.println("One chipmunk");
       else if (numChipmunks == 2)
          System.out.println("Two chipmunks");
       else if (numChipmunks == 3)
          System.out.println("Three chipmunks");
       
    4. All three code snippets do the same thing.
  34. Which statements about loops are correct? (Choose three.)
    1. A do/while loop requires a body.
    2. A while loop cannot be exited early with a return statement.
    3. A while loop requires a conditional expression.
    4. A do/while loop executes the body (if present) at least once.
    5. A do/while loop cannot be exited early with a return statement.
    6. A while loop executes the body (if present) at least once.
  35. Given the following enum and class, which option fills in the blank and allows the code to compile?
     enum Season { SPRING, SUMMER, WINTER }
     public class Weather {
        public int getAverageTemperate(Season s) {
           switch (s) {
              default:
              _________________ return 30;
           } } }
     
    1. case Season.WINTER:
    2. case WINTER, SPRING:
    3. case SUMMER | WINTER:
    4. case SUMMER ->
    5. case FALL:
    6. None of the above
  36. Fill in the blank with the line of code that causes the application to compile and print exactly one line at runtime.
     package nyc;
     public class TourBus {
        public static void main(String… args) {
           var nycTour = new String[] { "Downtown", "Uptown",
              "Brooklyn" };
           var times = new String[] { "Day", "Night" };
           for (_________________ i<nycTour.length && j<times.length;
                 i++, j++)
              System.out.println(nycTour[i] + "-" + times[j]);
        } }
     
    1. int i=1; j=1;
    2. int i=0, j=1;
    3. int i=1; int j=0;
    4. int i=1, int j=0;
    5. int i=1, j=0;
    6. None of the above
  37. What statements are true of the following code? (Choose two.)
     public class Penguins {
         public static void main(String[] args) {
            var pen = new Penguins();
            pen.length("penguins");
            pen.length(5);
            pen.length(new Object());
        }
        public void length(Object obj) {
           if (obj instanceof String) {
              System.out.println(obj.length());
          } } }
     
    1. The code compiles as is.
    2. One line causes compiler errors.
    3. Two lines cause compiler errors.
    4. If any lines that do not compile are removed, this code does not print anything.
    5. If any lines that do not compile are removed, this code prints one line.
    6. If any lines that do not compile are removed, this code prints two lines.
  38. The following code contains six pairs of curly braces. How many pairs can be removed without changing the behavior?
     12: public static void main(String[] args) {
     13:    int secret = 0;
     14:    for (int i = 0; i < 10; i++) {
     15:       while (i < 10) {
     16:          if (i == 5) {
     17:             System.out.println("if");
     18:          } else {
     19:             System.out.println("in");
     20:             System.out.println("else");
     21:          }
     22:       }
     23:    }
     24:    switch (secret) {
     25:       case 0:  System.out.println("zero");
     26:    }
     27: }
     
    1. One
    2. Two
    3. Three
    4. Four
    5. Five
    6. Six
  39. Which of the following can replace the body of the travel() method to produce the same output on any nonempty input?
     public void travel(List<Integer> roads) {
        for (int w = 1; w <= roads.size(); w++)
           System.out.print(roads.get(w-1));
     }
     

    1.  for (int r = 0; r < roads.size(); r += 1)
          System.out.print(roads.get(0));
       

    2.  for(var z : roads)
          System.out.print(z);
       

    3.  for (int t = roads.size(); t> 0; t--)
          System.out.print(roads.get(t));
       

    4.  for (var var : roads)
          System.out.print(roads);
       

    5.  for (int q = roads.size(); q>= 0; q++)
          System.out.print(roads.get(q));
       
    6. None of the above
  40. Which statement about the following code snippet is correct?
     3: final var javaVersions = List.of(17,11,8);
     4: var names = List.of("JDK", "Java");
     5: V: for (var e1 : javaVersions) {
     6:    E: for (String e2 : names)
     7:       System.out.println(e1 + "_" + e2);
     8:       break;
     9: }
     
    1. One line does not compile.
    2. Two lines do not compile.
    3. Three lines do not compile.
    4. It compiles and prints two lines at runtime.
    5. It compiles and prints three lines at runtime.
    6. None of the above.
..................Content has been hidden....................

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