Java Software Solutions, 8e, Global Edition (Lewis/Loftus)
Chapter 4 Writing Classes
4.1 Multiple-Choice Questions
1) The behavior of an object is defined by the object’s
A) instance data
B) constructor
C) visibility modifiers
D) methods
E) all of the above
2) The relationship between a class and an object is best described as
A) classes are instances of objects
B) objects are instances of classes
C) objects and classes are the same thing
D) classes are programs while objects are variables
E) objects are the instance data of classes
3) To define a class that will represent a car, which of the following definitions is most
appropriate?
A) private class car
B) public class car
C) public class Car
D) public class CAR
E) private class Car
4) Which of the following reserved words in Java is used to create an instance of a class?
A) class
B) public
C) public or private, either could be used
D) import
E) new
5) In order to preserve encapsulation of an object, we would do all of the following except for
which one?
A) Make the instance data private
B) Define the methods in the class to access and manipulate the instance data
C) Make the methods of the class public
D) Make the class final
E) All of the above preserve encapsulation
6) If a method does not have a return statement, then
A) it will produce a syntax error when compiled
B) it must be a void method
C) it can not be called from outside the class that defined the method
D) it must be defined to be a public method
E) it must be an int, double, float or String method
7) Consider a sequence of method invocations as follows: main calls m1, m1 calls m2, m2 calls
m3 and then m2 calls m4, m3 calls m5. If m4 has just terminated, what method will resume
execution?
A) m1
B) m2
C) m3
D) m5
E) main
8) A variable whose scope is restricted to the method where it was declared is known as a(n)
A) parameter
B) global variable
C) local variable
D) public instance data
E) private instance data
9) A class’ constructor usually defines
A) how an object is initialized
B) how an object is interfaced
C) the number of instance data in the class
D) the number of methods in the class
E) if the instance data are accessible outside of the object directly
10) 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
11) Instance data for a Java class
A) are limited to primitive types (e.g., int, float, char)
B) are limited to Strings
C) are limited to objects(e.g., Strings, classes defined by other programmers)
D) may be primitive types or objects, but objects must be defined to be private
E) may be primitive types or objects
12) An example of passing message to a String where the message has a String parameter would
occur in which of the following messages?
A) length
B) substring
C) equals
D) toUpperCase
E) none of the above, it is not possible to pass a String as a parameter in a message to a String
13) Which of the following patterns should be used in place of “xxxx” when instantiating df so
that the gpa to be output in typical form (like 3.810)?
A) “#.###”
B) “#.0”
C) “0.# ”
D) “0.000”
E) “0.0##”
14) Which of the following could be used to instantiate a new Student s1?
A) Student s1 = new Student( );
B) s1 = new Student( );
C) Student s1 = new Student(“Jane Doe”, “Computer Science”, 3.333, 33);
D) new Student s1 = (“Jane Doe”, “Computer Science”, 3.333, 33);
E) new Student(s1);
15) Assume that another method has been defined that will compute and return the student’s class
rank (Freshman, Sophomore, etc). It is defined as:
public String getClassRank( )
Given that s1 is a student, which of the following would properly be used to get s1’s class rank?
A) s1 = getClassRank( );
B) s1.toString( );
C) s1.getHours( );
D) s1.getClassRank( );
E) getClassRank(s1);
16) Another method that might be desired is one that updates the Student’s number of credit
hours. This method will receive a number of credit hours and add these to the Student’s current
hours. Which of the following methods would accomplish this?
A) public int updateHours( )
{ return hours; }
B) public void updateHours( )
{ hours++; }
C) public updateHours(int moreHours)
{ hours += moreHours; }
D) public void updateHours(int moreHours)
{ hours += moreHours; }
E) public int updateHours(int moreHours)
{ return hours + moreHours; }
17) Consider a Rational class designed to represent rational numbers as a pair of int’s, along with
methods reduce (to reduce the rational to simplest form), gcd (to find the greatest common
divisor of two int’s), as well as methods for addition, subtraction, multiplication, and division.
Why should the reduce and gcd methods be declared to be private.
A) Because they will never be used
B) Because they will only be called from methods inside of Rational
C) Because they will only be called from the constructor of Rational
D) Because they do not use any of Rational’s instance data
E) Because it is a typo and they should be declared as public
18) StringTokenizer is a class in the java.util library that can divide a String based on some
delimiter String (a delimiter is a separator). If the instruction StringTokener st = new
StringTokenizer(str, “&&”); is executed, where str is some String, then st divides up str into
separate Strings whenever
A) a blank space is found
B) two blank spaces are found
C) a single ampersand (“&”) is found
D) two ampersands are found
E) two ampersands or the substring “and” or “AND” is found
19) Which of the following Applet methods is called automatically when the applet is first
loaded?
A) Applet (the Applet’s constructor)
B) init
C) start
D) getCodeBase
E) getImage
20) Consider a method defined with the header: public void foo(int a, int b). Which of the
following method calls is legal?
A) foo(0, 0.1);
B) foo(0 / 1, 2 * 3);
C) foo(0);
D) foo( );
E) foo(1 + 2, 3 * 0.1);
21) Consider a method defined with the header: public void doublefoo(double x). Which of the
following method calls is legal?
A) doublefoo(0);
B) doublefoo(0.555);
C) doublefoo(0.1 + 0.2);
D) doublefoo(0.1, 0.2);
E) all of the above are legal except for D
22) In a UML diagram for a class
A) classes are represented as rectangles
B) there may be a section containing the name of the class
C) there may be a section containing the attributes (data) of the class
D) there may be a section containing the methods of the class
E) all of the above
23) Visibility modifiers include
A) public, private
B) public, private, protected
C) public, private, protected, final
D) public, protected, final, static
E) public, private, protected, static
24) The expressions that are passed to a method in an invocation are called
A) actual parameters
B) formal parameters
C) formal arguments
D) formals
E) any of the above
25) What are the objects needed in order to create a Java GUI (graphical user interface)?
A) components
B) events
C) listeners
D) all of the above
E) only classes from AWT and/or Swing are needed
26) What happens if you declare a class constructor to have a void return type?
A) You’ll likely receive a syntax error
B) The program will compile with a warning, but you’ll get a runtime error
C) There’s nothing wrong with declaring a constructor to be void
D) The class’ default constructor will be used instead of the one you’re declaring
E) None of the above
27) The software failure at the Denver International Airport’s baggage handling system is a good
example of
A) how a large system can be obsolete by the time it is developed
B) how designers sometimes have too much faith in the technology they are using
C) how failures are often a result of multiple variables caused by a system as a whole in actual
operation
D) how the use of a centralized computer is really unrealistic in today’s distributed processing
E) all of the above
1) Java methods can return only primitive types (int, double, float, char, boolean, etc).
2) Formal parameters are those that appear in the method call and actual parameters are those
that appear in the method header.
3) All Java classes must contain a main method which is the first method executed when the Java
class is called upon.
4) Java methods can return more than one item if they are modified with the reserved word
continue, as in public continue int foo( ) { … }
5) The following method header definition will result in a syntax error: public void aMethod( );
6) A method defined in a class can access the class’ instance data without needing to pass them
as parameters or declare them as local variables.
7) The interface of a class is based on those data instances and methods that are declared public.
8) Defining formal parameters requires including each parameters type.
9) Every class definition must include a constructor.
10) While multiple objects of the same class can exist, in a given program there can be only one
version of each class.
11) A constructor may contain a return statement so long as no value (or expression) is returned.
12) Listener objects “wait” for an event to occur.
13) A GUI container is an object that defines a screen element.
14) An object should be encapsulated in order to guard its data and methods from inappropriate
access.
15) Accessors and mutators provide mechanisms for controlled access to a well-encapsulated
class.
16) Regarding the software failure described at the Denver International Airport, it is critical to
factor in errors and inefficiencies that always occur in a complex system.
4.3 Free-Form Questions
1) Assume method0 calls method1 and method2, method1 calls method3 which calls method4
and method5. Explain the chain of method calls (what order methods are called and from which
method).
2) Write the constructor, which is passed the player’s name and position.
3) Write a toString method that returns the player’s name, position and batting average formatted
to have 1 digit to the left of the decimal and 3 digits to the right of the decimal. Assume
DecimalFormat has been imported.
}
4) When reasoning about a car, we use such information as its make and model, year, color, cost
(or worth), number of miles, gas mileage, insurance cost, and so forth. Consider a program that
will attempt to determine how practical a car might be on a road trip. Which of these types of
information might you use in your program as instance data? Provide a justification for your
selection.
5) An Employee will have a name, social security number, position, and hourly wages. Define
the instance data for this class.
6) Write the constructor for this class.
7) What visibility modifiers would you use for the methods that move the elevator up one floor,
move the elevator down one floor, that request the elevator, and that start the elevator moving?
8) Explain why it would be a poor decision to make instance data public instead of private. Give
an example of why this could result in a problem.
9) Consider the “Push Me!” program described in section 4.7. As given the push counter starts at
zero and is incremented each time the “Push Me!” button is pressed. Let’s say that you want to
add a class called ResetListener that resets the count to zero. Write the ResetListener class.
10) Write the statement to instantiate an Box object, blueBox, with a length of 6, width of 4, and
height of 2.
11) Write a statement to output the volume of the blue box.
12) Add a constructor to class Box for a cube, which has only 1 parameter, side.
13) Write a statement to instantiate a cube with a length of 3, width of 3, and height of 3.