Implementing Classes Chapter.3 Test Bank Answers - Big Java Early Objects 5e Complete Test Bank by Cay S. Horstmann. DOCX document preview.
Course Title: Big Java, Early Objects
Chapter 3: Implementing Classes
Multiple Choice
1) What does an object store its data in?
a) files
b) methods
c) instance variables
d) access specifiers
Section Ref: 3.1 Instance Variables and Encapsulation
Title: What does an object store its data in?
Difficulty: Easy
2) Each object of a class has its own set of ___.
a) methods
b) instance variables
c) constructors
d) classes
Section Ref: 3.1 Instance Variables and Encapsulation
Title: Each object of a class has its own set of:
Difficulty: Easy
3) 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.
Section Ref: 3.1 Instance Variables and Encapsulation
Title: An instance variable declaration consists of which parts?
Difficulty: Medium
4) You should declare all instance variables as ___.
a) protected
b) class
c) public
d) private
Section Ref: 3.1 Instance Variables and Encapsulation
Title: You should declare all instance variables as ___.
Difficulty: Easy
5) What statement is used to specify the value that a method gives back to its caller?
a) new
b) public
c) private
d) return
Section Ref: 3.1 Instance Variables and Encapsulation
Title: Which statement is used to specify the value that a method gives back to its caller?
Difficulty: Easy
6) 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
Section Ref: Section Ref: 3.1 Instance Variables and Encapsulation
Title: Private instance variables
Difficulty: Medium
7) What is the name of the instance variable for a BankAccount object?
a) makeDeposit
b) makeWithdrawl
c) getBalance
d) balance
Section Ref: Section Ref: 3.1 Instance Variables and Encapsulation
Title: Identify BankAccount instance variable
Difficulty: Easy
8) Encapsulation allows a programmer to use a class without having to know its ____.
a) interface
b) name
c) methods
d) implementation
Section Ref: 3.1 Instance Variables and Encapsulation
Title: Encapsulation allows a programmer to use a class without having to know its ____.
Difficulty: Medium
9) The black boxes from which a program is manufactured are called ___.
a) objects
b) access specifiers
c) methods
d) instance variables
Section Ref: 3.1 Instance Variables and Encapsulation
Title: Black boxes for constructing programs are called ___.
Difficulty: Easy
10) What is the process of hiding object data and providing methods for data access called?
a) documentation
b) encapsulation
c) instantiation
d) abstraction
Section Ref: 3.1 Instance Variables and Encapsulation
Title: What is process of hiding object data called?
Difficulty: Easy
11) 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
Section Ref: 3.1 Instance Variables and Encapsulation
Title: Information hiding makes it simpler for the implementor of a class to ___.
Difficulty: Medium
12) 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)
Section Ref: 3.2 Specifying the Public Interface of a Class
Title: A method header consists of which of the following parts?
Difficulty: Medium
13) What contains the instructions to initialize the instance variables of an object?
a) constructor
b) access specifier
c) initializer
d) type name
Section Ref: 3.2 Specifying the Public Interface of a Class
Title: What contains the instructions to initialize instance variables?
Difficulty: Easy
14) What contains the instructions to initialize the instance variables of an object?
a) constructor
b) access specifier
c) initializer
d) type name
Section Ref: 3.2 Specifying the Public Interface of a Class
Title: What contains the instructions to initialize instance variables?
Difficulty: Easy
15) What is the return type of the println method of the PrintStream class?
a) void
b) public
c) String
d) double
Section Ref: 3.2 Specifying the Public Interface of a Class
Title: What is the return type of println?
Difficulty: Easy
16) What is the return type of a constructor?
a) void
b) A constructor does not have a return type.
c) private
d) public
Section Ref: 3.2 Specifying the Public Interface of a Class
Title: What is the return type of a constructor?
Difficulty: Easy
17) 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
Section Ref: 3.2 Specifying the Public Interface of a Class
Title: A class declaration consists of which of the following parts?
Difficulty: Medium
18) 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
Section Ref: 3.2 Specifying the Public Interface of a Class
Title: The name of the constructor is always the same as the name of the ___.
Difficulty: Easy
19) 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
Section Ref: 3.3 Providing the Class Implementation
Title: Fill in the blank in the javadoc comment
Difficulty: Medium
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
Section Ref: 3.3 Providing the Class Implementation
Title: Fill in the blank in the javadoc comment
Difficulty: Medium
21) 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
Section Ref: 3.3 Providing the Class Implementation
Title: Fill in the blank in the javadoc comment
Difficulty: Medium
22) What is the name of the constructor for the BankAccount class?
a) BankAccount
b) deposit
c) balance
d) withdraw
Section Ref: 3.3 Providing the Class Implementation
Title: What is the name of the BankAccount constructor?
Difficulty: Easy
23) 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()
Section Ref: 3.2 Specifying the Public Interface of a Class
Title: Which of the following corresponds to a valid constructor header for the Player class?
Difficulty: Medium
24) 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.
Section Ref: 3.2 Specifying the Public Interface of a Class
Title: Which of the following statements is true about constructors?
Difficulty: Medium
25) 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)
Section Ref: 3.2 Specifying the Public Interface of a Class
Title: Which of the following is a valid constructor header with a parameter for the Player class?
Difficulty: Medium
26) 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.
Section Ref: 3.2 Specifying the Public Interface of a Class
Title: Which statement is true about the constructor code provided?
Difficulty: Hard
27) The public constructors and methods of a class form the public _____ of the class.
a) interface
b) initialization
c) implementation
d) encapsulation
Section Ref: 3.2 Specifying the Public Interface of a Class
Title: The public constructors and methods of a class form the public _____ of the class.
Difficulty: Easy
28) 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
Section Ref: 3.2 Specifying the Public Interface of a Class
Title: What are the operations that anyone can use to manipulate objects of a class called?
Difficulty: Easy
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;
Section Ref: 3.2 Specifying the Public Interface of a Class
Title: Define an Instance Variable?
Difficulty: Easy
30) 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?
- public BankAccount (double initialBalance)
{
balance = initialBalance;
monthlyFee = 20;
}
- public BankAccount (double initialBalance)
{
balance = initialBalance;
double monthlyFee = 20;
}
- public BankAccount (double initialBalance)
{
balance = initialBalance;
monthlyFee = initialBalance - 20;
}
- public BankAccount (double initialBalance)
{
balance = initialBalance - 20;
}
Section Ref: 3.3 Providing the Class Implementation
Title: Which constructor correctly sets a default value?
Difficulty: Medium
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 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
}
Section Ref: 3.3 Providing the Class Implementation
Title: Which of the following is the correct public interface for a constructor that sets both the initial balance and monthly fee
Difficulty: Medium
32) 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?
- public void chargeFee()
{
balance = balance – monthlyFee;
}
- public void chargeFee()
{
initialBalance = initialBalance - monthlyFee;
}
- public void chargeFee()
{
balance = monthlyFee;
}
- public void chargeFee()
{
balance - monthlyFee;
}
Section Ref: 3.3 Providing the Class Implementation
Title: Which is the correct method?
Difficulty: Medium
33) 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)
Section Ref: 3.3 Providing the Class Implementation
Title: Which of the constructor specifications below will allow this code to behave as desired?
Difficulty: Medium
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;
}
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;
Section Ref: 3.3 Providing the Class Implementation
Title: What should the body of the constructor be?
Difficulty: Medium
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()
Section Ref: 3.3 Providing the Class Implementation
Title: Which of the method specifications below will allow this code to behave as desired?
Difficulty: Medium
36) 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;
Section Ref: 3.3 Providing the Class Implementation
Title: What should the body of the next method be?
Difficulty: Medium
37) Documentation ___ can be used to describe the classes and public methods of programs.
a) components
b) comments
c) constants
d) commands
Section Ref: 3.3 Providing the Class Implementation
Title: Documentation ___ can be used to describe the classes and public methods of programs.
Difficulty: Easy
38) 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
Section Ref: 3.3 Providing the Class Implementation
Title: What is the name of the utility that formats comments?
Difficulty: Easy
39) 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
Section Ref: 3.3 Providing the Class Implementation
Title: What is the name of the utility that formats comments?
Difficulty: Medium
40) 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
Section Ref: 3.3 Providing the Class Implementation
Title: You should provide documentation comments for ___.
Difficulty: Easy
41) 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
Section Ref: 3.3 Providing the Class Implementation
Title: When you declare a method, you also need to provide the method ___.
Difficulty: Easy
42) 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
Section Ref: 3.3 Providing the Class Implementation
Title: The private implementation of a class consists of ___.
Difficulty: Medium
43) 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)
Section Ref: 3.3 Providing the Class Implementation
Title: Which line of code is part of the private implementation of the BankAccount class?
Difficulty: Easy
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;
Section Ref: 3.3 Providing the Class Implementation
Title: Which line of code is part of the public implementation of the BankAccount class?
Difficulty: Easy
45) 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
Section Ref: 3.3 Providing the Class Implementation
Title: Fill in the blank in this method comment.
Difficulty: Medium
46) 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
Section Ref: 3.3 Providing the Class Implementation
Title: Given this method comment, fill in the blank in the method implementation.
Difficulty: Medium
47) 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
Section Ref: 3.3 Providing the Class Implementation
Title: Given this method implementation, fill in the blank in the method comment.
Difficulty: Medium
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
Section Ref: 3.3 Providing the Class Implementation
Title: Given this method comment, fill in the blank in the method implementation.
Difficulty: Medium
49) 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
Section Ref: 3.3 Providing the Class Implementation
Title: Given this method implementation, fill in the blank in the method comment.
Difficulty: Medium
50) 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
Section Ref: 3.3 Providing the Class Implementation
Title: Given this method comment, fill in the blank in the method implementation.
Difficulty: Medium
51) 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)
Section Ref: 3.3 Providing the Class Implementation
Title: Choose the method header that goes with this method comment.
Difficulty: Medium
52) 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);
Section Ref: 3.3 Providing the Class Implementation
Title: Fill in the blank in the method body for the given method header.
Difficulty: Medium
53) 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
Section Ref: 3.3 Providing the Class Implementation
Title: Choose the method header that goes with this method comment.
Difficulty: Medium
54) 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
Section Ref: 3.3 Providing the Class Implementation
Title: Fill in the blank in the javadoc comment for the given method header.
Difficulty: Medium
55) 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;
Section Ref: 3.3 Providing the Class Implementation
Title: Fill in the blank in the method body for the given method header.
Difficulty: Medium
56) 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
Section Ref: 3.3 Providing the Class Implementation
Title: Fill in the blank in the javadoc comment for the given method header.
Difficulty: Medium
57) 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
Section Ref: 3.3 Providing the Class Implementation
Title: Fill in the blank in the javadoc comment for the given constructor header.
Difficulty: Medium
58) What verifies that a class works correctly in isolation, outside a complete program?
a) unit test
b) encapsulation
c) abstraction
d) enumeration
Section Ref: 3.4 Unit Testing
Title: What verifies that a class works correctly in isolation?
Difficulty: Easy
59) 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.
Section Ref: 3.4 Unit Testing
Title: What is a tester class?
Difficulty: Easy
60) 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");
Section Ref: 3.4 Unit Testing
Title: Choose line to complete BankAccountTester program
Difficulty: Medium
61) 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.
Section Ref: 3.6 Local Variables
Title: What is a local variable?
Difficulty: Medium
62) 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.
Section Ref: 3.6 Local Variables
Title: What is a parameter variable?
Difficulty: Medium
63) When a method exits, its ____ are removed.
a) local variables
b) classes
c) comments
d) instance variables
Section Ref: 3.6 Local Variables
Title: When a method exits, its ___ are removed.
Difficulty: Easy
64) What do parameters and local variables belong to?
a) an object
b) a class
c) a method
d) a package
Section Ref: 3.6 Local Variables
Title: What do parameters and local variables belong to?
Difficulty: Medium
65) What do instance variables belong to?
a) an object
b) a class
c) a method
d) a package
Section Ref: 3.6 Local Variables
Title: What do instance variables belong to?
Difficulty: Medium
66) What do static variables belong to?
a) a method
b) a package
c) a class
d) an object
Section Ref: 3.6 Local Variables
Title: What do static variables belong to?
Difficulty: Medium
67) What is the name of the parameter variable of the recordPurchase method of the CashRegister class?
a) amount
b) payment
c) purchase
d) change
Section Ref: 3.6 Local Variables
Title: Name the parameter variable of the recordPurchase method
Difficulty: Easy
68) What is the name of the local variable of the giveChange method of the CashRegister class?
a) amount
b) change
c) payment
d) purchase
Section Ref: 3.6 Local Variables
Title: Name the local variable of the giveChange method
Difficulty: Easy
69) Which of the following is an instance variable of the CashRegister class?
a) amount
b) balance
c) change
d) purchase
Section Ref: 3.6 Local Variables
Title: Identify an instance variable of the CashRegister class
Difficulty: Easy
70) 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.
Section Ref: 3.6 Local Variables
Title: When are instance variables initialized?
Difficulty: Medium
71) 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.
Section Ref: 3.6 Local Variables
Title: what output is generated by a call to this constructor?
Difficulty: Medium
72) 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.
Section Ref: 3.6 Local Variables
Title: When are local variables initialized?
Difficulty: Medium
73) 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
Section Ref: 3.6 Local Variables
Title: What output does this main method code generate?
Difficulty: Medium
74) 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
Section Ref: 3.6 Local Variables
Title: What output does this code fragment generate?
Difficulty: Medium
75) 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
Section Ref: 3.6 Local Variables
Title: Instance variables that are object references are initialized to what default value?
Difficulty: Medium
76) 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
Section Ref: 3.6 Local Variables
Title: Instance variables that are numbers are initialized to what default value?
Difficulty: Medium
77) Which of the following denotes the implicit parameter?
a) void
b) this
c) extends
d) public
Section Ref: 3.7 The this Reference
Title: Which of the following denotes the implicit parameter?
Difficulty: Easy
78) A method is invoked on what type of parameter?
a) public parameter
b) explicit parameter
c) private parameter
d) implicit parameter
Section Ref: 3.7 The this Reference
Title: A method is invoked on what type of parameter?
Difficulty: Easy
79) 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
Section Ref: 3.7 The this Reference
Title: The use of an instance variable name in a method denotes the instance variable of what?
Difficulty: Easy
80) Identify the explicit parameter of the withdraw method of the BankAccount class.
a) public
b) double
c) balance
d) amount
Section Ref: 3.7 The this Reference
Title: Identify the explicit parameter of the withdraw method of the BankAccount class.
Difficulty: Medium
81) In the statement below, amount is referred to as the ____ parameter.
public void deposit(double amount)
a) private
b) public
c) explicit
d) implicit
Section Ref: 3.7 The this Reference
Title: The variable amount is referred to as the __________ parameter.
Difficulty: Medium
82) Consider the following invocation of the deposit method:
mySavings.deposit(250);
What is the implicit parameter?
a) deposit
b) mySavings
c) 250
d) There is no implicit parameter.
Section Ref: 3.7 The this Reference
Title: What is the implicit parameter of this call?
Difficulty: Easy
83) Consider the following invocation of the deposit method:
mySavings.deposit(250);
What is the explicit parameter?
a) There is no explicit parameter.
b) deposit
c) 250
d) mySavings
Section Ref: 3.7 The this Reference
Title: What is the explicit parameter?
Difficulty: Easy
84) 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;
Section Ref: 3.7 The this Reference
Title: Which line of code is equivalent to this constructor body?
Difficulty: Medium
85) Which statement is true about the following constructor of the BankAccount class?
public BankAccount(double balance)
{
this.balance = balance;
}
a) The code has a syntax error.
b) The code has a logic error.
c) You can't have an instance variable and a parameter variable with the same name.
d) The code sets the instance variable balance to the parameter variable balance.
Section Ref: 3.7 The this Reference
Title: Which statement is true about the following constructor of the BankAccount class?
Difficulty: Medium
86) When drawing complex shapes, provide a(n) ____ to set the position of the shape.
a) constructor
b) viewer
c) component
d) frame
Section Ref: 3.8 Shape Classes
Title: When drawing complex shapes, provide a(n) ___ to set the position of the shape.
Difficulty: Easy
87) Consider the following code fragment from the Italian Flag program in How To 3.2:
public class ItalianFlagComponent
{
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2d) g;
ItalianFlag flag = new ItalianFlag(100, 100, 90);
flag.draw(g2);
}
}
Which of the following statements is true?
a) It is impossible to construct an ItalianFlagComponent because no constructor is implemented.
b) It is impossible to add an ItalianFlagComponent object to a frame because the class does not extend JComponent.
c) The code will not compile because it should have called g2.draw(flag);
d) The code has a syntax error and will not compile.
Section Ref: 3.8 Shape Classes
Title: Which statement is true about this ItalianFlagComponent code?
Difficulty: Hard
88) Which of the following declares a sideLength instance variable for a Square class that stores an integer value?
a) private integer sideLength;
b) private int sideLength;
c) public integer sideLength
d) public static int sideLength;
Section Ref: 3.1 Instance Variables and Encapsulation
Title: Which of the following declares a sideLength instance variable for a Square class?
Difficulty: Medium
89) 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;
b) initialLength = sideLength;
c) initialLength = 0;
d) sideLength = 0;
Section Ref: 3.3 Providing the Class Implementation
Title: Which is a constructor body with a parameter for Square class …?
Difficulty: Medium
90) Which of the following corresponds to the getArea method header for a Square class assuming an integer value for a side length?
a) public int getArea
b) public integer getArea
c) public int getArea()
d) public integer getArea()
Section Ref: 3.2 Specifying the Public Interface of a Class
Title: Which corresponds to the getArea method header for a Square class …?
Difficulty: Medium
91) Which of the following corresponds to the getArea method body for a Square class where the instance variable is named sideLength?
a) return sideLength;
b) return area;
c) return width * height;
d) return sideLength * sideLength;
Section Ref: 3.3 Providing the Class Implementation
Title: Which corresponds to the getArea method body for a Square class …?
Difficulty: Easy
92) Fill in the first line of this SquareTester program so that it declares and initializes a variable mySquare as an instance of a Square class with a side length of 6.
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) Square mySquare = new Square(6);
b) mySquare = new Square(6);
c) mySquare = Square(6);
d) Square mySquare = Square(6);
Section Ref: 3.4 Unit Testing
Title: Complete this SquareTester program to create an instance of a Square object
Difficulty: Medium
93) 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");
Section Ref: 3.4 Unit Testing
Title: Complete the SquareTester program to print out the expected outcome
Difficulty: Medium
94) If the CarComponent class had the call below added to it, where will car3 be placed?
int y3 = (getHeight() - 30)/2;
Car car3 = new Car(0, y3);
car3.draw(g2);
a) At the middle of the right side of the window
b) At the middle of the left side of the window
c) At the middle of the top of the window
d) At the middle of the bottom of the window
Section Ref: 3.8 Shape Classes
Title: Place the Call within a Class
Difficulty: Medium
95) Which lines would need to be added to CarComponent to put car3 at the middle of the left side of the window?
a) int y3 = (getHeight() - 30)/2;
Car car3 = new Car(0, y3);
car3.draw(g2);
b) int x3 = (getHeight() - 30)/2;
Car car3 = new Car(x3, 0);
car3.draw(g2);
c) int y3 = (getWidth() - 30)/2;
Car car3 = new Car(0, y3);
car3.draw(g2);
d) int x3 = (getWidth() - 30)/2;
Car car3 = new Car(x3, 0);
car3.draw(g2);
Section Ref: 3.8 Shape Classes
Title: Which lines would need to be added to CarComponent to put car3 at the middle of the left side of the window
Difficulty: Medium
96) What will be output from the following statements that use BankAccount class?
BankAccount first = new BankAccount (100);
BankAccount second = new BankAccount (100);
BankAccount third = first;
first.deposit (50.0);
second.deposit (50.0);
third.deposit (50.0);
System.out.println (first.getBalance() + " "
+ second.getBalance() + third.getBalance());
a) 150.0 200.0 250.0
b) 150.0 150.0 150.0
c) 250.0 250.0 250.0
d) 200.0 150.0 200.0
Section Ref: 3.8 Shape Classes
Title: What will be output from the following statements that use BankAccount class
Difficulty: Medium
97) 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
b) 250.0 250.0 250.0
c) 250.0 150.0 200.0
d) 250.0 200.0 250.0
Section Ref: 3.8 Shape Classes
Title: What will be output from the following statements that use BankAccount class
Difficulty: Medium
98) Assume the method below has been added to the BankAccount class.
public void giveBonus ()
{
balance = balance + 5.0;
}
What will be output from the following statements that use the revised BankAccount class?
BankAccount premiumAccount = new BankAccount (100);
premiumAccount.giveBonus ();
premiumAccount.giveBonus ();
premiumAccount.giveBonus ();
System.out.println (premiumAccount.getBalance());
a) 110.0
b) 100.0
c) 105.0
d) 115.0
Section Ref: 3.8 Shape Classes
Title: What will be output from the following statements that use the revised BankAccount class
Difficulty: Medium
99) 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
b) 150.0 250.0
c) 150.0 300.0
d) 100.0 250.0
Section Ref: 3.8 Shape Classes
Title: What will be output from the following statements that use the revised BankAccount class
Difficulty: Medium
100) 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;
Section Ref: 3.8 Shape Classes
Title: What should the body of the constructor with zero parameters be
Difficulty: Easy
101) 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)
Section Ref: 3.8 Shape Classes
Title: What should the body of the constructor with zero parameters be
Difficulty: Medium
102) 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;
}
What should the body of the constructor be?
a) day = d;
month = m;
year = y;
b) d = day;
m = month;
y = year;
c) int day = d;
int month = m;
int year = y;
d) day = 1;
month = 1;
year = 1990;
Section Ref: 3.8 Shape Classes
Title: What should the body of the constructor be?
Difficulty: Medium
103) 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";
Section Ref: 3.8 Shape Classes
Title: Give the body of the toString method
Difficulty: Medium