csa chapter 5

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Consider the following class definition. public class Person { private String name; private int feet; private int inches; public Person(String nm, int ft, int in) { name = nm; feet = ft; inches = in; } public int heightInInches() { return feet * 12 + inches; } public String getName() { return name; } public String compareHeights(person other) { if (this.heightInInches() < other.heightInInches()) { return name; } else if (this.heightInInches() > other.heightInInches()) { return other.getName(); } else return "Same" } } The following code segment appears in a method in a class other than Person. Person andy = new Person("Andrew", 5, 6); Person ben = new Person("Benjamin", 6, 5); System.out.println(andy.compareHeights(ben)); What, if anything, is printed as a result of executing the code segment? A. Andrew B. Benjamin C. Same D. Nothing is printed because the method heightInInches cannot be called on this. E. Nothing is printed because the compareHeights method in the Person class cannot take a Person object as a parameter.

A. Andrew

Consider the class definition below. The method levelUp is intended to increase a Superhero object's strength attribute by the parameter amount. The method does not work as intended. public class Superhero { private String name; private String secretIdentity; private int strength; public Superhero(String realName, String codeName) { name = realName; secretIdentity = codeName; strength = 5; } public int levelUp(int amount) // line 14 { strength += amount; // line 16 } } Which of the following changes should be made so that the levelUp method works as intended? A. In line 14, levelUp should be declared as type void. B. In line 14, amount should be declared as type void. C. Line 16 should be changed to strength + amount;. D. Line 16 should be changed to return strength + amount;. E. Line 16 should be changed to return amount;.

A. In line 14, levelUp should be declared as type void.

Consider the following class. The method getTotalSalaryAndBonus is intended to return an employee's total salary with the bonus added. The bonus should be doubled when the employee has 10 or more years of service. public class Employee { private String name; private double salary; private int yearsOfService; public Employee(String n, double sal, int years) { name = n; salary = sal; yearsOfService = years; } public double getTotalSalaryAndBonus(double bonus) { /* missing code */ } } Which of the following could replace /* missing code */ so that method getTotalSalaryAndBonus will work as intended? A. if (years >= 10) { bonus *= 2; } return salary + bonus; B. if (yearsOfService >= 10) { bonus *= 2; } return salary + bonus; C. return salary + bonus; D. if (years >= 10) { bonus *= 2; } return sal + bonus; E. if (yearsOfService >= 10) { bonus *= 2; } return sal + bonus;

B. if (yearsOfService >= 10) { bonus *= 2; } return salary + bonus;

Consider the following class definition. public class Contact { private String contactName; private String contactNumber; public Contact(String name, String number) { contactName = name; contactNumber = number; } public void doSomething() { System.out.println(this); } public String toString() { return contactName + " " + contactNumber; } } The following code segment appears in another class. Contact c = new Contact("Alice", "555-1234"); c.doSomething(); c = new Contact("Daryl", ""); c.doSomething(); What is printed as a result of executing the code segment? A. Daryl B. Daryl 555-1234 C. Alice 555-1234 Daryl D. Alice 555-1234 Daryl 555-1234 E. this this

C. Alice 555-1234 Daryl

Consider the following class definition. Each object of the class Employee will store the employee's name as name, the number of weekly hours worked as wk_hours, and hourly rate of pay as pay_rate. public class Employee { private String name; private int wk_hours; private double pay_rate; public Employee(String nm, int hrs, double rt) { name = nm; wk_hours = hrs; pay_rate = rt; } public Employee(String nm, double rt) { name = nm; wk_hours = 20; pay_rate = rt; } } Which of the following code segments, found in a class other than Employee, could be used to correctly create an Employee object representing an employee who worked for 20 hours at a rate of $18.50 per hour? I. Employee e1 = new Employee("Lili", 20, 18.5); II. Employee e2 = new Employee("Steve", 18.5); III. Employee e3 = new Employee("Carol", 20); A. I only B. III only C. I and II only D. I and III only E. I, II, and III

C. I and II only

Consider the following class, which models a bank account. The deposit method is intended to update the account balance by a given amount; however, it does not work as intended. public class BankAccount { private String accountOwnerName; private double balance; private int accountNumber; public BankAccount(String name, double initialBalance, int acctNum) { accountOwnerName = name; balance = initialBalance; accountNumber = acctNum; } public void deposit(double amount) { double balance = balance + amount; } } What is the best explanation of why the deposit method does not work as intended? A. The deposit method must have a return statement. B. In the deposit method, the variable balance should be replaced by the variable initialBalance. C. In the deposit method, the variable balance is declared as a local variable and is different from the instance variable balance. D. The method header for the deposit method should be public void deposit(amount). E. The variable balance must be passed to the deposit method.

C. In the deposit method, the variable balance is declared as a local variable and is different from the instance variable balance.

Consider the following class definition. public class Pet { private String name; private int age; public Pet(String str, int a) { name = str; age = a; } public getName() { return name; } } Which choice correctly explains why this class definition fails to compile? A. The class is missing a mutator method. B. The class is missing an accessor method. C. The accessor method is missing a return type. D. The accessor method returns a variable other than an instance variable. E. The instance variables should be designated public instead of private.

C. The accessor method is missing a return type.

Consider the following class definition. public class Gadget { private static int status = 0; public Gadget() { status = 10; } public static void setStatus(int s) { status = s; } } The following code segment appears in a method in a class other than Gadget. Gadget a = new Gadget(); Gadget.setStatus(3); Gadget b = new Gadget(); Which of the following best describes the behavior of the code segment? A. The code segment does not compile because the setStatus method should be called on an object of the class Gadget, not on the class itself. B. The code segment does not compile because the static variable status is not properly initialized. C. The code segment creates two Gadget objects a and b. The class Gadget's static variable status is set to 10, then to 3, and then back to 10. D. The code segment creates two Gadget objects a and b. After executing the code segment, the object a has a status value of 3 and the object b has a status value of 3. E. The code segment creates two Gadget objects a and b. After executing the code segment, the object a has a status value of 3 and the object b has a status value of 10.

C. The code segment creates two Gadget objects a and b. The class Gadget's static variable status is set to 10, then to 3, and then back to 10.

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 Employee class will contain a String attribute for an employee's name and a double attribute for the employee's salary. Which of the following is the most appropriate implementation of the class? A. public class Employee { public String name; public double salary; // constructor and methods not shown } B. public class Employee { public String name; private double salary; // constructor and methods not shown } C. public class Employee { private String name; private double salary; // constructor and methods not shown } D. private class Employee { public String name; public double salary; // constructor and methods not shown } E. private class Employee { private String name; private double salary; // constructor and methods not shown }

C. public class Employee { private String name; private double salary; // constructor and methods 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 */ }

Consider the following definition of the class Student. public class Student { private int grade_level; private String name; private double GPA; public Student (int lvl, String nm, double gr) { grade_level = lvl; name = nm; GPA = gr; } } Which of the following object initializations will compile without error? A. Student max = new Student ("Max", 10, 3.75); B. Student max = new Student (3.75, "Max", 10); C. Student max = new Student (3.75, 10, "Max"); D. Student max = new Student (10, "Max", 3.75); E. Student max = new Student (10, 3.75, "Max");

D. Student max = new Student (10, "Max", 3.75);

Consider the following class declaration. public class Student { private String name; private int age; public Student(String n, int a) { name = n; age = a; } public boolean isOlderThan5() { if (age > 5) { return true; } } } Which of the following best describes the reason this code segment will not compile? A. The return type for the isOlderThan5 method should be void. B. The return type for the isOlderThan5 method should be String. C. The return type for the isOlderThan5 method should be int. D. The isOlderThan5 method is missing a return statement for the case when age is less than or equal to 5. E. The isOlderThan5 method should receive the variable age as a parameter.

D. The isOlderThan5 method is missing a return statement for the case when age is less than or equal to 5.

Consider the following class definition. public class Email { private String username; public Email(String u) { username = u; } public void printThis() { System.out.println(this); } public String toString() { return username + "@example.com"; } } The following code segment appears in a method in another class. Email e = new Email("default"); e.printThis(); What, if anything, is printed as a result of executing the code segment? A. e B. default C. [email protected] D. [email protected] E. Nothing is printed because the class will not compile.

D. [email protected]

Consider the following class definition. public class Person { private String name; /* missing constructor */ } The statement below, which is located in a method in a different class, creates a new Person object with its attribute name initialized to "Washington". Person p = new Person("Washington"); Which of the following can be used to replace /* missing constructor */ so that the object p is correctly created? A. private Person() { name = n; } B. private Person(String n) { name = n; } C. public Person() { name = n; } D. public Person(String n) { name = n; } E. public Person(String name) { String n = name; }

D. public Person(String n) { name = n; }

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

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;

The following class is used to represent shipping containers. Each container can hold a number of units equal to unitsPerContainer. public class UnitsHandler { private static int totalUnits = 0; private static int containers = 0; private static int unitsPerContainer = 0; public UnitsHandler(int containerSize) { unitsPerContainer = containerSize; } public static void update(int c) { containers = c; totalUnits = unitsPerContainer * containers; } } The following code segment appears in a method in a class other than UnitsHandler. Assume that no other code segments have created or modified UnitsHandler objects. UnitsHandler large = new UnitsHandler(100); UnitsHandler.update(8); Which of the following best describes the behavior of the code segment? A. The code segment does not compile, because it is not possible to create the object large from outside the UnitsHandler class. B. The code segment does not compile, because it attempts to change the values of private variables from outside the UnitsHandler class. C. The code segment does not compile, because the update method should be called on the object large instead of on the UnitsHandler class. D. The code segment creates a UnitsHandler object called large and sets the static variable unitsPerContainer to 100. The static variables containers and totalUnits each retain the default value 0. E. The code segment creates a UnitsHandler object called large and sets the static variables unitsPerContainer, containers, and totalUnits to 100, 8, and 800, respectively.

E. The code segment creates a UnitsHandler object called large and sets the static variables unitsPerContainer, containers, and totalUnits to 100, 8, and 800, respectively.

Consider the following class declaration. public class Student { private String firstName; private String lastName; private int age; public Student(String firstName, String lastName, int age) { firstName = firstName; lastName = lastName; age = age; } public String toString() { return firstName + " " + lastName; } } The following code segment appears in a method in a class other than Student. It is intended to create a Student object and then to print the first name and last name associated with that object. Student s = new Student("Priya", "Banerjee", -1); System.out.println(s); Which of the following best explains why the code segment does not work as expected? A. The code segment will not compile because an object cannot be passed as a parameter in a call to println. B. The code segment will not compile because firstName, lastName, and age are names of instance variables and cannot be used as parameter names in the constructor. C. The code segment will not compile because the constructor needs to ensure that age is not negative. D. The code segment will compile, but the instance variables will not be initialized correctly because the variable names firstName, lastName, and age refer to the instance variables inside the constructor. E. The code segment will compile, but the instance variables will not be initialized correctly because the variable names firstName, lastName, and age refer to the local variables inside the constructor.

E. The code segment will compile, but the instance variables will not be initialized correctly because the variable names firstName, lastName, and age refer to the local variables inside the constructor.

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 definition. public class ClassP { private String str; public ClassP(String newStr) { String str = newStr; } } The ClassP constructor is intended to initialize the str instance variable to the value of the formal parameter newStr. Which of the following statements best describes why the ClassP constructor does not work as intended? A. The constructor should have a return type of String. B. The constructor should have a return type of void. C. The instance variable str should be designated public. D. The variable str should be designated public in the constructor. E. The variable str should not be declared as a String in the constructor.

E. The variable str should not be declared as a String in the constructor.

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 class definition. public class Beverage { private int numOunces; private static int numSold = 0; public Beverage(int numOz) { numOunces = numOz; } public static void sell(int n) { /* implementation not shown */ } } Which of the following best describes the sell method's level of access to the numOunces and numSold variables? A. Both numOunces and numSold can be accessed and updated. B. Both numOunces and numSold can be accessed, but only numOunces can be updated. C. Both numOunces and numSold can be accessed, but only numSold can be updated. D. numSold can be accessed but not updated; numOunces cannot be accessed or updated. E. numSold can be accessed and updated; numOunces cannot be accessed or updated.

E. numSold can be accessed and updated; numOunces cannot be accessed or updated.


Set pelajaran terkait

Unit 3 Fundamentals Documenting, Reporting, ....

View Set

Chapter 12 Musculoskeletal Medications

View Set

Orion Series 65 Quick Quiz 18, 19, and 20

View Set

Regulation 4 Partnership Interest 1 of 4

View Set

Physics Review Chapters 27 and 28 Unit Test Wednesday May 6, 2015

View Set

LESSON 4: COLOR VISION DEFICIENCY

View Set