CS - Chapter 3

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Instance variables that are numbers are initialized to what default value?

0

Given the following constructor for the BankAccount class, which references the instance variable balance declared to be of type double, what output is generated by a call to new BankAccount()? public BankAccount() { System.out.println(balance); }

0.0

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

1000.0

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

115.0

Consider the following invocation of the deposit method: mySavings.deposit(250); What is the explicit parameter?

250

/** 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.

@param

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

@param

Which special delimeter is used with the javadoc utility to document method arguments?

@param

Fill in the blank in the comment for this method header. /** Constructs a player with the given name _________________________________ */ public Player(String playerName)

@param playerName the name of the player

Consider the following method header: /** Adds interest to the bank account _________________________________ */ public void addInterest(double rate) . . . Fill in the blank in the javadoc comment:

@param rate the rate of interest

/** Gets the current balance of the bank account _________ the current balance */ public double getBalance() { return balance; }

@return

/** 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

@return

Fill in the blank in the comment for this method header. /** Gets the interest for the bank account _________________________________ */ public double getInterest()

@return the interest

What is a tester class?

A class with a main method that contains statements to run methods of another class.

What is the return type of a constructor?

A constructor does not have a return type.

When using index cards to help with the tracing of the execution of an object by hand, what is written on the back of the card?

A table with the values of all of the instance variables.

What is a local variable?

A variable that is declared in the body of a method.

What is a parameter variable?

A variable that is declared in the header of a method.

What is the purpose of a clean and concise design for an object's public interface?

Allows programmers to design solutions using the object without needing to know the details of the class implementation.

What is the name of the constructor for the BankAccount class?

BankAccount

When tracing the execution of an object by hand, what is one way to indicate the change of an instance variable when a mutator method is executed?

Cross out the old value and write down the new value.

Which statement describes a central benefit of information hiding?

Easier to change the private implementation

When are instance variables initialized?

Instance variables are initialized with a default value before a constructor is invoked.

Why is it a good idea for a programmer to write comments about a method first, before implementing it?

It is an excellent test to ensure that the programmer firmly understands what is needed.

If a method has two parameters, one explicit and one implicit, and a return type of void, then the documentation comments should include:

One @param statement, and no @return statement

Which of the following statements is true about constructors?

Providing a constructor for a class is optional.

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 */ } }

Square mySquare = new Square(6);

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 */ _____________________________________ } }

System.out.println("Expected: 36");

Which statement is true about the following constructor of the BankAccount class? public BankAccount(double balance) { this.balance = balance; }

The code sets the instance variable balance to the parameter variable balance.

When using index cards to help with the tracing of the execution of an object by hand, what is written on the front of the card?

The methods that the object can execute.

Which statement about private instance variables is true?

They can only be accessed by methods of the same class

When are local variables initialized?

You must initialize local variables in a method body.

/** Deposits money into the bank account @param _________ the amount to deposit */ public void deposit(double amount) { balance = balance + amount; }

amount

Identify the explicit parameter of the withdraw method of the BankAccount class.

amount

What is the name of the parameter variable of the recordPurchase method of the CashRegister class?

amount

A method header consists of which of the following parts?

an access specifier, a return type, a method name, and a list of the parameters (if any)

A class declaration consists of which of the following parts?

an access specifier, the keyword class, the name of the class, declarations for instance variables, constructors, and methods

An instance variable declaration consists of which of the following parts?

an access specifier, the type of the instance variable, and the name of the instance variable.

The javadoc utility is used to

automatically generate HTML pages that describe classes.

When you declare a method, you also need to provide the method ____, which consists of statements that are executed when the method is called.

body

What is the name of the local variable of the giveChange method of the CashRegister class?

change

The class constructor always has the same name as __.

class

Descriptive text used to communicate design and implementation issues about the classes and public methods of programs is called _______?

comments

When drawing complex shapes, what is used to initialize the position of the shape?

constructor

Which part of a class implementation contains the instructions to initialize an object's instance variables?

constructor

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?

day = 1; month = 1; year = 1990;

Which term means the process of hiding object data and providing methods for data access?

encapsulation

You should provide documentation comments for ___.

every class, every method, every parameter, and every return value

In the statement below, amount is referred to as the ____ parameter. public void deposit(double amount)

explicit

/** 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.

fromMeasurement

Encapsulation allows a programmer to use a class without having to know its ____.

implementation

A method is invoked on what type of parameter?

implicit parameter

Where do parameters and local variables belong?

in a method

/** Constructs a bank account with a given balance @param initialBalance the initial balance */ public BankAccount(double _________) { balance = initialBalance; }

initialBalance

Each object of a class has its own set of ___.

instance variables

What mechanism does an object use to store its data?

instance variables

The private implementation of a class consists of ___.

instance variables and the implementation of the constructors and methods

The public constructors and methods of a class form the public _____ of the class.

interface

What is the name of the utility that formats comments into a set of documents that you can view in a Web browser?

javadoc

When a method exits, its ____ are removed.

local variables

Consider the following invocation of the deposit method: mySavings.deposit(250); What is the implicit parameter?

mySavings

Instance variables that are object references are initialized to what default value?

null

The black boxes from which a program is manufactured are called ___.

objects

The access specifier in the declaration of instance variables should be ___.

private

We want to change the BankAccount class so that all accounts will have a monthly fee. Which of the following will properly define the instance variable monthlyFee that holds the monthly fee?

private double monthlyFee;

Which of the following declares a sideLength instance variable for a Square class that stores an integer value?

private int sideLength;

We want to change the BankAccount class so that all accounts will have a monthly fee. 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?

public BankAccount (double initialBalance, double monthlyFee)

Which line of code is part of the public implementation of the BankAccount class?

public BankAccount(double initialBalance)

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?

public Date (int d, int m, int y)

Which of the following corresponds to a valid constructor header for the Player class?

public Player()

Choose the method header that goes with this method comment. /** Gets the salary of the employee @return the salary of the employee */

public double getSalary()

Which of the following corresponds to the getArea method header for a Square class assuming an integer value for a side length?

public int getArea()

What are the operations that any programmer can use to create and manipulate objects of the class called?

public interface

We want to change the BankAccount class so that all accounts will have a monthly fee. 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; }

Choose the method header that goes with this method comment. /** Raises the salary of the employee @param percentRaise salary percentage raise */

public void raiseSalary(double percentRaise)

Which of the following is an instance variable of the CashRegister class?

purchase

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

return

What statement is used to specify the value that a method gives back to its caller?

return

We want the toString method to return strings like 3/16/2014. Give the body of the toString method.

return month + "/" + day + "/" + year;

Which of the following corresponds to the getArea method body for a Square class where the instance variable is named sideLength?

return sideLength * sideLength;

Which of the following corresponds to the constructor body for a Square class that accepts an initial side length value called initialLength where the instance variable is named sideLength?

sideLength = initialLength;

The code below for the mutator method public void setLength(double length) of the Square class is intended to set the value of the sideLength instance variable to the parameter variable of the method. Which line of code avoids the common error of ignoring the parameter variables of a method?

sideLength = length;

The use of an instance variable name inside a method denotes the instance variable of what?

the implicit parameter

Which of the following denotes the implicit parameter?

this

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;

this.balance = initialBalance;

What verifies that a class works correctly in isolation, outside a complete program?

unit test

What is the name of the instance variable for a Counter object?

value

/** Deposits money into the bank account @param amount the amount to deposit */ public _____ deposit(double amount) { balance = balance + amount; }

void

What is the return type of the println method of the PrintStream class?

void


Kaugnay na mga set ng pag-aaral

Macroeconomics Midterm 1 - Unemployment

View Set

GRADE 10 - Biology / Digestive System

View Set

UH Manoa - PH 203 (T. Lee) - Certification Test : How To Recognize Plagiarism

View Set

15. Commercial General Liability Insurance

View Set

Sociologia applicata- corso di sociologia

View Set

Chapter 9: Violence and Serious Misbehavior

View Set