Chapter 3: Implementing Classes

Ace your homework & exams now with Quizwiz!

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

A

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;

A

Consider the following method comment and method header: /** Converts from a source measurement to a target measurement. __________ from Measurement the measurement @return the input value converted to the target unit */ public double convert To (double from Measurement) { . . . } Fill in the blank. A) @param B) param C) @parameter D) parameter

A

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);

A

Consider the following method header for the Ban kAccount 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;

A

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

A

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

A

The black boxes from which a program is manufactured are called ___. A) objects B) access specifiers C) methods D) instance variables

A

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 GeometricSeque nce { 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;

A

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 mont hly 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; }

A

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 ; }

A

We want to create a class that represents a geometric sequence. A geometric sequence is a sequence o f 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 element s 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;

A

What contains the instructions to initialize the instance variables of an object? A) constructor B) access specifier C) initializer D) type name

A

What do instance variables belong to? A) an object B) a class C) a method D) a package

A

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.

A

What is the name of the constructor for the BankAccount class? A) BankAccount B) deposit C) balance D) withdraw

A

What is the name of the parameter variable of the recordP urchase method of the CashRegister class? A) amount B) payment C) purchase D) change

A

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

A

What is the return type of the println method of the PrintStream class? A) void B) public C) String D) double

A

What verifies that a class works correctly in isolation, outside a complete program? A) unit test B) encapsulation C) abstraction D) enumeration

A

When a method exits, its ____ are removed. A) local variables B) classes C) comments D) instance variables

A

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

A

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()

A

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 construct or will not successfully compile. D) A constructor has a voidreturn type

A

he public constructors and methods of a class form the public _____ of the class. A) interface B) initialization C) implementation D) encapsulation

A

nformation 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

A

ssuming 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

A

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 param eters (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 specifi er, a list of the parameters (if any), and the body of the method.

B

Complete the following tester p rogram 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");

B

Consider the following code to declare a constructor for the Playerclass: 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

B

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.

B

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 convert To (double from Measurement) { . . . } Fill in the blank. A) return B) from Measurement C) double D) convert To

B

Documentation ___ can be used t o describe the classes and public methods of programs. A) components B) comments C) constants D) commands

B

Each object of a class has its own set of ___. A) methods B) instance variables C) constructors D) classes

B

Given this method comment, fill in the blank in the method implementation. /** Deposits money into the bank account @param amount the amount to deposit */ publ ic _____ deposit(double amount) { balance = balance + amount; } A) double B) void C) return D) null

B

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

B

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

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

B

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 (doub le 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 }

B

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

B

What is the name of the local variable of the giveChange method of the CashRegister class? A) amount B) change C) payment D) purchase

B

What is the process of hiding object data and providing methods for data access called? A) documentation B) encapsulation C) instantiation D) abstraction

B

What is the return type of a constructor? A) void B) A constructor does not have a return type. C) private D) public

B

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 const ructor. D) You must initialize instance variables in a method body.

B

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)

B

Which of the following denotes the implicit parameter? A) void B) this C) extends D) public

B

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

C

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

C

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

C

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

C

Given the following constructor for the BankAccount class, what outpu t 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.

C

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

C

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

C

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

C

In the statement below, amount is referred to as the ____ parameter. public void deposit(double amount) A) private B) public C) explicit D) implici

C

Instance variable s 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

C

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

C

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;

C

What do parameters and local variables belong to? A) an object B) a class C) a method D) a package

C

What do static variables belong to? A) a method B) a package C) a class D) an object

C

What does an object store its data in? A) files B) methods C) instance variables D) access specifiers

C

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

C

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;

C

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

C

A method header con sists 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 variabl e, 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)

D

A method is invoked on what type of parameter? A) public parameter B) explicit parameter C) private parameter D) implicit parameter

D

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

D

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)

D

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

D

Encapsulation allows a programmer to use a class without having to know its ____. A) interface B) name C) methods D) implementation

D

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

D

Identify the explicit parameter of the withdrawmethod of the BankAccount class. A) public B) double C) balance D) amount

D

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 an d methods

D

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

D

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 e ach 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 init ialValue; 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.printl n (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()

D

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 constr uctor 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)

D

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.

D

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

D

What is the name of the instance variable for a Bank Account object? A) make Deposit B) make Withdrawal C) get Balance D) balance

D

What statement is used to specify the value that a method gives back to its caller? A) new B) public C) private D) return

D

Which of the foll owing 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)

D

Which of the following is an instance variable of the CashRegister class? A) amount B) balance C) change D) purchase

D

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 a nd a parameter variable with the same name. D) The code sets the instance variable balance to the parameter variable balance

D

You should declare all instance variables as ___. A) protected B) class C) public D) private

D


Related study sets

Health Ch 6: Eating disorders and managing weight

View Set

Chapter 30 Prepu: Management of Patients with Hematologic Neoplasms

View Set

Sentences that use ser + de to indicate where the people are from

View Set

Lilley Ch. 29 Fluid & Electrolytes Q&A

View Set

Chapter 14 - Transportation in a Supply Chain

View Set

Human Nutrition and Diet Final Exam Review

View Set