Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Test Bank for Java Software Solutions 7th Edition (International Edition).

John Lewis / Will

Test Bank for Java Software Solutions 7th Edition


(International Edition). John Lewis / William Loftus

To download the complete and accurate content document, go to:


https://testbankbell.com/download/test-bank-for-java-software-solutions-7th-edition-int
ernational-edition-john-lewis-william-loftus/

Visit TestBankBell.com to get complete for all chapters


Java Software Solutions, 7e (Lewis/Loftus)
Chapter 7 Object-Oriented Design

7.1 Multiple-Choice Questions

1) During program development, software requirements specify


A) how the program will accomplish the task
B) what the task is that the program must perform
C) how to divide the task into subtasks
D) how to test the program when it is done
E) all of the above
Answer: B
Explanation: B) The specification phase is to understand the problem at hand so that the
programmer can determine what needs to be done to solve the problem. The other efforts listed
above are part of the design phase (A, C) and testing phase (D).

2) Once we have implemented the solution, we are not done with the problem because
A) the solution may not be the best (most efficient)
B) the solution may have errors and need testing and fixing before we are done
C) the solution may, at a later date, need revising to handle new specifications
D) the solution may, at a later date, need revising because of new programming language
features
E) all of the above
Answer: E
Explanation: E) A program should not be considered as a finished product until we are
reasonably assured that it is efficient and error-free. Further, it is common that programs require
modification in the future because of a change to specifications or a change to the language or
computer running the program.

3) Of the various phases in software development, which of the following is usually the
lengthiest?
A) specification
B) design
C) implementation
D) testing
E) maintenance
Answer: E
Explanation: E) The maintenance phase exists for as long as the software is in use. Software
requires modification (such as new requirements such as new features or I/O specifications) and
so the maintenance phase is on-going whereas the other phases end once the software has been
released and is in use.

1
Copyright © 2012 Pearson Education, Inc.
4) A bad programming habit is to build an initial program and then spend a great deal of time
modifying the code until it is acceptable. This is known as
A) the prototyping approach
B) the waterfall model
C) iterative development
D) the recursive approach
E) the build-and-fix approach
Answer: E
Explanation: E) Programmers who do not think things through will often build a program that
does not fit the original requirements. They then spend an inordinate amount of time trying to
repair the program to more properly fit. This is known as "build-and-fix" and is a poor
programming practice.

5) The activities of the development cycle are generally thought to


A) be strictly linear
B) be rigidly ordered
C) overlap
D) have optional steps
E) both A and B are true
Answer: C
Explanation: C) A flaw of the waterfall model is that it does not permit repetition or
backtracking through the development cycle. However, phases often need revisiting resulting in
a development cycle where the development activities overlap.

6) The idea of having programmers and developers meet in order to critique a software design or
implementation is known as
A) an interview
B) a walkthrough
C) prototyping
D) aggregation
E) evolutionary development
Answer: B
Explanation: B) Such meetings are very useful so that the various members can analyze the
solution to that point in time and offer suggestions to enhance the design and implementation.
This is called a walkthrough because it involves going step-by-step through the proposed or
implemented design.

2
Copyright © 2012 Pearson Education, Inc.
7) Modifying a program in order to eliminate deficiencies is done in the ________ phase of the
development cycle.
A) design
B) implementation
C) testing
D) use
E) maintenance
Answer: E
Explanation: E) While testing is used to find errors, deficiencies are more commonly identified
by the users of the system once the system has been released. After such deficiencies have been
identified, it is up to the software maintenance group to eliminate them in future versions of the
software.

8) It is easier to correct errors found in a program if


A) they are identified early in the development cycle
B) they are identified during testing
C) they are identified during program use
D) they are identified during maintenance
E) all of the above are equally true, errors are easily corrected in any of these stages
Answer: A
Explanation: A) Errors introduced early in the development cycle often cause tremendous
problems if not caught early on. Therefore, errors found in testing, use or maintenance phases
may be too embedded in the systems' design to be easily corrected. But if found early on, the
errors can be completely eliminated.

9) In general, spending more time in development to ensure better software will


A) shorten testing time
B) slightly reduce maintenance efforts
C) slightly increase maintenance efforts
D) greatly reduce maintenance efforts
E) not alter the time it takes for any other stage whatsoever
Answer: D
Explanation: D) Spending more time in development promises better software, which in turn
leads to less maintenance effort.

3
Copyright © 2012 Pearson Education, Inc.
10) The idea that an object can exist separate from the executing program that creates it is called
A) transience
B) static
C) persistence
D) serialization
E) finality
Answer: C
Explanation: C) Objects are stored in memory and are reclaimed by the garbage collector when
they are no longer referenced. When a Java program terminates, no object is referenced and so
all objects are reclaimed. It is desirable, however, to be able to save any given object for future
use. This trait is called persistence, and the ability to do this is by saving the instance data of the
object to a file. This can be done by writing each instance data to a data file, but is simplified
using Object Serialization.

11) In order to create a constant, you would use which of the following Java reserved words?
A) private
B) static
C) int
D) final
E) class
Answer: D
Explanation: D) The reserved word final indicates that this is the final value that will be stored
in this variable, thus making it unchangeable, or constant. While constants can be of type int,
constants can be of any other type as well. It is the final reserved word that makes the value
unchangeable.

12) Which of the following methods is a static method? The class in which the method is
defined is given in parentheses following the method name.
A) equals (String)
B) toUpperCase (String)
C) sqrt (Math)
D) format (DecimalFormat)
E) paint (Applet)
Answer: C
Explanation: C) The Math class defines all of its methods to be static. Invoking Math methods
is done by using Math rather than a variable of type Math. The other methods above are not
static.

4
Copyright © 2012 Pearson Education, Inc.
13) Static methods cannot
A) reference instance data
B) reference non-static instance data
C) reference other objects
D) invoke other static methods
E) invoke non-static methods
Answer: B
Explanation: B) A static method is a method that is part of the class itself, not an instantiated
object, and therefore the static method is shared among all instantiated objects of the class. Since
the static method is shared, it cannot access non-static instance data because all non-static
instance data are specific to instantiated objects. A static method can access static instance data
because, like the method, the instance data is shared among all objects of the class. A static
method can also access parameters passed to it.

14) An object that refers to part of itself within its own methods can use which of the following
reserved words to denote this relationship?
A) inner
B) i
C) private
D) this
E) static
Answer: D
Explanation: D) The reserved word this is used so that an object can refer to itself. For instance,
if an object has an instance data x, then this.x refers to the object's value x. While this is not
necessary, it can be useful if a local variable or parameter is named the same as an instance data.
The reserved word this is also used to refer to the class as a whole, for instance, if the class is
going to implement an interface class rather than import an implementation of an interface class.

15) An example of an aggregation relationship is


A) parent and child
B) animal and dog
C) teacher and computer
D) phone and fax machine
E) all of the above
Answer: C
Explanation: C) An aggregate relationship describes how one object accomplishes a task by
using another object. In the above list, the teacher uses the computer. The relationship in A is
one of lineage (possibly ownership), in B is an is-a relationship as a dog is a child class of
animal, and in D is one of similarity, phone and fax machine might be thought of as sibling
classes, related to each other in that they are both children of a more general communication
class.

5
Copyright © 2012 Pearson Education, Inc.
16) Inheritance through an extended (derived) class supports which of the following concepts?
A) interfaces
B) modulary
C) information hiding
D) code reuse
E) correctness
Answer: D
Explanation: D) By extending a class and inheriting from it, the new class does not have to
reimplement any of those inherited methods or instance data, thus saving the programmer an
effort. So, code reuse is the ability to reuse someone else's code for your benefit by extending it
for your need.

17) Java does not support multiple inheritance, but some of the abilities of multiple inheritance
are available by
A) importing classes
B) implementing interfaces
C) overriding parent class methods
D) creating aliases
E) using public rather than protected or private modifiers
Answer: B
Explanation: B) Since a class can implement any number of interfaces, that class is in essence
using the interface classes as if those interfaces were defined in this class. So, this class is
inheriting the methods and constants of the interfaces. Further, the class could extend another
class and thus inherit directly and indirectly from multiple classes. This is not the exact same as
multiple inheritance, but it is as close as Java comes to that concept.

18) Abstract methods are used when defining


A) interface classes
B) derived classes
C) classes that have no constructor
D) arrays
E) classes that have no methods
Answer: A
Explanation: A) An interface is a class that has defined some of its components, but leaves other
components (methods) for you to implement. So, these components (methods) are referred to as
abstract and defined in the interface class as abstract.

6
Copyright © 2012 Pearson Education, Inc.
19) Which of the following is not a method of the Object class?
A) clone
B) compareTo
C) equals
D) toString
E) all of the above are methods of the Object class
Answer: B
Explanation: B) The Object class defines clone to create a copy of any object, equals to
determine if two objects are the same object, and toString to translate an Object into a String.
However, compareTo is not implement by Object and must be explicitly implemented in any
class that wants to implement the Comparable interface.

20) Which of the following interfaces would be used to implement a class that represents a group
(or collection) of objects?
A) Iterator
B) Speaker
C) Comparable
D) MouseListener
E) KeyListener
Answer: A
Explanation: A) Iterator is an abstract class allowing the user to extend a given class that
implements Iterator by using the features defined there. These features include being able to
store a group of objects and iterate (step) through them.

21) In order to implement Comparable in a class, what method(s) must be defined in that class?
A) equals
B) compares
C) both lessThan and greaterThan
D) compareTo
E) both compares and equals
Answer: D
Explanation: D) The Comparable class requires the definition of a compareTo method that will
compare two objects and determine if one is equal to the other, or if one is less than or greater
than the other and respond with a negative int, 0 or a positive int. Since compareTo responds
with 0 if the two objects are equal, there is no need to also define an equals method.

For the questions below, consider a class called ChessPiece. This class has two instance data,
String type and int player. The variable type will store "King", "Queen", "Bishop", etc and the
int player will store 0 or 1 depending on whose piece it is. We wish to implement Comparable
for the ChessPiece class. Assume that, the current ChessPiece is compared to a ChessPiece
passed as a parameter. Pieces are ordered as follows: "Pawn" is a lesser piece to a "Knight"
and a "Bishop", "Knight" and "Bishop" are equivalent for this example, both are lesser pieces to
a "Rook" which is a lesser piece to a "Queen" which is a lesser piece to a "King."

7
Copyright © 2012 Pearson Education, Inc.
22) Which of the following method headers would properly define the method needed to make
this class Comparable?
A) public boolean comparable(Object cp)
B) public int comparable(Object cp)
C) public int compareTo(Object cp)
D) public int compareTo( )
E) public boolean compareTo(Object cp)
Answer: C
Explanation: C) To implement Comparable, you must implement a method called compareTo
which returns an int. Further, since this class will compare this ChessPiece to another, we would
except the other ChessPiece to be passed in as a parameter (although compareTo is defined to
accept an Object, not a ChessPiece).

23) Which of the following pieces of logic could be used in the method that implements
Comparable? Assume that the method is passed Object a, which is really a ChessPiece. Also
assume that ChessPiece has a method called returnType which returns the type of the given
piece. Only one of these answers has correct logic.
A) if (this.type < a.returnType( )) return -1;
B) if (this.type = = a.returnType( )) return 0;
C) if (this.type.equals(a.returnType( )) return 0;
D) if (a.returnType( ).equals("King")) return -1;
E) if (a.returnType( ).equals("Pawn")) return 1;
Answer: C
Explanation: C) If the type of this piece and of a are the same type, then they are considered
equal and the method should return 0 to indicate this. Note that this does not cover the case
where this piece is a "Knight" and a is a "Bishop", so additional code would be required for the
"equal to" case. The answer in B is not correct because it compares two Strings to see if they are
the same String, not the same value. The logic in D and E are incorrect because neither of these
takes into account what the current piece is, only what the parameter's type is. In D, if a is a
"King", it will be greater than this piece if this piece is not a "King", but will be equal if this
piece is a "King" and similarly in E, it does not consider if this piece is a "Pawn" or not. Finally,
A would give a syntax error because two Strings cannot be compared using the "<" operator.

24) Which of the following would not be considered an algorithm?


A) a recipe
B) a computer program
C) pseudocode
D) a shopping list
E) travel directions
Answer: D
Explanation: D) An algorithm is a step-by-step description of how to solve a problem, written at
some level of specificity. The recipe and probably the pseudocode are written at a "high level"
whereas a program and perhaps travel directions are written in more detail. A shopping list is
not a step-by-step description, so it would not qualify as an algorithm.

8
Copyright © 2012 Pearson Education, Inc.
25) It is important to dissect a problem into manageable pieces before trying to solve the problem
because
A) most problems are too complex to be solved as a single, large activity
B) most problems are solved by multiple people and it is easy to assign each piece to a separate
person
C) it is easier to integrate small pieces of a program into one program than it is to integrate one
big chunk of code into one program
D) our first solution may not solve the problem correctly
E) all of the above
Answer: A
Explanation: A) Any interesting problem will be too complex to solve easily as a single activity.
By decomposing the problem, we can build small solutions to each piece and then integrate the
pieces. Answer D is true, but does is not the reason why we will break a problem down into
pieces.

26) Having multiple class methods of the same name where each method has a different number
of or type of parameters is known as
A) encapsulation
B) information hiding
C) tokenizing
D) importing
E) method overloading
Answer: E
Explanation: E) When methods share the same name, they are said to be overloaded. The
number and type of parameters passed in the message provides the information by which the
proper method is called.

27) The goal of testing is to


A) ensure that the software has no errors
B) find syntax errors
C) find logical and run-time errors
D) evaluate how well the software meets the original requirements
E) give out-of-work programmers something to do
Answer: C
Explanation: C) Testing is required because all software will have errors. Complex systems
especially need testing before they can be released. The types of errors sought are logical errors
and run-time errors. All syntax errors will have been identified and fixed during implementation.

9
Copyright © 2012 Pearson Education, Inc.
28) Arranging components in a GUI container is accomplished by using which of the following?
A) Layout manager
B) Listener interface
C) String array
D) Event generator
E) JComboBox
Answer: A
Explanation: A) There are several classes of Layout managers. A Layout manager is used to
add GUI components to the container in some manner. The type of Layout manager determines
how items are added.

29) Which Layout Manager type would you use if you want GUI components to be placed at the
North, South, East, West and Center of the container?
A) FlowLayout
B) BorderLayout
C) BoxLayout
D) GridLayout
E) TabbedPane
Answer: B
Explanation: B) The BorderLayout specifically allows you to specify the component's location
as one of NORTH, SOUTH, EAST, WEST or CENTER where the value (e.g., NORTH) is a
constant predefined in the class.

30) Which Layout Manager type would you use if you want GUI components to be placed in 2
columns over 5 rows?
A) FlowLayout
B) BorderLayout
C) BoxLayout
D) GridLayout
E) TabbedPane
Answer: D
Explanation: D) The GridLayout manager allows you to specify the number of rows and
columns and then add elements across the column on one row until it is filled, and then on to the
next row, until all of the rows are filled.

31) Which Layout Manager is used by default if you do not specify a Layout Manager for your
GUI container?
A) FlowLayout
B) BorderLayout
C) BoxLayout
D) GridLayout
E) TabbedPane
Answer: A
Explanation: A) The FlowLayout is the default manager. It places GUI items across in one row
only. Other Layout Managers are more sophisticated and lead to better designed containers.

32) Which of the following GUI classes requires that it have a LayoutManager before any GUI
10
Copyright © 2012 Pearson Education, Inc.
components are added to it?
A) JButton
B) JSlider
C) JPanel
D) JFrame
E) both C and D but not A or B
Answer: C
Explanation: C) The JPanel requires adding a LayoutManager to it before any components are
added to it. This allows the components added to be arranged on the JPanel as desired. Neither
the JButton nor JSlider have components added to them and so do not require a LayoutManager.
The JFrame does not use a LayoutManager but instead adds elements through getContentPane( ).

33) In using the BoxLayout, adding space between components in a container can be
accomplished by
A) including TabbedPanes
B) including JButtons that have no names and thus will appear invisible
C) adding invisible components
D) inserting IconImages of blank images
E) any of the above
Answer: C
Explanation: C) The BoxLayout manager allows for the inclusion of specially designated
"invisible" components to take up space between real components and thus provide spacing.

34) In order to display three components vertically in a container, you could use all but which of
the following layout managers?
A) FlowLayout
B) BoxLayout
C) GridLayout
D) BorderLayout
E) you could use any of the above
Answer: A
Explanation: A) FlowLayout puts components horizontally and you cannot change that.
BoxLayout can place items horizontally or vertically. GridLayout allows you to specify the
number of rows and columns and to place the three components vertically, you would use (3, 1)
that is, 3 rows, 1 column. BorderLayout can place three components vertically by placing them
in the NORTH, CENTER and SOUTH positions.

11
Copyright © 2012 Pearson Education, Inc.
35) What is wrong with the following message to create a BoxLayout for the JPanel jp?
jp.setLayout(new BoxLayout(BoxLayout.X_AXIS));
A) X_AXIS is not a valid constant for BoxLayout, it should instead be HORIZONTAL
B) Aside from X_AXIS, the constructor also requires a Dimension to determine the size of the
Box
C) Aside from X_AXIS, the constructor also needs a command regarding horizontal glue as in
createHorizontalGlue( )
D) The BoxLayout constructor is lacking a reference to the container that the BoxLayout
manager will govern
E) There is nothing wrong with it at all
Answer: D
Explanation: D) The BoxLayout manager constructor requires two parameters, a reference to the
container that the manager will govern, and an axis for the layout, (vertical or horizontal) as
specified by either X_AXIS or Y_AXIS.

36) In which phase of program development would you expect the programmer(s) to create the
pseudocode?
A) Software requirements
B) Software design
C) Software implementation
D) Software testing
E) Could occur in any of the above
Answer: B
Explanation: B) Pseudocode is a description of an algorithm written in an English-like way
rather than in a specific programming language. This is part of the program's design. In the
implementation phase, the programmer(s) translates the pseudocode into the programming
language being used.

37) Which of the following is considered a top-level container?


A) Panel
B) Frame
C) Box
D) Layout Manager
E) A, B, and C
Answer: B
Explanation: B) While the answers in A, B and C are all types of containers, the Box and Panel
are made available to contain other components and are themselves placed into other containers.
The Frame is a top-level container because a Frame will not store other Frames, but instead, the
Frame contains Panels which can contain Boxes, Panels and other components.

12
Copyright © 2012 Pearson Education, Inc.
38) The JFrame below has a JBorder created using which of the following BorderFactory
formats?

A) TitledBorder
B) MatteBorder
C) EtchedBorder
D) RaisedBevelBorder
E) LoweredBevelBorder
Answer: D
Explanation: D) The JFrame has a Bevel border which is raised. A TitledBorder has a title
running along the top of it. The MatteBorder is of a Color or contains an image of some kind.
The EtchedBorder is not raised, and the LoweredBevelBorder is lowered, not raised.

39) To take 2 borders and use one as an outer border and the other as an inner border, you would
create a
A) compound border
B) nested border
C) split border
D) border factory
E) matte border
Answer: A
Explanation: A) The compound border comprises two borders that are combined together, one
border being the outside border and the other being the inside border.

40) In which phase of program development would you expect the programmer(s) to determine
the classes and objects needed?
A) software requirements
B) software design
C) software implementation
D) software testing
E) could occur in any of the above
Answer: B
Explanation: B) Determining which classes and objects to use or create is part of the design.

13
Copyright © 2012 Pearson Education, Inc.
41) If a programmer follows the four phases of program development as intended, which of the
four phases should require the least amount of creativity?
A) software requirements
B) software design
C) software implementation
D) software testing
E) none of the above, all four levels would require equal creativity
Answer: C
Explanation: C) Once the implementation phase has been reached, the algorithm should have
already been specified, so the only effort involved in the implementation phase is of translating
from the design (which is probably in an English-like pseudocode) to the programming language,
and entering the code through an editor. The requirements and design phases require
understanding the problem and coming up with a solution respectively, requiring creativity, and
the testing phase will require diagnostic abilities usually forcing the programmer(s) to be creative
in how the errors are found and fixed.

7.2 True/False Questions

1) The most important decisions regarding the development of a system are made during the
implementation phase while code is actively being written.
Answer: FALSE
Explanation: All of the important decisions should be made during earlier phases of
development, particularly during the design phase. By the time the implementation phase is
reached, the entire system design should be available and this phase should only entail
translating the design into code.

2) Unlike the String class where you must pass a message to an object (instance) of the class, as
in x.length( ), in order to use the Math class, you pass messages directly to the class name, as in
Math.abs( ) or Math.sqrt( ).
Answer: TRUE
Explanation: The Math class uses methods known as static methods (or class methods) which
are invoked by passing a message directly to the class name itself rather than to an object of the
class.

3) Assume that the class Bird has a static method fly( ). If b is a Bird, then to invoke fly, you
could do Bird.fly( );.
Answer: TRUE
Explanation: Static methods are invoked through the class and not an object of the class. This is
because the static method is shared among all instances. So, Bird.fly( ); will invoke fly. It
should be noted that fly can also be invoked through b, so b.fly( ); will also work.

4) Interface classes cannot be extended but classes that implement interfaces can be extended.
Answer: FALSE
Explanation: Any class can be extended whether it is an interface, implements an interface, or
neither. The only exception to this is if the class is explicitly modified with the word "final" in
which case it cannot be extended.

14
Copyright © 2012 Pearson Education, Inc.
5) If classes C1 and C2 both implement an interface Cint, which has a method whichIsIt, and if
C1 c = new C1( ); is performed at one point of the program, then a later instruction c.whichIsIt(
); will invoke the whichIsIt method defined in C1.
Answer: FALSE
Explanation: Because C1 and C2 implement the same interface, they both implement whichIsIt.
The variable c is known as a polymorphic variable, meaning that it can change from being an C1
to a C2. So, the message c.whichIsIt( ); may invoke C1's whichIsIt or C2's whichIsIt. There is
no way to tell until run-time.

6) Any class can implement an interface, but no classes can implement more than a single
interface.
Answer: FALSE
Explanation: Classes can implement any number of interfaces, 0, 1, or more.

7) All objects implement Comparable.


Answer: FALSE
Explanation: Comparable is an interface, and the class must define the compareTo method and
explicitly state that it implements Comparable to be considered an implementation of
Comparable. Most classes do not implement Comparable.

8) Formal parameters are those that appear in the method call and actual parameters are those
that appear in the method header.
Answer: FALSE
Explanation: The question has the two definitions reversed. Formal parameters are those that
appear in the method header, actual parameters are the parameters in the method call (those
being passed to the method).

9) Defining formal parameters requires including each parameters type.


Answer: TRUE
Explanation: In order for the compiler to check to see if a method call is correct, the compiler
needs to know the types for the parameters being passed. Therefore, all formal parameters (those
defined in the method header) must include their type. This is one element that makes Java a
Strongly Typed language.

10) It is not possible to test out any single method or class of a system until the entire system has
been developed, and so all testing is postponed until after the implementation phase.
Answer: FALSE
Explanation: While it is true that testing occurs after implementation, some testing can take
place during implementation. In Java, it is possible to test out single methods or classes by
creating stubs of other methods and classes that are simple placeholders. For instance, if one
method calls another method and the second method is not yet written, a stub of the second
method can be written that simply returns a value that would normally be expected so that the
first method can be tested.

15
Copyright © 2012 Pearson Education, Inc.
11) All parts of a container in BorderLayout must be used.
Answer: FALSE
Explanation: BorderLayout allows components to be inserted in any of North, South, East, West
and Center of the GUI, but as many or as few of these five sections can be used as needed.

12) A JPanel can itself contain JPanels.


Answer: TRUE
Explanation: It is common to place GUI components into a JPanel and then place several
JPanels into another JPanel. Each JPanel has its own layout manager allowing the programmer
to specify in detail how components should appear. For instance, one JPanel might use a
GridLayout and another a BoxLayout and then the two JPanels be placed in the NORTH and
EAST sections of a JPanel using BorderLayout.

13) If a GUI container is resized by the user, all components of the GUI are automatically
increased or decreased by the same fraction to fit in the newly sized container.
Answer: FALSE
Explanation: The layout manager will reposition the GUI items to fit in the newly sized
container.

14) The size and shape of a GUI container is based on the layout manager selected and the type
and number of items added to the container.
Answer: TRUE
Explanation: It is the layout manager's task to arrange the components in the container. Based
on the order that the items are entered and the type of layout manager, the size and shape are
determined.

15) A border can be put around any Swing component with the exception of a JButton and
JScrollPane component.
Answer: FALSE
Explanation: A border can be up around any Swing component no matter the type.

16) The term "test case" is used to express a set of inputs (or user actions) and the expected
outputs to be used in testing out software.
Answer: TRUE
Explanation: In order to test software to see if it is working correctly, a tester will often start
with a series of inputs and the expected outputs. If, when running the program on the inputs, the
outputs are not the expected results, then errors are assumed to exist. These input/output pairs
are known as test cases.

16
Copyright © 2012 Pearson Education, Inc.
17) In black-box testing, the tester should already know something about how the program is
implemented so that he/she can more carefully identify what portion(s) of the software are
leading to errors.
Answer: FALSE
Explanation: In black-box testing, the tester should know nothing about how the software is
implemented. In essence, the software is a black-box which has an input and an output and the
mechanisms of the program are opaque. If the tester does know something about how the
program works, then the tester's test cases might be biased. If the tester knows how the program
works, then testing is known as glass-box testing.

7.3 Free-Form Questions

1) Provide a reason why an instance data would be declared static.


Answer: If an instance data is to be shared among all objects of the class, the instance data
would be static. As an example, an instance data that counts the number of times something has
happened across all objects of the class would be made static. If we are writing a chess game
and have a ChessPiece class that includes a method called movePiece, we would want to know
how many moves a player has made, but not necessarily how many times a single piece has been
moved. So, all of the ChessPieces share a numberOfTimesMoved instance data that is
incremented by any ChessPiece whenever it is moved.

2) Provide a reason why a method would be declared static.


Answer: A method is declared static if it might be used, not by the current object, but by any
objects of the class. That is, the method does not require access to any single object's instance
data. Consider a class called Point that consists of an x and y coordinate. To determine the
distance between two Points, we could make a static computeDistance method. It is passed two
Points as parameters and computes the distance. Another example is the Keyboard class from
the textbook, which does not itself have any instance data.

3) Write a static method that is passed two ChessPieces and determines if the two pieces are
owned by the same player. It should return true or false.
Answer: public static boolean samePlayer(ChessPiece p1, ChessPiece p2)
{
return (p1.returnPlayer( ) = = p2.returnPlayer( ));
}

4) Explain the difference between implementing an interface and a derived class.


Answer: Implementing an interface means that you are adding to your class any mechanisms
defined in an abstract class. Your class essentially is expanded to contain those items defined in
the interface class but you have to implement any of those items that are abstract. The appeal is
that other objects might call upon your class to use the interface methods without knowing
anything else about your class. The derived class is similar in that you are gaining items
previously defined, although in this case, those items are defined in the parent class (as long as
those items are protected or public) and you do not have to implement any of them unless you
want to redefine them. One difference between the two is that the interface cannot be
instantiated directly. Another difference is that your class "inherits" abstract methods from the
interface class as opposed to implemented methods from a super class.
17
Copyright © 2012 Pearson Education, Inc.
5) Define an interface class that contains two int constants, X = 5 and Y = 10 and one int method
called useXY which receives no parameters. Call your interface class XYClass
Answer: public interface XYClass
{
public final int X = 5;
public final int Y = 10;

public int useXY( );


}

6) Assume a class Foo implements Comparable. Without knowing anything else about the Foo
class, write an equal method that returns true if the Foo parameter passed to this Foo is equal to
this Foo as determined by using the implementation of Comparable.
Answer: public boolean equals(Foo a)
{
if(compareTo(a)) = = 0)
return true;
else
return false;
}

18
Copyright © 2012 Pearson Education, Inc.
7) To implement Comparable for the ChessPiece class, the current ChessPiece's type is
compared to a ChessPiece passed as a parameter. The method should return 0 if the types are
equal, -1 if this piece's type is a lesser piece than the parameter's type, and 1 if this piece's type is
a greater piece than the parameter's type. Pieces are ordered as follows: "Pawn" is a lesser piece
to a "Knight" and a "Bishop", "Knight" and "Bishop" are equivalent for this example, both are
lesser pieces to a "Rook" which is a lesser piece to a "Queen" which is a lesser piece to a "King."
Write the proper method to implement Comparable for this class.
Answer: public int compareTo(ChessPiece a)
{
if (this.type.equals(a.returnType( )) | |
(this.type.equals("Knight") && a.returnType( ).equals("Bishop")) | |
(this.type.equals("Bishop") && a.returnType( ).equals("Knight")))
return 0;
else if (this.type.equals("King"))
return 1;
else if (a.returnType( ).equals("King"))
return-1;
else if (this.type.equals("Queen"))
return 1;
else if (a.returnType( ).equals("Queen"))
return -1;
else if (this.type.equals("Rook"))
return 1;
else if (a.returnType( ).equals("Rook"))
return -1;
else if (this.type.equals("Knight") | | (this.type.equals("Bishop"))
return 1;
else if (a.returnType( ).equals("Knight") | | (a.returnType( ).equals("Bishop"))
return -1;
}

8) Why was the Y2K problem a problem of software maintenance and not a problem of software
testing?
Answer: The Y2K problem was caused by older software that stored the year as a 2-digit value
instead of a 4-digit value (older software attempted to save memory space in any way possible,
this was a common way to save 2 bytes). Since the 2-digit year was not a logical or run-time
error, but instead, a flaw in the design, this problem was not detected during software testing.
Instead, it was not until the late 1990s, when people began to realize that a 2-digit year would
cause a problem starting in 2000, that the Y2K problem was identified and solutions sought, so
the problem was one of software maintenance how to maintain this older software so that it still
functions starting in the year 2000.

19
Copyright © 2012 Pearson Education, Inc.
9) In what way is black-box testing better than glass-box testing? In what way is glass-box
testing better than black-box testing?
Answer: Black-box testing is superior because the tester knows nothing of the software and so
will test the software without preconceived ideas of where errors might exist. The tester is
testing the software blindly, much as a user will use the software–without intimate knowledge of
how the software works. The result should be that if errors exist that a user might stumble
across, the tester should find them. While black-box testing of software has the advantage that
the tester operates much like the users are expected to, the disadvantage is that it is a blind test
and knowledge of how the software functions could be useful. If programmers suspect that some
awkward or difficult implementation may be buggy, then spending more time testing that portion
of the software is advantageous. This form of testing requires knowledge of how the software
works. So, both black-box and glass-box testing have their own advantages and disadvantages.

10) Write a set of code that will allow a user to select an input image and then place this image in
a JLabel lab1 and add lab1 to a JPanel as the northern component in a JFrame using
BorderLayout. The JFrame has already been instantiated as jf and the BorderLayout already
established. Hint: a JFileChooser returns a File and JLabel requires the String name of a file as
an argument. A File can return its name using File class' method getAbsolutePath( )
Answer: JFileChooser jfc = new JFileChooser( );
jfc.showOpenDialog(null);
File f = jfc.getSelectedFile( );
ImageIcon im = new ImageIcon(f. getAbsoluteFile( ));
JLabel lab1 = new JLabel(im);
jp.add(lab1, BorderLayout.NORTH);
jf.getContentPane( ).add(jp);
jf.pack( );
jf.setVisible(true);

20
Copyright © 2012 Pearson Education, Inc.
11) Write code to create a JPanel with 4 JCheckBox GUI components and two JLabels for
output, one that says "Current cost is" and the other that outputs the computed cost based on
which of the food items has been selected.
Answer: FoodListener fl = new FoodListener( );
JPanel jp = new JPanel(new GridLayout(3, 2));
jcb1 = new JCheckBox("Burger");
jcb2 = new JCheckBox("Fries");
jcb3 = new JCheckBox("Soda");
jcb4 = new JCheckBox("Cookie");
jcb1.addItemListener(fl);
jcb2.addItemListener(fl);
jcb3.addItemListener(fl);
jcb4.addItemListener(fl);
jl1 = new JLabel("Current cost is");
jl2 = new JLabel(" ");
jp.add(jcb1);
jp.add(jcb2);
jp.add(jcb3);
jp.add(jcb4);
jp.add(jl1)
jpadd(jl2);
add(jp);

21
Copyright © 2012 Pearson Education, Inc.
12) Provide code to generate the following JFrame. Each lettered item is a JButton. Hint:
Create several JPanels, each one having two or more JButtons and taken up a portion of the
JFrame.

Answer: JFrame jf = new JFrame( );


JPanel jp1 = new JPanel(new BorderLayout( ));
JPanel jp2 = new JPanel(new FlowLayout( ));
JPanel jp3 = new JPanel(new GridLayout(2, 3));
JButton lab1 = new JButton("A");
JButton lab2 = new JButton("B");
JButton lab3 = new JButton("C");
JButton lab4 = new JButton("D");
JButton lab5 = new JButton("E");
JButton lab6 = new JButton("F");
JButton lab7 = new JButton("G");
JButton lab8 = new JButton("H");
JButton lab9 = new JButton("I");
JButton lab10 = new JButton("J");
JButton lab11 = new JButton("K");
JButton lab12 = new JButton("L");
JButton lab13 = new JButton("M");
jp1.add(lab1, BorderLayout.NORTH);
jp1.add(lab2, BorderLayout.WEST);
jp1.add(lab3, BorderLayout.CENTER);
jp1.add(lab4, BorderLayout.EAST);
jp1.add(lab5, BorderLayout.SOUTH);
jp2.add(lab6);
jp2.add(lab7);
jp3.add(lab8);
jp3.add(lab9);
jp3.add(lab10);
jp3.add(lab11);

22
Copyright © 2012 Pearson Education, Inc.
jp3.add(lab12);
jp3.add(lab13);

JPanel jp4 = new JPanel(new GridLayout(3, 1));


jp4.add(jp1);
jp4.add(jp2);
jp4.add(jp3);
jf.getContentPane( ).add(jp4);
jf.pack( );
jf.setVisible(true);

23
Copyright © 2012 Pearson Education, Inc.
13) Provide code to generate the following JFrame. Each lettered item is a JButton. Hint:
Create several JPanels, each one having two or more JButtons and taken up a portion of the
JFrame.

Answer: JFrame jf = new JFrame( );


JPanel jp1 = new JPanel(new GridLayout(1, 3));
JPanel jp2 = new JPanel(new GridLayout(3, 1));
JPanel jp3 = new JPanel(new GridLayout(3, 1));
JPanel jp4 = new JPanel(new GridLayout(1, 3));
JPanel jp5 = new JPanel(new BorderLayout( ));
JButton lab1 = new JButton("A");
JButton lab2 = new JButton("B");
JButton lab3 = new JButton("C");
JButton lab4 = new JButton("D");
JButton lab5 = new JButton("E");
JButton lab6 = new JButton("F");
JButton lab7 = new JButton("G");
JButton lab8 = new JButton("H");
JButton lab9 = new JButton("I");
JButton lab10 = new JButton("J");
JButton lab11 = new JButton("K");
JButton lab12 = new JButton("L");
JButton lab13 = new JButton("M");
jp1.add(lab1);
jp1.add(lab2);
jp1.add(lab3);
jp2.add(lab4);
jp2.add(lab5);
jp2.add(lab6);
jp3.add(lab7);
jp3.add(lab8);
jp3.add(lab9);
jp4.add(lab11);
jp4.add(lab12);
jp4.add(lab13);
jp5.add(jp1, BorderLayout.NORTH);
jp5.add(jp2, BorderLayout.WEST);
jp5.add(lab10, BorderLayout.CENTER);
24
Copyright © 2012 Pearson Education, Inc.
jp5.add(jp3, BorderLayout.EAST);
jp5.add(jp4, BorderLayout.SOUTH);
jf.getContentPane( ).add(jp5);
jf.pack( );
jf.setVisible(true);

14) Provide code to generate the following JFrame. Each lettered item is a JButton. Hint:
Create several JPanels, each one having two or more JButtons and taken up a portion of the
JFrame.

Answer: JFrame jf = new JFrame( );


JPanel jp1 = new JPanel(new GridLayout(2, 2));
JPanel jp2 = new JPanel(new GridLayout(2, 2));
JPanel jp3 = new JPanel(new FlowLayout( ));
JPanel jp4 = new JPanel(new BorderLayout( ));
JPanel jp5 = new JPanel(new GridLayout(2, 2));
JButton lab1 = new JButton("A");
JButton lab2 = new JButton("B");
JButton lab3 = new JButton("C");
JButton lab4 = new JButton("D");
JButton lab5 = new JButton("E");
JButton lab6 = new JButton("F");
JButton lab7 = new JButton("G");
JButton lab8 = new JButton("H");
JButton lab9 = new JButton("I");
JButton lab10 = new JButton("J");
JButton lab11 = new JButton("K");
JButton lab12 = new JButton("L");
JButton lab13 = new JButton("M");
JButton lab14 = new JButton("N");
JButton lab15 = new JButton("O");
JButton lab16 = new JButton("P");
jp1.add(lab1);
jp1.add(lab2);
jp1.add(lab3);
25
Copyright © 2012 Pearson Education, Inc.
jp1.add(lab4);
jp2.add(lab5);
jp2.add(lab6);
jp2.add(lab7);
jp2.add(lab8);
jp3.add(lab9);
jp3.add(lab10);
jp3.add(lab11);
jp4.add(lab12, BorderLayout.WEST);
jp4.add(lab13, BorderLayout.NORTH);
jp4.add(lab14, BorderLayout.CENTER);
jp4.add(lab15, BorderLayout.EAST);
jp4.add(lab16, BorderLayout.SOUTH);
jp5.add(jp1);
jp5.add(jp2);
jp5.add(jp3);
jp5.add(jp4);
jf.getContentPane( ).add(jp5);
jf.pack( );
jf.setVisible(true);

26
Copyright © 2012 Pearson Education, Inc.
15) Draw the JFrame as you think it would appear given the following set of code:
JButton jb1 = new JButton("A");
JButton jb2 = new JButton("B");
JButton jb3 = new JButton("C");
JButton jb4 = new JButton("D");
JButton jb5 = new JButton("E");
JButton jb6 = new JButton("F");
JButton jb7 = new JButton("G");
JButton jb8 = new JButton("H");
JButton jb9 = new JButton("I");
JButton jb10 = new JButton("J");
JButton jb11 = new JButton("K");
JButton jb12 = new JButton("L");
JPanel jp1 = new JPanel( );
jp1.add(jb1);
jp1.add(jb2);
jp1.add(jb3);
jp1.add(jb4);
JPanel jp2 = new JPanel(new GridLayout(4, 1));
jp2.add(jb5);
jp2.add(jb6);
jp2.add(jb7);
jp2.add(jb8);
JPanel jp3 = new JPanel(new GridLayout(2, 2));
jp3.add(jb9);
jp3.add(jb10);
jp3.add(jb11);
jp3.add(jb12);
JPanel jp4 = new JPanel(new BorderLayout( ));
jp4.add(jp1, BorderLayout.NORTH);
jp4.add(jp2, BorderLayout.EAST);
jp4.add(jp3, BorderLayout.WEST);
JFrame jf = new JFrame( );
jf.getContentPane( ).add(jp4);
jf.pack( );
jf.setVisible(true);

27
Copyright © 2012 Pearson Education, Inc.
Answer:

16) Draw the JFrame as you think it would appear given the following set of code:
JButton jb1 = new JButton("A");
JButton jb2 = new JButton("B");
JButton jb3 = new JButton("C");
JButton jb4 = new JButton("D");
JButton jb5 = new JButton("E");
JButton jb6 = new JButton("F");
JButton jb7 = new JButton("G");
JPanel jp1 = new JPanel(new GridLayout(3, 3));
jp1.add(jb1);
jp1.add(jb2);
jp1.add(jb3);
jp1.add(jb4);
jp1.add(jb5);
jp1.add(jb6);
jp1.add(jb7);
JFrame jf = new JFrame( );
jf.getContentPane( ).add(jp1);
jf.pack( );
jf.setVisible(true);
Answer:

28
Copyright © 2012 Pearson Education, Inc.
17) Draw the JFrame as you think it would appear given the following set of code:
JButton jb1 = new JButton("A");
JButton jb2 = new JButton("B");
JButton jb3 = new JButton("C");
JButton jb4 = new JButton("D");
JButton jb5 = new JButton("E");
JButton jb6 = new JButton("F");
JButton jb7 = new JButton("G");
JButton jb8 = new JButton("H");
JButton jb9 = new JButton("I");
JPanel jp1 = new JPanel(new BorderLayout( ));
jp1.add(jb1, BorderLayout.NORTH);
jp1.add(jb2, BorderLayout.WEST);
jp1.add(jb3, BorderLayout.EAST);
JPanel jp2 = new JPanel(new BorderLayout( ));
jp2.add(jb4, BorderLayout.WEST);
jp2.add(jb5, BorderLayout.SOUTH);
jp2.add(jb6, BorderLayout.EAST);
JPanel jp3 = new JPanel(new BorderLayout( ));
jp3.add(jb7, BorderLayout.WEST);
jp3.add(jb8, BorderLayout.NORTH);
jp3.add(jb9, BorderLayout.EAST);
JPanel jp4 = new JPanel(new BorderLayout( ));
jp4.add(jp1, BorderLayout.NORTH);
jp4.add(jp2, BorderLayout.EAST);
jp4.add(jp3, BorderLayout.SOUTH);
JFrame jf = new JFrame( );
jf.getContentPane( ).add(jp4);
jf.pack( );
jf.setVisible(true );

Answer:

29
Copyright © 2012 Pearson Education, Inc.
Test Bank for Java Software Solutions 7th Edition (International Edition). John Lewis / Will

18) Why should the add( ), subtract( ), multiply( ), divide( ) methods in the RationalNumber
class in the textbook be declared to be public, while the gcd( ) method is declared to be private?
Answer: The numeric operations must be public because they must be accessible to users of the
RationalNumber class. Declaring them private or protected don't accomplish this. The gcd( )
method is declared private because it is a "utility" method within the class. It should only be
used by class members because it has access to the internal structure of the class. One might
want to declare a static gcd( ) method one that just accepts a pair of ints, or longs, or whatever.
This static method would be similar to those in the Math class, like abs( ), sqrt( ), max( ), etc.

30
Copyright © 2012 Pearson Education, Inc.

Visit TestBankBell.com to get complete for all chapters

You might also like