Designing Classes Complete Test Bank Chapter 8 - Big Java Early Objects 5e Complete Test Bank by Cay S. Horstmann. DOCX document preview.

Designing Classes Complete Test Bank Chapter 8

Chapter 08: Designing Classes

Test Bank

Multiple Choice

1. Which of the following is considered by the text to be the most important consideration when designing a class?

a) Each class should represent an appropriate mathematical concept.

b) Each class should represent a single concept or object from the problem domain.

c) Each class should represent no more than three specific concepts.

d) Each class should represent multiple concepts only if they are closely related.

Section Reference: 8.1 Discovering Classes

Title: What is the most important consideration when designing a class?

Difficulty: Medium

2. Which of the following questions should you ask yourself in order to determine if you have named your class properly?

a) Does the class name contain 8 or fewer characters?

b) Is the class name a verb?

c) Can I visualize an object of the class?

d) Does the class name describe the tasks that this class will accomplish?

Section Reference: 8.1 Discovering Classes

Title: How do I ensure I have named a class properly?

Difficulty: Easy

3. Which class category has static methods and constants, but no objects?

a) Real-life entity abstraction

b) Actor class

c) Utility class

d) Concept class

Section Reference: 8.1 Discovering Classes

Title: Which class category has static methods and constants, but no objects?

Difficulty: Medium

4. A class named CurrencyTranslator would most probably fall into which of the following class categories?

a) Actor classes

b) Starter classes

c) Abstract entities

d) Utility classes

Section Reference: 8.1 Discovering Classes

Title: Into which class category does CurrencyTranslator fall?

Difficulty: Medium

5. Why does the Scanner class belong to the category of classes known as actors?

a) It acts as an interface between your program and the command line.

b) It serves as an abstract entity that represents a user of your program.

c) It plays an important part in streamlining the operation of your system.

d) It performs a task, such as scanning a stream for numbers and characters.

Section Reference: 8.1 Discovering Classes

Title: Why is the Scanner class categorized as an actor class?

Difficulty: Hard

6. Which of the following would be an appropriate name for a game-related class?

a) CompletedLevelOne

b) InitialLevel

c) ResetCurrentLevel

d) AscendToFinalLevel

Section Reference: 8.1 Discovering Classes

Title: Which would be an appropriate class name?

Difficulty: Medium

7. Which of the following most likely indicates that you have chosen a good name for your class?

a) The name consists of a single word.

b) You can tell by the name what an object of the class is supposed to represent.

c) You can easily determine by the name how many concepts the class represents.

d) The name describes what task the class will perform.

Section Reference: 8.1 Discovering Classes

Title: How can you tell when you’ve chosen a good class name?

Difficulty: Medium

8. Which of the following describes the first thing you should do when beginning a new object-oriented programming activity?

a) Consider how many concepts each class should represent.

b) Determine which categories of classes may be needed for the project.

c) Decide what combination of functions and methods are appropriate for the project.

d) Identify the objects and the classes to which they belong.

Section Reference: 8.1 Discovering Classes

Title: What is the first thing you do when beginning a new programming activity?

Difficulty: Easy

9. Under which of the following conditions would the public interface of a class be considered cohesive?

a) All of its features are public and none of its features are static.

b) The quality of the public interface is rated as moderate to high.

c) All of its features are related to the concept that the class represents.

d) It is obvious that the public interface refers to multiple concepts.

Section Reference: 8.2 Designing Good Methods

Title: When is the public interface of a class considered cohesive?

Difficulty: Hard

10. General Java variable naming conventions would suggest that a variable named NICKEL_VALUE would most probably be declared using which of the following combinations of modifiers?

a) public void final

b) public static final double

c) private static double

d) private static

Section Reference: 8.2 Designing Good Methods

Title: Which modifiers are used to declare a variable named NICKEL_VALUE?

Difficulty: Medium

11. Which of the following is a good indicator that a class is overreaching and trying to accomplish too much?

a) The class has more constants than methods

b) The public interface refers to multiple concepts

c) The public interface exposes private features

d) The class is both cohesive and dependent.

Section Reference: 8.2 Designing Good Methods

Title: What is a good indicator that a class is trying to accomplish too much?

Difficulty: Hard

12. Given the following class definition, which of the following are considered part of the class’s public interface?

public class CashRegister

{

public static final double DIME_VALUE = 0.1;

private static int objectCounter;

public void updateDimes(int dimes) {. . .}

private boolean updateCounter(int counter) {. . .}

}

a) objectCounter and updateCounter

b) DIME_VALUE and objectCounter

c) DIME_VALUE and updateDimes

d) updateDimes and updateCounter

Section Reference: 8.2 Designing Good Methods

Title: Which are part of the class’s public interface?

Difficulty: Easy

13. A class (ClassOne) is considered to have a dependency on another class (ClassTwo) under which of the following conditions?

a) Each class uses objects of the other.

b) The public interfaces of both classes are cohesive.

c) ClassTwo uses objects of ClassOne.

d) ClassOne uses objects of ClassTwo.

Section Reference: 8.2 Designing Good Methods

Title: Under what conditions does one class have a dependency on another?

Difficulty: Medium

14. A UML class diagram would be most useful in visually illustrating which of the following?

a) the cohesiveness of a class’s interface.

b) the amount of complexity of a class’s interface.

c) dependencies between classes.

d) relationships between classes and their interfaces.

Section Reference: 8.2 Designing Good Methods

Title: For what are UML class diagrams most useful?

Difficulty: Medium

15. Why is it generally considered good practice to minimize coupling between classes?

a) Low coupling increases the operational efficiency of a program.

b) High coupling implies less interface cohesion.

c) High coupling increases program maintenance and hinders code reuse.

d) Low coupling decreases the probability of code redundancy.

Section Reference: 8.2 Designing Good Methods

Title: Why is it considered good practice to minimize class coupling?

Difficulty: Hard

16. Which of the following represents a good strategy regarding cohesion and coupling?

a) Maximize cohesion and remove unnecessary coupling.

b) Minimize cohesion and remove unnecessary coupling..

c) Maximize both cohesion and coupling.

d) Minimize cohesion and maximize coupling..

Section Reference: 8.2 Designing Good Methods

Title: Which is a good strategy regarding cohesion and coupling?

Difficulty: Medium

17. Which of the following is a true statement regarding consistency in coding?

a) Inconsistencies nearly always produce fatal flaws.

b) Inconsistencies are an annoyance, because they can be so easily avoided.

c) Consistency is not important in programming.

d) Consistency always requires extra coding.

Section Reference: 8.2 Designing Good Methods

Title: Which is a true statement regarding consistency?

Difficulty: Medium

18. Which of the following describes an immutable class?

a) A class that has no accessor or mutator methods.

b) A class that has no accessor methods, but does have mutator methods.

c) A class that has accessor methods, but does not have mutator methods.

d) A class that has both accessor and mutator methods.

Section Reference: 8.2 Designing Good Methods

Title: Which of the following describes an immutable class?

Difficulty: Easy

19. Which type of method modifies the object on which it is invoked?

a) Constructor method.

b) Static method.

c) Accessor method.

d) Mutator method.

Section Reference: 8.2 Designing Good Methods

Title: Which type of method modifies the object on which it is invoked?

Difficulty: Medium

20. The String class is an example of which of the following types of classes?

a) Static class.

b) Immutable class.

c) Abstract class.

d) Mutable class.

Section Reference: 8.2 Designing Good Methods

Title: The String class is an example of which type of class?

Difficulty: Easy

21. Which of the following is true regarding objects created from immutable classes?

a) It is safe to give out references to immutable class objects freely.

b) An immutable object can only be modified by its mutator method.

c) It is not advisable to return immutable object references from accessor methods.

d) Immutable classes do not have constructors.

Section Reference: 8.2 Designing Good Methods

Title: Which is true regarding objects of immutable classes?

Difficulty: Hard

22. Which of the following is an example of an immutable class?

a) Math.

b) Scanner.

c) String.

d) System.

Section Reference: 8.2 Designing Good Methods

Title: Which is an example of an immutable class?

Difficulty: Easy

23. Judging by the name of the method, which of the following methods would most likely be a mutator method?

a) getListOfDeposits.

b) printAccountBalance.

c) isOverdrawn.

d) Deposit.

Section Reference: 8.2 Designing Good Methods

Title: Which method would most likely be a mutator method?

Difficulty: Easy

24. Which of the following statements is true relative to classes that will successfully compile?

a) They must have both mutator and accessor methods.

b) They must have accessors, and optionally may have mutators.

c) They must have mutators, and optionally may have accessors.

d) They may have either mutator or accessor methods, or both.

Section Reference: 8.2 Designing Good Methods

Title: Which is true relative to classes that will successfully compile?

Difficulty: Medium

25. Mutator methods exhibit which of the following types of side effect?

a) Modification of the implicit parameter.

b) Modification of an explicit parameter.

c) Production of printed output.

d) Acceptance of text input.

Section Reference: 8.2 Designing Good Methods

Title: Mutator methods exhibit which type of side effect?

Difficulty: Medium

26. Which of the following is true regarding side effects of methods?

a) Modification of implicit parameters should be restricted to primitive data types.

b) Modification of explicit parameters should never be allowed.

c) Side effects involving standard output should be limited to String data.

d) Minimize side effects that go beyond modification of implicit parameters.

Section Reference: 8.2 Designing Good Methods

Title: Which is true regarding side effects of methods?

Difficulty: Hard

27. Side effects related to standard output generally violate which of the following rules?

a) Never display the values of explicit parameters.

b) Never display the values of implicit parameters.

c) Minimize the coupling of classes.

d) Maximize the coupling of classes.

Section Reference: 8.2 Designing Good Methods

Title: Side effects related to standard output generally violate which rule?

Difficulty: Hard

28. Given the following method header, other would be classified as which of the following parameter types?

public void transfer(double amount, BankAccount other)

a) Implicit parameter.

b) Explicit parameter.

c) Global parameter.

d) Mutable parameter.

Section Reference: 8.2 Designing Good Methods

Title: This parameter would be classified as which type?

Difficulty: Easy

29. Which of the following does NOT describe a particular side effect related to standard output?

a) Messages printed in a particular language may be of limited usefulness.

b) Methods that rely on standard output have limited usefulness in embedded systems.

c) Programmers may be surprised by unexpected method actions.

d) Programmers may need values for purposes other than informational messages.

Section Reference: 8.2 Designing Good Methods

Title: Which does NOT describe a side effect related to standard output?

Difficulty: Medium

30. Why can’t Java methods change parameters of primitive type?

a) Java methods can have no actual impact on parameters of any type.

b) Parameters of primitive type are considered by Java methods to be local variables.

c) Parameters of primitive type are immutable.

d) Java methods cannot accept parameters of primitive type.

Section Reference: 8.2 Designing Good Methods

Title: Why can’t Java methods change parameters of primitive type?

Difficulty: Medium

31. Which of the following classifications of method behavior is accurate?

a) Methods that do not change explicit parameters always have side effects.

b) Methods that change explicit parameters do not have side effects.

c) Methods that change explicit parameters always have side effects.

d) Methods that do not change explicit parameters never have side effects.

Section Reference: 8.2 Designing Good Methods

Title: Which classification of method behavior is accurate?

Difficulty: Hard

32. Which of the following can potentially be changed by a Java method?

a) An object reference parameter.

b) The state of an object reference parameter’s attribute.

c) A primitive parameter.

d) A String parameter.

Section Reference: 8.2 Designing Good Methods

Title: Which can potentially be changed by a Java method?

Difficulty: Medium

33. Which of the following types of side effects potentially violates the rule of minimizing the coupling of classes?

a) Standard output.

b) Modification of implicit parameters.

c) Modification of explicit parameters.

d) Modification of both implicit and explicit parameters.

Section Reference: 8.2 Designing Good Methods

Title: Which potentially violates the rule of minimizing the coupling of classes?

Difficulty: Medium

34. Which of the following classifications of method behavior produces acceptable side effects?

a) Accessor methods that do not change explicit parameters.

b) Mutator methods that do not change explicit parameters.

c) Any method that changes an explicit parameter.

d) Any method that changes another object.

Section Reference: 8.2 Designing Good Methods

Title: Which classification of method behavior produces acceptable side effects?

Difficulty: Medium

35. Which of the following statements is true regarding method parameters in Java?

a) All method parameters use the call-by-value mechanism.

b) Only method parameters of primitive type use the call-by-value mechanism.

c) All method parameters use the call-by-reference mechanism.

d) Only method parameters of object type use the call-by-value mechanism.

Section Reference: 8.2 Designing Good Methods

Title: Which is true regarding method parameters in Java?

Difficulty: Medium

36. In Java, which of the following mechanisms describe the copying of an object reference into a parameter variable?

a) Call-by-reference .

b) Call-by-value.

c) Call-by-precondition.

d) Call-by-object.

Section Reference: 8.2 Designing Good Methods

Title: Which mechanism describes the copying of an object reference into a parameter variable?

Difficulty: Easy

37. A class that has only accessor methods and no mutators methods is called a/an ____ class.

  1. Utility.
  2. Static.
  3. Immutable.
  4. Actor.

Section Reference: 8.2 Designing Good Methods

Title: What is a class with only accessor methods called?

Difficulty: Easy

38. Consider the method in the following code snippet:

public void getRewardPoints()

{

System.out.println(“Your Reward Points balance is now “ + pointBalance);

}

Which of the following statements would not be a valid criticism of the design of this method?

a) It violates the design rule of minimizing the coupling of classes.

b) It assumes that the message will always need to be in English.

c) It might not work in an embedded system.

d) It should not be a separate method since it is only one line long.

Section Reference: 8.2 Designing Good Methods

Title: Which is not a valid criticism of this method’s design?

Difficulty: Medium

39. Which of the following statements describes a precondition?

a) A logical condition in a program that you believe to be true.

b) A guarantee that the object is in a certain state after the method call is completed.

c) A requirement that the caller of a method must meet.

d) A set of criteria defined by the caller of a method.

Section Reference: 8.2 Designing Good Methods

Title: Which statement describes a precondition?

Difficulty: Easy

40. Which of the following statements describes one aspect of a postcondition?

a) A logical condition in a program that you believe to be true.

b) A guarantee that the object is in a certain state after the method call is completed.

c) A requirement that the caller of a method must meet.

d) A set of criteria defined by the caller of a method.

Section Reference: 8.2 Designing Good Methods

Title: Which statement describes one aspect of a postcondition?

Difficulty: Easy

41. Which of the following statements describes an assertion?

a) A logical condition in a program that you believe to be true.

b) A guarantee that the object is in a certain state after the method call is completed.

c) A requirement that the caller of a method must meet.

d) A set of criteria defined by the caller of a method.

Section Reference: 8.2 Designing Good Methods

Title: Which statement describes an assertion?

Difficulty: Easy

42. Which of the following statements is true regarding the precondition of a method?

a) It is a suggestion to the caller of the method regarding method input values.

b) It guarantees the object will be in a certain state after the method call is completed.

c) It limits the potential data type of the parameter variables.

d) If it is not met, the method is not responsible for computing the correct result.

Section Reference: 8.2 Designing Good Methods

Title: Which statement is true regarding the precondition of a method?

Difficulty: Medium

43. An AssertionError will be generated under which of the following conditions?

a) Assertion checking is turned off.

b) Assertion checking is turned off and an assertion fails.

c) An assertion fails and assertion checking is turned on.

d) Any time an assertion fails.

Section Reference: 8.2 Designing Good Methods

Title: An AssertionError is generated under which condition?

Difficulty: Medium

44. Pre- and postconditions may be viewed as a contract between which of the following parties?

a) A method and its caller.

b) A class and its interface.

c) A method signature and its implementation.

d) An object and its class.

Section Reference: 8.2 Designing Good Methods

Title: Pre- and postconditions may be viewed as a contract between which of these?

Difficulty: Easy

45. Which perspective should be used in formulating pre- and postconditions?

a) The private implementation of the class.

b) The public interface of the class.

c) The knowledge of the designer.

d) The requirements of the user.

Section Reference: 8.2 Designing Good Methods

Title: Which perspective should be used in formulating pre- and postconditions?

Difficulty: Hard

46. Consider the method signature in the following code snippet:

public static double getTriangleArea (double heightLen, double baseLen)

Which of the following would be a valid postcondition for this method?

a) Postcondition: getTriangleArea() >= 0

b) Postcondition: getTriangleArea() > 0

c) Postcondition: getTriangleArea() <= baseLen

d) There are no valid postcoditions that could be specified for this method

Section Reference: 8.2 Designing Good Methods

Title: Which would be a valid postcondition for this method?

Difficulty: Easy

47. Which command would execute a program with assertion checking turned on?

  1. java –assertionson MainClass
  2. java -assertionsenabled MainClass
  3. java –enableassert MainClass
  4. java –enableassertions MainClass

Section Reference: 8.2 Designing Good Methods

Title: Which annotation is used to mark test methods when using JUnit?

Difficulty: Easy

48. Consider the partial Date class below which represents a day of the year.

public class Date

{

private int month; // A value between 1 and 12

private int day; // A value between 1 and the last day

// of the month

public int getMonth() { return month; }

public void setMonth(int newMonth)

{

if (newMonth >= 1 && newMonth <= 12) { month = newMonth; }

}

public int getDay() { return day; }

private int lastDayOfMonth()

{

switch (month)

{

case 9: case 4: case 6: case 11:

return 30;

case 2: return 28;

default: return 31;

}

}

}

What type of method is getMonth?

A) mutator

B) constructor

C) accessor

Section Reference: 8.2 Designing Code Methods

Title: What type of method is getMonth?

Difficulty: Easy

49. Consider the partial Date class below which represents a day of the year.

public class Date

{

private int month; // A value between 1 and 12

private int day; // A value between 1 and the last day

// of the month

public int getMonth() { return month; }

public void setMonth(int newMonth)

{

if (newMonth >= 1 && newMonth <= 12) { month = newMonth; }

}

public int getDay() { return day; }

private int lastDayOfMonth()

{

switch (month)

{

case 9: case 4: case 6: case 11:

return 30;

case 2: return 28;

default: return 31;

}

}

}

What type of method is setMonth?

a) mutator

b) accessor

c) constructor

Section Reference: 8.2 Designing Code Methods

Title: What type of method is setMonth?

Difficulty: Easy

50. It has been suggested the accessor method below be added to the class to return the Date one day later than the given date.

public Date getNextDate()

{

if (day == lastDayOfMonth())

{

month++;

if (month == 12) { month = 1; }

day = 1;

}

else { day++; }

return this;

}

What is wrong with this design?

a) You cannot return this from a method.

b) It uses lastDayOfMonth, a private method of the class.

c) It has the side effect of changing the Date.

d) Since it returns a Date other than this one, the class is not consistent.

Section Reference: 8.2 Designing Code Methods

Title: What type of method is setMonth?

Difficulty: Easy

51. Which of the following types of methods are invoked on objects?

a) Static method.

b) Class method.

c) Instance method.

d) Either static or instance methods.

Section Reference: 8.4 Static Variables and Methods

Title: Which types of methods are invoked on objects?

Difficulty: Easy

52. A static method can have which of the following types of parameters?

a) Only implicit parameters.

b) Only explicit parameters.

c) Both implicit and explicit parameters.

d) A static method cannot have parameters.

Section Reference: 8.4 Static Variables and Methods

Title: A static method can have which type of parameters?

Difficulty: Hard

53. Which of the following constitutes a common reason for creating a static method?

a) To encapsulate a computation that involves only numbers.

b) To implement a computation that only involves implicit parameters.

c) To test a computation prior to implementing it in a class.

d) To improve efficiency by reducing overhead associated with the use of objects.

Section Reference: 8.4 Static Variables and Methods

Title: Which constitutes a common reason for creating a static method?

Difficulty: Hard

54. The use of the static keyword in a method declaration implies which of the following?

a) The method can only operate on immutable objects.

b) The method can only be called from within the main method.

c) The method cannot be overloaded.

d) The method can have only explicit parameters.

Section Reference: 8.4 Static Variables and Methods

Title: What does the use of the static keyword in a method declaration imply?

Difficulty: Hard

55. Which of the following statements regarding static methods is true?

  1. The textbook recommends that you minimize the use of static methods.
  2. The textbook recommends that static methods should be liberally used within classes.
  3. Use of static methods usually leads to good object-oriented design.
  4. Static methods can be easily evolved to add more functionality.

Section Reference: 8.4 Static Variables and Methods

Title: Which statement regarding static methods is true?

Difficulty: Medium

56. Why is a static variable also referred to as a class variable?

a) There is a single copy available to all objects of the class.

b) It is encapsulated within the class.

c) Each class has one and only one static variable.

d) It is stored in the separate class area of each object.

Section Reference: 8.4 Static Variables and Methods

Title: Why is a static field also referred to as a class field?

Difficulty: Medium

57. In which of the following cases could a static variable be declared as something other than private?

a) When it will be accessed by multiple objects.

b) When implementing static constants.

c) When declared inside a private method.

d) Static variables should never be declared as anything but private.

Section Reference: 8.4 Static Variables and Methods

Title: In which case could a static variable be declared as something other than private?

Difficulty: Medium

58. Which of the following classes has a static variable that is commonly used when communicating with standard output?

a) Scanner.

b) String.

c) System.

d) Object.

Section Reference: 8.4 Static Variables and Methods

Title: Which class has a static variable that is commonly used with standard output?

Difficulty: Easy

59. Which of the following statements is generally true regarding static methods and variables?

a) The use of static variables and methods should be minimized.

b) The use of static variables and methods should be maximized.

c) A lot of static methods and variables indicate a well-designed object-oriented system.

d) Always choose static methods and variables over similar object-based implementations.

Section Reference: 8.4 Static Variables and Methods

Title: Which statement is true regarding static methods and variables?

Difficulty: Medium

60. Where in a class should static variables be declared and initialized?

a) Inside the constructor method.

b) Inside the class, but outside of any method body.

c) Outside the class – in a separate designated static class.

d) Outside the class, but inside the same physical file.

Section Reference: 8.4 Static Variables and Methods

Title: Where in a class should static variables be declared and initialized?

Difficulty: Easy

61. When are statements in a class-level initialization block executed?

a) Before the code in the class constructor is executed.

b) Once when the class is loaded.

c) Whenever objects of the class are used to access methods.

d) After the code in the class constructor is executed.

Section Reference: 8.4 Static Variables and Methods

Title: When are statements in a class-level initialization block executed?

Difficulty: Medium

62. When are statements in a class-level static initialization block executed?

a) Before the code in the class constructor is executed.

b) Once when the class is loaded.

c) Whenever objects of the class are used to access methods.

d) After the code in the class constructor is executed.

Section Reference: 8.4 Static Variables and Methods

Title: When are statements in a class-level static initialization block executed?

Difficulty: Medium

63. Which of the following statements generally describes the scope of a variable?

a) The ability of a variable to access other variables in the same program.

b) The amount of data in bytes that the variable can contain.

c) The range of data types that the variable can legally be cast into.

d) The region of a program in which the variable can be accessed.

Section Reference: 8.4 Static Variables and Methods

Title: Which statement generally describes the scope of a variable?

Difficulty: Easy

64. Which of the following statements describes the scope of a local variable?

a) From the beginning through the end of the block that encloses it.

b) From the point of its declaration to the point where it is set to null.

c) From the point of its initialization to the end of the block that encloses it.

d) From the point of its declaration to the end of the block that encloses it.

Section Reference: 8.4 Static Variables and Methods

Title: Which statement describes the scope of a local variable?

Difficulty: Easy

65. Under which of the following conditions can you have local variables with identical names?

a) If their scopes are nested.

b) If they are of different data types.

c) If their scopes do not overlap.

d) If they both reference the same object.

Section Reference: 8.4 Static Variables and Methods

Title: Under which condition can you have local variables with identical names?

Difficulty: Medium

66. Which of the following are considered members of a class?

a) Private instance variables and methods.

b) All instance variables and methods.

c) Non-static instance variables and methods.

d) Public instance variables and methods.

Section Reference: 8.4 Static Variables and Methods

Title: Which are considered members of a class?

Difficulty: Easy

67. What is the scope of a private instance variable?

a) The entire class in which the variable is defined.

b) All classes in the same package as the class in which the variable is defined.

c) The entire program.

d) Only the method in which the variable is defined.

Section Reference: 8.4 Static Variables and Methods

Title: What is the scope of a private instance variable?

Difficulty: Easy

68. Which of the following names would be considered to be qualified?

a) studentOne.getGraduationYear()

b) this.getGraduationYear()

c) getGraduationYear()

d) getGraduationYear().print()

Section Reference: 8.4 Static Variables and Methods

Title: Which name would be considered to be qualified?

Difficulty: Medium

69. Consider the following code snippet:

public class RewardPointsAccount

{

private int currentRewardPointBalance;

private static int level1Cutoff = 15000;

...

}

If a program instantiates four objects using this class, which of the following statements will be true?

a) Each object will have a currentRewardPointBalance instance variable and a level1Cutoff instance variable.

b) All objects will share a single currentRewardPointBalance class variable and a level1Cutoff class variable.

c) Each object will have a currentRewardPointBalance instance variable, but all objects will share a level1Cutoff class variable.

d) All objects will share a currentRewardPointBalance class variable and each object will have a level1Cutoff instance variable.

Section Reference: 8.4 Static Variables and Methods

Title: Which statement about instance variables is true?

Difficulty: Medium

70. Consider the following code snippet:

public class RewardPointsAccount

{

private int currentRewardPointBalance;

private int level1Cutoff = 15000;

public static void main (String [] args)

{

int level1Cutoff = 15000;

if (blnEliteCustomer)

{

this.Level1Cutoff = 12500;

. . .

}

}

Which of the following statements is true?

a) It is illegal to declare the level1Cutoff variable within the main method

b) The code within the main method changes the value of the instance variable level1Cutoff for elite customers

c) The level1Cutoff variable within the main method does not change the level1Cutoff instance variable for elite customers

d) The level1Cutoff class variable shadows the level1Cutoff variable within the main method

Section Reference: 8.4 Static Variables and Methods

Title: Which statement about this instance variable is true?

Difficulty: Medium

71. Consider the following code snippet:

public class Vehicle

{

String vehicleType;

. . .

}

Which of the following statements about the variable vehicleType is true?

a) It can be accessed by all classes in the same package as the Vehicle class.

b) It can be accessed only by code within the Vehicle class.

c) It can be accessed only by public methods declared within the Vehicle class.

d) It can be accessed only by private methods declared within the Vehicle class.

Section Reference: 8.4 Static Variables and Methods

Title: Which statement about this variable is true?

Difficulty: Medium

72. To use a public variable or method outside of its class, you must ____ the name.

a) bind

b) encapsulate

c) instantiate

d) qualify

Section Reference: 8.4 Static Variables and Methods

Title: How can you use a public variable or method outside of its class?

Difficulty: Easy

73. Under which of the following conditions must you qualify the name of a public class member?

a) If it is to be used outside its class.

b) If it is to be used inside its class.

c) Public class members must always be qualified.

d) Public class members never need to be qualified.

Section Reference: 8.4 Static Variables and Methods

Title: Under which conditions must you qualify the name of a public class member?

Difficulty: Medium

74. A new method, getMonthName, to get the name of the Date's month ("January", February", etc.) is to be added to the class Date. Which of the following headers should it have?

a) public String getMonthName(int month)

b) public String getMonthName()

c) public void getMonthName(String monthName)

d) public void getMonthName(int month, String monthName)

Section Reference: 8.4 Static Variables and Methods

Title: Which header should the new method have?

Difficulty: Easy

75. A new method, getMonthName, to get the name of the Date's month ("January", February", etc.) is to be added to the class Date. How will the instance variables need to be changed to handle this new method?

a) The type of the month instance variable will need to be changed to String.

b) A new String variable called monthName will need to be added to the class.

c) A new class called MonthNameAndNumber will need to be created to store both the name and number of the month and the type of the instance variable month will need to be changed to MonthNameAndNumber.

d) No change is needed.

Section Reference: 8.4 Static Variables and Methods

Title: How will the instance variables need to be changed to handle this new method?

Difficulty: Medium

76. Can the method lastDayOfMonth be changed to be a static method by just changing the header to the following?

private static int lastDayOfMonth()

a) No. It could no longer access the month instance variable.

b) No. A class cannot have both static and non-static methods.

c) Yes. No other change is necessary.

d) Yes, but it must be changed to public as well.

Section Reference: 8.4 Static Variables and Methods

Title: Can the method lastDayOfMonth be changed to be a static method by just changing the header to the following?

Difficulty: Medium

77. What will have to change if the instance variables month and day of the Date class are replaced by the single instance integer variable dayOfTheYear?

a) The implementation of the methods will change, but the interface will not need to change.

b) The implementation of getDay will not change, but the implementations of the other methods will not change and the interface will not change.

c) The interface will need to change to remove setMonth, but no other parts of the interface will change.

d) All of the interface will need to be changed when the instance variables change.

Section Reference: 8.4 Static Variables and Methods

Title: What will have to change if the instance variables month and day of the Date class are replaced by the single instance integer variable dayOfTheYear?

Difficulty: Medium

78. A new class is proposed to collect information about a group of DVDs. Which of the following is the best name for this class?

a) DVDCollection

b) DVD

c) StoreDVDInformation

d) Collection

Section Reference: 8.4 Static Variables and Methods

Title: A new class is proposed to collect information about a group of DVDs. Which of the following is the best name for this class?

Difficulty: Medium

79. A new class is proposed to collect information about a group of DVDs. Which of the following is the best structure for this class?

a) Have separate classes for a single DVD and the entire DVD collection.

b) Have one class to store information about DVDs and the collection named DVDsInformation

c) Have one class to store information about the collection name and send information about the DVDs as simple data (integers, Strings, etc.)

d) Have one class to store information about a single DVD and just use an ArrayList to store the collection.

Section Reference: 8.4 Static Variables and Methods

Title: A new class is proposed to collect information about a group of DVDs. Which of the following is the best structure for this class?

Difficulty: Medium

80. A new class is proposed to collect information about a group of DVDs. A separate class containing information about a single DVD, named DVD, has already been created. Which of the following is the best design to store the data and size of this collection?

a) Have an ArrayList of DVDs and use its size as the size of the collection.

b) Have an ArrayList of DVDs and an integer representing the number of DVDs in the collection.

c) Have an array of DVDs and use its size as the size of the collection.

d) Have a single DVD variable representing the last DVD added and an integer representing the number of DVDs in the collection.

Section Reference: 8.4 Static Variables and Methods

Title: Which of the following is the best design to store the data and size of this collection?

Difficulty: Medium

81. A theater needs a TicketCounter to keep track of the number of tickets sold. There are two types of ticket: regular and discount. What instance data should be used for this class?

a) String[] ticketsSold;
// Each entry is either "regular" or "discount"

b) int regularTicketsSold; int discountTicketsSold;

c) ArrayList<String> ticketsSold;
// Each entry is either "regular" or "discount"

d) double ticketsSold;
// Add 1 for regular and 0.5 for discount tickets

Section Reference: 8.4 Static Variables and Methods

Title: What instance data should be used for this class?

Difficulty: Medium

82. A theater needs a TicketCounter to keep track of the number of tickets sold. There are two types of ticket: regular and discount. Which technique would be the best approach to printing the number of tickets sold?

a) Create a toString() method in the class that creates a representation with the number of discount and regular priced tickets sold.

b) Create a print() method that will print out the number of discount and regular priced tickets sold.

c) Make the instance variables public so programs can access the values and print them.

d) Create a getValues(int regular, int discount) method that will return the number of discount and regular priced tickets sold.

Section Reference: 8.4 Static Variables and Methods

Title: Which technique would be the best approach to printing the number of tickets sold?

Difficulty: Medium

83. Which of the following is not a reason to place classes into a package?

a) to avoid name clashes (most important)

b) to organize related classes

c) to show authorship of source code for classes

d) to make it easier to include a set of frequently used but unrelated classes

Section Reference: 8.5 Packages

Title: Which is not a reason to place classes into a package?

Difficulty: Easy

84. Which of the following is not accomplished by using the import directive?

a) It allows you refer to a class of a package without having to qualify it.

b) It allows you import all classes from a package.

c) It allows you organize packages into groups.

d) It helps to document all of the class resources used in a program in one place.

Section Reference: 8.5 Packages

Title: Which is not accomplished by using the import directive?

Difficulty: Easy

85. If you do not include a package statement at the top of your class source file, its classes will be placed in which package?

a) java.lang

b) java.util

c) the default package, which has no name

d) java.awt

Section Reference: 8.5 Packages

Title: By default, a class is placed in which package?

Difficulty: Easy

86. Consider the Checker class below.

public class Checker

{

public static int count = 0;

public int number = 0;

public Checker()

{

count++;

number = count;

}

public int getCount() { return count; }

public int getNumber() { return number; }

}

What is output from the code fragment below?

Checker one = new Checker();

Checker two = new Checker();

System.out.println(one.getNumber() + " " + two.getNumber());

a) 1 2

b) 1 1

c) 2 1

d) 2 2

Section Reference: 8.5 Packages

Title: What is output from the code fragment below?

Difficulty: Medium

87. Consider the Checker class below.

public class Checker

{

public static int count = 0;

public int number = 0;

public Checker()

{

count++;

number = count;

}

public int getCount() { return count; }

public int getNumber() { return number; }

}

What is output from the code fragment below?

Checker one = new Checker();

Checker two = new Checker();

System.out.println(one.getCount() + " " + two.getCount());

a) 2 2

b) 1 1

c) 1 2

d) 2 1

Section Reference: 8.5 Packages

Title: What is output from the code fragment below?

Difficulty: Medium

88. Consider the partial class below:

public class Thing

{

private int number;

private char letter;

{

number = -10;

letter = 'Z';

}

...

}

Which code is equivalent to the code above?

a) public class Thing

{

private int number = -10;

private char letter = 'Z';

...

}

b) public class Thing

{

private int number;

private char letter;

public Thing()

{

number = -10;

letter = 'Z';

}

...

}

c) public class Thing

{

public Thing()

{

int number;

char letter;

number = -10;

letter = 'Z';

}

...

}

d) public class Thing

{

public static void main(String[] args)

{

int number;

char letter;

number = -10;

letter = 'Z';

}

...

}

Section Reference: 8.5 Packages

Title: What is equivalent to code fragment?

Difficulty: Medium

89. Which class does not require an import statement at the beginning of the program that uses it?

a) java.lang.Double

b) java.awt.Color

c) java.math.BigDecimal

d) java.util.GregorianCalendar

Section Reference: 8.5 Packages

Title: Which class does not require an import statement at the beginning of the program that uses it?

Difficulty: Medium

90. Which statement will import the static methods of the Math class into a program so they do not need the Math. prefix before them in the code?

a) import static java.lang.Math.*;

b) import * from java.lang.Math;

c) static import * from java.lang.Math;

d) import java.lang.Math.*;

Section Reference: 8.5 Packages

Title: Which statement will import the static methods of the Math class into a program so they do not need the Math. prefix before them in the code?

Difficulty: Medium

91. If the Math class has been imported into a program using the line
"import static java.lang.Math.*;", how can the statement below be rewritten?

result = Math.tan(Math.toRadians(degrees));

a) result = tan(toRadians(degrees));

b) result = Math.tan(toRadians(degrees));

c) result = tan.toRadians(degrees);

d) result = tan(Math.toRadians(degrees));

Section Reference: 8.5 Packages

Title: If the Math class has been imported into a program using the line
"import static java.lang.Math.*;", how can the statement below be rewritten?

Difficulty: Medium

92. How would you create the class PackageSample in a package named bigjava.Chapter8?

a) package bigjava.chapter8;
public class PackageSample {}

b) public package bigjava.chapter8.PackageSample {}

c) public class bigjava.chapter8.PackageSample {}

d) public class PackageSample : package bigjava.chapter8 {}

Section Reference: 8.5 Packages

Title: How would you create the class PackageSample in a package named chapter8 which is in a package named bigjava?

Difficulty: Easy

93. Consider the partial JUnit test below, designed to test a class that keeps a count:

@Test public void twoIncrements()

{

Counter ctr = new Counter(10);

ctr.increment();

ctr.increment();

// Put test line here

}

Which line will test that the resulting value is 12?

a) Assert.assertEquals(12, ctr.getValue());

b) Assert.assertEquals(ctr.getValue());

c) System.out.println(12 == ctr.getValue());

d) if (12 == ctr.getValue()) { System.out.println("Passed"); }

else { System.out.println("Failed");}

Section Reference: 8.5 Packages

Title: Which line will test that the resulting value is 12?

Difficulty: Medium

94. Which of the following is one of the most popular testing frameworks?

a) JUnit

b) JUnitTest

c) JTester

d) JQATester

Section Reference: 8.6 Unit Test Frameworks

Title: Which is a popular testing framework?

Difficulty: Easy

95. Which annotation is used to mark test methods when using JUnit?

a) @TestMethod

b) @TestCode

c) @Test

d) @JUnitTest

Section Reference: 8.6 Unit Test Frameworks

Title: Which annotation is used to mark test methods when using JUnit?

Difficulty: Easy

Document Information

Document Type:
DOCX
Chapter Number:
8
Created Date:
Aug 21, 2025
Chapter Name:
Chapter 8 Designing Classes
Author:
Cay S. Horstmann

Connected Book

Big Java Early Objects 5e Complete Test Bank

By Cay S. Horstmann

Test Bank General
View Product →

$24.99

100% satisfaction guarantee

Buy Full Test Bank

Benefits

Immediately available after payment
Answers are available after payment
ZIP file includes all related files
Files are in Word format (DOCX)
Check the description to see the contents of each ZIP file
We do not share your information with any third party