Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 3. Declarations > Review Questions

Review Questions

3.7Which statements about the enum type are true?

Select the three correct answers.

  1. An enum type is a subclass of the abstract class java.lang.Enum, hence it is Comparable and Serializable.

  2. An enum type can implement interfaces.

  3. We can instantiate an enum type using the new operator.

  4. An enum type can define constructors.

  5. We can explicitly use the extend clause to extend an enum type.

  6. Enum types do not inherit members from the Object class.

3.8What will be the result of attempting to compile and run the following code?
public enum Drill {
  ATTENTION("Attention!"), EYES_RIGHT("Eyes right!"),
  EYES_LEFT("Eyes left!"), AT_EASE("At ease!");

  private String command;

  Drill(String command) {
    this.command = command;
  }

  public static void main(String[] args) {
    System.out.println(ATTENTION);             // (1)
    System.out.println(AT_EASE);               // (2)
  }
}

Select the one correct answer.

  1. The code compiles, but reports a ClassNotFoundException when run, since an enum type cannot be run as a standalone application.

  2. The compiler reports errors in (1) and (2), as the constants must be qualified by the enum type name Drill.

  3. The compiler reports errors in (1) and (2), as the constants cannot be accessed in a static context.

  4. The code compiles and prints:

    ATTENTION
    AT_EASE
  5. The code compiles and prints:

    Attention!
    At ease!
  6. None of the above.

3.9What will be the result of compiling and running the following code?
import java.util.Arrays;

public enum Priority {
  ONE(1) { public String toString() { return "LOW"; } },        // (1)
  TWO(2),
  THREE(3) { public String toString() { return "NORMAL"; } },   // (2)
  FOUR(4),
  FIVE(5) { public String toString() { return "HIGH"; } };      // (3)

  private int pValue;

  Priority(int pValue) {
    this.pValue = pValue;
  }

  public static void main(String[] args) {
    System.out.println(Arrays.toString(Priority.values()));
  }
}

Select the one correct answer.

  1. The code compiles, but reports a ClassNotFoundException when run, since an enum type cannot be run as a standalone application.

  2. The compiler reports syntax errors in (1), (2), and (3).

  3. The code compiles and prints:

    [LOW, TWO, NORMAL, FOUR, HIGH]
  4. The code compiles and prints:

    [ONE, TWO, THREE, FOUR, HIGH]
  5. None of the above.

3.10Which statement about the following program is true?
public enum Scale {
  GOOD('C'), BETTER('B'), BEST('A');

  private char grade;

  Scale(char grade) {
    this.grade = grade;
  }
  abstract public char getGrade();

  public static void main (String[] args) {
    System.out.println (GOOD.getGrade());     // (1)
  }
}

Select the one correct answer.

  1. Since the enum type declares an abstract method, the enum type must be declared as abstract.

  2. The method call GOOD.getGrade() in (1) can be written without the enum type name.

  3. An enum type cannot declare an abstract method.

  4. An enum type can declare an abstract method, but each enum constant must provide an implementation.

3.11What will be the result of compiling and running the following code?
public enum TrafficLight {
  RED("Stop"), YELLOW("Caution"), GREEN("Go");

  private String action;

  TrafficLight(String action) {
    this.action = action;
  }

  public static void main(String[] args) {
    TrafficLight green = new TrafficLight("Go");
    System.out.println(GREEN.equals(green));
  }
}

Select the one correct answer.

  1. The code will compile and print: true.

  2. The code will compile and print: false.

  3. The code will not compile, as an enum type cannot be instantiated.

  4. An enum type does not have the equals() method.

3.12Given the following program:
public enum Scale2 {
  GOOD('C')   { public char getGrade() { return grade; } },
  BETTER('B') { public char getGrade() { return grade; } },
  BEST('A')   { public char getGrade() { return grade; } };

  private char grade;

  Scale2(char grade) {
    this.grade = grade;
  }
  // (1) INSERT CODE HERE

  public static void main (String[] args) {
    System.out.println(GOOD.getGrade());
  }
}

Which code, when inserted at (1), will make the program print C?

Select the two correct answers.

  1. public char getGrade() { return grade; }

  2. public int getGrade() { return grade; }

  3. abstract public int getGrade();

  4. abstract public char getGrade();

3.13Given the following program:
enum Scale3 {
  GOOD(Grade.C), BETTER(Grade.B), BEST(Grade.A);

  enum Grade {A, B, C}
  private Grade grade;

  Scale3(Grade grade) {
    this.grade = grade;
  }

  public Grade getGrade() { return grade; }
}

public class Scale3Client {
  public static void main (String[] args) {
    System.out.println(/* (1) INSERT CODE HERE */);
  }
}

Which code, when inserted at (1), will make the program print true?

Select the four correct answers.

  1. Scale3.GOOD.getGrade() != Scale3.Grade.C

  2. Scale3.GOOD.getGrade().compareTo(Scale3.Grade.C) != 0

  3. Scale3.GOOD.getGrade().compareTo(Scale3.Grade.A) > 0

  4. Scale3.GOOD.compareTo(Scale3.BEST) > 0

  5. Scale3.GOOD.getGrade() instanceof Scale3.Grade

  6. Scale3.GOOD instanceof Scale3

  7. Scale3.GOOD.getGrade().toString().equals(Scale3.Grade.C.toString())

3.14What will be the result of compiling and running the following code?
public enum Scale5 {
  GOOD, BETTER, BEST;

  public char getGrade() {
    char grade = '\u0000';
    switch(this){
      case GOOD:   grade = 'C'; break;
      case BETTER: grade = 'B'; break;
      case BEST:   grade = 'A'; break;
    }
    return grade;
  }

  public static void main (String[] args) {
    System.out.println(GOOD.getGrade());
  }
}

Select the one correct answer.

  1. The program will not compile, as the switch expression is not compatible with the case labels.

  2. The program will not compile, as enum constants cannot be used as case labels.

  3. The case labels must be qualified with the enum type name.

  4. The program compiles, and when run, prints: C

  5. The program compiles, and when run, prints: GOOD

  6. None of the above.

3.15Given the following code:
package p1;
enum March {LEFT, RIGHT}                          // (1)
public class Defence {
  enum March {LEFT, RIGHT}                        // (2)
  static enum Military {
    INFANTRY, AIRFORCE;
    enum March {LEFT, RIGHT}                      // (3)
  }
  class Secret {
    enum March {LEFT, RIGHT}                      // (4)
  }
  static class Open {
    enum March {LEFT, RIGHT}                      // (5)
  }
  public static void declareWar() {
    enum March {LEFT, RIGHT}                      // (6)
  }
  public void declarePeace() {
    enum March {LEFT, RIGHT}                      // (7)
  }
}

Which enum declarations are not legal?

Select the three correct answers.

  1. The enum declaration at (1) is not legal.

  2. The enum declaration at (2) is not legal.

  3. The enum declaration at (3) is not legal.

  4. The enum declaration at (4) is not legal.

  5. The enum declaration at (5) is not legal.

  6. The enum declaration at (6) is not legal.

  7. The enum declaration at (7) is not legal.

3.16Given the following code:
public enum Direction {
  EAST, WEST, NORTH, SOUTH;

  public static void main (String[] args) {
    // (1) INSERT LOOP HERE
  }
}

Which loops, when inserted independently at (1), will give the following output:

EAST
WEST
NORTH
SOUTH

Select the three correct answers.

  1. for (Direction d : Direction.values()) {
      System.out.println(d);
    }

  2. for (Direction d : Direction.values()) {
      System.out.println(d.name());
    }

  3. for (String name : Direction.names()) {
      System.out.println(name);
    }

  4. for (Direction d : java.util.Arrays.asList(Direction.values())) {
      System.out.println(d);
    }

  5. for (Direction d : java.util.Arrays.asList(Direction.class)) {
      System.out.println(d);
    };

3.17What will be the result of compiling and running the following code?
enum Rank {
  FIRST(20), SECOND(0), THIRD(8);
  Rank(int value) {
    System.out.print(value);
  }
}
public class EnumCreation {
  public static void main (String[] args) {
    System.out.println("\n" + Rank.values().length);
  }
}

Select the one correct answer.

  1. The program will compile and print:

    3
  2. The program will compile and print:

    2008
    3
  3. The program will compile. When run, it will print:

    2008

    and throw an exception.

  4. None of the above.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint