Name: 
 

Java Chapter 3



Multiple Choice
Identify the choice that best completes the statement or answers the question.
 

 1. 

In the statement below, amount is referred to as the ____ parameter.

public void deposit(double amount)
a.
private
c.
explicit
b.
public
d.
implicit
 

 2. 

What do parameters and local variables belong to?
a.
an object
c.
a method
b.
a class
d.
a package
 

 3. 

Fill in the blank in the comment for this method header.

/**
   Gets the interest for the bank account
   _________________________________
*/
public double getInterest()
. . .
a.
@return double the interest
c.
@return the interest
b.
return the interest
d.
return double the interest
 

 4. 

What is a local variable?
a.
A variable that is declared in the header of a class.
b.
A variable that is declared in the body of the class.
c.
A variable that is declared in the header of a method.
d.
A variable that is declared in the body of a method.
 

 5. 

Consider the constructor of the BankAccount class that has a parameter for the initial balance. Which line of code is equivalent to this constructor body?

balance = initialBalance;
a.
this.balance = initialBalance;
b.
this.initialBalance = balance
c.
balance = this.initialBalance;
d.
this.balance = this.initialBalance;
 

 6. 

What is the name of the parameter variable of the recordPurchase method of the CashRegister class?
a.
amount
c.
purchase
b.
payment
d.
change
 

 7. 

Assume the method below has been added to the BankAccount class.
public void transfer (BankAccount source, double amount)
{
   balance = balance + amount;
   source.balance = source.balance – amount;
}

What will be output from the following statements that use the revised BankAccount class?

BankAccount first = new BankAccount (100.0);
BankAccount second = new BankAccount (300.0);
first.transfer (second, 50.0);
System.out.println (first.getBalance() + " "
   + second.getBalance());
a.
100.0 300.0
c.
150.0 300.0
b.
150.0 250.0
d.
100.0 250.0
 

 8. 

A method is invoked on what type of parameter?
a.
public parameter
c.
private parameter
b.
explicit parameter
d.
implicit parameter
 

 9. 

Fill in the blank in the comment for this method header.

/**
   Constructs a player with the given name
   _________________________________
*/
public Player(String playerName)
. . .
a.
@return the player
b.
@parameter playerName the name of the player
c.
@param playerName the name of the player
d.
return the player
 

 10. 

When a method exits, its ____ are removed.
a.
local variables
c.
comments
b.
classes
d.
instance variables
 

 11. 

Fill in the third line of this SquareTester program so that it prints out the expected outcome.

public class SquareTester
{
   public static void main(String[] args)
   {
      /*
         Step 1: declare and initialize a variable mySquare as an
         instance of a Square class with a side length of 6
      */
      /*
         Step 2: print out the area of the object referenced by the
         variable mySquare using the getArea method
      */
      /*
         Step 3: print the expected outcome
      */
      _____________________________________
   }
}
a.
System.out.println("Expected: 12");
b.
System.out.println("Expected: 6");
c.
System.out.println("Expected: 36");
d.
System.out.println("Expected: 18");
 

 12. 

We want to create a class that represents a date.  A date has a day, month, and year.  For example, the date March 16, 2014 has the day 16, month 3, and year 2014. The basic framework of a date class is below:
public class Date
{
   private int day;
   private int month;
   private int year;
}
We want to create a specific date using code like:
Date first = new Date (16, 3, 2014);
//  Creates March 16, 2014

Date second = new Date (1, 9, 2013);
//  Creates September 1, 2013
Which of the constructor specifications below will allow this code to behave as desired?
a.
public void Date (int d, int m, int y)
b.
public init (int d, int m, int y)
c.
public Date (int d, int m, int y)
d.
public Date Date (int d, int m, int y)
 

 13. 

We want the toString method to return strings like 3/16/2014.  Give the body of the toString method.
a.
return "m/d/y";
b.
return month + "/" + day + "/" + year;
c.
return m + "/" + d + "/" + y;
d.
return "month/day/year";
 

 14. 

Which of the following denotes the implicit parameter?
a.
void
c.
extends
b.
this
d.
public
 

 15. 

We want to create a class that represents a geometric sequence.  A geometric sequence is a sequence of numbers that begin at some value and then multiplies each value by some constant to get the next value.  For example, the geometric sequence 1, 2, 4, 8, 16 starts at 1 and multiplies each term by 2 to get the next.  The geometric sequence 10.8, 5.4, 2.7, 1.35 starts at 10.8 and multiplies each term by 0.5 to get the next.  The basic framework of a geometric sequence class is below:
public class GeometricSequence
{
   private double initialValue;
   private double multiplier;
}
What should the body of the constructor be?
a.
initialValue = initial;
   multiplier = mult;
b.
initial = initialValue;
   mult = multiplier;
c.
double initialValue = initial;
   double multiplier = mult;
d.
double initial = initialValue;
   double mult = multiplier;
 

 16. 

What does an object store its data in?
a.
files
c.
instance variables
b.
methods
d.
access specifiers
 

 17. 

Assuming the following code is the body of the main method, what output is generated?

BankAccount myAccount;
System.out.println(myAccount.getBalance());
a.
The code fragment does not compile because the local variable is not initialized.
b.
0.0
c.
The code fragment has a syntax error and does not compile.
d.
1000.0
 

 18. 

We want to create a class that represents a geometric sequence.  A geometric sequence is a sequence of numbers that begin at some value and then multiplies each value by some constant to get the next value.  For example, the geometric sequence 1, 2, 4, 8, 16 starts at 1 and multiplies each term by 2 to get the next.  The geometric sequence 10.8, 5.4, 2.7, 1.35 starts at 10.8 and multiplies each term by 0.5 to get the next.  The basic framework of a geometric sequence class is below:
public class GeometricSequence
{
   private double initialValue;
   private double multiplier;
}
We want to produce elements of the geometric sequence using codeSystem.out.println (first.next());      //  Prints 1 and advances
System.out.println (first.next());      //  Prints 2 and advances
System.out.println (first.next());      //  Prints 4 and advances
System.out.println (first.next());      //  Prints 8 and advances

System.out.println (second.next());  //Prints 10.8 and advances
System.out.println (second.next());  //Prints 5.4 and advances
System.out.println (second.next());  //Prints 2.7 and advances

What should the body of the next method be?
a.
double result = initialValue;
   initialValue = initialValue * multiplier;
   return result;
b.
return initialValue;
   initialValue = initialValue * multiplier;
c.
double result = initialValue;
   multiplier = initialValue * multiplier;
   return result;
d.
initialValue = initialValue * multiplier;
   return initialValue;
 

 19. 

You should declare all instance variables as ___.
a.
protected
c.
public
b.
class
d.
private
 

 20. 

Which of the following corresponds to the constructor body for a Square class that accepts an initial side length value where the instance variable is named sideLength?
a.
sideLength = initialLength;
c.
initialLength = 0;
b.
initialLength = sideLength;
d.
sideLength = 0;
 

 21. 

A method header consists of which of the following parts?
a.
the return type, the name of the method, and a list of the parameters (if any)
b.
an access specifier, the type of the instance variable, and the name of the instance variable
c.
the type of the instance variable, an access specifier, and a list of the parameters (if any)
d.
an access specifier, a return type, a method name, and a list of the parameters (if any)
 

 22. 

What do instance variables belong to?
a.
an object
c.
a method
b.
a class
d.
a package
 

 23. 

Encapsulation allows a programmer to use a class without having to know its ____.
a.
interface
c.
methods
b.
name
d.
implementation
 

 24. 

You should provide documentation comments for ___.
a.
only classes
b.
only methods with parameters
c.
every class, every method, every parameter, and every return value
d.
only methods with return values
 

 25. 

What will be output from the following statements that use BankAccount class?
BankAccount first = new BankAccount (100);
first.deposit (50.0);
BankAccount second = new BankAccount (first.getBalance());
first.deposit (50.0);
BankAccount third = new BankAccount (first.getBalance());
first.deposit (50.0);
System.out.println (first.getBalance() + " "
   + second.getBalance() + third.getBalance());
a.
150.0 200.0 250.0
c.
250.0 150.0 200.0
b.
250.0 250.0 250.0
d.
250.0 200.0 250.0
 

 26. 

Which line of code is part of the private implementation of the BankAccount class?
a.
public BankAccount()
b.
balance = balance - amount;
c.
public void deposit(double amount)
d.
public void withdraw(double amount)
 

 27. 

We want to change the BankAccount class so that all accounts will have a monthly fee.  When a BankAccount is created, its monthly fee is set and cannot be changed. The instance variable monthlyFee will hold the monthly fee.  Which of the following is the correct public interface for a constructor that sets both the initial balance and monthly fee?
a.
public BankAccount (double initialBalance, monthlyFee)
b.
public BankAccount (double initialBalance, double monthlyFee)
c.
public BankAccount (double initialBalance) has monthlyFee
d.
public BankAccount (double initialBalance)
  {
  double monthlyFee;
  //  The rest of the constructor code follows
  }
 

 28. 

Consider the following method header for the BankAccount class:

public void addInterest(double rate) 
{
   ______________________________________
}

Fill in the blank in the method body.
a.
balance = balance * (1 + rate);
b.
balance = balance * rate;
c.
balance = balance * (1 + interestRate);
d.
balance = balance * interestRate;
 

 29. 

We want to create a class that represents a date.  A date has a day, month, and year.  For example, the date March 16, 2014 has the day 16, month 3, and year 2014. The basic framework of a date class is below:
public class Date
{
   private int day;
   private int month;
   private int year;
}
The default date will be set to January 1, 1990.  What should the body of the constructor with zero parameters be?
a.
day = 1;
   month = 1;
   year = 1990;
b.
int day = 1;
   int month = 1;
   int year = 1990;
 

 30. 

Consider the following method comment and method header:

/**
   Converts from a source measurement to a target measurement.
   @param _______________ the measurement
   @return the input value converted to the target unit
*/
public double convertTo(double fromMeasurement) { . . . }


Fill in the blank.
a.
return
c.
double
b.
fromMeasurement
d.
convertTo
 



 
Check Your Work     Start Over