Chapter 3
Utilizing Java Object-Oriented Approach

  1. How many lines would have to be changed for the following code to compile?
    1: enum Color {
    2:    public static Color DEFAULT = BROWN;
    3:    BROWN, YELLOW, BLACK;
    4: }
    5: public record Pony {
    6:    String name;
    7:    static int age;
    8:    { age = 10;}
    9: }
    
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four or more
  2. Which modifiers can be applied to a sealed subclass? (Choose three.)
    1. nonsealed
    2. default
    3. sealed
    4. unsealed
    5. non-sealed
    6. closed
    7. final
  3. What is the output of the following application?
    package dnd;
    final class Story {
       void recite(int chapter) throws Exception {}
    }
    public class Adventure extends Story {
       final void recite(final int chapter) {  // g1
          switch(chapter) {                    // g2
             case 2: System.out.print(9);
             default: System.out.print(3);
          }
       }
       public static void main(String… u) {
          var bedtime = new Adventure();
          bedtime.recite(2);
       } }
    
    1. 3
    2. 9
    3. 93
    4. The code does not compile because of line g1.
    5. The code does not compile because of line g2.
    6. None of the above.
  4. Which of the following lines of code are not permitted as the first line of a Java class file? (Choose two.)
    1. import widget.*;
    2. // Widget Manager
    3. int facilityNumber;
    4. package sprockets;
    5. /** Author: Cid **/
    6. void produce() {}
  5. Which of the following modifiers can be applied to an abstract method? (Choose two.)
    1. final
    2. private
    3. public
    4. default
    5. protected
    6. concrete
  6. What is the result of compiling and executing the following class?
    1: public class ParkRanger {
    2:    final static int birds = 10;
    3:    public static void main(String[] data) {
    4:       var r = new ParkRanger();
    5:       var trees = 5f;
    6:       System.out.print(trees + r.birds);
    7:    } }
    
    1. It compiles and outputs 5.
    2. It compiles and outputs 5.0.
    3. It compiles and outputs 15.
    4. It compiles and outputs 15.0.
    5. It does not compile.
    6. It compiles but throws an exception at runtime.
  7. Fill in the blanks: The ___________________ access modifier allows access to everything the ___________________ access modifier does and more.
    1. package, protected
    2. private, package
    3. private, protected
    4. private, public
    5. public, private
    6. None of the above
  8. Which set of modifiers, when added to a default method within an interface, prevents it from being overridden by a class implementing the interface?
    1. const
    2. final
    3. static
    4. private
    5. private static
    6. None of the above
  9. Given the following application, fill in the missing values in the table starting from the top and going down.
    package competition;
    public class Robot {
       static String weight = "A lot";
       double ageMonths = 5, ageDays = 2;
       private static boolean success = true;
     
       public void compete() {
          final String retries = "1";
          // P1
       } }
    
    Variable TypeNumber of Variables Accessible at P1
    Class_____
    Instance_____
    Local_____
    1. 2, 0, 1
    2. 2, 2, 1
    3. 1, 0, 2
    4. 0, 2, 0
    5. 2, 1, 1
    6. The class does not compile.
  10. Given the following code, what values inserted, in order, into the blank lines allow the code to compile? (Choose two.)
    _____________ agent;
    public _____________ Banker {
       private static _____________ getMaxWithdrawal() {
          return 10;
       } }
    
    1. package, new, int
    2. package, class, long
    3. import, class, null
    4. //, class, int
    5. import, interface, void
    6. package, class, void
  11. Which of the following are correct? (Choose two.)
    public class Phone {
       private int size;
     
       // LINE X
     
       public static void sendHome(Phone p, int newSize) {
          p = new Phone(newSize);
          p.size = 4;
       }
       public static final void main(String… params) {
          final var phone = new Phone(3);
          sendHome(phone,7);
          System.out.print(phone.size);
       } }
    
    1. Insert this constructor at LINE X:
      public static Phone create(int size) {
         return new Phone(size);
      }
      
    2. Insert this constructor at LINE X:
      public static Phone newInstance(int size) {
         return new Phone();
      }
      
    3. Insert this constructor at LINE X:
      public Phone(int size) {
         size = this.size;
      }
      
    4. Insert this constructor at LINE X:
      public void Phone(int size) {
         size = this.size;
      }
      
    5. With the correct constructor, the output is 0.
    6. With the correct constructor, the output is 3.
    7. With the correct constructor, the output is 7.
  12. Given the following class structures, which lines can be inserted into the blank independently that would allow the class to compile? (Choose two.)
    public class Dinosaur {
       class Pterodactyl extends Dinosaur {}
       public void roar() {
          var dino = new Dinosaur();
          ____________________________;
       } }
    
    1. dino.Pterodactyl()
    2. Dinosaur.new Pterodactyl()
    3. dino.new Pterodactyl()
    4. new Dino().new Pterodactyl()
    5. new Dinosaur().Pterodactyl()
    6. new Dinosaur.Pterodactyl()
  13. What is the output of the Computer program?
    class Laptop extends Computer {
        public void startup() {
            System.out.print("laptop-");
        } }
    public class Computer {
       public void startup() {
          System.out.print("computer-");
       }
       public static void main(String[] args) {
          Computer computer = new Laptop();
          Laptop laptop = new Laptop();
          computer.startup();
          laptop.startup();
       } }
    
    1. computer-laptop-
    2. laptop-computer-
    3. laptop-laptop-
    4. The code does not compile.
    5. None of the above.
  14. How many lines does the following code output?
    public class Cars {
       private static void drive() {
           {
              System.out.println("zoom");
           }
           System.out.println("fast");
       }
       static { System.out.println("faster"); }
       public static void main(String[] args) {
          drive();
          drive();
       } }
    
    1. One
    2. Two
    3. Three
    4. Four
    5. Five
    6. Six or more
    7. None of the above. The code does not compile.
  15. Which statements about static interface methods are correct? (Choose three.)
    1. A static interface method can be final.
    2. A static interface method can be declared private.
    3. A static interface method can be declared with package access.
    4. A static interface method can be declared public.
    5. A static interface method can be declared protected.
    6. A static interface method can be declared without an access modifier.
  16. Not counting the Planet declaration, how many declarations compile? Assume they are all declared within the same .java file.
    public abstract sealed class Planet permits Mercury, Venus, Earth {}
     
    non-sealed class Venus {}
    non-sealed class Mars extends Planet {}
    non-sealed class Mercury {}
    abstract non-sealed class Earth {}
    
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
  17. What is the result of executing the following program?
    public class Canine {
       public String woof(int bark) {
          return "1"+bark.toString();
       }
       public String woof(Integer bark) {
          return "2"+bark.toString();
       }
       public String woof(Object bark) {
          return "3"+bark.toString();
       }
       public static void main(String[] a) {
          System.out.println(woof((short)5));
       } }
    
    1. 15
    2. 25
    3. 35
    4. One line does not compile.
    5. Two lines do not compile.
    6. The program compiles but throws an exception at runtime.
  18. What statement best describes the notion of effectively final in Java?
    1. A local variable that is marked final.
    2. A static variable that is marked final.
    3. A local variable whose primitive value or object reference does not change after it is initialized.
    4. A local variable whose primitive value or object reference does not change after a certain point in the method.
    5. None of the above.
  19. What is the output of the Turnip class?
    interface GameItem {
       int sell();
    }
    abstract class Vegetable implements GameItem {
       public final int sell() { return 5; }
    }
    public class Turnip extends Vegetable {
       public final int sell() { return 3; }
       public static void main(String[] expensive) {
          System.out.print(new Turnip().sell());
       } }
    
    1. 3
    2. 5
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
    5. None of the above.
  20. What is the output of the following application?
    package holiday;
    enum DaysOff {
       Thanksgiving, PresidentsDay, ValentinesDay
    }
    public class Vacation {
       public static void main(String[] unused) {
          final DaysOff input = DaysOff.Thanksgiving;
          switch(input) {
             default:
             case DaysOff.ValentinesDay:
                System.out.print("1");
             case DaysOff.PresidentsDay:
                System.out.print("2");
          } } }
    
    1. 1
    2. 2
    3. 12
    4. The code does not compile.
    5. The code compiles but throws an exception at runtime.
    6. None of the above.
  21. What is the output of the following program?
    record Animal(boolean isMammal) {}
    public record Panda(String name) extends Animal {
       public Panda() {
          this("TaiShan");
       }
       public Panda {
          this.name = name.toLowerCase();
       }
       public static void main(String[] args) {
          System.out.print(new Panda().name());
       } }
    
    1. TaiShan
    2. taishan
    3. Exactly one line needs to be corrected for the program to compile.
    4. Exactly two lines need to be corrected for the program to compile.
    5. Three or more lines need to be corrected for the program to compile.
    6. The code compiles but prints an exception at runtime.
  22. Which statements about instance keywords are correct? (Choose two.)
    1. The that keyword can be used to read public members in the direct parent class.
    2. The this keyword can be used to read all members declared within the class.
    3. The super keyword can be used to read all members declared in a parent class.
    4. The that keyword can be used to read members of another class.
    5. The this keyword can be used to read public members in the direct parent class.
    6. The super keyword can be used in static methods.
  23. Fill in the blanks: A class ____________ an interface and ______________ an abstract class. An interface ______________ another interface.
    1. extends, extends, implements
    2. extends, implements, extends
    3. extends, implements, implements
    4. implements, extends, extends
    5. implements, extends, implements
    6. implements, implements, extends
  24. Suppose you have the following code. Which of the images best represents the state of the references c1, c2, and c3, right before the end of the main() method, assuming garbage collection hasn't run? In the diagrams, each box represents a Chicken object with a number of eggs.
    1:  public class Chicken {
    2:     private Integer eggs = 2;
    3:     { this.eggs = 3; }
    4:     public Chicken(int eggs) {
    5:        this.eggs = eggs;
    6:     }
    7:     public static void main(String[] r) {
    8:        var c1 = new Chicken(1);
    9:        var c2 = new Chicken(2);
    10:       var c3 = new Chicken(3);
    11:       c1.eggs = c2.eggs;
    12:       c2 = c1;
    13:       c3.eggs = null;
    14:    } }
    
    An illustration of four options for a code: Option A, Option B, Option C, and Option D.

    1. Option A
    2. Option B
    3. Option C
    4. Option D
    5. The code does not compile.
    6. None of the above.
  25. What is the output of the following application?
    package musical;
    interface Speak { default int talk() { return 7; } }
    interface Sing { default int talk() { return 5; } }
    public class Performance implements Speak, Sing {
       public int talk(String… x) {
          return x.length;
       }
       public static void main(String[] notes) {
          System.out.print(new Performance().talk());
       } }
    
    1. 7
    2. 5
    3. The code does not compile.
    4. The code compiles without issue, but the output cannot be determined until runtime.
    5. None of the above.
  26. What is the output of the following application?
    package ai;
    interface Pump { public abstract String toString(); }
    interface Bend extends Pump { void bend(double tensileStrength); }
    public class Robot {
       public static final void apply(
          Bend instruction, double input) {
          instruction.bend(input);
       }
       public static void main(String… future) {
          final Robot r = new Robot();
          r.apply(x -> System.out.print(x+" bent!"), 5);
       } }
    
    1. 5 bent!
    2. 5.0 bent!
    3. The code does not compile because Bend is not a functional interface.
    4. The code does not compile because of the apply() method declaration.
    5. None of the above.
  27. Which statement is true about encapsulation while providing the broadest access allowed?
    1. Variables are private and methods are private.
    2. Variables are private and methods are public.
    3. Variables are public and methods are private.
    4. Variables are public and methods are public.
    5. Variables are private and methods are protected.
    6. None of the above.
  28. Fill in the blanks: ____________ means the state of an object cannot be changed, while _____________ means that it can.
    1. Encapsulation, factory method
    2. Immutability, mutability
    3. Rigidity, flexibility
    4. Static, instance
    5. Tightly coupled, loosely coupled
    6. None of the above
  29. Which statement about the following interface is correct?
    public interface Swimming {
       String DEFAULT = "Diving!";      // k1
       abstract int breath();
       private static void stroke() {
          if(breath()==1) {             // k2
             System.out.print("Go!");
          } else {
             System.out.print(dive());  // k3
          } }
       static String dive() {
          return DEFAULT;               // k4
       } }
    
    1. The code compiles without issue.
    2. The code does not compile because of line k1.
    3. The code does not compile because of line k2.
    4. The code does not compile because of line k3.
    5. The code does not compile because of line k4.
    6. None of the above.
  30. Which is the first line to fail to compile?
    class Tool {
       private void repair() {}            // r1
       void use() {}                       // r2
    }
    class Hammer extends Tool {
       private int repair() { return 0; } // r3
       private void use() {}              // r4
       public void bang() {}              // r5
    }
    
    1. r1
    2. r2
    3. r3
    4. r4
    5. r5
    6. None of the above
  31. Which modifier can be applied to an abstract interface method?
    1. final
    2. interface
    3. protected
    4. volatile
    5. sealed
    6. None of the above
  32. What is the output of the Plant program?
    class Bush extends Plant {
       String type = "bush";
    }
    public class Plant {
       String type = "plant";
       public static void main(String[] args) {
          Plant w1 = new Bush();
          Bush w2 = new Bush();
          Plant w3 = w2;
          System.out.print(w1.type+","+w2.type+","+w3.type);
       } }
    
    1. plant,bush,plant
    2. plant,bush,bush
    3. bush,plant,bush
    4. bush,bush,bush
    5. The code does not compile.
    6. None of the above.
  33. The following Organ class is included, unmodified, in a larger program at runtime. At most, how many classes can inherit from Organ (excluding Organ itself)?
    package body;
    public sealed class Organ {
       sealed class Heart extends Organ {}
       final class Lung extends Organ {}
       static non-sealed class Stomach extends Organ {}
       final class Valentine extends Heart {}
    }
    
    1. None
    2. Three
    3. Four
    4. Five
    5. One of the nested classes does not compile.
    6. Two or more of the nested classes do not compile.
    7. The number cannot be determined with the information given.
  34. What is the correct order of statements for a Java class file?
    1. import statements, package statement, class declaration
    2. package statement, class declaration, import statements
    3. class declaration, import statements, package statement
    4. package statement, import statements, class declaration
    5. import statements, class declaration, package statement
    6. class declaration, package statement, import statements
  35. Which are true of the following code? (Choose three.)
    1:  class Penguin {
    2:     private enum Baby { EGG }
    3:     static class Chick { 
    4:        enum Baby { EGG }
    5:     }
    6:     public static void main(String[] args) {
    7:        boolean match = false;
    8:        Baby egg = Baby.EGG;
    9:        switch (egg) {
    10:          case EGG:
    11:             match = true;
    12:       } } }
    
    1. It compiles as is.
    2. It does not compile as is.
    3. Removing private on line 2 would create an additional compiler error.
    4. Removing private on line 2 would not create an additional compiler error.
    5. Removing the static modifier on line 3 would create an additional compiler error.
    6. Removing the static modifier on line 3 would not create an additional compiler error.
  36. Which are true of the following? (Choose two.)
    package beach;
    public class Sand {
       private static int numShovels;
       private int numRakes;
     
       public static int getNumShovels() {
          return numShovels;
       }
       public static int getNumRakes() {
          return numRakes;
       }
       public Sand() {
          System.out.print("a");
       }
       public void Sand() {
          System.out.print("b");
       }
       public void run() {
          new Sand();
          Sand();
       }
       public static void main(String… args) {
          new Sand().run();
       } }
    
    1. The code compiles.
    2. Exactly one line doesn't compile.
    3. Exactly two lines don't compile.
    4. If the code compiles or if any constructors/methods that do not compile are removed, the remaining code prints a.
    5. If the code compiles or if any constructors/methods that do not compile are removed, the remaining code prints ab.
    6. If the code compiles or if any constructors/methods that do not compile are removed, the remaining code prints aab.
  37. Which of the following class types cannot be marked abstract?
    1. static nested class
    2. Local class
    3. Anonymous class
    4. Member inner class
    5. Sealed class
    6. All of the above can be marked abstract.
  38. Fill in the blanks: The ___________________ access modifier allows access to everything the ___________________ access modifier does and more. (Choose three.)
    1. package, protected
    2. package, public
    3. protected, package
    4. protected, public
    5. public, package
    6. public, protected
  39. Which is the first line containing a compiler error?
    var var = "var";               // line x1
    var title = "Weather";         // line x2
    var hot = 100, var cold = 20;  // line x3
    var f = 32, int c = 0;         // line x4
    
    1. x1
    2. x2
    3. x3
    4. x4
    5. None of the above
  40. How many of the following members of the Telephone interface are public?
    public interface Telephone {
       static int call() { return 1; }
       default void dial() {}
       long answer();
       String home = "555-555-5555";
    }
    
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
    6. The code does not compile.
  41. Which best describes what the new keyword does?
    1. Creates a copy of an existing object and treats it as a new one.
    2. Creates a new primitive.
    3. Instantiates a new object.
    4. Switches an object reference to a new one.
    5. The behavior depends on the class implementation.
  42. How many lines will not compile?
    11: public class PrintShop {
    12:    public void printVarargs(String… names) {
    13:       System.out.println(Arrays.toString(names));
    14:    }
    15:    public void printArray(String[] names) {
    16:       System.out.println(Arrays.toString(names));
    17:    }
    18:    public void stormy() {
    19:       printVarargs("Arlene");
    20:       printVarargs(new String[]{"Bret"});
    21:       printVarargs(null);
    22:       printArray ("Cindy");
    23:       printArray (new String[]{"Don"});
    24:       printArray (null);
    25:    } }
    
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
    6. Five
  43. How do you change the value of an instance variable in an immutable class?
    1. Call the setter method.
    2. Remove the final modifier and set the instance variable directly.
    3. Create a new instance with an inner class.
    4. Use a method other than option A, B, or C.
    5. You can't.
  44. What is the minimum number of lines that need to be removed to make this code compile?
    @FunctionalInterface
    public interface Play {
       public static void baseball() {}
       private static void soccer() {}
       default void play() {}
       void fun();
    }
    
    1. One
    2. Two
    3. Three
    4. Four
    5. The code compiles as is.
  45. Fill in the blanks: A class that defines an instance variable with the same name as a variable in the parent class is referred to as ___________________ a variable, while a class that defines a static method with the same signature as a static method in a parent class is referred to as ___________________ a method.
    1. hiding, overriding
    2. overriding, hiding
    3. masking, masking
    4. hiding, masking
    5. replacing, overriding
    6. hiding, hiding
  46. Which statements about records are correct? (Choose two.)
    1. A record is implicitly public.
    2. A record can extend other classes.
    3. A record can implement interfaces.
    4. A record can contain multiple regular constructors.
    5. A record can contain multiple compact constructors.
    6. A record is always immutable.
  47. What change is needed to make Secret well encapsulated?
    import java.util.*;
    public class Secret {
       private int number = new Random().nextInt(10);
       public boolean guess(int candidate) {
          return number == candidate;
       } }
    
    1. Change number to use a protected access modifier.
    2. Change number to use a public access modifier.
    3. Declare a private constructor.
    4. Declare a public constructor.
    5. Remove the guess method.
    6. None. It is already well encapsulated.
  48. What is the output of the following application?
    interface Toy { String play(); }
    public class Gift {
       public static void main(String[] matrix) {
          abstract class Robot {}
          class Transformer extends Robot implements Toy {
             public String name = "GiantRobot";
             public String play() {return "DinosaurRobot";}  // y1
          }
          Transformer prime = new Transformer () {
             public String play() {return name;}             // y2
          };
          System.out.print(prime.play() + " " + name);
       } }
    
    1. GiantRobot
    2. GiantRobot DinosaurRobot
    3. DinosaurRobot
    4. The code does not compile because of line y1.
    5. The code does not compile because of line y2.
    6. None of the above.
  49. What is the output of the following application?
    package space;
    public class Bottle {
       public static class Ship {
          private enum Sail {          // w1
             TALL {protected int getHeight() {return 100;}},
             SHORT {protected int getHeight() {return 2;}};
             protected abstract int getHeight();
          }
          public Sail getSail() {
             return Sail.TALL;
          } }
       public static void main(String[] stars) {
          var bottle = new Bottle();
          Ship q = bottle.new Ship();  // w2
          System.out.print(q.getSail());
       } }
    
    1. TALL
    2. The code does not compile because of line w1.
    3. The code does not compile because of line w2.
    4. The code does not compile for another reason.
    5. The code compiles but throws an exception at runtime.
  50. Which of the following is not a valid order for elements within a class?
    1. Constructor, instance variables, method declarations
    2. Instance variables, static initializer constructor, method declarations
    3. Method declarations, instance variables, constructor
    4. Instance initializer, constructor, instance variables, constructor
    5. None of the above
  51. Which line of code, inserted at line p1, causes the application to print 5?
    package games;
    public class Jump {
       private int rope = 1;
       protected boolean outside;
       public Jump() {
          // line p1
          outside = true;
       }
       public Jump(int rope) {
          this.rope = outside ? rope : rope+1;
       }   
       public static void main(String[] bounce) {
          System.out.print(new Jump().rope);
       } }
    
    1. this(4);
    2. new Jump(4);
    3. this(5);
    4. rope = 4;
    5. super(4);
    6. super(5);
  52. Which of the following are not reasons to use encapsulation when designing a class? (Choose two.)
    1. Improve security.
    2. Increase concurrency and improve performance.
    3. Maintain class data integrity of data elements.
    4. Prevent users from modifying the internal attributes of a class.
    5. Prevent variable state from changing.
    6. Promote usability by other developers.
  53. Which is not a true statement given this diagram? Assume all classes are public.
    Two tables represent two statements.
    1. Instance methods in the Blanket class can call the Flashlight class's turnOn().
    2. Instance methods in the Flashlight class can call the Flashlight class's replaceBulb().
    3. Instance methods in the Phone class can call the Blanket class's wash().
    4. Instance methods in the Tent class can call the Tent class's pitch().
    5. Instance methods in the Tent class can call the Blanket class's wash().
    6. All of the statements are true.
  54. Given the diagram in the previous question, how many of the classes shown in the diagram can call the display() method?
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
  55. Which of the following statements are correct? (Choose two.)
    1. Java allows multiple inheritance using two abstract classes.
    2. Java allows multiple inheritance using two interfaces.
    3. Java does not allow multiple inheritance.
    4. An interface can extend another interface.
    5. An interface can extend a record.
    6. An interface can implement another interface.
  56. Which changes, taken together, would make the Tree class immutable? (Choose three.)
    1:  public class Tree {
    2:     String species;
    3:     public Tree(String species) {
    4:        this.species = species;
    5:     }
    6:     public String getSpecies() {
    7:        return species;
    8:     }
    9:     private final void setSpecies(String newSpecies) {
    10:       species = newSpecies;
    11:    } }
    
    1. Make all constructors private.
    2. Change the access level of species to private.
    3. Change the access level of species to protected.
    4. Remove the setSpecies() method.
    5. Mark the Tree class final.
    6. Make a defensive copy of species in the Tree constructor.
  57. What is the output of the following application?
    package ocean;
    abstract interface CanSwim {
       public void swim(final int distance);
    }
    public class Turtle {
       final int distance = 2;
       public static void main(String[] seaweed) {
          final int distance = 3;
          CanSwim seaTurtle = {
             final int distance = 5;
             @Override
             public void swim(final int distance) {
                System.out.print(distance);
             }
          };
          seaTurtle.swim(7);
       } }
    
    1. 2
    2. 3
    3. 5
    4. 7
    5. The code does not compile.
    6. None of the above.
  58. What is the output of the following application?
    package pet;
    public class Puppy {
       public static int wag = 5;   // q1
       public void Puppy(int wag) { // q2
          this.wag = wag;
       }
       public static void main(String[] tail) {
          System.out.print(new Puppy(2).wag); // q3
       } }
    
    1. 2
    2. 5
    3. The first line with a compiler error is line q1.
    4. The first line with a compiler error is line q2.
    5. The first line with a compiler error is line q3.
  59. Given the following method signature used in a class, which classes can call it?
    void run(String government)
    
    1. Classes in other packages
    2. Classes in the same package
    3. Subclasses in a different package
    4. All classes
    5. None of the above
  60. Which is the first declaration to not compile?
    package desert;
     
    interface CanBurrow {
       public abstract void burrow();
    }
     
    @FunctionalInterface interface HasHardShell
       extends CanBurrow {} 
     
    abstract class Tortoise implements HasHardShell {
       public abstract int toughness();
    }
     
    public class DesertTortoise extends Tortoise {
       public int toughness() { return 11; }
    }
    
    1. CanBurrow
    2. HasHardShell
    3. Tortoise
    4. DesertTortoise
    5. All of the declarations compile.
  61. Which is the first line to not compile?
    interface Building {
       default Double getHeight() { return 1.0; }         // m1
    }
    interface Office {
       public default String getHeight() { return null; } // m2
    }
    abstract class Tower implements Building, Office {}   // m3
    public class Restaurant extends Tower {}              // m4
    
    1. Line m1
    2. Line m2
    3. Line m3
    4. Line m4
    5. None of the above
  62. What is the output of the following code snippet?
    public class Nature {
      public static void main(String[] seeds) {
         record Tree() {}
         var tree = "pine";
         int count = 0;
         if (tree.equals("pine")) {
            int height = 55;
            count = count + 1;
         }
         System.out.print(height + count);
       } }
    
    1. 1
    2. 55
    3. 56
    4. It does not compile because a record cannot be nested in a method.
    5. It does not compile because a record must have at least one value passed in.
    6. It does not compile for another reason.
  63. Which of the following are valid comments in Java? (Choose three.)
    1. /****** TODO */
    2. # Fix this bug later
    3. ‘ Error closing pod bay doors
    4. / Invalid record /
    5. /* Page not found */
    6. // IGNORE ME
  64. Which of the following pairs of modifiers can both be applied to a method? (Choose three.)
    1. private and final
    2. abstract and final
    3. static and private
    4. private and abstract
    5. abstract and static
    6. static and protected
  65. Given the following class, what should be inserted into the two blanks to ensure the class data is properly encapsulated?
    package storage;
    public class Box {
       public String stuff;
       _________________ String  _________________() {
          return stuff;
       }
     
       public void setStuff(String stuff) {
          this.stuff = stuff;
       } }
    
    1. private and getStuff
    2. private and isStuff
    3. public and getStuff
    4. public and isStuff
    5. default and getStuff
    6. None of the above
  66. How many rows of the following table contain an error?
    Interface memberMembership typeRequires method body?
    static methodClassYes
    private non-static methodClassYes
    abstract methodInstanceNo
    default methodInstanceNo
    private static methodClassYes
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
  67. Fill in the blanks: ___________________ is used to call a constructor in the parent class, while ___________________ is used to reference a member of the parent class.
    1. super, this()
    2. super, super()
    3. super(), this
    4. super(), super
    5. None of the above
  68. Which of the following declarations can be inserted into the blank and have the class compile?
    public sealed class Toy {
      ___________________________  extends Toy {}
    }
    
    1. nonsealed class Book
    2. abstract class Robot
    3. class ActionFigure
    4. record Doll()
    5. interface Game
    6. non-sealed class Ball
    7. None of the above, because Toy does not include a permits clause
  69. What is the output of the Watch program?
    1:  class SmartWatch extends Watch {
    2:     private String getType() { return "smart watch"; }
    3:     public String getName(String suffix) {
    4:        return getType() + suffix;
    5:     } }
    6:  public class Watch {
    7:     private String getType() { return "watch"; }
    8:     public String getName(String suffix) {
    9:        return getType() + suffix;
    10:    }
    11:    public static void main(String[] args) {
    12:       var watch = new Watch();
    13:       var smartWatch = new SmartWatch();
    14:       System.out.print(watch.getName(","));
    15:       System.out.print(smartWatch.getName(""));
    16:    } }
    
    1. smart watch,watch
    2. watch,smart watch
    3. watch,watch
    4. The code does not compile
    5. An exception is printed at runtime
    6. None of the above
  70. What is the output of the Movie program?
    package theater;
    class Cinema {
       private String name = "Sequel";
       public Cinema(String name) {
          this.name = name;
       } }
    public class Movie extends Cinema {
       private String name = "adaptation";
       public Movie(String movie) {
          this.name = "Remake";
       }
       public static void main(String[] showing) {
          System.out.print(new Movie("Trilogy").name);
       } }
    
    1. Sequel
    2. Trilogy
    3. Remake
    4. Adaptation
    5. null
    6. None of the above
  71. Which statement best describes this class?
    import java.util.*;
    public final class Ocean {
       private final List<String> algae;
       private final double wave;
       private int sun;
       public Ocean(double wave) {
          this.wave = wave;
          this.algae = new ArrayList<>();
       }
       public int getSun() {
          return sun;
       }
       public void setSun(int sun) {
          sun = sun;
       }
       public double getWave() {
          return wave;
       }
       public List<String> getAlgae() {
          return new ArrayList<String>(algae);
       } }
    
    1. It can be serialized.
    2. It is well encapsulated.
    3. It is immutable.
    4. It is both well encapsulated and immutable.
    5. None of the above, as the code does not compile.
  72. Given the file Magnet.java shown, which of the marked lines can you independently insert the line var color; into and still have the code compile?
    // line a1
    public class Magnet {
       // line a2
       public void attach() {
          // line a3
       }
       // line a4
    }
    
    1. a2
    2. a3
    3. a2 and a3
    4. a1, a2, a3, and a4
    5. None of the above
  73. Which of the following results is not a possible output of this program?
    package sea;
    enum Direction { north, south, east, west; };
    public class Ship {
       public static void main(String[] compass) {
          System.out.print(Direction.valueOf(compass[0]));
       } }
    
    1. WEST is printed.
    2. south is printed.
    3. An ArrayIndexOutOfBoundsException is thrown at runtime.
    4. An IllegalArgumentException is thrown at runtime.
    5. All of the above are possible.
  74. What is the output of the following application?
    package radio;
    public class Song {
       public void playMusic() {
          System.out.print("Play!");
       }
       private static void playMusic() {
          System.out.print("Music!");
       }
       private static void playMusic(String song) {
          System.out.print(song);
       }
       public static void main(String[] tracks) {
          new Song().playMusic();
       } }
    
    1. Play!
    2. Music!
    3. The code does not compile.
    4. The code compiles, but the answer cannot be determined until runtime.
  75. Which of the following statements about overriding a method are correct? (Choose three.)
    1. The return types must be covariant.
    2. The access modifier of the method in the child class must be the same as or narrower than the method in the superclass.
    3. The return types must be the same.
    4. A checked exception declared by the method in the parent class must be declared by the method in the child class.
    5. A checked exception declared by a method in the child class must be the same as or narrower than the exception declared by the method in the parent class.
    6. The access modifier of the method in the child class must be the same as or broader than the method in the superclass.
  76. How many lines of the following code do not compile?
    10: interface Flavor {
    11:    public default void happy() {
    12:       printFlavor("Rocky road");
    13:    }
    14:    private static void excited() {
    15:       printFlavor("Peanut butter");
    16:    }
    17:    private void printFlavor(String f) {
    18:       System.out.println("Favorite Flavor is: "+f);
    19:    }
    20:    public static void sad() {
    21:       printFlavor("Butter pecan");
    22:    }
    23: }
    24: public class IceCream implements Flavor {
    25:    @Override public void happy() {
    26:       printFlavor("Cherry chocolate chip");
    27:    } }
    
    1. None, they all compile.
    2. One
    3. Two
    4. Three
    5. Four
    6. Five or more
  77. What is the output of the following program?
    1:  public record Disco(int beats) {
    2:     public Disco(String beats) {
    3:        this(20);
    4:     }
    5:     public Disco {
    6:        beats = 10;
    7:     }
    8:     public int getBeats() {
    9:        return beats;
    10:    }
    11:    public static void main(String[] args) {
    12:       System.out.print(new Disco(30).beats());
    13:    } }
    
    1. 10
    2. 20
    3. 30
    4. Exactly one line needs to be corrected for the program to compile.
    5. Exactly two lines need to be corrected for the program to compile.
    6. Three or more lines need to be corrected for the program to compile.
    7. The code compiles but prints an exception at runtime.
  78. Which of the following words are modifiers that are implicitly applied to all interface variables? (Choose three.)
    1. const
    2. final
    3. abstract
    4. static
    5. public
    6. constant
  79. Given the following method, what is the first line that does not compile?
    public static void main(String[] args) {
       int Integer = 0;        // k1
       Integer int = 0;        // k2
       Integer ++;             // k3
       int++;                  // k4
       int var = null;         // k5
    }
    
    1. k1
    2. k2
    3. k3
    4. k4
    5. k5
  80. What is the result of compiling and executing the following class?
    public class Tolls {
       private static int yesterday = 1;
       int tomorrow = 10;
     
       public static void main(String[] args) {
          var tolls = new Tolls();
          int today = 20, tomorrow = 40;  // line x
          System.out.print(
             today + tolls.tomorrow + tolls.yesterday); // line y
       } }
    
    1. The code does not compile due to line x.
    2. The code does not compile due to line y.
    3. 31
    4. 61
    5. None of the above.
  81. What is the output of the following application?
    package weather;
    public class Forecast {
       public enum Snow {
          BLIZZARD, SQUALL, FLURRY
          @Override public String toString() { return "Sunny"; }
       }
       public static void main(String[] modelData) {
          System.out.print(Snow.BLIZZARD.ordinal() + " ");
          System.out.print(Snow.valueOf("flurry".toUpperCase()));
       } }
    
    1. 0 FLURRY
    2. 1 FLURRY
    3. 0 Sunny
    4. 1 Sunny
    5. The code does not compile.
    6. None of the above.
  82. Which of the following is an invalid statement?
    1. The first line of every constructor is a call to the parent constructor via the super() command.
    2. A class is not required to have a constructor explicitly defined.
    3. A constructor may pass arguments to the parent constructor.
    4. A final instance variable whose value is not set when it is declared or in an initialization block should be set by the constructor.
    5. All of the above are valid statements.
  83. What can fill in the blank so that the play() method can be called from all classes in the com.mammal and com.mammal.eland package, but not in the com.mammal.gopher package?
    package com.mammal;
    public class Enrichment {
      _______  void play() {}
    }
    
    1. Leave it blank
    2. private
    3. protected
    4. public
    5. None of the above
  84. What is the output of the Rocket program?
    package transport;
    class Ship {
       protected int weight = 3;
       protected int height = 5;
       public int getWeight() { return weight; }
       public int getHeight() { return height; }
    }
    public class Rocket extends Ship {
       public int weight = 2;
       public int height = 4;
       public void printDetails() {
          System.out.print(super.getWeight() + "," + this.height);
       }
       public static final void main(String[] fuel) {
          new Rocket().printDetails();
       } }
    
    1. 2,5
    2. 3,4
    3. 5,2
    4. 3,5
    5. The code does not compile.
    6. None of the above.
  85. Imagine you are working with another team to build an application. You are developing code that uses a class that the other team has not yet finished writing. You want to allow easy integration once the other team's code is complete. Which statements would meet this requirement? (Choose two.)
    1. An abstract class is best.
    2. An interface is best.
    3. Either an abstract class or interface would meet the requirement.
    4. The methods should be protected.
    5. The methods should be public.
    6. The methods should be static.
  86. Fill in the blank with the line of code that allows the program to compile and print 10 at runtime.
    interface Speak {
       public default int getVolume() { return 20; }
    }
    interface Whisper {
       public default int getVolume() { return 10; } 
    }
    public class Debate implements Speak, Whisper {
       public int getVolume() { return 30; }
       public static void main(String[] a) {
          var d = new Debate();
          System.out.println(_________________);
       } }
    
    1. Whisper.d.getVolume()
    2. d.Whisper.getVolume()
    3. Whisper.super.getVolume()
    4. d.Whisper.super.getVolume()
    5. The code does not compile regardless of what is inserted into the blank.
    6. None of the above.
  87. Which of the following properties of an enum can be marked abstract?
    1. The enum type definition
    2. An enum method
    3. An enum value
    4. An enum constructor
    5. None of the above
  88. How many lines does the following code output?
    public class Cars {
       static {
          System.out.println("static");
       }
       private static void drive() {
          System.out.println("fast");
       }
       { System.out.println("faster"); }
       public static void main(String[] args) {
          drive();
          drive();
       } }
    
    1. One
    2. Two
    3. Three
    4. Four
    5. Five
    6. None of the above. The code does not compile.
  89. What does the following program output?
    public record Passenger(String firstName, String lastName) {
       static String middleName;
       @Override public String toString() {
          return null;
       }
       @Override public String getFirstName() {
          return null;
       }
       public static void main(String[] args) {
          var p = new Passenger("John", "Colbert");
          System.out.println(p.getFirstName());
       } }
    
    1. john
    2. colbert
    3. null
    4. The code does not compile.
    5. The code compiles but prints an exception at runtime.
    6. None of the above.
  90. Suppose foo is a reference to an instance of a class Foo. Which of the following is not possible about the variable reference foo.bar?
    1. bar is an instance variable.
    2. bar is a static variable.
    3. bar is a local variable.
    4. It can be used to read from bar.
    5. It can be used to write to bar.
    6. All of the above are possible.
  91. The following diagram shows two reference variables pointing to the same Bunny object in memory. The reference variable myBunny is of type Bunny, while unknownBunny is a valid but unknown data type. Which statements about the reference variables are true? Assume the instance methods and variables shown in the diagram are marked public. (Choose three.)
    Table represents two reference variables pointing to the same Bunny object in memory.

    1. The reference type of unknownBunny must be Bunny or a supertype of Bunny.
    2. The reference type of unknownBunny cannot be cast to a reference type of Bunny.
    3. The reference type of unknownBunny must be Bunny or a subclass of Bunny.
    4. If the reference type of unknownBunny is Bunny, it has access to all of the same methods and variables as myBunny.
    5. The reference type of unknownBunny could be an interface, class, or abstract class.
    6. If the reference type of unknownBunny is Object, it has access to all of the same methods and variables as myBunny without a cast.
  92. Which of the following interface methods are inherited by classes that implement the interface? (Choose two.)
    1. private methods
    2. private static methods
    3. default methods
    4. static methods
    5. abstract methods
    6. final methods
  93. Which of these are functional interfaces?
    interface Lion {
       public void roar();
       default void drink() {}
       String toString();
    }
    interface Tiger {
       public void roar();
       default void drink() {}
       int hashCode();
    }
    
    1. Lion
    2. Tiger
    3. Both Lion and Tiger
    4. Neither is a functional interface.
    5. The code does not compile.
  94. Given the following code, which lines when placed independently in the blank allow the code to compile and print bounce? (Choose two.)
    public class TennisBall {
       public TennisBall() {
          System.out.println("bounce");
       }
       public static void main(String[] slam) {
           _____________________________________;
       } }
    
    1. var new = TennisBall
    2. TennisBall()
    3. var var = new TennisBall()
    4. new TennisBall
    5. new TennisBall()
  95. How many of the public methods in the class compile?
    public class Singing {
       private void sing(String key) {}
     
       public void sing_do(String key, String… harmonies) {
         this.sing(key);
       }
       public void sing_re(int note, String… sound, String key) {
          this.sing(key);
       }
       public void sing_me(String… keys, String… pitches) {
          this.sing(key);
       }
       public void sing_fa(String key, String… harmonies) {
          this.Singing.sing(key);
       }
       public void sing_so(int note, String… sound, 
          String key) {
         this.Singing.sing(key);
       }
       public void sing_la(String… keys, String… pitches) {
          this.Singing.sing(key);
       } }
    
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
    6. Five
    7. Six
  96. What is the output of the following application?
    package world;
    public class Matrix {
       private int level = 1;
       class Deep {
          private int level = 2;
          class Deeper {
             private int level = 5;
             public void printReality(int level) {
                System.out.print(this.level+" ");
                System.out.print(Matrix.Deep.this.level+" ");
                System.out.print(Deep.this.level);
             } } }
       public static void main(String[] bots) {
          Matrix.Deep.Deeper simulation = new Matrix()
             .new Deep().new Deeper();
          simulation.printReality(6);
       } }
    
    1. 1 1 2
    2. 5 2 2
    3. 5 2 1
    4. 6 2 2
    5. 6 2 1
    6. The code does not compile.
  97. Not counting the Time declaration, how many declarations compile? Assume they are all declared within the same .java file.
    public sealed interface Time permits Hour, Minute, Second {}
     
    record Hour() implements Time {}
    interface Minute extends Time {}
    non-sealed class Second implements Time {}
    class Micro extends Second {}
    
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
  98. Given that Integer and Long are direct subclasses of Number, what type can be used to fill in the blank in the following class to allow it to compile?
    package orchestra;
    interface MusicCreator { public Number play(); }
    abstract class StringInstrument {
       public Long play() {return 3L;}
    }
    public class Violin extends StringInstrument 
          implements MusicCreator {
       public _________________ play() {
          return null;
       } }
    
    1. Long
    2. Integer
    3. Long or Integer
    4. Long or Number
    5. Long, Integer, or Number
    6. None of the above
  99. What is the output of the RightTriangle program?
    package shapes;
    abstract class Triangle {
       abstract String getDescription();
    }
    abstract class IsoRightTriangle extends RightTriangle { // g1
       public String getDescription() { return "irt"; }
    }
    public class RightTriangle extends Triangle {
       protected String getDescription() { return "rt"; }  // g2
       public static void main(String[] edges) {
          final var shape = new IsoRightTriangle();        // g3
          System.out.print(shape.getDescription());
       } }
    
    1. rt
    2. irt
    3. The code does not compile due to line g1.
    4. The code does not compile due to line g2.
    5. The code does not compile due to line g3.
    6. None of the above.
  100. What is the output of the following program?
    interface Dog {
       private void buryBone() { chaseTail(); }
       private static void wagTail() { chaseTail(); }
       public default String chaseTail() { return "So cute!"; }
    }
    public class Puppy implements Dog {
       public String chaseTail() throws IllegalArgumentException {
          throw new IllegalArgumentException("Too little!");
       }
       public static void main(String[] t) {
          var p = new Puppy();
          System.out.print(p.chaseTail());
       } }
    
    1. So cute!
    2. An exception is thrown with a Too little! message.
    3. A different exception is thrown.
    4. The code does not compile because buryBone() is not used.
    5. The code does not compile because chaseTail() cannot declare any exceptions in the Puppy class.
    6. None of the above.
  101. How do you force garbage collection to occur at a certain point?
    1. Calling System.forceGc()
    2. Calling System.gc()
    3. Calling System.requireGc()
    4. Calling GarbageCollection.clean()
    5. None of the above
  102. Which changes made to the following class would help to properly encapsulate the data in the class?
    package shield;
    public class Protect {
       private String material;
       protected int strength;
     
       public int getStrength() {
          return strength;
       }
       public void setStrength(int strength) {
          this.strength = strength;
       } }
    
    1. Add a getter method for material.
    2. Add a setter method for material.
    3. Change the access modifier of material to protected.
    4. Change the access modifier of strength to private.
    5. None of the above.
  103. Which are true statements about referencing variables from a lambda? (Choose two.)
    1. Instance and static variables can be used regardless of whether they are effectively final.
    2. Instance and local variables can be used regardless of whether they are effectively final.
    3. Instance variables and method parameters must be effectively final to be used.
    4. Local variables and method parameters must be effectively final to be used.
    5. Local and static variables can be used regardless of whether they are effectively final.
    6. Method parameters and static variables can be used regardless of whether they are effectively final.
  104. Which statement about the following classes is correct?
    import java.util.*;
    final class Faucet {
       private final String water;
       private final List<Double> pipes;
       public Faucet(String water, List<Double> pipes) {
          this.water = water;
          this.pipes = pipes;
       }
       public String getWater() { return water; }
       public List<Double> getPipes() { return pipes; } }
     
    public final class Spout {
       private final String well;
       private final List<Boolean> buckets;
       public Spout(String well, List<Boolean> buckets) {
          this.well = well;
          this.buckets = new ArrayList<>(buckets);
       }
       public String getWell() { return well; }
     
       public List<Boolean> getBuckets() {
          return new ArrayList<>(buckets);
       } }
    
    1. Only Faucet is immutable.
    2. Only Spout is immutable.
    3. Both classes are immutable.
    4. Neither class is immutable.
    5. None of the above, as one of the classes does not compile.
  105. Given the following structure, which snippets of code evaluate to true? (Choose three.)
    interface Friendly {}
    abstract class Dolphin implements Friendly {}
    class Animal implements Friendly {}
    class Whale extends Object {}
    class Fish {}
    class Coral extends Animal {}
    
    1. new Coral() instanceof Friendly
    2. null instanceof Object
    3. new Coral() instanceof Object
    4. new Fish() instanceof Friendly
    5. new Whale() instanceof Object
    6. new Dolphin() instanceof Friendly
  106. What is true of the following code?
    public class Eggs {
       enum Animal {
          CHICKEN(21), PENGUIN(75);
     
          private int numDays;
          private Animal(int numDays) {
             this.numDays = numDays;
          }
          public int getNumDays() {
             return numDays;
          }
          public void setNumDays(int numDays) {
             this.numDays = numDays;
          } }
       public static void main(String[] args) {
           Animal chicken = Animal.CHICKEN;
           chicken.setNumDays(20);
           System.out.print(chicken.getNumDays());
           System.out.print(" ");
           System.out.print(Animal.CHICKEN.getNumDays());
           System.out.print(" ");
           System.out.print(Animal.PENGUIN.getNumDays());
       } }
    
    1. It prints 20 20 20.
    2. It prints 20 20 75.
    3. It prints 20 21 75.
    4. It prints 21 21 75.
    5. It does not compile due to setNumDays().
    6. It does not compile for another reason.
  107. What is the first line to not compile in this interface?
    1: public interface Thunderstorm {
    2:    float rain = 1;
    3:    char getSeason() { return 'W'; }
    4:    boolean isWet();
    5:    private static void hail() {}
    6:    default String location() { return "Home"; }
    7:    private static int getTemp() { return 35; }
    8: }
    
    1. Line 2
    2. Line 3
    3. Line 4
    4. Line 5
    5. Line 6
    6. Line 7
    7. All of the lines compile.
  108. What is the output of the following application?
    package finance;
    enum Currency { DOLLAR, YEN, EURO }
    abstract class Provider {
       protected Currency c = Currency.EURO;
    }
    public class Bank extends Provider {
       protected Currency c = Currency.DOLLAR;
       public static void main(String[] pennies) {
          int value = 0;
          switch(new Bank().c) {
             case 0:
                value--; break;
             case 1:
                value++; break;
          }
          System.out.print(value);
       } }
    
    1. -1
    2. 0
    3. 1
    4. The Provider class does not compile.
    5. The Bank class does not compile.
    6. None of the above.
  109. Which of the following cannot be declared within a record? (Choose two.)
    1. An implementation of hashCode()
    2. Instance variables
    3. static initializers
    4. Nested classes
    5. static methods
    6. Constructors
    7. Instance initializers
  110. Which is equivalent to var q = 4.0f;?
    1. float q = 4.0f;
    2. Float q = 4.0f;
    3. double q = 4.0f;
    4. Double q = 4.0f;
    5. Object q = 4.0f;
  111. Fill in the blanks: A class may be assigned to a(n) ___________________ reference variable automatically but requires an explicit cast when assigned to a(n) ___________________ reference variable.
    1. subclass, outer class
    2. superclass, subclass
    3. concrete class, subclass
    4. subclass, superclass
    5. abstract class, static class
  112. Which statement about functional interfaces is invalid?
    1. A functional interface can have any number of static methods.
    2. A functional interface can have any number of default methods.
    3. A functional interface can have any number of private static methods.
    4. A functional interface can have any number of abstract methods.
    5. A functional interface can have any number of private methods.
    6. All of the above are correct.
  113. What are possible outputs of the following given that the comment on line X can be replaced by arbitrary code?
    // Mandrill.java
    public class Mandrill {
       public int age;
       public Mandrill(int age) {
          this.age = age;
       }
       public String toString() {
          return "" + age;
       }
    }
     
    // PrintAge.java
    public class PrintAge {
       public static void main (String[] args) {
          var mandrill = new Mandrill(5);
     
          // line X
     
          System.out.println(mandrill);
       } }
    
    1. 0
    2. 5
    3. Either 0 or 5
    4. Any int value
    5. Does not compile
  114. How many of the String objects are eligible for garbage collection right before the end of the main() method?
    public static void main(String[] ohMy) {
       String animal1 = new String("lion");
       String animal2 = new String("tiger");
       String animal3 = new String("bear");
     
       animal3 = animal1;
       animal2 = animal3;
       animal1 = animal2;
    }
    
    1. None
    2. One
    3. Two
    4. Three
    5. None of the above
  115. Suppose Panther and Cub are interfaces and neither contains any default methods. Which statements are true? (Choose two.)
    A flow diagram of cub to panther.
    1. If Panther has a single abstract method, Cub is guaranteed to be a functional interface.
    2. If Panther has a single abstract method, Cub may be a functional interface.
    3. If Panther has a single abstract method, Cub cannot be a functional interface.
    4. If Panther has two abstract methods, Cub is guaranteed to be a functional interface.
    5. If Panther has two abstract methods, Cub may be a functional interface.
    6. If Panther has two abstract methods, Cub cannot be a functional interface.
  116. What does the following output?
    1:  public class InitOrder {
    2:     public String first = "instance";
    3:     public InitOrder() {
    4:        first = "constructor";
    5:     }
    6:     { first = "block";  }
    7:     public void print() {
    8:        System.out.println(first);
    9:     }
    10:    public static void main(String… args) {
    11:       new InitOrder().print();
    12:    } }
    
    1. block
    2. constructor
    3. instance
    4. The code does not compile.
    5. None of the above.
  117. Which statement about the following interface is correct?
    public interface Tree {
       public static void produceSap() {
          growLeaves();
       }
       public abstract int getNumberOfRings() {
          return getNumberOfRings();
       }
       private static void growLeaves() {
          produceSap();
       }
       public default int getHeight() {
          return getHeight ();
       } }
    
    1. The code compiles.
    2. The method produceSap() does not compile.
    3. The method getNumberOfRings() does not compile.
    4. The method growLeaves() does not compile.
    5. The method getHeight() does not compile.
    6. The code does not compile because it contains a cycle.
  118. Which statements about a variable with a type of var are true? (Choose two.)
    1. The variable can be assigned null at initialization without any type information.
    2. The variable can be assigned null after initialization.
    3. The variable can never be assigned null.
    4. Only primitives can be used with the variable.
    5. Only objects can be used with the variable.
    6. Either a primitive or an object can be used with the variable.
  119. Assume there is a class Bouncer with a protected variable. Methods in which class can access this variable?
    1. Any subclass of Bouncer or any class in the same package as Bouncer
    2. Any superclass of Bouncer
    3. Only subclasses of Bouncer
    4. Only classes in the same package as Bouncer
    5. None of the above
  120. What is the output of the following application?
    package forest;
    public class Woods {
       static class Tree {}
       public static void main(String[] leaves) {
          int heat = 2;
          int water = 10-heat;
          final class Oak extends Tree {  // p1
             public int getWater() {
                return water;             // p2
             }
          }
          System.out.print(new Oak().getWater());
          water = 0;
       } }
    
    1. 8
    2. Line p1 contains a compiler error.
    3. Line p2 contains a compiler error.
    4. Another line of code contains a compiler error.
    5. None of the above.
  121. Which of the following can fill in the blank to make the code compile? (Choose two.)
    interface Australian {}
    interface Mammal {}
    ___________________ Australian, Mammal {}
    
    1. class Quokka extends
    2. class Quokka implements
    3. Neither A nor B. Only one interface can be implemented.
    4. interface Quokka extends
    5. interface Quokka implements
    6. Neither D nor E. Only one interface can be extended.
  122. What is true of the following method?
    public void setColor(String color) {
       color = color;
    }
    
    1. It is a correctly implemented accessor method.
    2. It is a correctly implemented mutator method.
    3. It is an incorrectly implemented accessor method.
    4. It is an incorrectly implemented mutator method.
    5. None of the above.
  123. Which of the following statements about calling this() in a constructor are true? (Choose three.)
    1. If arguments are provided to this(), then there must be a constructor in the class that can take those arguments.
    2. If arguments are provided to this(), then there must be a constructor in the superclass that can take those arguments.
    3. If the no-argument this() is called within a constructor, then the class must explicitly implement the no-argument constructor.
    4. If super() and this() are both used in the same constructor, super() must appear on the line immediately after this().
    5. If super() and this() are both used in the same constructor, this() must appear on the line immediately after super().
    6. If this() is used, it must be the first line of the constructor.
  124. What is the result of compiling and executing the following class?
    public class RollerSkates {
       static int wheels = 1;
       int tracks = 5;
       public static void main(String[] wheels) {
          RollerSkates s = new RollerSkates();
          int feet = 4, tracks = 15;
          System.out.print(feet + tracks + s.wheels);
       } }
    
    1. The code does not compile.
    2. 4
    3. 5
    4. 10
    5. 20
  125. Which statements about the following program are correct? (Choose two.)
    package vessel;
    class Problem extends Exception {}
    abstract class Danger {
       protected abstract void isDanger() throws Problem; // m1
    }
    public class SeriousDanger extends Danger { // m2
       protected void isDanger() throws Exception { // m3
          throw new RuntimeException(); // m4
       }
       public static void main(String[] w) throws Throwable { // m5
          var sd = new SeriousDanger().isDanger(); // m6
       } }
    
    1. The code does not compile because of line m1.
    2. The code does not compile because of line m2.
    3. The code does not compile because of line m3.
    4. The code does not compile because of line m4.
    5. The code does not compile because of line m5.
    6. The code does not compile because of line m6.
  126. Which statements about top-level and member inner classes are correct? (Choose three.)
    1. Both can be marked protected.
    2. Only top-level classes can be declared final.
    3. Both can declare constructors.
    4. Member inner classes cannot be marked private.
    5. Member inner classes can access private variables of the top-level class in which it is defined.
    6. Both can be marked abstract.
  127. What is required to define a valid Java class file?
    1. A class declaration
    2. A package statement
    3. An import statement
    4. A class declaration and package statement
    5. A class declaration and at least one import statement
    6. The public modifier
  128. How many objects are eligible for garbage collection right before the end of the main() method?
    1:  public class Person {
    2:     public Person youngestChild;
    3: 
    4:     public static void main(String… args) {
    5:        Person elena = new Person();
    6:        Person janeice = new Person();
    7:        elena.youngestChild = janeice;
    8:        janeice = null;
    9:        Person zoe = new Person();
    10:       elena.youngestChild = zoe;
    11:       zoe = null;
    12:    } }
    
    1. None
    2. One
    3. Two
    4. Three
    5. The code does not compile.
  129. What is the output of the following application?
    package race; 
    interface Drive {
       int SPEED = 5;
       default Integer getSpeed() { return SPEED; }
    }
    interface Hover {
       String MAX_SPEED = "10";
       default String getSpeed() { return MAX_SPEED; }
    }
    public class Car implements Drive, Hover {
       @Override public Object getSpeed() { return 15; }
       public static void main(String[] gears) {
          System.out.print(new Car().getSpeed());
       } }
    
    1. 5
    2. 10
    3. 15
    4. The code does not compile.
    5. The code compiles but produces an exception at runtime.
    6. The answer cannot be determined with the information given.
  130. What is the output of the following application? (Choose two.)
    1:  public class ChooseWisely {
    2:     public ChooseWisely() { super(); }
    3:     public int choose(int choice) { return 5; }
    4:     public int choose(short choice) { return 2; }
    5:     public int choose(long choice) { return 11; }
    6:     public int choose(double choice) { return 6; }
    7:     public int choose(Float choice) { return 8; }
    8:     public static void main(String[] path) {
    9:        ChooseWisely c = new ChooseWisely();
    10:       System.out.println(c.choose(2f));
    11:       System.out.println(c.choose((byte)2+1));
    12:    } }
    
    1. 2
    2. 3
    3. 5
    4. 6
    5. 8
    6. 11
  131. How many lines of the following program do not compile?
    1: public enum Color {
    2:    RED(1,2) { void toSpectrum() {} },
    3:    BLUE(2) { void toSpectrum() {} void printColor() {} },
    4:    ORANGE() { void toSpectrum() {} },
    5:    GREEN(4);
    6:    public Color(int… color) {}
    7:    abstract void toSpectrum();
    8:    final void printColor() {}
    9: }
    
    1. Zero
    2. One
    3. Two
    4. Three
    5. More than three
  132. What is the output of the Square program?
    package shapes;
    abstract class Trapezoid {
       private int getEqualSides() {return 0;}
    }
    abstract class Rectangle extends Trapezoid {
       public static int getEqualSides() {return 2;}  // x1
    }
    public class Square extends Rectangle {
       public int getEqualSides() {return 4;}         // x2
       public static void main(String[] corners) {
          final Square myFigure = new Square() {};       // x3
          System.out.print(myFigure.getEqualSides());
       } }
    
    1. 0
    2. 2
    3. 4
    4. The code does not compile due to line x1.
    5. The code does not compile due to line x2.
    6. The code does not compile due to line x3.
  133. What can fill in the blank so that the play() method can be called from all classes in the com.mammal package but not the com.mammal.gopher package?
    package com.mammal;
    public class Enrichment {
      ___________ void play() {}
    }
    
    1. Leave it blank
    2. private
    3. protected
    4. public
    5. None of the above
  134. How many cells in the following table are incorrect?
    TypeAllows abstract methods?Allows constants?Allows constructors?
    Abstract classYesYesNo
    Concrete classYesYesYes
    InterfaceYesYesYes
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
    6. Five
    7. Six
  135. Which modifiers are permitted on a direct subclass of a sealed class? (Choose three.)
    1. void
    2. default
    3. public
    4. private
    5. volatile
    6. final
    7. nonsealed
  136. Which statements are true about a functional interface? (Choose three.)
    1. It may contain any number of abstract methods.
    2. It must contain a single abstract method.
    3. It may contain any number of private methods.
    4. It must contain a single private method.
    5. It may contain any number of static methods.
    6. It must contain a single static method.
  137. What is a possible output of the following application?
    package wrap;
    public class Gift {
       private final Object contents;
       protected Object getContents() {
          return contents;
       }
       protected void setContents(Object contents) {
          this.contents = contents;
       }
       public void showPresent() {
          System.out.print("Your gift: "+contents);
       }
       public static void main(String[] treats) {
          var gift = new Gift();
          gift.setContents(gift);
          gift.showPresent();
       } }
    
    1. Your gift: wrap.Gift@29ca2745
    2. Your gift: Your gift:
    3. It does not compile.
    4. It compiles but throws an exception at runtime.
    5. None of the above.
  138. How many lines would need to be corrected for the following code to compile?
    1:  package animal;
    2:  interface CanFly {
    3:     public void fly() {}
    4:     int speed = 5;
    5:  }
    6:  final class Bird {
    7:     public int fly(int speed) {}
    8:  }
    9:  public class Eagle extends Bird implements CanFly {
    10:    public void fly() {}
    11: }
    
    1. None
    2. One
    3. Two
    4. Three
    5. Four
  139. What is the output of the following code?
    public class Bunny {
       static class Rabbit {
          void hop() {
             System.out.print("hop");
          }
       }
       static class FlemishRabbit extends Rabbit {
          void hop() {
             System.out.print("HOP");
          }
        }
        public static void main(String[] args) {
           Rabbit r1 = new FlemishRabbit();
           FlemishRabbit r2 = new FlemishRabbit();
           r1.hop();
           r2.hop();
        } }
    
    1. hophop
    2. HOPhop
    3. hopHOP
    4. HOPHOP
    5. The code does not compile.
    6. None of the above.
  140. Which of the following are valid class declarations? (Choose three.)
    1. class _ {}
    2. class river {}
    3. class Str3@m {}
    4. class Pond2$ {}
    5. class _var_ {}
    6. class 5Ocean {}
  141. What is the output of the InfiniteMath program?
    class Math {
       public final double secret = 2;
    }
    class ComplexMath extends Math {
       public final double secret = 4;
    }
    public class InfiniteMath extends ComplexMath {
       public final double secret = 8;
       public static void main(String[] numbers) {
          Math math = new InfiniteMath();
          System.out.print(math.secret);
       } }
    
    1. 2.0
    2. 4.0
    3. 8.0
    4. The code does not compile.
    5. The code compiles but prints an exception at runtime.
    6. None of the above.
  142. Given the following application, which diagram best represents the state of the mySkier, mySpeed, and myName variables in the main() method after the call to the slalom() method?
    package slopes;
    public class Ski {
       private int age = 18;
       private static void slalom(Ski racer,
             int[] speed, String name) {
          racer.age = 18;
          name = "Wendy";
          speed = new int[1];
          speed[0] = 11;
          racer = null;
       }
       public static void main(String[] mountain) {
          final var mySkier = new Ski();
          mySkier.age = 16;
          final int[] mySpeed = new int[1];
          final String myName = "Rosie";
          slalom(mySkier, mySpeed, myName);
       } }
    
    1. An illustration of the state of the mySkier, mySpeed, and myName variables.
    2. An illustration of the state of the mySkier, mySpeed, and myName variables.
    3. An illustration of the state of the mySkier, mySpeed, and myName variables.
    4. An illustration of the state of the mySkier, mySpeed, and myName variables.

  143. What is the output of the following application?
    package zoo;
    public class Penguin {
       private int volume = 1;
       private class Chick {
          private static int volume = 3;
          void chick(int volume) {
             System.out.print("Honk("+Penguin.this.volume+")!");
          } }
       public static void main(String… eggs) {
          Penguin pen = new Penguin();
          final Penguin.Chick littleOne = pen.new Chick();
          littleOne.chick(5);
       } }
    
    1. Honk(1)!
    2. Honk(3)!
    3. Honk(5)!
    4. The code does not compile.
    5. The code compiles, but the output cannot be determined until runtime.
    6. None of the above.
  144. What is a possible output of the following program?
    record Name(String v) {}
    public record Fruit(Name n) {
       public static void main(String[] vitamins) {
          var x = new Name("Apple");
          System.out.println(new Fruit(x));
       } }
    
    1. Fruit@3f2f5b24
    2. Fruit[Apple]
    3. Fruit[Name[Apple]]
    4. Fruit[n=Apple]
    5. Fruit[n=Name[v=Apple]]
    6. Fruit[v=Apple]
    7. The code does not compile.
    8. None of the above.
  145. Fill in the blank with the line of code that allows the program to compile and print X at runtime.
    interface Fruit {
       public default char getColor() { return 'Z'; }
    }
    interface Edible  {
       public default char getColor() { return 'X'; } 
    }
    public class Banana implements Fruit, Edible {
       public char getColor() { return _________________; }
       public static void main(String[] a) {
          var d = new Banana();
          System.out.println(d.getColor());
       } }
    
    1. Edible.getColor()
    2. Edible.super.getColor()
    3. super.Edible.getColor()
    4. super.getColor()
    5. The code does not compile regardless of what is inserted into the blank.
    6. None of the above.
  146. Given the following two classes, each in a different package, which line inserted into the code allows the second class to compile?
    package clothes;
    public class Store {
       public static String getClothes() { return "dress"; }
    }
     
    package wardrobe;
    // INSERT CODE HERE
    public class Closet {
       public void borrow() {
          System.out.print("Borrowing clothes: "+getClothes());
       } }
    
    1. static import clothes.Store.getClothes;
    2. import clothes.Store.*;
    3. import static clothes.Store.getClothes;
    4. import static clothes.Store;
    5. None of the above
  147. What is the output of the ElectricCar program?
    package vehicles;
    class Automobile {
       private final String drive() { return "Driving vehicle"; }
    }
    class Car extends Automobile {
       protected String drive() { return "Driving car"; }
    }
    public class ElectricCar extends Car {
       public final String drive() { return "Driving electric car"; }
       public static void main(String[] wheels) {
          final Automobile car = new ElectricCar();
          var v = (Car)car;
          System.out.print(v.drive());
       } }
    
    1. Driving vehicle
    2. Driving electric car
    3. Driving car
    4. The code does not compile.
    5. The code compiles but produces a ClassCastException at runtime.
    6. None of the above.
  148. Which statements about sealed classes are correct? (Choose three.)
    1. A sealed class may be extended by another sealed class.
    2. In an unnamed module, a sealed class must include all its subclasses within the same file.
    3. A sealed class cannot contain nested classes.
    4. A sealed class can be declared abstract.
    5. A sealed class can be declared final.
    6. In an unnamed module, a sealed class must include all its subclasses within the same package.
  149. What is the output of the following program?
    public class Music {
       { System.out.print("do-"); }
       static { System.out.print("re-"); }
       { System.out.print("mi-"); }
       static { System.out.print("fa-"); }
       public Music() {
          System.out.print("so-");
       }
       public Music(int note) {
          System.out.print("la-");
       }
       public static void main(String[] sound) {
          System.out.print("ti-");
          var play = new Music();
       } }
    
    1. re-fa-ti-do-mi-so-
    2. do-re-mi-fa-ti-so-
    3. ti-re-fa-do-mi-so-
    4. re-fa-la-mi-ti-do-
    5. do-re-mi-fa-so-ti
    6. The code does not compile.
    7. None of the above.
  150. Given the following class declaration, which options correctly declare a local variable containing an instance of the class?
    public class Earth {
       private abstract class Sky {
          void fall() {
             var e = _________________
          } } }
    
    1. new Sunset() extends Sky {};
    2. new Sky();
    3. new Sky() {}
    4. new Sky() { final static int blue = 1; };
    5. The code does not compile regardless of what is placed in the blank.
    6. None of the above.
  151. What is the output of the Encyclopedia program?
    package paper;
    abstract class Book {
       protected static String material = "papyrus";
       public Book() {}
       abstract String read() {}
       public Book(String material) {this.material = material;}
    }
    public class Encyclopedia extends Book {
       public static String material = "cellulose";
       public Encyclopedia() {super();}
       public String read() { return "Reading is fun!"; }
       public String getMaterial() {return super.material;}
     
       public static void main(String[] pages) {
          System.out.print(new Encyclopedia().read());
          System.out.print("-" + new Encyclopedia().getMaterial());
       } }
    
    1. Reading is fun!-papyrus
    2. Reading is fun!-cellulose
    3. null-papyrus
    4. null-cellulose
    5. The code does not compile.
    6. None of the above.
  152. What does the following print?
    interface Vehicle {}
    class Bus implements Vehicle {}
    public class Transport { 
       public static void main(String[] args) {
          Vehicle vehicle = new Bus();
          boolean n = null instanceof Bus;
          boolean v = vehicle instanceof Vehicle;
          boolean b = vehicle instanceof Bus;
          System.out.println(n + " " + v + " " + b);
      } }
    
    1. false false false
    2. false false true
    3. false true true
    4. true false true
    5. true true false
    6. true true true
  153. How many rows of the following table contain an error?
    Interface memberOptional modifier(s)Required modifier(s)
    Private methodprivate-
    Default methodpublicdefault
    Static methodpublic static-
    Abstract methodpublicabstract
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
  154. What is the output of the following program?
    public class Dwarf {
       private final String name;
       public Dwarf() {
          this("Bashful");
       }
       public Dwarf(String name) {
          name = "Sleepy";
       }
       public static void main(String[] sound) {
          var d = new Dwarf("Doc");
          System.out.println(d.name);
       } }
    
    1. Sleepy
    2. Bashful
    3. Doc
    4. The code does not compile.
    5. An exception is thrown at runtime.
  155. What is the output of the following application?
    package pocketmath;
    interface AddNumbers {
       int add(int x, int y);
       static int subtract(int x, int y) { return x-y; }
       default int multiply(int x, int y) { return x*y; }
    }
    public class Calculator {
       protected void calculate(AddNumbers n, int a, int b) {
          System.out.print(n.add(a, b));
       }
       public static void main(String[] moreNumbers) {
          final var ti = new Calculator() {};
          ti.calculate((k,p) -> p+k+1, 2, 5);  // x1
       } }
    
    1. 0
    2. 7
    3. 8
    4. The code does not compile because AddNumbers is not a functional interface.
    5. The code does not compile because of line x1.
    6. The code does not compile for a different reason.
    7. None of the above.
  156. Which of the following variables are always in scope for the entire program once defined?
    1. Package variables
    2. Class variables
    3. Instance variables
    4. Local variables
    5. Pseudo variables
  157. What is the command to call one constructor from another constructor in the same class?
    1. construct()
    2. parent()
    3. super()
    4. this()
    5. that()
    6. overthere()
  158. Which of the following statements about no-argument constructors and inheritance are correct? (Choose two.)
    1. The compiler cannot insert a no-argument constructor into an abstract class.
    2. If a parent class does not include a no-argument constructor, a child class cannot declare one.
    3. If a parent class only declares constructors that take at least one parameter, then a child class must declare at least one constructor.
    4. The no-argument constructor is sometimes inserted by the compiler.
    5. If a parent class declares a no-argument constructor, a child class must declare a no-argument constructor.
    6. If a parent class declares a no-argument constructor, a child class must declare at least one constructor.
  159. What is the result of executing the Grasshopper program?
    // Hopper.java
    package com.animals;
    public class Hopper {
       protected void hop() {
          System.out.println("hop");
       }
    }
     
    // Grasshopper.java
    package com.insect;
    import com.animals.Hopper;
    public class Grasshopper extends Hopper {
       public void move() {
          hop();  // p1
       }
       public static void main(String[] args) {
          var hopper = new Grasshopper();
          hopper.move();  // p2
          hopper.hop();   // p3
       } }
    
    1. The code prints hop once.
    2. The code prints hop twice.
    3. The first compiler error is on line p1.
    4. The first compiler error is on line p2.
    5. The first compiler error is on line p3.
  160. What is the minimum number of lines that need to be removed to make this code compile?
    @FunctionalInterface
    public interface Play {
       public static void baseball() {}
       private static void soccer() {}
       default void play() {}
       void fun();
       void game();
       void toy();
    }
    
    1. One
    2. Two
    3. Three
    4. Four
    5. The code compiles as is.
  161. Which statement about the following classes is correct?
    import java.util.*;
    public class Flower {
       private final String name;
       private final List<Integer> counts;
       public Flower(String name, List<Integer> counts) {
          this.name = name;
          this.counts = new ArrayList<>(counts);
       }
       public final String getName() { return name; }
       public final List<Integer> getCounts() {
          return new ArrayList<>(counts);
       } }
     
    class Plant {
       private final String name;
       private final List<Integer> counts;
       public Plant(String name, List<Integer> counts) {
          this.name = name;
          this.counts = new ArrayList<>(counts);
       }
       public String getName() { return name; }
       public List<Integer> getCounts() {
          return new ArrayList<>(counts);
       } }
    
    1. Only Flower is immutable.
    2. Only Plant is immutable.
    3. Both classes are immutable.
    4. Neither class is immutable.
    5. None of the above, as one of the classes does not compile.
  162. What is the result of executing the Sounds program?
    // Sheep.java
    package com.mammal;
    public class Sheep {
       private void baa() {
          System.out.println("baa!");
       }
       private void speak() {
          baa();
       } }
     
    // Sounds.java
    package com.animals;
    import com.mammal.Sheep;
    public class Sounds {
       public static void main(String[] args) {
          var sheep = new Sheep();
          sheep.speak();
       } }
    
    1. The code runs and prints baa!.
    2. The Sheep class does not compile.
    3. The Sounds class does not compile.
    4. Neither class compiles.
    5. None of the above.
  163. What is the output of the following application?
    package stocks;
    public class Bond {
       private static int price = 5;
       public boolean sell() {
          if(price<10) {
             price++;
             return true;
          } else if(price>=10) {
             return false;
          } }
       public static void main(String[] cash) {
          new Bond().sell();
          new Bond().sell();
          new Bond().sell();
          System.out.print(price);
       } }
    
    1. 5
    2. 6
    3. 7
    4. 8
    5. The code does not compile.
    6. The output cannot be determined with the information given.
  164. Given the following class declaration, what expression can be used to fill in the blank so that 88 is printed at runtime?
    final public class Racecar {
       final private int speed = 88;
       final protected class Engine {
          private final int speed = 100;
          public final int getSpeed() {
             return ________________________;
          }
       }
       final Engine e = new Engine();
       final public static void main(String[] feed) {
          System.out.print(new Racecar().e.getSpeed());
       } }
    
    1. Racecar.speed
    2. this.speed
    3. this.Racecar.speed
    4. Racecar.Engine.this.speed
    5. Racecar.this.speed
    6. The code does not compile regardless of what is placed in the blank.
  165. Which statements about static initializers are correct? (Choose three.)
    1. They cannot be used to create instances of the class they are contained in.
    2. They can assign a value to a static final variable.
    3. They are executed at most once per program.
    4. They are executed each time an instance of the class is created from a local cache of objects.
    5. They are executed each time an instance of the class is created using the new keyword.
    6. They may never be executed.
  166. What is the output of the BlueCar program?
    package race;
    abstract class Car {
       static { System.out.print("1"); }
       public Car(String name) {
          super();
          System.out.print("2");
       }
       { System.out.print("3"); } }
    public class BlueCar extends Car {
       { System.out.print("4"); }
       public BlueCar() {
          super("blue");
          System.out.print("5");
       }
       public static void main(String[] gears) {
          new BlueCar();
       } }
    
    1. 23451
    2. 12345
    3. 14523
    4. 13245
    5. 23154
    6. The code does not compile.
    7. None of the above.
  167. The following Fish class is included, unmodified, in a larger program at runtime. As most, how many classes can inherit Fish (excluding Fish itself)?
    public sealed class Fish {
       final class Blobfish extends Clownfish {}
       private non-sealed class Dory extends BlueTang {}
       sealed class Clownfish extends Fish {}
       sealed class BlueTang extends Fish {}
       final class Swordfish extends Fish {}
       private non-sealed class Nemo extends Clownfish {}
    }
    
    1. None
    2. Four
    3. Five
    4. Six
    5. One of the nested classes does not compile.
    6. Two or more of the nested classes do not compile.
    7. The number cannot be determined with the information given.
  168. Given the following class declaration, which value cannot be inserted into the blank line that would allow the code to compile?
    package mammal;
    interface Pet {
       public Object getDoggy();
    }
    public class Canine implements Pet {
       public __________ getDoggy() {
          return this;
       } }
    
    1. Canine
    2. List
    3. Object
    4. Pet
    5. All of the above can be inserted.
    6. The code does not compile regardless of what is inserted into the blank.
  169. Which statement about the following interface is correct?
    public interface Movie {
       String pass = "TICKET";
       private void buyPopcorn() {
          purchaseTicket();
       }
       public static int getDrink() {
          buyPopcorn();
          return 32;
       }
       private static String purchaseTicket() {
          getDrink();
          return pass;
       } }
    
    1. The code compiles.
    2. The code contains an invalid constant.
    3. The method buyPopcorn() does not compile.
    4. The method getDrink() does not compile.
    5. The method purchaseTicket() does not compile.
    6. The code does not compile for a different reason.
  170. Which methods compile?
    class YardWork {
       private static int numShovels;
       private int numRakes;
       public int getNumShovels() {
          return numShovels;
       }
       public int getNumRakes() {
          return numRakes;
       } }
    
    1. Just getNumRakes()
    2. Just getNumShovels()
    3. Both methods
    4. Neither method
  171. How many lines of the following class contain compilation errors?
    1: class Fly {
    2:    public Fly Fly() { return new Fly(); }
    3:    public void Fly(int kite) {}
    4:    public int Fly(long kite) { return 1; }
    5:    public static void main(String[] a) {
    6:       var f = new Fly();
    7:       f.Fly();
    8:    } }
    
    1. None
    2. One
    3. Two
    4. Three
    5. Four
    6. The answer cannot be determined with the information given.
  172. How many of the classes in the diagram can write code that references the sky() method?
    Table represents com.color and com.light.
    1. None
    2. One
    3. Two
    4. Three
    5. Four
  173. For the diagram in Question 172, how many classes can write code that references the light variable?
    1. None
    2. One
    3. Two
    4. Three
    5. Four
  174. Which of the following are the best reasons for creating a public static interface method? (Choose two.)
    1. Allow static methods to access instance methods.
    2. Allow an interface to define a method at the class level.
    3. Provide an implementation that a class implementing the interface can override.
    4. Improve code reuse within the interface.
    5. Add backward compatibility to existing interfaces.
    6. Improve encapsulation of the interface.
  175. What is the output of the HighSchool application?
    package edu;
    import java.io.FileNotFoundException;
    abstract class School {
       abstract Float getNumTeachers();
       public int getNumStudents() {
          return 10;
       } }
    public class HighSchool extends School {
       final Float getNumTeachers() { return 4f; }
       public int getNumStudents() throws FileNotFoundException {
          return 20;
       }
       public static void main(String[] s) throws Exception {
          var school = new HighSchool();
          System.out.print(school.getNumStudents());
       } }
    
    1. 10
    2. 20
    3. 4.0
    4. One line of the program does not compile.
    5. Two lines of the program do not compile.
    6. None of the above.
  176. What is the output of the following application?
    package track;
    interface Run {
       default CharSequence walk() {
          return "Walking and running!";
       } }
    interface Jog {
       default String walk() {
          return "Walking and jogging!";
       } }
    public class Sprint implements Run, Jog {
       public String walk() {
          return "Sprinting!";
       }
       public static void main(String[] args) {
          var s = new Sprint();
          System.out.println(s.walk());
       } }
    
    1. Walking and running!
    2. Walking and jogging!
    3. Sprinting!
    4. The code does not compile.
    5. The code compiles but prints an exception at runtime.
    6. None of the above.
  177. What is true of these two interfaces?
    interface Crawl {
       abstract void wriggle();
    }
    interface Dance {
       public void wriggle();
    }
    
    1. A concrete class can implement both interfaces, but must implement wriggle().
    2. A concrete class can implement both interfaces, but must not implement wriggle().
    3. A concrete class would only be able to implement both interfaces if the public modifier were removed but must implement wriggle().
    4. If the public modifier were removed, a concrete class could implement both interfaces, but must not implement wriggle().
    5. None of the above.
  178. Which of these are functional interfaces?
    interface Lion {
       public void roar();
       default void drink() {}
       boolean equals(Lion lion);
    }
    interface Tiger {
       public void roar();
       default void drink() {}
       String toString(String name);
    }
    interface Bear {
       void ohMy();
       default void drink() {}
    }
    
    1. Lion
    2. Tiger
    3. Bear
    4. Lion and Tiger
    5. Tiger and Bear
    6. Bear and Lion
    7. None of them
    8. All of them
  179. How many lines of the following class contain a compiler error?
    1:  public class Dragon {
    2:     boolean scaly;
    3:     static final int gold;   
    4:     Dragon protectTreasure(int value, boolean scaly) {
    5:        scaly = true;
    6:        return this;
    7:     }   
    8:     static void fly(boolean scaly) {
    9:        scaly = true;
    10:    }   
    11:    int saveTheTreasure(boolean scaly) {
    12:       return this.gold;
    13:    }   
    14:    static void saveTheDay(boolean scaly) {
    15:       this.gold = 0;
    16:    }
    17:    static { gold = 100; } }
    
    1. None
    2. One
    3. Two
    4. Three
    5. More than three
  180. Which are requirements for a class to be immutable? (Choose three.)
    1. A private constructor is provided.
    2. Any instance variables are private.
    3. Any instance variables are initialized in a constructor.
    4. Methods cannot be overridden.
    5. There are no mutator methods that modify instance variables.
    6. Any instance variables are marked final.
  181. Which statement about the following interface is correct?
    public interface Planet {
       int circumference;
       public abstract void enterAtmosphere();
       public default int getCircumference() {
          enterAtmosphere();
          return circumference;
       }
       private static void leaveOrbit() {
          var earth = new Planet() {
             public void enterAtmosphere() {}
          };
          earth.getCircumference();
       } }
    
    1. The code compiles.
    2. The method enterAtmosphere() does not compile.
    3. The method getCircumference() does not compile.
    4. The method leaveOrbit() does not compile.
    5. The code does not compile for a different reason.
    6. None of the above.
  182. What is the output of the following program?
    import java.time.*;
    import java.time.temporal.*;
    public record User(LocalDate creationDate) {
       static LocalDate today = LocalDate.now();
       public User {
          creationDate = today;
          creationDate = today;
       }
       public static void main(String[] p) {
          LocalDate yesterday = LocalDate.now()
             .minus(1, ChronoUnit.DAYS);
          var u = new User(yesterday);
          System.out.print(u.creationDate());
       } }
    
    1. null
    2. Today's date
    3. Yesterday's date
    4. An exception is thrown at runtime.
    5. Exactly one line needs to be corrected for the code to compile.
    6. Two or more lines need to be corrected for the code to compile.
    7. None of the above.
  183. Fill in the blanks: ___________________ methods always have the same name but a different list of parameters, while ___________________ methods always have the same name and the same return type.
    1. Overloaded, overridden
    2. Inherited, overridden
    3. Overridden, overloaded
    4. Hidden, overloaded
    5. Overridden, hidden
    6. None of the above
  184. What is the output of the following program?
    public class Husky {
       { this.food = 10; }
       { this.toy = 2; }
       private final int toy;
       private static int food;
       public Husky(int friend) {
          this.food += friend++;
          this.toy -= friend--;
       }
       public static void main(String… unused) {
          var h = new Husky(2);
          System.out.println(h.food+","+h.toy);
       } }
    
    1. 12,-1
    2. 12,2
    3. 13,-1
    4. Exactly one line of this class does not compile.
    5. Exactly two lines of this class do not compile.
    6. None of the above.
  185. Suppose you have the following code. Which of the images best represents the state of the references right before the end of the main() method, assuming garbage collection hasn't run?
    1:  public class Link {
    2:     private String name;
    3:     private Link next;
    4:     public Link(String name, Link next) {
    5:        this.name = name;
    6:        this.next = next;
    7:     }
    8:     public void setNext(Link next) {
    9:        this.next = next;
    10:    }
    11:    public Link getNext() {
    12:       return next;
    13:    }
    14:    public static void main(String… args) {
    15:       var apple = new Link("x", null);
    16:       var orange = new Link("y", apple);
    17:       var banana = new Link("z", orange);
    18:       orange.setNext(banana);
    19:       banana.setNext(orange);
    20:       apple = null;
    21:       banana = null;
    22:    } }
    
    An illustration of four options for a code: Option A, Option B, Option C, and Option D.

    1. Option A
    2. Option B
    3. Option C
    4. Option D
    5. The code does not compile.
    6. None of the above.
  186. Which statement about a no-argument constructor is true?
    1. The Java compiler will always insert a default no-argument constructor if you do not define a no-argument constructor in your class.
    2. For a class to call super() in one of its constructors, its parent class must explicitly implement a constructor.
    3. If a class extends another class that has only one constructor that takes a value, then the child class must explicitly declare at least one constructor.
    4. A class may contain more than one no-argument constructor.
    5. None of the above.
  187. Which variable declaration is the first line not to compile?
    public class Complex {
       class Building {}
       final class House extends Building {}
     
       public void convert() {
          Building b1 =  new Building();
          House h1 = new House();
          Building b2 = new House();
          Building b3 = (House) b1;
          House h2 = (Building) h1;
          Building b4 = (Building) b2;
          House h3 = (House) b2;
       } }
    
    1. b2
    2. b3
    3. h2
    4. b4
    5. h3
    6. All of the lines compile.
  188. What is the output of the following application?
    1:  interface Tasty {
    2:     default void eat() {
    3:        System.out.print("Spoiled!");
    4:     } }
    5:  public class ApplePicking {
    6:     public static void main(String[] food) {
    7:        var apple = new Tasty() {
    8:           @Override
    9:           void eat() {
    10:             System.out.print("Yummy!");
    11:          }
    12:       } 
    13:    }
    14: }
    
    1. Spoiled!
    2. Yummy!
    3. The application completes without printing anything.
    4. One line needs to be corrected for the program to compile.
    5. Two lines need to be corrected for the program to compile.
    6. None of the above.
  189. Which of the following statements about functional interfaces is true?
    1. It is possible to define a functional interface that returns two data types.
    2. It is possible to define a primitive functional interface that uses float, char, or short.
    3. All functional interfaces must take arguments or return a value.
    4. None of the primitive functional interfaces includes generic arguments.
    5. None of these statements is true.
  190. What is the result of executing the Tortoise program?
    // Hare.java
    package com.mammal;
    public class Hare {
       void init() {
          System.out.print("init-");
       }
       protected void race() {
          System.out.print("hare-");
       } }
     
    // Tortoise.java
    package com.reptile;
    import com.mammal.Hare;
    public class Tortoise {
       protected void race(Hare hare) {
          hare.init();    // x1
          hare.race();    // x2
          System.out.print("tortoise-");
        }
        public static void main(String[] args) {
           var tortoise = new Tortoise();
           var hare = new Hare();
           tortoise.race(hare);
       } }
    
    1. init-hare-tortoise
    2. init-hare
    3. The first line with a compiler error is line x1.
    4. The first line with a compiler error is line x2.
    5. The code does not compile due to a different line.
    6. The code throws an exception.
  191. How many lines of the following program do not compile?
    interface Tool {
       void use(int fun);
    }
    abstract class Childcare {
       abstract void use(int fun);
    }
    final public class Stroller extends Childcare implements Tool {
       final public void use(int fun) {
          final int width = 5;
          class ParkVisit {
             final int getValue() { return width + fun; }
          }
          System.out.print(new ParkVisit().getValue());
       } }
    
    1. Zero
    2. One
    3. Two
    4. Three
    5. More than three
  192. What is the result of executing the Bush program?
    // Evergreen.java
    package tree;
    public class Evergreen {
       String leaves = "Green ";
       default void season() {
          System.out.println("spring");
       }
       default void bloom() {
          season();
       } }
     
    // Bush.java
    package shrub;
    import tree.Evergreen;
    public class Bush {
       public static void main(String[] args) {
          var var = new Evergreen();
          System.out.print(var.leaves);
          var.bloom();
       } }
    
    1. The code runs and prints Green spring.
    2. The code runs and prints Green springspring.
    3. The Evergreen class does not compile.
    4. The Bush class does not compile.
    5. Neither class compiles.
  193. What is the output of the following program?
    public sealed class Seasons {
       final static class Spring extends Seasons {}
       non-sealed class Summer extends Seasons {}
       public static void main(String[] w) {
          var t = new Spring();
          final String m = switch (t) {
             case Spring -> "Flowers";
             case Summer -> "Pool";
             default -> "Snow";
          };
          System.out.print(m);
       } }
    
    1. Flowers
    2. Pool
    3. Snow
    4. The Spring declaration does not compile.
    5. The Summer declaration does not compile.
    6. The main() method does not compile.
  194. What is the best reason for marking an existing static method private within an interface?
    1. It allows the method to be overridden in a subclass.
    2. It hides the secret implementation details from another developer using the interface.
    3. It improves the visibility of the method.
    4. It ensures the method is not replaced with an overridden implementation at runtime.
    5. It allows the method to be marked abstract.
    6. Trick question! All static methods are implicitly private within an interface.
  195. What is the output of the following application?
    package jungle;
    public class RainForest extends Forest {
       public RainForest(long treeCount) {
          this.treeCount = treeCount+1;
       }
       public static void main(String[] birds) {
          System.out.print(new RainForest(5).treeCount);
       } }
    class Forest {
       public long treeCount;
       public Forest(long treeCount) {
          this.treeCount = treeCount+2;
       } }
    
    1. 5
    2. 6
    3. 7
    4. 8
    5. The code does not compile.
  196. What is the result of compiling and executing the following class?
    package sports;
    public class Bicycle {
       String color = "red";
       private void printColor(String color) {
          color = "purple";
          System.out.print(color);
       }
       public static void main(String[] rider) {
          new Bicycle().printColor("blue");
       } }
    
    1. red
    2. purple
    3. blue
    4. null
    5. It does not compile.
  197. Given that Short and Integer extend Number directly, what type can be used to fill in the blank in the following class to allow it to compile?
    package band;
    interface Horn {
       public Integer play();
    }
    abstract class Woodwind {
       public Short play() {
          return 3;
       } }
    public final class Saxophone extends Woodwind implements Horn {
       public _________________ play() {
          return null;
       } }
    
    1. Object
    2. Integer
    3. Short
    4. Number
    5. None of the above
  198. Which statements about abstract classes and methods are correct? (Choose three.)
    1. An abstract class can be extended by a final class.
    2. An abstract method can be overridden by a final method.
    3. An abstract class can be extended by multiple classes directly.
    4. An abstract class can extend multiple classes directly.
    5. An abstract class cannot implement an interface.
    6. An abstract class can extend an interface.
  199. Given the following enum declaration, how many lines would need to be corrected for the code to compile?
    public enum Proposition {
       TRUE(1) { String getNickName() { return "RIGHT"; }},
       FALSE(2) { public String getNickName() { return "WRONG"; }},
       UNKNOWN(3) { public String getNickName() { return "LOST"; }}
       public int value;
       Proposition(int value) {
          this.value = value;
       }
       public int getValue() {
          return this.value;
       }
       protected abstract String getNickName();
    }
    
    1. Zero
    2. One
    3. Two
    4. Three
    5. More than three
  200. What is the result of executing the HopCounter program?
    // Hopper.java
    package com.animals;
    public class Hopper {
       protected void hop() {
          System.out.println("hop");
       } }
     
    // Grasshopper.java
    package com.insect;
    import com.animals.Hopper;
    public class Grasshopper extends Hopper {
       public void move() {
          hop();  // p1
       } }
     
    // HopCounter.java
    package com.insect;
    public class HopCounter {
       public static void main(String[] args) {
          var hopper = new Grasshopper();
          hopper.move();  // p2
          hopper.hop();   // p3
       } }
     
    
    1. The code prints hop once.
    2. The code prints hop twice.
    3. The first compiler error is on line p1.
    4. The first compiler error is on line p2.
    5. The first compiler error is on line p3.
    6. None of the above.
  201. Which of the following are not attributes common to both abstract classes and interfaces? (Choose two.)
    1. They both can contain abstract methods.
    2. They both can contain default methods.
    3. They both can contain protected methods.
    4. They both can contain public methods.
    5. They both can contain static variables.
  202. Given the following class, which method signature could be successfully added to the class as an overloaded version of the findAverage() method? (Choose two.)
    public class Calculations {
       public Integer findAverage(int sum) { return sum; }
    }
    
    1. public Long findAverage(int x)
    2. public Long findAverage(int x, int y)
    3. public Integer average(int x)
    4. private void findAverage(int x)
    5. public Integer findAverage(int… x) throws Exception
    6. private Integer findAverage(int x)
  203. Which of the following is a valid method name in Java? (Choose two.)
    1. Go_$Outside$2()
    2. have-Fun()
    3. new()
    4. 9enjoyTheWeather()
    5. $sprint()
    6. walk#()
  204. Fill in the blanks: A functional interface must contain or inherit ______________ and may optionally include ______________.
    1. at least one abstract method, the @Override annotation
    2. exactly one method, static methods
    3. exactly one abstract method, the @FunctionalInterface annotation
    4. at least one static method, at most one default method
    5. None of the above
  205. Fill in the blank with the line of code that allows the program to compile and print 15 at runtime.
    package love;
    interface Sport {
       private int play() { return 15; }
    }
    interface Tennis extends Sport {
       private int play() { return 30; }
    }
    public class Game implements Tennis {
       public int play() { return _________________; }
       public static void main(String… ace) {
          System.out.println(new Game().play());
       } }
    
    1. Sport.play()
    2. Sport.super.play()
    3. Sport.Tennis.play()
    4. Tennis.Sport.super.play()
    5. The code does not compile regardless of what is inserted into the blank.
    6. None of the above.
  206. What is the output of the following program?
    public class MoreMusic {
       {
          System.out.print("do-"); 
          System.out.print("re-"); 
       }
       public MoreMusic() {
          System.out.print("mi-");
       }
       public MoreMusic(int note) {
          this(null);
          System.out.print("fa-");
       }
       public MoreMusic(String song) {
          this(9);
          System.out.print("so-");
       }
       public static void main(String[] sound) {
          System.out.print("la-");
          var play = new MoreMusic(1);
       } }
    
    1. la-do-re-mi-so-fa-
    2. la-do-re-mi-fa-
    3. do-re-mi-fa-so-la-
    4. fa-re-do-mi-so-
    5. The code does not compile.
    6. None of the above.
  207. Given the following two classes in the same package, what is the result of executing the Hug program?
    public class Kitten {
       /** private **/ float cuteness;
       /* public */ String name;
       // default double age;
       void meow() { System.out.println(name + " - "+cuteness); }
    }
     
    public class Hug {
       public static void main(String… friends) {
          var k = new Kitten();
          k.cuteness = 5;
          k.name = "kitty";
          k.meow();
       } }
    
    1. kitty - 5.0
    2. The Kitten class does not compile.
    3. The Hug class does not compile.
    4. The Kitten and Hug classes do not compile.
    5. None of the above.
  208. What is the output of the following application?
    package prepare;
    interface Ready {
       static int first = 2;
       final short DEFAULT_VALUE = 10;
       GetSet go = new GetSet();            // n1
    }
    public class GetSet implements Ready {
       int first = 5;
       static int second = DEFAULT_VALUE;   // n2
       public static void main(String[] begin) {
          var r = new Ready() {};
          System.out.print(r.first);        // n3
          System.out.print(" " + second);   // n4
       } }
    
    1. 2 10
    2. 5 10
    3. 5 2
    4. The code does not compile because of line n1.
    5. The code does not compile because of line n2.
    6. The code does not compile because of line n3.
    7. The code does not compile because of line n4.
  209. What is the result of executing the Tortoise program?
    // Hare.java
    package com.mammal;
    public class Hare {
       public void init() {
          System.out.print("init-");
       }
       protected void race() {
          System.out.print("hare-");
       } }
     
    // Tortoise.java
    package com.reptile;
    import com.mammal.Hare;
     
    public class Tortoise extends Hare {
       protected void race(Hare hare) {
          hare.init();    // x1
          hare.race();    // x2
          System.out.print("tortoise-");
        }
        public static void main(String[] args) {
           var tortoise = new Tortoise();
           var hare = new Hare();
           tortoise.race(hare);
       } }
    
    1. init-hare-tortoise
    2. init-hare
    3. The first line with a compiler error is line x1.
    4. The first line with a compiler error is line x2.
    5. The code does not compile due to a different line.
    6. The code throws an exception.
  210. What is the output of the following program?
    interface Autobot {}
    public record Transformer(Boolean matrix) implements Autobot {
       public boolean isMatrix() {
          return matrix;
       }
       abstract void transform() {}
       public Transformer {
          if(matrix == null)
             throw new IllegalArgumentException();
       }
       public static void main(String[] u) {
          var prime = new Transformer(null) {
             public void transform() {}
          };
          System.out.print(prime.matrix());
       } }
    
    1. true
    2. false
    3. An exception is thrown at runtime.
    4. Exactly one line needs to be corrected for the code to compile.
    5. Two or more lines need to be corrected for the code to compile.
    6. None of the above.
  211. What is the result of executing the Movie program?
    // Story.java
    package literature;
    public abstract class Story {
       private void tell() {
          System.out.println("Once upon a time");
       }
       public static void play() {
          tell();
       } }
     
    // Movie.java
    package media;
    import literature.Story;
    public class Movie {
       public static void main(String[] args) {
          var story = new Story();
          story.play();
       } }
    
    1. The code runs and prints Once upon a time.
    2. The code runs but does not print anything.
    3. The Story class does not compile.
    4. The Movie class does not compile.
    5. Neither class compiles.
  212. What is the output of the Helicopter program?
    package flying;
    class Rotorcraft {
       protected final int height = 5;
       abstract int fly();
    }
    interface CanFly {}
    public class Helicopter extends Rotorcraft implements CanFly {
       private int height = 10;
       protected int fly() {
          return super.height;
       }
       public static void main(String[] unused) {
          Helicopter h = (Helicopter)new Rotorcraft();
          System.out.print(h.fly());
       } }
    
    1. 5
    2. 10
    3. The code does not compile.
    4. The code compiles but produces a ClassCastException at runtime.
    5. None of the above
  213. Given the following program, what is the first line to fail to compile?
    1: public class Electricity {
    2:    interface Power {}
    3:    public static void main(String[] light) {
    4:       class Source implements Power {};
    5:       final class Super extends Source {};
    6:       var start = new Super() {};
    7:       var end = new Source() { static boolean t = true; };
    8:    } }
    
    1. Line 2
    2. Line 4
    3. Line 5
    4. Line 6
    5. Line 7
    6. All of the lines compile.
  214. What is the output of the following application?
    package prepare;
    public class Ready {
       protected static int first = 2;
       private static final short DEFAULT_VALUE = 10;
       private static class GetSet {
          int first = 5;
          static int second = DEFAULT_VALUE;
       }
       private GetSet go = new GetSet();
       public static void main(String[] begin) {
          Ready r = new Ready();
          System.out.print(r.go.first);
          System.out.print(", "+r.go.second);
       } }
    
    1. 2, 5
    2. 5, 10
    3. 5, 5
    4. 2, 10
    5. The code does not compile because of the GetSet class declaration.
    6. The code does not compile for another reason.
  215. Which of the following are true about the following code? (Choose two.)
    public class Values {
       static _________  defaultValue = 8;
       static _________ DEFAULT_VALUE;
     
       public static void main(String[] args) {
          System.out.println("" + defaultValue + DEFAULT_VALUE);
       } }
    
    1. When you fill in both blanks with double, it prints 8.00.0.
    2. When you fill in both blanks with double, it prints 8.0.
    3. When you fill in both blanks with int, it prints 8.
    4. When you fill in both blanks with int, it prints 80.
    5. When you fill in both blanks with var, it prints 8.
    6. When you fill in both blanks with var, it prints 80.
  216. How many Gems objects are eligible for garbage collection right before the end of the main() method?
    public record Gems(String name) {
       public static void main(String… args) {
          var g1 = Gems("Garnet");
          var g2 = Gems("Amethyst");
          var g3 = Gems("Pearl");
          var g4 = Gems("Steven");
          g2 = g3;
          g3 = g2;
          g1 = g2;
          g4 = null;
       } }
    
    1. None
    2. One
    3. Two
    4. Three
    5. Four
    6. The code does not compile.
  217. How many lines of the following program contain compilation errors?
    package sky;
    public class Stars {
       private int inThe = 4;
       public void Stars() {
          super();
       }
       public Stars(int inThe) {
          this.inThe = this.inThe;
       }
       public static void main(String[] endless) {
          System.out.print(new sky.Stars(2).inThe);
       } }
    
    1. None
    2. One
    3. Two
    4. Three
    5. Four
  218. What is the output of the following application?
    package sports;
    abstract class Ball {
       protected final int size;
       public Ball(int size) {
          this.size = size;
       } }
    interface Equipment {}
    public class SoccerBall extends Ball implements Equipment {
       public SoccerBall() {
          super(5);
       }
       public Ball get() { return this; }
       public static void main(String[] passes) {
          var equipment = (Equipment)(Ball)new SoccerBall().get();
          System.out.print(((SoccerBall)equipment).size);
       } }
    
    1. 5
    2. 55
    3. The code does not compile due to an invalid cast.
    4. The code does not compile for a different reason.
    5. The code compiles but throws a ClassCastException at runtime.
  219. Which statement about the Elephant program is correct?
    package stampede;
    interface Long {
       Number length();
    }
    public class Elephant {
       public class Trunk implements Long {
          public Number length() { return 6; }   // k1
       }
       public class MyTrunk extends Trunk {      // k2
          public Integer length() { return 9; }  // k3
       }
       public static void charge() {
          System.out.print(new MyTrunk().length());
       }
       public static void main(String[] cute) {
          new Elephant().charge();               // k4
       } }
    
    1. It compiles and prints 9.
    2. The code does not compile because of line k1.
    3. The code does not compile because of line k2.
    4. The code does not compile because of line k3.
    5. The code does not compile because of line k4.
    6. None of the above.
  220. What is the minimum number of lines that need to be removed for this code to compile?
    1:  package figures;
    2:  public class Dolls {
    3:     public int num() { return 3.0; }
    4:     public int size() { return 5L; }
    5:
    6:     public void nested() { nested(2,true); }
    7:     public int nested(int w, boolean h) { return 0; }
    8:     public int nested(int level) { return level+1; }
    9:
    10:    public static void main(String[] outOfTheBox) {
    11:       System.out.print(new Dolls().nested());
    12:    } }
    
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
    6. Five or more
  221. How many of these lines compile?
    18: Comparator<String> c1 = (j, k) -> 0;
    19: Comparator<String> c2 = (String j, String k) -> 0;
    20: Comparator<String> c3 = (var j, String k) -> 0;
    21: Comparator<String> c4 = (var j, k) -> 0;
    22: Comparator<String> c5 = (var j, var k) -> 0;
    
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
    6. Five
  222. What is the output of the following code?
    public class Bunny {
       static interface Rabbit { }
       static class FlemishRabbit implements Rabbit { }
     
       private static void hop(Rabbit r) {
          System.out.print("hop");
       }
       private static void hop(FlemishRabbit r) {
          System.out.print("HOP");
       }
       public static void main(String[] args) {
          Rabbit r1 = new FlemishRabbit();
          FlemishRabbit r2 = new FlemishRabbit();
          hop(r1);
          hop(r2);
       } }
    
    1. hophop
    2. HOPhop
    3. hopHOP
    4. HOPHOP
    5. The code does not compile.
  223. Which is one of the lines output by this code?
    10: var list = new ArrayList<Integer>();
    11: list.add(10);
    12: list.add(9);
    13: list.add(8);
    14:
    15: var num = 9;
    16: list.removeIf(x -> {int keep = num; return x != keep;});
    17: System.out.println(list);
    18:
    19: list.removeIf(x -> {int keep = num; return x == keep;});
    20: System.out.println(list);
    
    1. []
    2. [8]
    3. [8, 10]
    4. [8, 9, 10]
    5. [10, 8]
    6. The code does not compile.
  224. What does this code output?
    var babies = Arrays.asList("chick", "cygnet", "duckling");
    babies.replaceAll(x -> { var newValue = "baby";
       return newValue; });
    System.out.println(babies);
    
    1. [baby]
    2. [baby, baby, baby]
    3. [chick, cygnet, duckling]
    4. []
    5. None of the above.
    6. The code does not compile.
  225. Which statement best describes this class?
    import java.util.*;
    public final class Forest {
       private final int flora;
       private final List<String> fauna;
       public Forest() {
          this.flora = 5;
          this.fauna = new ArrayList<>();
       }
       public int getFlora() {
          return flora;
       }
       public List<String> getFauna() {
          return fauna;
       } }
    
    1. It is serializable.
    2. It is well encapsulated.
    3. It is immutable.
    4. It is both well encapsulated and immutable.
    5. None of the above, as the code does not compile.
  226. What is the output of the following program?
    public record Light(String type, float lumens) {
       final static String DEFAULT_TYPE = "PAR";
       public Light {
          if(type == null)
             throw new IllegalArgumentException();
          else type = DEFAULT_TYPE;
       }
       public Light(String type) {
          this.type = "B";
          this.lumens = 10f;
       }
       public static void main(String[] p) {
          final var bulb = new Light("A", 300);
          System.out.print(bulb.type());
       } }
    
    1. null
    2. A
    3. PAR
    4. An exception is thrown at runtime.
    5. The code does not compile.
    6. None of the above.
  227. Which statement about the following code is correct?
    public class Dress {
       int size = 10;
       default int getSize() {
          display();
          return size;
       }
       static void display() {
          System.out.print("What a pretty outfit!");
       }
       private int getLength() {
          display();
          return 15;
       }
       private static void tryOn() {
          display();
       } }
    
    1. The code contains an invalid constant.
    2. The getSize() method does not compile.
    3. The getLength() method does not compile.
    4. The tryOn() method does not compile.
    5. The code compiles.
    6. None of the above.
  228. Which of the following are the best reasons for creating a private interface method? (Choose two.)
    1. Add backward compatibility to existing interfaces.
    2. Provide an implementation that a class implementing the interface can override.
    3. Increase code reuse within the interface.
    4. Allow interface methods to be inherited.
    5. Improve encapsulation of the interface.
    6. Allow static methods to access instance methods.
  229. How many subclasses of Snack compile?
    public abstract sealed class Snack permits Snack.Lollipop {
       final static class Toast extends Snack {}
       sealed static class Lollipop extends Snack {}
       final class Treat extends Lollipop {}
       abstract non-sealed class IceCream extends Snack {}
    }
    
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
    6. Trick question! Snack does not compile.
  230. Given the following two classes, each in a different package, which line inserted into the code allows the second class to compile?
    package commerce;
    public class Bank {
       public static void withdrawal(int amountInCents) {}
       public static void deposit(int amountInCents) {}
    }
     
    package employee;
    // INSERT CODE HERE
    public class Teller {
       public void processAccount(int deposit, int withdrawal) {
          withdrawal(withdrawal);
          deposit(deposit);
       } }
    
    1. import static commerce.Bank.*;
    2. import static commerce.Bank;
    3. static import commerce.Bank.*;
    4. static import commerce.Bank;
    5. 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.145.70.60