IS5103 WEEK4 LESSON 7 QUIZ

1. Which of the following statements can be inserted in the blank line so
that the code will compile successfully?

interface CanHop {}
public class Frog implements CanHop {
public static void main(String[] args) {
_ frog = new TurtleFrog();
}
}
class BrazilianHornedFrog extends Frog {}
class TurtleFrog extends Frog {}

Each correct answer represents a complete solution. Choose all that apply.

  1. Frog
  2. TurtleFrog
  3. CanHop
  4. var

2. Which statements about the following program are correct?

  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. }

The code will not compile because of line 8.

3. Which statements about the following program are correct?

  1. public abstract interface Herbivore {
  2. int amount = 10;
  3. public void eatGrass();
  4. public abstract int chew() { return 13; }
  5. }
  6. abstract class IsAPlant extends Herbivore {
  7. Object eatGrass(int season) { return null; }
  8. }

Each correct answer represents a complete solution. Choose all that apply.

  1. The code will not compile because of line 4.
  2. The code will not compile because of line 7.

4. 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. } }

The code will not compile because of line 6

5. What types can be inserted in the blanks on the lines marked X and Z
that allow the code to compile?

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;
}
}

Each correct answer represents a part of the solution. Choose all that apply.

1.Integer on the line marked X

  1. ArrayList on the line marked X
  2. List on the line marked X
  3. ArrayList on the line marked Z

6. Which lines of the following interface declarations do not compile?

  1. public interface Omnivore {
  2. int amount = 10;
  3. static boolean gather = true;
  4. static void eatGrass() {}
  5. int findMore() { return 2; }
  6. default float rest() { return 2; }
  7. protected int chew() { return 13; }
  8. private static void eatLeaves() {}
  9. }

Each correct answer represents a complete solution. Choose all that apply
A) 1.Line 5

  1. Line 7

7. Given the following program, what can be inserted into the blank line
that would allow it to print Swim! at runtime?

interface Swim {
default void perform() { System.out.print(“Swim!”); }
}
interface Dance {
default void perform() { System.out.print(“Dance!”); }
}
public class Penguin implements Swim, Dance {
public void perform() { System.out.print(“Smile!”); }
private void doShow() {
______________;
}
public static void main(String[] eggs) {
new Penguin().doShow();
}
}

Swim.super.perform()

8. Which lines of the following interface do not compile?

  1. public interface BigCat {
  2. abstract String getName();
  3. static int hunt() { getName(); return 5; }
  4. default void climb() { rest(); }
  5. private void roar() { getName(); climb(); hunt(); }
  6. private static boolean sneak() { roar(); return true; }
  7. private int rest() { return 2; };
  8. }

Each correct answer represents a complete solution. Choose all that apply.
A) 1.Line 3

  1. Line 6

9. Which of the following classes and interfaces do not compile?

public abstract class Camel { void travel(); }
public interface EatsGrass { private abstract int chew(); }
public abstract class Elephant {
abstract private class SleepsAlot {
abstract int sleep();
} }
public class Eagle { abstract soar(); }
public interface Spider { default void crawl() {} }

Each correct answer represents a part of the solution. Choose all that apply.
A)

  1. Camel
  2. EatsGrass
  3. Eagle

10. 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) Exactly one line of code does not compile.

11. 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) More than one line of code does not compile.

12. What is printed by the following program?

public class Deer {
enum Food {APPLES, BERRIES, GRASS}
protected class Diet {
private Food getFavorite() {
return Food.BERRIES;
}
}
public static void main(String[] seasons) {
System.out.print(switch(new Diet().getFavorite()) {
case APPLES -> “a”;
case BERRIES -> “b”;
default -> “c”;
});
} }

A) The main() method does not compile.

13. Which of the following are printed by the Bear program?

public class Bear {
enum FOOD {
BERRIES, INSECTS {
public boolean isHealthy() { return true; }},
FISH, ROOTS, COOKIES, HONEY;
public abstract boolean isHealthy();
}
public static void main(String[] args) {
System.out.print(FOOD.INSECTS);
System.out.print(FOOD.INSECTS.ordinal());
System.out.print(FOOD.INSECTS.isHealthy());
System.out.print(FOOD.COOKIES.isHealthy());
}
}

A) The code does not compile.

14. Which statements about the following enum are true?

  1. public enum Animals {
  2. MAMMAL(true), INVERTEBRATE(Boolean.FALSE), BIRD(false),
  3. REPTILE(false), AMPHIBIAN(false), FISH(false) {
  4. public int swim() { return 4; }
  5. }
  6. final boolean hasHair;
  7. public Animals(boolean hasHair) {
  8. this.hasHair = hasHair;
  9. }
  10. public boolean hasHair() { return hasHair; }
  11. public int swim() { return 0; }
  12. }

Each correct answer represents a complete solution. Choose all that apply.
A) 1. Compiler error on line 7

  1. Compiler error on another line

15. 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) The program does not compile.

16. Which statements about sealed classes are correct?

Each correct answer represents a complete solution. Choose all that apply.

  1. A sealed interface restricts which subinterfaces may extend it.
  2. A sealed class can be extended by an abstract class.
  3. A sealed interface restricts which subclasses may implement it.

17. Assuming the following classes are declared as top-level types in
the same file, which classes contain compiler errors?

sealed class Bird {
public final class Flamingo extends Bird {}
}
sealed class Monkey {}
class EmperorTamarin extends Monkey {}
non-sealed class Mandrill extends Monkey {}
sealed class Friendly extends Mandrill permits Silly {}
final class Silly {}

A) 1. EmperorTamarin
2.Friendly

18. When inserted in order, which modifiers can fill in the blank to
create a properly encapsulated class?

public class Rabbits {
int numRabbits = 0;
void multiply() {
numRabbits *= 6;
}
_ int getNumberOfRabbits() {

return numRabbits;
}
}

A) 1. private, public, and public

  1. private, protected, and private
  2. private, private, and protected

19. Which of the following are valid record declarations?

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) 1. Gecko

  1. BeardedDragon

20. Which of the following are true about encapsulation?

1.It allows getters.

  1. It allows setters.
  2. It requires private instance variables.

21. Given the following record declaration, which lines of code can fill in the
blank and allow the code to compile?

public record RabbitFood(int size, String brand, LocalDate expires) {
public static int MAX_STORAGE = 100;
public RabbitFood() {
_____________;

}
}

A) None of these

22. Assuming a record is defined with at least one field, which
components does the compiler always insert, each of which may be
overridden or redeclared?
Each correct answer represents a part of the solution. Choose all that
apply

  1. An accessor method for each field
  2. The toString() method
  3. The equals() method
  4. The hashCode() method

23. How many lines of the following program contain a compilation
error?

3

24. What is the result of the following code?

  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 10.

25. . Which lines, when entered independently into the blank, allow the code
to print Not scared at runtime?

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) None of these

26. The following code appears in a file named Ostrich.java. What is the
result of compiling the source file?

  1. public class Ostrich {
  2. private int count;
  3. static class OstrichWrangler {
  4. public int stampede() {
  5. return count;
  6. } } }

A) A compiler error occurs on line 5.

27. Which of the following can be inserted in the rest() method?

public class Lion {
class Cub {}
static class Den {}
static void rest() {
___________;
} }

A) 1. Lion.Cub c = new Lion().new Cub()

  1. vard = new Den()
  2. Lion.Den g = new Lion.Den()

28. What does the following program print?

  1. public class Zebra {
  2. private int x = 24;
  3. public int hunt() {
  4. String message = “x is “;
  5. abstract class Stripes {
  6. private int x = 0;
  7. public void print() {
  8. System.out.print(message + Zebra.this.x);
  9. }
  10. }
  11. var s = new Stripes() {};
  12. s.print();
  13. return x;
  14. }
  15. public static void main(String[] args) {
  16. new Zebra().hunt();
  17. } }

A) x is 24

29. Which statements about polymorphism and method inheritance
are correct?

  1. Given an arbitrary instance of a class, it cannot be
    determined until runtime which overridden method will be
    executed in a parent class.
  2. Marking a method final prevents it from being overridden or
    hidden.
  3. The reference type of the variable determines which hidden
    method will be called at runtime.

Other Links:

Statistics Quiz

Networking Quiz

See other websites for quiz:

Check on QUIZLET

Check on CHEGG

Leave a Reply

Your email address will not be published. Required fields are marked *