Deck 3: Implementing Classes

Full screen (f)
exit full mode
Question
Private instance variables ___.

A) can only be accessed by methods of a different class
B) can only be accessed by methods of the same class
C) cannot be accessed by methods of the same class
D) can only be accessed by the constructor of the class
Use Space or
up arrow
down arrow
to flip the card.
Question
An instance variable declaration 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) an access specifier, a list of the parameters (if any), and the body of the method.
D) the type of the instance variable, an access specifier, a list of the parameters (if any), and the body of the method.
Question
What is the return type of the println method of the PrintStream class?

A) void
B) public
C) String
D) double
Question
A class declaration consists of which of the following parts?

A) an access specifier, the keyword class, the name of the class, declarations for instance variables, constructors, and methods
B) an access specifier, a return type, a method name, a list of the parameters (if any), and the body of the method
C) the keyword class, the name of the class, declarations for instance variables, constructors, and methods
D) an access specifier, the name of the class, a list of the parameters (if any), and the body of the constructor
Question
What contains the instructions to initialize the instance variables of an object?

A) constructor
B) access specifier
C) initializer
D) type name
Question
The black boxes from which a program is manufactured are called ___.

A) objects
B) access specifiers
C) methods
D) instance variables
Question
What is the name of the instance variable for a BankAccount object?

A) makeDeposit
B) makeWithdrawl
C) getBalance
D) balance
Question
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)
Question
The name of the constructor is always the same as the name of the __.

A) access specifier
B) class
C) instance variable
D) parameter variable
Question
What is the process of hiding object data and providing methods for data access called?

A) documentation
B) encapsulation
C) instantiation
D) abstraction
Question
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
B) fromMeasurement
C) double
D) convertTo
Question
What does an object store its data in?

A) files
B) methods
C) instance variables
D) access specifiers
Question
Encapsulation allows a programmer to use a class without having to know its ____.

A) interface
B) name
C) methods
D) implementation
Question
Each object of a class has its own set of ___.

A) methods
B) instance variables
C) constructors
D) classes
Question
You should declare all instance variables as ___.

A) protected
B) class
C) public
D) private
Question
What contains the instructions to initialize the instance variables of an object?

A) constructor
B) access specifier
C) initializer
D) type name
Question
What is the return type of a constructor?

A) void
B) A constructor does not have a return type.
C) private
D) public
Question
What statement is used to specify the value that a method gives back to its caller?

A) new
B) public
C) private
D) return
Question
Information hiding makes it simpler for the implementor of a class to _____.

A) change the private implementation
B) change the method headers
C) change the name of the class
D) change the public interface
Question
Consider the following method comment and method header: /**
Converts from a source measurement to a target measurement.
__________ fromMeasurement the measurement
@return the input value converted to the target unit
*/
Public double convertTo(double fromMeasurement) { . . . }
Fill in the blank.

A) @param
B) param
C) @parameter
D) parameter
Question
What is the name of the utility that formats comments into a set of documents that you can view in a Web browser?

A) javadoc
B) javac
C) javad
D) java
Question
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 methods deducts the value of the monthly fee from the account? a. public void chargeFee()
{
Balance = balance - monthlyFee;
}
B) public void chargeFee()
{
InitialBalance = initialBalance - monthlyFee;
}
C) public void chargeFee()
{
Balance = monthlyFee;
}
D) public void chargeFee()
{
Balance - monthlyFee;
}
Question
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;
Question
What are the operations that any programmer can use to create and manipulate objects of the class called?

A) public implementation
B) public interface
C) private implementation
D) private interface
Question
The public constructors and methods of a class form the public _____ of the class.

A) interface
B) initialization
C) implementation
D) encapsulation
Question
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
Question
Which of the following corresponds to a valid constructor header for the Player class?

A) public Player()
B) private Player
C) public void Player()
D) private void Player()
Question
Consider the following method comment and method header: /**
Converts from a source measurement to a target measurement.
@param fromMeasurement the measurement
__________ the input value converted to the target unit
*/
Public double convertTo(double fromMeasurement) { . . . }
Fill in the blank.

A) return double
B) return
C) @return double
D) @return
Question
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. Which of the following will properly define the instance variable monthlyFee that holds the monthly fee?

A) monthlyFee: double;
B) instance var monthlyFee;
C)private double monthlyFee;
D)private field monthlyFee;
Question
Which of the following statements is true about constructors?

A) Providing a constructor for a class is optional.
B) You can only provide one constructor for a class.
C) The body of the constructor must initialize all instance variables or the constructor will not successfully compile.
D) A constructor has a void return type.
Question
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 constructors properly sets the monthly fee to a default value of 20? a. public BankAccount (double initialBalance)
{
Balance = initialBalance;
MonthlyFee = 20;
}
B) public BankAccount (double initialBalance)
{
Balance = initialBalance;
Double monthlyFee = 20;
}
C) public BankAccount (double initialBalance)
{
Balance = initialBalance;
MonthlyFee = initialBalance - 20;
}
D) public BankAccount (double initialBalance)
{
Balance = initialBalance - 20;
}
Question
What is the name of the constructor for the BankAccount class?

A) BankAccount
B) deposit
C) balance
D) withdraw
Question
Documentation ___ can be used to describe the classes and public methods of programs.

A) components
B) comments
C) constants
D) commands
Question
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 create a geometric sequence using code like:
GeometricSequence first = new GeometricSequence (1, 2);
// Creates 1, 2, 4, 8, 16…
GeometricSequence second = new GeometricSequence (10.8, 0.5);
// Creates 10.8, 5.4, 2.7, 1.35 …
Which of the constructor specifications below will allow this code to behave as desired?

A) public void GeometricSequence(double initial, double mult)
B) public GeometricSequence init(double initial, double mult)
C) public GeometricSequence GeometricSequence(double initial, double mult)
D) public GeometricSequence(double initial, double mult)
Question
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 code like:
System.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
Which of the method specifications below will allow this code to behave as desired?

A) public next() : double
B) public int next()
C) public void next(double result)
D) public double next()
Question
Which of the following is a valid constructor header for the Player class that accepts the player name as a parameter?

A) public void Player(String playerName)
B) private Player(playerName)
C) private void Player(playerName)
D) public Player(String playerName)
Question
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;
Question
Consider the following code to declare a constructor for the Player class: public void Player(String playerName)
{
Name = playerName;
}
Which statement is true?

A) The code compiles successfully and results in the instantiation of a Player object when called.
B) The code compiles successfully but results in a compiler error in the code that calls the constructor.
C) The code does not compile.
D) The code compiles successfully but results in a run-time error in the code that calls the constructor.
Question
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
}
Question
If a method has two parameters, one explicit and one implicit, and a return type of void, then the documentation comments should include:

A) One @param statement, and one @return statement
B) Two @param statements, and one @return statement
C) One @param statement, and no @return statement
D) Two @param statements, and no @return statement
Question
Choose the method header that goes with this method comment. /**
Gets the salary of the employee
@return the salary of the employee
*/

A) public void getSalary()
B) public void getSalary
C) public double getSalary()
D) public double getSalary
Question
Given this method comment, fill in the blank in the method implementation. /**
Gets the current balance of the bank account
@return the current balance
*/
Public double getBalance()
{
__________ balance;
}

A) balance
B) @return
C) double
D) return
Question
Given this method implementation, fill in the blank in the method comment. /**
Gets the current balance of the bank account
_________ the current balance
*/
Public double getBalance()
{
Return balance;
}

A) return
B) double
C) @return
D) balance
Question
Which line of code is part of the public implementation of the BankAccount class?

A) balance = balance + amount;
B) balance = balance - amount;
C) public BankAccount(double initialBalance)
D) return balance;
Question
Complete the following tester program by choosing the line that prints the expected outcome. public class BankAccountTester
{
Public static void main(String[] args)
{
BankAccount account = new BankAccount(1000);
Account.deposit(account.getBalance());
System.out.println(account.getBalance());
___________________
}
}

A) System.out.println("Expected: 1000");
B) System.out.println("Expected: 2000");
C) System.out.println("Expected: 2000")
D) println("Expected: 2000");
Question
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
Question
Consider the following method header: /**
Adds interest to the bank account
_________________________________
*/
Public void addInterest(double rate)
) . .
Fill in the blank in the javadoc comment:

A) @param rate the rate of interest
B) @parameter rate the rate of interest
C) @param interestRate the rate of interest
D) @parameter interestRate the rate of interest
Question
Given this method comment, fill in the blank in the method implementation. /**
Constructs a bank account with a given balance
@param initialBalance the initial balance
*/
Public BankAccount(double _________)
{
Balance = initialBalance;
}

A) amount
B) parameter
C) initialBalance
D) balance
Question
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
B) return the interest
C) @return the interest
D) return double the interest
Question
Given this method implementation, fill in the blank in the method comment. /**
Withdraws money from the bank account
_________ amount the amount to withdraw
*/
Public void withdraw(double amount)
{
Balance = balance - amount;
}

A) parameter
B) @param
C) param
D) @parameter
Question
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;
Question
What is a tester class?

A) A class that constructs objects.
B) A class that invokes one or more methods.
C) A class that is named Tester.
D) A class with a main method that contains statements to run methods of another class.
Question
What verifies that a class works correctly in isolation, outside a complete program?

A) unit test
B) encapsulation
C) abstraction
D) enumeration
Question
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)
Question
The private implementation of a class consists of ___.

A) instance variables and the method headers
B) local variables and the method headers
C) parameter variables and the method bodies
D) instance variables and the implementation of the constructors and methods
Question
When you declare a method, you also need to provide the method ____, which consists of statements that are executed when the method is called.

A) body
B) header
C) return type
D) access specifier
Question
Given this method comment, fill in the blank in the method implementation. /**
Deposits money into the bank account
@param amount the amount to deposit
*/
Public _____ deposit(double amount)
{
Balance = balance + amount;
}

A) double
B) void
C) return
D) null
Question
Fill in the blank in the following method comment. /**
Deposits money into the bank account
@param _________ the amount to deposit
*/
Public void deposit(double amount)
{
Balance = balance + amount;
}

A) amount
B) balance
C) double amount
D) money
Question
Choose the method header that goes with this method comment. /**
Raises the salary of the employee
@param percentRaise salary percentage raise
*/

A) public void raiseSalary(double percent)
B) public double raiseSalary(double percent)
C) public double raiseSalary(double percentRaise)
D) public void raiseSalary(double percentRaise)
Question
Consider the following method header for an Employee class: public void raiseSalary(double percentRaise)
{
______________________________________
}
Fill in the blank in the method body:

A) salary = salary * (1 + percentRaise);
B) salary = salary * percentRaise;
C) salary = salary * raise;
D) salary = salary * (1 + raise);
Question
Identify the explicit parameter of the withdraw method of the BankAccount class.

A) public
B) double
C) balance
D) amount
Question
Instance variables that are object references are initialized to what default value?

A) empty
B) Instance variables are not initialized to a default value.
C) null
D) nil
Question
What do instance variables belong to?

A) an object
B) a class
C) a method
D) a package
Question
A method is invoked on what type of parameter?

A) public parameter
B) explicit parameter
C) private parameter
D) implicit parameter
Question
When a method exits, its ____ are removed.

A) local variables
B) classes
C) comments
D) instance variables
Question
Assuming the following code is the body of the deposit method, what output is generated by the valid call myAccount.deposit(1000) for an account with an initial balance of 500? public void deposit(double amount)
{
System.out.println(amount);
Double newBalance = balance + amount;
Balance = newBalance;
}

A) 1500.0
B) The code fragment has a syntax error and does not compile.
C) The code fragment does not compile because the parameter variable is not initialized.
D) 1000.0
Question
Instance variables that are numbers are initialized to what default value?

A) Instance variables are not initialized to a default value.
B) nil
C) 0
D) null
Question
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
Question
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.
Question
What do parameters and local variables belong to?

A) an object
B) a class
C) a method
D) a package
Question
When are local variables initialized?

A) Local variables are initialized with a default value before a constructor is invoked.
B) Local variables are initialized when the method is called.
C) You must initialize local variables in a method body.
D) You must initialize local variables in the constructor.
Question
When are instance variables initialized?

A) Instance variables are initialized when the method is called.
B) Instance variables are initialized with a default value before a constructor is invoked.
C) You must initialize instance variables in the constructor.
D) You must initialize instance variables in a method body.
Question
What is the name of the local variable of the giveChange method of the CashRegister class?

A) amount
B) change
C) payment
D) purchase
Question
The use of an instance variable name inside a method denotes the instance variable of what?

A) the parameter variable
B) the access specifier
C) the explicit parameter
D) the implicit parameter
Question
What is a parameter variable?

A) A variable that is declared in the header of a method.
B) A variable that is declared in the body of the class.
C) A variable that is declared in the body of a method.
D) A variable that is declared in the header of a class.
Question
Which of the following denotes the implicit parameter?

A) void
B) this
C) extends
D) public
Question
What do static variables belong to?

A) a method
B) a package
C) a class
D) an object
Question
Which of the following is an instance variable of the CashRegister class?

A) amount
B) balance
C) change
D) purchase
Question
What is the name of the parameter variable of the recordPurchase method of the CashRegister class?

A) amount
B) payment
C) purchase
D) change
Question
Given the following constructor for the BankAccount class, what output is generated by a call to new BankAccount()?
Public BankAccount()
{
System.out.println(balance);
}

A) The code fragment has a syntax error and does not compile.
B) 1000.0
C) 0.0
D) You cannot print out the value of an uninitialized instance variable.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/103
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 3: Implementing Classes
1
Private instance variables ___.

A) can only be accessed by methods of a different class
B) can only be accessed by methods of the same class
C) cannot be accessed by methods of the same class
D) can only be accessed by the constructor of the class
B
2
An instance variable declaration 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) an access specifier, a list of the parameters (if any), and the body of the method.
D) the type of the instance variable, an access specifier, a list of the parameters (if any), and the body of the method.
B
3
What is the return type of the println method of the PrintStream class?

A) void
B) public
C) String
D) double
A
4
A class declaration consists of which of the following parts?

A) an access specifier, the keyword class, the name of the class, declarations for instance variables, constructors, and methods
B) an access specifier, a return type, a method name, a list of the parameters (if any), and the body of the method
C) the keyword class, the name of the class, declarations for instance variables, constructors, and methods
D) an access specifier, the name of the class, a list of the parameters (if any), and the body of the constructor
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
5
What contains the instructions to initialize the instance variables of an object?

A) constructor
B) access specifier
C) initializer
D) type name
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
6
The black boxes from which a program is manufactured are called ___.

A) objects
B) access specifiers
C) methods
D) instance variables
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
7
What is the name of the instance variable for a BankAccount object?

A) makeDeposit
B) makeWithdrawl
C) getBalance
D) balance
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
8
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)
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
9
The name of the constructor is always the same as the name of the __.

A) access specifier
B) class
C) instance variable
D) parameter variable
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
10
What is the process of hiding object data and providing methods for data access called?

A) documentation
B) encapsulation
C) instantiation
D) abstraction
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
11
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
B) fromMeasurement
C) double
D) convertTo
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
12
What does an object store its data in?

A) files
B) methods
C) instance variables
D) access specifiers
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
13
Encapsulation allows a programmer to use a class without having to know its ____.

A) interface
B) name
C) methods
D) implementation
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
14
Each object of a class has its own set of ___.

A) methods
B) instance variables
C) constructors
D) classes
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
15
You should declare all instance variables as ___.

A) protected
B) class
C) public
D) private
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
16
What contains the instructions to initialize the instance variables of an object?

A) constructor
B) access specifier
C) initializer
D) type name
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
17
What is the return type of a constructor?

A) void
B) A constructor does not have a return type.
C) private
D) public
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
18
What statement is used to specify the value that a method gives back to its caller?

A) new
B) public
C) private
D) return
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
19
Information hiding makes it simpler for the implementor of a class to _____.

A) change the private implementation
B) change the method headers
C) change the name of the class
D) change the public interface
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
20
Consider the following method comment and method header: /**
Converts from a source measurement to a target measurement.
__________ fromMeasurement the measurement
@return the input value converted to the target unit
*/
Public double convertTo(double fromMeasurement) { . . . }
Fill in the blank.

A) @param
B) param
C) @parameter
D) parameter
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
21
What is the name of the utility that formats comments into a set of documents that you can view in a Web browser?

A) javadoc
B) javac
C) javad
D) java
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
22
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 methods deducts the value of the monthly fee from the account? a. public void chargeFee()
{
Balance = balance - monthlyFee;
}
B) public void chargeFee()
{
InitialBalance = initialBalance - monthlyFee;
}
C) public void chargeFee()
{
Balance = monthlyFee;
}
D) public void chargeFee()
{
Balance - monthlyFee;
}
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
23
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;
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
24
What are the operations that any programmer can use to create and manipulate objects of the class called?

A) public implementation
B) public interface
C) private implementation
D) private interface
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
25
The public constructors and methods of a class form the public _____ of the class.

A) interface
B) initialization
C) implementation
D) encapsulation
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
26
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
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
27
Which of the following corresponds to a valid constructor header for the Player class?

A) public Player()
B) private Player
C) public void Player()
D) private void Player()
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
28
Consider the following method comment and method header: /**
Converts from a source measurement to a target measurement.
@param fromMeasurement the measurement
__________ the input value converted to the target unit
*/
Public double convertTo(double fromMeasurement) { . . . }
Fill in the blank.

A) return double
B) return
C) @return double
D) @return
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
29
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. Which of the following will properly define the instance variable monthlyFee that holds the monthly fee?

A) monthlyFee: double;
B) instance var monthlyFee;
C)private double monthlyFee;
D)private field monthlyFee;
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
30
Which of the following statements is true about constructors?

A) Providing a constructor for a class is optional.
B) You can only provide one constructor for a class.
C) The body of the constructor must initialize all instance variables or the constructor will not successfully compile.
D) A constructor has a void return type.
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
31
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 constructors properly sets the monthly fee to a default value of 20? a. public BankAccount (double initialBalance)
{
Balance = initialBalance;
MonthlyFee = 20;
}
B) public BankAccount (double initialBalance)
{
Balance = initialBalance;
Double monthlyFee = 20;
}
C) public BankAccount (double initialBalance)
{
Balance = initialBalance;
MonthlyFee = initialBalance - 20;
}
D) public BankAccount (double initialBalance)
{
Balance = initialBalance - 20;
}
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
32
What is the name of the constructor for the BankAccount class?

A) BankAccount
B) deposit
C) balance
D) withdraw
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
33
Documentation ___ can be used to describe the classes and public methods of programs.

A) components
B) comments
C) constants
D) commands
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
34
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 create a geometric sequence using code like:
GeometricSequence first = new GeometricSequence (1, 2);
// Creates 1, 2, 4, 8, 16…
GeometricSequence second = new GeometricSequence (10.8, 0.5);
// Creates 10.8, 5.4, 2.7, 1.35 …
Which of the constructor specifications below will allow this code to behave as desired?

A) public void GeometricSequence(double initial, double mult)
B) public GeometricSequence init(double initial, double mult)
C) public GeometricSequence GeometricSequence(double initial, double mult)
D) public GeometricSequence(double initial, double mult)
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
35
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 code like:
System.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
Which of the method specifications below will allow this code to behave as desired?

A) public next() : double
B) public int next()
C) public void next(double result)
D) public double next()
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
36
Which of the following is a valid constructor header for the Player class that accepts the player name as a parameter?

A) public void Player(String playerName)
B) private Player(playerName)
C) private void Player(playerName)
D) public Player(String playerName)
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
37
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;
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
38
Consider the following code to declare a constructor for the Player class: public void Player(String playerName)
{
Name = playerName;
}
Which statement is true?

A) The code compiles successfully and results in the instantiation of a Player object when called.
B) The code compiles successfully but results in a compiler error in the code that calls the constructor.
C) The code does not compile.
D) The code compiles successfully but results in a run-time error in the code that calls the constructor.
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
39
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
}
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
40
If a method has two parameters, one explicit and one implicit, and a return type of void, then the documentation comments should include:

A) One @param statement, and one @return statement
B) Two @param statements, and one @return statement
C) One @param statement, and no @return statement
D) Two @param statements, and no @return statement
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
41
Choose the method header that goes with this method comment. /**
Gets the salary of the employee
@return the salary of the employee
*/

A) public void getSalary()
B) public void getSalary
C) public double getSalary()
D) public double getSalary
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
42
Given this method comment, fill in the blank in the method implementation. /**
Gets the current balance of the bank account
@return the current balance
*/
Public double getBalance()
{
__________ balance;
}

A) balance
B) @return
C) double
D) return
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
43
Given this method implementation, fill in the blank in the method comment. /**
Gets the current balance of the bank account
_________ the current balance
*/
Public double getBalance()
{
Return balance;
}

A) return
B) double
C) @return
D) balance
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
44
Which line of code is part of the public implementation of the BankAccount class?

A) balance = balance + amount;
B) balance = balance - amount;
C) public BankAccount(double initialBalance)
D) return balance;
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
45
Complete the following tester program by choosing the line that prints the expected outcome. public class BankAccountTester
{
Public static void main(String[] args)
{
BankAccount account = new BankAccount(1000);
Account.deposit(account.getBalance());
System.out.println(account.getBalance());
___________________
}
}

A) System.out.println("Expected: 1000");
B) System.out.println("Expected: 2000");
C) System.out.println("Expected: 2000")
D) println("Expected: 2000");
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
46
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
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
47
Consider the following method header: /**
Adds interest to the bank account
_________________________________
*/
Public void addInterest(double rate)
) . .
Fill in the blank in the javadoc comment:

A) @param rate the rate of interest
B) @parameter rate the rate of interest
C) @param interestRate the rate of interest
D) @parameter interestRate the rate of interest
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
48
Given this method comment, fill in the blank in the method implementation. /**
Constructs a bank account with a given balance
@param initialBalance the initial balance
*/
Public BankAccount(double _________)
{
Balance = initialBalance;
}

A) amount
B) parameter
C) initialBalance
D) balance
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
49
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
B) return the interest
C) @return the interest
D) return double the interest
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
50
Given this method implementation, fill in the blank in the method comment. /**
Withdraws money from the bank account
_________ amount the amount to withdraw
*/
Public void withdraw(double amount)
{
Balance = balance - amount;
}

A) parameter
B) @param
C) param
D) @parameter
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
51
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;
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
52
What is a tester class?

A) A class that constructs objects.
B) A class that invokes one or more methods.
C) A class that is named Tester.
D) A class with a main method that contains statements to run methods of another class.
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
53
What verifies that a class works correctly in isolation, outside a complete program?

A) unit test
B) encapsulation
C) abstraction
D) enumeration
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
54
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)
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
55
The private implementation of a class consists of ___.

A) instance variables and the method headers
B) local variables and the method headers
C) parameter variables and the method bodies
D) instance variables and the implementation of the constructors and methods
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
56
When you declare a method, you also need to provide the method ____, which consists of statements that are executed when the method is called.

A) body
B) header
C) return type
D) access specifier
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
57
Given this method comment, fill in the blank in the method implementation. /**
Deposits money into the bank account
@param amount the amount to deposit
*/
Public _____ deposit(double amount)
{
Balance = balance + amount;
}

A) double
B) void
C) return
D) null
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
58
Fill in the blank in the following method comment. /**
Deposits money into the bank account
@param _________ the amount to deposit
*/
Public void deposit(double amount)
{
Balance = balance + amount;
}

A) amount
B) balance
C) double amount
D) money
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
59
Choose the method header that goes with this method comment. /**
Raises the salary of the employee
@param percentRaise salary percentage raise
*/

A) public void raiseSalary(double percent)
B) public double raiseSalary(double percent)
C) public double raiseSalary(double percentRaise)
D) public void raiseSalary(double percentRaise)
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
60
Consider the following method header for an Employee class: public void raiseSalary(double percentRaise)
{
______________________________________
}
Fill in the blank in the method body:

A) salary = salary * (1 + percentRaise);
B) salary = salary * percentRaise;
C) salary = salary * raise;
D) salary = salary * (1 + raise);
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
61
Identify the explicit parameter of the withdraw method of the BankAccount class.

A) public
B) double
C) balance
D) amount
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
62
Instance variables that are object references are initialized to what default value?

A) empty
B) Instance variables are not initialized to a default value.
C) null
D) nil
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
63
What do instance variables belong to?

A) an object
B) a class
C) a method
D) a package
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
64
A method is invoked on what type of parameter?

A) public parameter
B) explicit parameter
C) private parameter
D) implicit parameter
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
65
When a method exits, its ____ are removed.

A) local variables
B) classes
C) comments
D) instance variables
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
66
Assuming the following code is the body of the deposit method, what output is generated by the valid call myAccount.deposit(1000) for an account with an initial balance of 500? public void deposit(double amount)
{
System.out.println(amount);
Double newBalance = balance + amount;
Balance = newBalance;
}

A) 1500.0
B) The code fragment has a syntax error and does not compile.
C) The code fragment does not compile because the parameter variable is not initialized.
D) 1000.0
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
67
Instance variables that are numbers are initialized to what default value?

A) Instance variables are not initialized to a default value.
B) nil
C) 0
D) null
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
68
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
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
69
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.
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
70
What do parameters and local variables belong to?

A) an object
B) a class
C) a method
D) a package
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
71
When are local variables initialized?

A) Local variables are initialized with a default value before a constructor is invoked.
B) Local variables are initialized when the method is called.
C) You must initialize local variables in a method body.
D) You must initialize local variables in the constructor.
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
72
When are instance variables initialized?

A) Instance variables are initialized when the method is called.
B) Instance variables are initialized with a default value before a constructor is invoked.
C) You must initialize instance variables in the constructor.
D) You must initialize instance variables in a method body.
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
73
What is the name of the local variable of the giveChange method of the CashRegister class?

A) amount
B) change
C) payment
D) purchase
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
74
The use of an instance variable name inside a method denotes the instance variable of what?

A) the parameter variable
B) the access specifier
C) the explicit parameter
D) the implicit parameter
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
75
What is a parameter variable?

A) A variable that is declared in the header of a method.
B) A variable that is declared in the body of the class.
C) A variable that is declared in the body of a method.
D) A variable that is declared in the header of a class.
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
76
Which of the following denotes the implicit parameter?

A) void
B) this
C) extends
D) public
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
77
What do static variables belong to?

A) a method
B) a package
C) a class
D) an object
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
78
Which of the following is an instance variable of the CashRegister class?

A) amount
B) balance
C) change
D) purchase
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
79
What is the name of the parameter variable of the recordPurchase method of the CashRegister class?

A) amount
B) payment
C) purchase
D) change
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
80
Given the following constructor for the BankAccount class, what output is generated by a call to new BankAccount()?
Public BankAccount()
{
System.out.println(balance);
}

A) The code fragment has a syntax error and does not compile.
B) 1000.0
C) 0.0
D) You cannot print out the value of an uninitialized instance variable.
Unlock Deck
Unlock for access to all 103 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 103 flashcards in this deck.