Review Questions
1. In Java, methods must include all of the following except ___________.
a. a declaration
b. a call to another method
c. curly braces
d. a body
A Java method can call another method, but is not required to do so.
2. All method declarations contain ___________.
a. the keyword static
b. one or more explicitly named access modifiers
c. arguments
d. parentheses
Some method declarations contain a, b or c, but d is the only answer that applies
to all methods.
3. A public static method named computeSum() is located in classA. To
call the method from within classB, use the statement ___________.
a. computeSum(classB);
b. classB(computeSum());
c. classA.computeSum();
d. You cannot call computeSum() from within classB.
4. Which of the following method declarations is correct for a static method
named displayFacts() if the method receives an int argument?
a. public static int displayFacts()
b. public void displayFacts(int data)
c. public static void displayFacts(int data)
d. Two of these are correct.
The method in a does not receive an int argument, and the method in b is not
static.
5. The method with the declaration public static int aMethod(double
d) has a method type of ___________.
a. static
b. int
c. double
d. You cannot determine the method type.
The method type is the return type.
6. Which of the following is a correct call to a method declared as public
static void aMethod(char code)?
a. void aMethod();
b. void aMethod(‘V’);
c. aMethod(char ‘M’);
d. aMethod(‘Q’);
Both a and b contain return types; these are not part of method calls. Answer c
incorrectly contains a data type in the parentheses.
7. A method is declared as public static void showResults(double
d, int i). Which of the following is a correct method call?
a. showResults(double d, int i);
b. showResults(12.2, 67);
c. showResults(4, 99.7);
d. Two of these are correct.
The answer in a incorrectly uses data types in the method call. In the answer in c,
the argument types are out of order.
8. The method with the declaration public static char
procedure(double d) has a method type of ___________.
a. public
b. static
c. char
d. double
The return type appears to the left of the method name.
9. The method public static boolean testValue(int response)
returns ___________.
a. a boolean value
b. an int value
c. no value
d. You cannot determine what is returned.
The return type appears to the left of the method name.
10. Which of the following could be the last legally coded line of a method declared
as public static int getVal(double sum)?
a. return;
b. return 77;
c. return 2.3;
d. Any of these could be the last coded line of the method.
The method cannot return nothing as in answer a, nor can it return a double as
in answer c.
11. The nonstatic data components of a class often are referred to as the ___________
of that class.
a. access types
b. instance variables
c. methods
d. objects
12. Objects contain methods and data items, which are also known as ___________.
a. fields
b. functions
c. themes
d. instances
Fields and data are synonymous with attributes. Objects are instances.
13. You send messages or information to an object through its ___________.
a. fields
b. methods
c. classes
d. type
14. A program or class that instantiates objects of another prewritten class is a(n)
___________.
a. class client
b. superclass
c. object
d. patron
15. The body of a class is always written ___________.
a. in a single line, as the first statement in a class
b. within parentheses
c. between curly braces
d. as a method call
16. Most class data fields are ___________.
a. private
b. public
c. static
d. final
17. The concept of allowing a class’s private data to be changed only by a class’s own
methods is known as ___________.
a. structured logic
b. object orientation
c. information hiding
d. data masking
18. Suppose you declare an object, as Book thisBook;. Before you store data in
thisBook, you ___________.
a. also must explicitly allocate memory for it
b. need not explicitly allocate memory for it
c. must explicitly allocate memory for it only if it has a constructor
d. can declare it to use no memory
19. If a class is named Student, the class constructor name is___________.
a. any legal Java identifier
b. any legal Java identifier that begins with S
c. StudentConstructor
d. Student
20. If you use the automatically-supplied default constructor when you create an
object, ___________.
a. numeric fields are set to 0 (zero)
b. character fields are set to blank
c. Boolean fields are set to true
d. All of these are true.
Only a is true. Character fields are set to Unicode ‘\u0000’ and Boolean fields are
set to false.