Unit 5 (College Board Questions)

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Consider the following methods. https://assets.learnosity.com/organisations/537/media.academicmerit.com/447fcdcdd3956feae5c2cbbb29c7289f/original.jpg When the call test ( ) is executed, what are the values of s and n at the point indicated by / * End of method * / ? A. s / n world / 6 B. s / n worldpeace / 6 C. s / n world / 12 D. s / n worldpeace / 12 E. s / n peace / 12

A. s / n world / 6

Directions: Select the choice that best fits each statement. Consider the following partial class declaration. https://assets.learnosity.com/organisations/537/media.academicmerit.com/a64f7ce087ad4e9b93506904244bdbb7/original.jpg Which of the following changes to SomeClass will allow other classes to access but not modify the value of myC ? A. Make myC public. B. https://assets.learnosity.com/organisations/537/media.academicmerit.com/5ad98ead3e2a8572623e896e70efe72d/original.jpg C. https://assets.learnosity.com/organisations/537/media.academicmerit.com/29b8647e93ca9e9acfad6fc7e65f4525/original.jpg D. https://assets.learnosity.com/organisations/537/media.academicmerit.com/744874316cb7e60f810fadcb323b3d2a/original.jpg E. https://assets.learnosity.com/organisations/537/media.academicmerit.com/4730f88a784d8f8e942967c2d52c872f/original.jpg A. Choice A B. Choice B C. Choice C D. Choice D E. Choice E

B. Choice B

Consider the following class definition. public class WordClass { private final String word; private static String max_word = ""; public WordClass (String s) { word = s; if (word.length() > max_word.length()) { max_word = word; } } } Which of the following is a true statement about the behavior of WordClass objects? A. A WordClass object can change the value of the variable word more than once. B. Every time a WordClass object is created, the max_word variable is referenced. C. Every time a WordClass object is created, the value of the max_word variable changes. D. No two WordClass objects can have their word length equal to the length of max_word. E. The value of the max_word variable cannot be changed once it has been initialized.

B. Every time a WordClass object is created, the max_word variable is referenced.

This class stores info about temperature readings on various dates public class TemperatureReading implements Comparable { private double temperature; private int month, day, year public int compareTo(Object obj) { TemperatureReading other = (TemperatureReading) obj /* missing code */ } // instance variables/constructors/methods not shown } Consider the segments that are replacements for /* missing code */ I. Double d1 = new Double(temperature) Double d2 = new Double(other.temperature) return d1.compareTo(d2) II. if (temperature < other.temperature) return -1 else if (temperature == other.temperature) return 0 else return 1 III. return (int) (temperature - other.temperature) Which segments could be used to replace /* missing code */ so compareTo can be used to order TemperatureReading objects by increasing temperature value? A. II only B. I and II only C. I and III only D. II and III only E. I, II, and III

B. I and II only

Consider the following class declarations. https://assets.learnosity.com/organisations/537/media.academicmerit.com/5e0d6aca01edaef3dfc91e8ae330af40/original.jpg Which of the following replacements for /* missing code */ will correctly implement the Circleconstructor? https://assets.learnosity.com/organisations/537/media.academicmerit.com/a9344a0f26b8b28cfeb8d8a0adc8ec91/original.jpg A. I only B. II only C. III only D. II and III only E. I, II, and III

B. II only

In the Toy class below, the raisePrice method is intended to increase the value of the instance variable price by the value of the parameter surcharge. The method does not work as intended. public class Toy { private String name; private double price; public Toy(String n, double p) { name = n; price = p; } public void raisePrice(double surcharge) // Line 12 { return price + surcharge; // Line 14 } Which of the following changes should be made so that the class definition compiles without error and the method raisePrice works as intended? A. Replace line 14 with surcharge += price;. B. Replace line 14 with price += surcharge;. C. Replace line 14 with return price += surcharge;. D. Replace line 12 with public raisePrice (double surcharge). E. Replace line 12 with public double raisePrice (double surcharge).

B. Replace line 14 with price += surcharge;.

public class Something { private static int count = 0 public Something() { count += 5 } public static void increment() { count++ } } The following segment appears in a method in a class other than Something. Something s = new Something() Something.increment() Which describes the behavior of the segment? A. The segment creates a Something object s. The class Something's static variable count is initially 0, then increased by 1 B. The segment creates a Something object s. The class Something's static variable count is initially 0, then increased by 5, then increased by 1 C. The segment creates a Something object s. After executing the code segment, the object s has a count value of 1 D. The code segment creates a Something object s. After executing the code segment, the object s has a count value of 5.

B. The segment creates a Something object s. The class Something's static variable count is initially 0, then increased by 5, then increased by 1.

Consider the following class definition. public class ItemInventory { private int numItems; public ItemInventory(int num) { numItems = num; } public updateItems(int newNum) { numItems = newNum; } } Which of the following best identifies the reason the class does not compile? A. The constructor header is missing a return type. B. The updateItems method is missing a return type. C. The constructor should not have a parameter. D. The updateItems method should not have a parameter. E. The instance variable numItems should be public instead of private.

B. The updateItems method is missing a return type.

public class Point { private double myX private double myyY / this Point has coordinates (0,0) public Point () { no implementation } / this Point has coordinates (x,y) public Point(double x, double y) { no implementation } / methods not shown } public class Circle { private Point myCenter private double myRadius / this Circle has center at (0, 0) and radius 0.0 public Circle() { no implementation } / this Circle has the given center and radius public Circle(Point center, double radius) { no implementation } / methods not shown } In a client program which of the following correctly declares and initializes Circle circ with center at (29.5, 33.0) and radius 10.0 ? A. Circle circ = new Circle(29.5, 33.0, 10.0) B. Circle circ = new Circle((29.5, 33.0), 10.0) C. Circle circ = new Circle(new Point (29.5, 33.0), 10.0) D. Circle circ = new Circle() circ.myCenter = new Point(29.5, 33.0) circ.myRadius = 10.0

C. Circle circ = new Circle(new Point (29.5, 33.0), 10.0)

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

public class Point { private double myX private double myyY // postcondition: this Point has coordinates (0,0) public Point () { no implementation } // postcondition: this Point has coordinates (x,y) public Point(double x, double y) { no implementation } // no methods } public class Circle { private Point myCenter private double myRadius // postcondition: this Circle has center (0, 0) and radius 0.0 public Circle() { no implementation } // postcondition: this Circle has given center and radius public Circle(Point center, double radius) { no implementation } // no methods } Which would be the best specification for a Circle method isInside determining whether a Point lies inside this Circle? A. public boolean isInside() B. public void isInside(boolean found) C. public boolean isInside(Point p) D. public void isInside(Point p, boolean found) E. public boolean isInside(Point p, Point center, double radius)

C. public boolean isInside(Point p)

Consider the following class definition. public class Example { private int x; // Constructor not shown. } Which of the following is a correct header for a method of the Example class that would return the value of the private instance variable x so that it can be used in a class other than Example ? A. private int getX() B. private void getX() C. public int getX() D. public void getX() E. public void getX(int x)

C. public int getX()

Consider the following class definition. public class Box { private double weight; /** Postcondition: weight is initialized to w. */ public Box(double w) { /* implementation not shown */ } public double getWeight() { return weight; } public void addWeight(double aw) { /* missing statement */ } } The following code segment, which appears in a class other than Box, is intended to create a Box object b1 with a weight of 2.2 units and then increase the weight of b1 by 1.5 units. Box b1 = new Box(2.2); b1.addWeight(1.5); Which of the following statements could replace /* missing statement */ so that the code segment works as intended? A. aw += weight; B. aw += getWeight(); C. weight += aw; D. weight += getWeight(); E. return weight + aw;

C. weight += aw;

Consider the following class definition. public class SomeClass { private int x = 0; private static int y = 0; public SomeClass(int pX) { x = pX; y++; } public void incrementY() { y++; } public void incrementY(int inc) { y += inc; } public int getY() { return y; } } The following code segment appears in a class other than SomeClass. SomeClass first = new SomeClass(10); SomeClass second = new SomeClass(20); SomeClass third = new SomeClass(30); first.incrementY(); second.incrementY(10); System.out.println(third.getY()); What is printed as a result of executing the code segment if the code segment is the first use of a SomeClass object? A. 0 B. 1 C. 11 D. 14 E. 30

D. 14

A rectangular box fits inside another rectangular box if and only if the height, width, and depth of the smaller box are each less than the corresponding values of the larger box. Consider the following three interface declarations that are intended to represent information necessary for rectangular boxes. https://assets.learnosity.com/organisations/537/media.academicmerit.com/8b235f70b17376e12ed99743d221b8d4/original.jpg Which of the interfaces, if correctly implemented by a Box class, would be sufficient functionality for a user of the Box class to determine if one Box can fit inside another? A. I only B. II only C. III only D. I and II only E. I, II, and III

D. I and II only

The variables years and months are used to represent the age of the item, and the value for months is always between 0 and 11, inclusive. Method updateAge is used to update these variables based on the parameter extraMonths that represents the number of months to be added to the age. private int years private int months // 0 <= months <= 11 // precondition extraMonths >= 0 public void updateAge(int extraMonths) { /* body of updateAge */ } Which code segments could be used to replace /* body of updateAge */ so the method works? I. int yrs = extraMonths % 12 int mos = extraMonths / 12 years = years + yrs months = months + mos II. int totalMonths = years * 12 + months + extraMonths years = totalMonths / 12 months = totalMonths % 12 III. int totalMonths = months + extraMonths years = years + totalMonths / 12 months = totalMonths % 12 A. I only B. II only C. III only D. II and III only E. I, II, and III

D. II and III only

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

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 two methods, which appear within a single class. https://assets.learnosity.com/organisations/537/media.academicmerit.com/b95c26a3cc8f679129c850612502ca36/original.jpg What is printed as a result of the call start() ? A. 0 0 0 0 0 0 black B. 0 0 0 0 0 6 blackboard C. 1 2 3 4 5 6 black D. 1 2 3 4 5 0 black E. 1 2 3 4 5 6 blackboard

E. 1 2 3 4 5 6 blackboard

Consider the following class definition. public class Element { public static int max_value = 0; private int value; public Element (int v) { value = v; if (value > max_value) { max_value = value; } } } The following code segment appears in a class other than Element. for (int i = 0; i < 5; i++) { int k = (int) (Math.random() * 10 + 1); if (k >= Element.max_value) { Element e = new Element(k); } } Which of the following best describes the behavior of the code segment? A. Exactly 5 Element objects are created. B. Exactly 10 Element objects are created. C. Between 0 and 5 Element objects are created, and Element.max_value is increased only for the first object created. D. Between 1 and 5 Element objects are created, and Element.max_value is increased for every object created. E. Between 1 and 5 Element objects are created, and Element.max_value is increased for at least one object created.

E. Between 1 and 5 Element objects are created, and Element.max_value is increased for at least one object created.

Consider the following class declaration. public class IntCell { private int myStoredValue; // constructor not shown public int getValue() { return myStoredValue; } public String toString () { return "" + myStoredValue; } } Assume that the following declaration appears in a client class. IntCell m = new IntCell(); Which of these statements can be used in the client class? I. System.out.println(m.getValue()); II. System.out.println(m.myStoredValue); III. System.out.println(m); A. I only B. II only C. III only D. I and II E. I and III

E. I and III

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.

Consider the following class definition. public class Password { private String password; public Password (String pwd) { password = pwd; } public void reset(String new_pwd) { password = new_pwd; } } Consider the following code segment, which appears in a method in a class other than Password. The code segment does not compile. Password p = new Password("password"); System.out.println("The new password is " + p.reset("password")); Which of the following best identifies the reason the code segment does not compile? A. The code segment attempts to access the private variable password from outside the Password class. B. The new password cannot be the same as the old password. C. The Password class constructor is invoked incorrectly. D. The reset method cannot be called from outside the Password class. E. The reset method does not return a value that can be printed.

E. The reset method does not return a value that can be printed.

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.


Ensembles d'études connexes

MKTG 301 (Clubb) Chapter 9: Pricing Understanding and Capturing Customer Value

View Set

Which of the following statements are true of the quantity mass?

View Set

Chapter 1: The Management Process Today

View Set