Chapter 7 Beyond Classes Flashcards
Review Questions
1. Which of the following are valid record declarations? (Choose all that apply.)
public record Iguana(int age) { private static final int age = 10; } public final record Gecko() {} public abstract record Chameleon() { private static String name; } public record BeardedDragon(boolean fun) { @Override public boolean fun() { return false; } } public record Newt(long size) { @Override public boolean equals(Object obj) { return false; } public void setSize(long size) { this.size = size; } }
A. Iguana
B. Gecko
C. Chameleon
D. BeardedDragon
E. Newt
F. None of the above
2. Which of the following statements can be inserted in the blank line so that the code will compile successfully? (Choose all that apply.)
interface CanHop {} public class Frog implements CanHop { public static void main(String[] args) { \_\_\_\_\_\_\_\_\_\_\_ frog = new TurtleFrog(); } } class BrazilianHornedFrog extends Frog {} class TurtleFrog extends Frog {}
A. Frog
B. TurtleFrog
C. BrazilianHornedFrog
D. CanHop
E. var
F. Long
G. None of the above; the code contains a compilation error.
3. What is the result of the following program?
public class Favorites { enum Flavors { VANILLA, CHOCOLATE, STRAWBERRY static final Flavors DEFAULT = STRAWBERRY; } public static void main(String[] args) { for(final var e : Flavors.values()) System.out.print(e.ordinal()+" "); } }
A. 0 1 2
B. 1 2 3
C. Exactly one line of code does not compile.
D. More than one line of code does not compile.
E. The code compiles but produces an exception at runtime.
F. None of the above
4. What is the output of the following program?
public sealed class ArmoredAnimal permits Armadillo { public ArmoredAnimal(int size) {} @Override public String toString() { return "Strong"; } public static void main(String[] a) { var c = new Armadillo(10, null); System.out.println(c); } } class Armadillo extends ArmoredAnimal { @Override public String toString() { return "Cute"; } public Armadillo(int size, String name) { super(size); } }
A. Strong
B. Cute
C. The program does not compile.
D. The code compiles but produces an exception at runtime.
E. None of the above
5. Which statements about the following program are correct? (Choose all that apply.)
1: interface HasExoskeleton { 2: double size = 2.0f; 3: abstract int getNumberOfSections(); 4: } 5: abstract class Insect implements HasExoskeleton { 6: abstract int getNumberOfLegs(); 7: } 8: public class Beetle extends Insect { 9: int getNumberOfLegs() { return 6; } 10: int getNumberOfSections(int count) { return 1; } 11: }
A. It compiles without issue.
B. The code will produce a ClassCastException if called at runtime.
C. The code will not compile because of line 2.
D. The code will not compile because of line 5.
E. The code will not compile because of line 8.
F. The code will not compile because of line 10.
6. Which statements about the following program are correct? (Choose all that apply.)
1: public abstract interface Herbivore { 2: int amount = 10; 3: public void eatGrass(); 4: public abstract int chew() { return 13; } 5: } 6: 7: abstract class IsAPlant extends Herbivore { 8: Object eatGrass(int season) { return null; } 9: }
A. It compiles and runs without issue.
B. The code will not compile because of line 1.
C. The code will not compile because of line 2.
D. The code will not compile because of line 4.
E. The code will not compile because of line 7.
F. The code will not compile because line 8 contains an invalid method
override.
7. What is the output of the following program?
1: interface Aquatic { 2: int getNumOfGills(int p); 3: } 4: public class ClownFish implements Aquatic { 5: String getNumOfGills() { return "14"; } 6: int getNumOfGills(int input) { return 15; } 7: public static void main(String[] args) { 8: System.out.println(new ClownFish().getNumOfGills(-1)); 9: } }
A. 14
B. 15
C. The code will not compile because of line 4.
D. The code will not compile because of line 5.
E. The code will not compile because of line 6.
F. None of the above
8. When inserted in order, which modifiers can fill in the blank to create a properly encapsulated class? (Choose all that apply.)
public class Rabbits { \_\_\_\_\_\_\_ int numRabbits = 0; \_\_\_\_\_\_\_ void multiply() { numRabbits *= 6; } \_\_\_\_\_\_\_ int getNumberOfRabbits() { return numRabbits; } }
A. private, public, and public
B. private, protected, and private
C. private, private, and protected
D. public, public, and public
E. The class cannot be properly encapsulated since multiply() does not
begin with set.
F. None of the above
9. Which of the following statements can be inserted in the blank so that the code will compile successfully? (Choose all that apply.)
abstract class Snake {} class Cobra extends Snake {} class GardenSnake extends Cobra {} public class SnakeHandler { private Snake snakey; public void setSnake(Snake mySnake) { this.snakey = mySnake; } public static void main(String[] args) { new SnakeHandler().setSnake(\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_); } }
A. new Cobra()
B. new Snake()
C. new Object()
D. new String(“Snake”)
E. new GardenSnake()
F. null
G. None of the above. The class does not compile, regardless of the value
inserted in the blank.
10. What types can be inserted in the blanks on the lines marked X and Z that allow the code to compile? (Choose all that apply.)
interface Walk { private static List move() { return null; } } interface Run extends Walk { public ArrayList move(); } class Leopard implements Walk { public \_\_\_\_\_\_\_\_ move() { // X return null; } } class Panther implements Run { public \_\_\_\_\_\_\_\_ move() { // Z return null; } }
A. Integer on the line marked X
B. ArrayList on the line marked X
C. List on the line marked X
D. List on the line marked Z
E. ArrayList on the line marked Z
F. None of the above, since the Run interface does not compile
G. The code does not compile for a different reason.
11. What is the result of the following code? (Choose all that apply.)
1: public class Movie { 2: private int butter = 5; 3: private Movie() {} 4: protected class Popcorn { 5: private Popcorn() {} 6: public static int butter = 10; 7: public void startMovie() { 8: System.out.println(butter); 9: } 10: } 11: public static void main(String[] args) { 12: var movie = new Movie(); 13: Movie.Popcorn in = new Movie().new Popcorn(); 14: in.startMovie(); 15: } }
A. The output is 5.
B. The output is 10.
C. Line 6 generates a compiler error.
D. Line 12 generates a compiler error.
E. Line 13 generates a compiler error.
F. The code compiles but produces an exception at runtime.
12. Which of the following are true about encapsulation? (Choose all that apply.)
A. It allows getters.
B. It allows setters.
C. It requires specific naming conventions.
D. It requires public instance variables.
E. It requires private instance variables.
13. What is the result of the following program?
public class Weather { enum Seasons { WINTER, SPRING, SUMMER, FALL } public static void main(String[] args) { Seasons v = null; switch (v) { case Seasons.SPRING -> System.out.print("s"); case Seasons.WINTER -> System.out.print("w"); case Seasons.SUMMER -> System.out.print("m"); default -> System.out.println("missing data"); } } }
A. s
B. w
C. m
D. missing data
E. Exactly one line of code does not compile.
F. More than one line of code does not compile.
G. The code compiles but produces an exception at runtime.
14. Which statements about sealed classes are correct? (Choose all that apply.)
A. A sealed interface restricts which subinterfaces may extend it.
B. A sealed class cannot be indirectly extended by a class that is not listed in its permits clause.
C. A sealed class can be extended by an abstract class.
D. A sealed class can be extended by a subclass that uses the non-sealed modifier.
E. A sealed interface restricts which subclasses may implement it.
F. A sealed class cannot contain any nested subclasses.
G. None of the above
15. Which lines, when entered independently into the blank, allow the code to print Not scared at runtime? (Choose all that apply.)
public class Ghost { public static void boo() { System.out.println("Not scared"); } protected final class Spirit { public void boo() { System.out.println("Booo!!!"); } } public static void main(String… haunt) { var g = new Ghost().new Spirit() {}; \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_; } }
A. g.boo()
B. g.super.boo()
C. new Ghost().boo()
D. g.Ghost.boo()
E. new Spirit().boo()
F. Ghost.boo()
G. None of the above