csa chapter 5

¡Supera tus tareas y exámenes ahora con Quizwiz!

Consider the following class declaration. public class Circle { private double radius; public double computeArea() { private double pi = 3.14159; public double area = pi * radius * radius; return area; } // Constructor not shown. } Which of the following best explains why the computeArea method will cause a compilation error? A. Local variables declared inside a method cannot be declared as public or private. B. Local variables declared inside a method must all be private. C. Local variables declared inside a method must all be public. D. Local variables used inside a method must be declared at the end of the method. E. Local variables used inside a method must be declared before the method header.

A. Local variables declared inside a method cannot be declared as public or private.

Consider the following Party class. The getNumOfPeople method is intended to allow methods in other classes to access a Party object's numOfPeople instance variable value; however, it does not work as intended. Which of the following best explains why the getNumOfPeople method does NOT work as intended?

A. The getNumOfPeople method should be declared as public.

Consider the BankAccount class below. public class BankAccount { private final String ACCOUNT_NUMBER; private double balance; public BankAccount(String acctNumber, double beginningBalance) { ACCOUNT_NUMBER = acctNumber; balance = beginningBalance; } public boolean withdraw(double withdrawAmount) { /* missing code */ } } The class contains the withdraw method, which is intended to update the instance variable balance under certain conditions and return a value indicating whether the withdrawal was successful. If subtracting withdrawAmount from balance would lead to a negative balance, balance is unchanged and the withdrawal is considered unsuccessful. Otherwise, balance is decreased by withdrawAmount and the withdrawal is considered successful. Which of the following code segments can replace /* missing code */ to ensure that the withdraw method works as intended? I. if (withdrawAmount > balance) { return "Overdraft"; } else { balance -= withdrawAmount; return true; } II. if (withdrawAmount > balance) { return false; } else { balance -= withdrawAmount; return balance; } III. if (withdrawAmount > balance) { return false; } else { balance -= withdrawAmount; return true; } A. I only B. II only C. III only D. I and II only E. I, II, and III

C. III only

Consider the following class definition. The class does not compile. public class Player { private double score; public getScore() { return score; } // Constructor not shown } The accessor method getScore is intended to return the score of a Player object. Which of the following best explains why the class does not compile? A. The getScore method should be declared as private. B. The getScore method requires a parameter. C. The return type of the getScore method needs to be defined as double. D. The return type of the getScore method needs to be defined as String. E. The return type of the getScore method needs to be defined as void.

C. The return type of the getScore method needs to be defined as double.

The Player class below will contain two int attributes and a constructor. The class will also contain a method getScore that can be accessed from outside the class. public class Player { /* missing code */ } Which of the following replacements for /* missing code */ is the most appropriate implementation of the class? A. private int score; private int id; private Player(int playerScore, int playerID) { /* implementation not shown */ } private int getScore() { /* implementation not shown */ } B. private int score; private int id; public Player(int playerScore, int playerID) { /* implementation not shown */ } private int getScore() { /* implementation not shown */ } C. private int score; private int id; public Player(int playerScore, int playerID) { /* implementation not shown */ } public int getScore() { /* implementation not shown */ } D. public int score; public int id; public Player(int playerScore, int playerID) { /* implementation not shown */ } private int getScore() { /* implementation not shown */ } E. public int score; public int id; public Player(int playerScore, int playerID) { /* implementation not shown */ } public int getScore() { /* implementation not shown */ }

C. private int score; private int id; public Player(int playerScore, int playerID) { /* implementation not shown */ } public int getScore() { /* implementation not shown */ }

The Car class will contain two string attributes for a car's make and model. The class will also contain a constructor. public class Car { /* missing code */ } Which of the following replacements for /* missing code */ is the most appropriate implementation of the class? A. public String make; public String model; public Car(String myMake, String myModel) { /* implementation not shown */ } B. public String make; public String model; private Car(String myMake, String myModel) { /* implementation not shown */ } C. private String make; private String model; public Car(String myMake, String myModel) { /* implementation not shown */ } D. public String make; private String model; private Car(String myMake, String myModel) ( /* implementation not shown */ } E. private String make; private String model; private Car(String myMake, String myModel) { /* implementation not shown */ }

C. private String make; private String model; public Car(String myMake, String myModel) { /* implementation not shown */ }

The Date class below will contain three int attributes for day, month, and year, a constructor, and a setDate method. The setDate method is intended to be accessed outside the class. public class Date { /* missing code */ } Which of the following replacements for /* missing code */ is the most appropriate implementation of the class? A. private int day; private int month; private int year; private Date() { /* implementation not shown */ } private void setDate(int d, int m, int y) { /* implementation not shown */ } B. private int day; private int month; private int year; public Date() { /* implementation not shown */ } private void setDate(int d, int m, int y) { /* implementation not shown */ } C. private int day; private int month; private int year; public Date() { /* implementation not shown */ } public void setDate(int d, int m, int y) { /* implementation not shown */ } D. public int day; public int month; public int year; private Date() { /* implementation not shown */ } private void setDate(int d, int m, int y) { /* implementation not shown */ } E. public int day; public int month; public int year; public Date() { /* implementation not shown */ } public void setDate(int d, int m, int y) { /* implementation not shown */ }

C. private int day; private int month; private int year; public Date() { /* implementation not shown */ } public void setDate(int d, int m, int y) { /* implementation not shown */ }

The class Worker is defined below. The class includes the method getEarnings, which is intended to return the total amount earned by the worker. public class Worker { private double hourlyRate; private double hoursWorked; private double earnings; public Worker(double rate, double hours) { hourlyRate = rate; hoursWorked = hours; } private void calculateEarnings() { double earnings = 0.0; earnings += hourlyRate * hoursWorked; } public double getEarnings() { calculateEarnings(); return earnings; } } The following code segment appears in a method in a class other than Worker. The code segment is intended to print the value 800.0, but instead prints a different value because of an error in the Worker class. Worker bob = new Worker(20.0, 40.0); System.out.println(bob.getEarnings()); Which of the following best explains why an incorrect value is printed? A. The private variables hourlyRate and hoursWorked are not properly initialized. B. The private variables hourlyRate and hoursWorked should have been declared public. C. The private method calculateEarnings should have been declared public. D. The variable earnings in the calculateEarnings method is a local variable. E. The variables hourlyRate and hoursWorked in the calculateEarnings method are local variables.

D. The variable earnings in the calculateEarnings method is a local variable.

Consider the following class definition. public class RentalCar { private double dailyRate; // the fee per rental day private double mileageRate; // the fee per mile driven public RentalCar(double daily, double mileage) { dailyRate = daily; mileageRate = mileage; } public double calculateFee(int days, int miles) { /* missing code */ } } The calculateFee method is intended to calculate the total fee for renting a car. The total fee is equal to the number of days of the rental, days, times the daily rental rate plus the number of miles driven, miles, times the per mile rate. Which of the following code segments should replace /* missing code */ so that the calculateFee method will work as intended? A. return dailyRate + mileageRate; B. return (daily * dailyRate) + (mileage * mileageRate); C. return (daily * days) + (mileage * miles); D. return (days * dailyRate) + (miles * mileageRate); E. return (days + miles) * (dailyRate + mileageRate);

D. return (days * dailyRate) + (miles * mileageRate);

5-2-3: Consider the definition of the Cat class below. The class uses the instance variable isSenior to indicate whether a cat is old enough to be considered a senior cat or not.

D. Cat c = new Cat ("Whiskers", 10);

5-5-1: Consider the class Party which keeps track of the number of people at the party. Which of the following method signatures could replace the missing header for the set method in the code above so that the method will work as intended?

D. public void setNum(int people)

Consider the following class definition. public class BoolTest { private int one; public BoolTest(int newOne) { one = newOne; } public int getOne() { return one; } public boolean isGreater(BoolTest other) { /* missing code */ } } The isGreater method is intended to return true if the value of one for this BoolTest object is greater than the value of one for the BoolTest parameter other, and false otherwise. The following code segments have been proposed to replace /* missing code */. return one > other.one; return one > other.getOne(); return getOne() > other.one; Which of the following replacements for /* missing code */ can be used so that isGreater will work as intended? A. I only B. II only C. III only D. I and II only E. I, II and III

E. I, II and III

Consider the following class declaration. The changeWeather method is intended to update the value of the instance variable weather and return the previous value of weather before it was updated. public class WeatherInfo { private String city; private int day; private String weather; public WeatherInfo(String c, int d, String w) { city = c; day = d; weather = w; } public String changeWeather(String w) { /* missing code */ } } Which of the following options should replace /* missing code */ so that the changeWeather method will work as intended? A. Sting prev = w; return weather; B. String prev = weather; return w; C. String prev = w; return prev; D weather = w; String prev = weather; return prev; E. String prev = weather; weather = w; return prev;

E. String prev = weather; weather = w; return prev;

Consider the following class definition. public class FishTank { private double numGallons; private boolean saltWater; public FishTank(double gals, boolean sw) { numGallons = gals; saltWater = sw; } public double getNumGallons() { return numGallons; } public boolean isSaltWater() { if (saltWater) { return "Salt Water"; } else { return "Fresh Water"; } } } Which of the following best explains the reason why the class will not compile? A. The variable numGallons is not declared in the getNumGallons method. B. The variable saltWater is not declared in the isSaltWater method. C. The isSaltWater method does not return the value of an instance variable. D. The value returned by the getNumGallons method is not compatible with the return type of the method. E. The value returned by the isSaltWater method is not compatible with the return type of the method.

E. The value returned by the isSaltWater method is not compatible with the return type of the method.

Consider the following class definition. public class Info { private String name; private int number; public Info(String n, int num) { name = n; number = num; } public void changeName(String newName) { name = newName; } public int addNum(int n) { num += n; return num; } } Which of the following best explains why the class will not compile? A. The class is missing an accessor method. B. The instance variables name and number should be designated public instead of private. C. The return type for the Info constructor is missing. D. The variable name is not defined in the changeName method. E. The variable num is not defined in the addNum method.

E. The variable num is not defined in the addNum method.

Consider the following class, which uses the instance variable balance to represent a bank account balance. public class BankAccount { private double balance; public double deposit(double amount) { /* missing code */ } } The deposit method is intended to increase the account balance by the deposit amount and then return the updated balance. Which of the following code segments should replace /* missing code */ so that the deposit method will work as intended? A. amount = balance + amount; return amount; B. balance = amount; return amount; C. balance = amount; return balance; D. balance = balance + amount; return amount; E. balance = balance + amount; return balance;

E. balance = balance + amount; return balance;

Consider the following Bugs class, which is intended to simulate variations in a population of bugs. The population is stored in the method's int attribute. The getPopulation method is intended to allow methods in other classes to access a Bugs object's population value; however, it does not work as intended. public class Bugs { private int population; public Bugs(int p) { population = p; } public int getPopulation() { return p; } } Which of the following best explains why the getPopulation method does NOT work as intended? A. The getPopulation method should be declared as private. B. The return type of the getPopulation method should be void. C. The getPopulation method should have at least one parameter. D. The variable population is not declared inside the getPopulation method. E. The instance variable population should be returned instead of p, which is local to the constructor.

E. The instance variable population should be returned instead of p, which is local to the constructor.


Conjuntos de estudio relacionados

Information Security (Exam 1, Chapter 1)

View Set

Macroeconomics Chapter 7 Questions

View Set

Ch. 7 - Electricity & Electrical Safety

View Set

Storia delle Relazioni Internazionali Date

View Set