chapter 8 part 2

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

88) Which operator should you use to test whether an object reference is null? a) == b) >= c) <= d) =

a) ==

56) Consider the following code snippet: public class Vehicle { private String type; public String Vehicle(String type) { . . . } } What is wrong with this code? a) The class instance variable type must be initialized when it is declared. b) The constructor must not have any arguments. c) The constructor must not have a return type declared. d) The constructor's return type must be void.

) The constructor must not have a return type declared.

69) Consider the following code snippet: public class Vehicle { private String model; private double rpm; . . . } Which of the following methods would correctly remove the values of the class instance variables? a) public void reset() { model = null; rpm = 0; } b) public void reset() { model.clear(); rpm = 0; } c) public void reset(String aModel, double theRpm) { model = aModel; rpm = theRpm; } d) public void reset("",0) { }

a) public void reset() { model = null; rpm = 0; }

90) Consider the following code snippet: Coin coin1 = null; System.out.println("Value of coin = " + coin1.getValue()); What is wrong with this code? a) coin1 does not refer to an object and will result in an error at run time. b) You cannot concatenate a numeric value with a string value. c) The value variable in coin1 has not been set, and will print as 0. d) There is nothing wrong with this code.

a) coin1 does not refer to an object and will result in an error at run time

84) Insert the missing code in the following code fragment. This fragment is intended to implement a method to increase the speed of the motor by one step if possible. public class Motor { public static final STOPPED = 0; public static final SLOW = 1; public static final MEDIUM = 2; public static final FAST = 3; private int motorSpeed; . . . public void increaseMotorSpeed() { if(motorSpeed < FAST) { ________ } } } a) motorSpeed ++; b) motorSpeed = SLOW; c) motorSpeed = MEDIUM; d) motorSpeed = FAST;

a) motorSpeed ++;

101) You have created an Employee class. You wish to have a unique, sequential employee number assigned to each new Employee object that is created. Which of the following declarations, if added to the Employee class, would allow you to determine which number was last assigned when you are creating a new Employee object? a) private static int lastAssignedEmpNum = 500; b) public int lastAssignedEmpNum = 500; c) private int lastAssignedEmpNum = 500; d) public static int lastAssignedEmpNum = 500;

a) private static int lastAssignedEmpNum = 500;

100) You have created a Student class. You wish to have a unique, sequential student number assigned to each new Student object that is created. Which of the following declarations, if added to the Student class, would allow you to determine which number was last assigned when you are creating a new Student object? a) private static int lastAssignedStudentNum = 1500; b) public int lastAssignedStudentNum = 1500; c) private int lastAssignedStudentNum = 1500; d) public static int lastAssignedStudentNum = 1500;

a) private static int lastAssignedStudentNum = 1500;

59) You are creating a class named Employee. Which statement correctly declares a constructor for this class? a) public Employee() b) public void Employee() c) void Employee() d) new Employee()

a) public Employee()

62) Complete the following code snippet to create a constructor that will initialize the class instance variables shown using data provided by the program which is creating an object from this class. public class Employee { private String empID; private boolean hourly; _______________________ { . . . } } Which of the following statements can be used to create an object of type Employee? a) public Employee(String employeeID, boolean isHourly) b) public Employee(employeeID, isHourly) c) public Employee(empID, Hourly) d) public Employee()

a) public Employee(String employeeID, boolean isHourly)

58) You are creating a class named Vessel. Which statement correctly declares a constructor for this class? a) public Vessel() b) public void Vessel() c) void Vessel() d) new Vessel()

a) public Vessel()

80) Insert the missing code in the following code fragment. This fragment is intended to implement a method to assign a value to an instance variable. public class Vehicle { private String model; private double rpm; . . . _______ { model = modelName; } } a) public void setModel(String modelName) b) public void getModel() c) public String getModel() d) public String setModel(String modelName)

a) public void setModel(String modelName)

75) You have created a Coin class and a Purse class. Which of the following methods will correctly add a Coin object to an array list named coins in the Purse class? a) public void add(Coin aCoin) { coins.add(new Coin(aCoin)); } b) public void add(Coin aCoin) { coins.add(aCoin); } c) public void add(Coin aCoin) { coins.addItem(new Coin(aCoin)); } d) public void add(Coin aCoin) { coins.addItem(aCoin); }

b) public void add(Coin aCoin) { coins.add(aCoin); }

77) You have created a Fruit class and a ShoppingCart class. Which of the following methods will correctly add a Fruit object to an array list named fruits in the ShoppingCart class? a) public void add(Fruit aFruit) { fruits.add(new Fruit(aFruit)); } b) public void add(Fruit aFruit) { fruits.add(aFruit); } c) public void add(Fruit aFruit) { fruits.addItem(new Fruit(aFruit)); } d) public void add(Fruit aFruit) { fruits.addItem(aFruit); }

b) public void add(Fruit aFruit) { fruits.add(aFruit); }

71) Consider the following code snippet: public class Course { private int studentCount; . . . } Which of the following methods would correctly keep track of the total number of enrollees in a course? a) public void addEnrollees(int newEnrollees) { studentCount = newEnrollees } b) public void addEnrollees(int newEnrollees) { studentCount = studentCount + newEnrollees } c) public void addEnrollees(int newEnrollees) { newEnrollees = studentCount } d) public void addEnrollees(int newEnrollees) { newEnrollees = newEnrollees + studentCount }

b) public void addEnrollees(int newEnrollees) { studentCount = studentCount + newEnrollees }

70) Consider the following code snippet: public class Transaction { private int transactionCount; . . . } Which of the following methods would correctly keep track of the total number of transactions? a) public void addTransactions(int newTransactions) { transactionCount = newTransactions } b) public void addTransactions(int newTransactions) { transactionCount = transactionCount + newTransactions } c) public void addTransactions(int newTransactions) { newTransactions = transactionCount } d) public void addTransactions(int newTransactions) { newTransactions = newTransactions + transactionCount }

b) public void addTransactions(int newTransactions) { transactionCount = transactionCount + newTransactions }

73) Consider the following code snippet: public class AutoRace { private int lapCount; private double totalTime; . . . } Which of the following completeLap methods would correctly track how many laps occurred? a) public void completeLap(double lapTime) { totalTime = totalTime + lapTime; } b) public void completeLap(double lapTime) { totalTime = totalTime + lapTime; lapCount ++; } c) public void completeLap(double lapTime) { totalTime = totalTime + lapTime; lapCount = lapCount + lapTime; } d) public void completeLap(double lapTime) { totalTime = lapCount + lapTime; }

b) public void completeLap(double lapTime) { totalTime = totalTime + lapTime; lapCount ++; }

72) Consider the following code snippet: public class BankAccount { private int transactionCount; private double balance; . . . } Which of the following deposit methods would correctly track how many deposits occurred? a) public void deposit(double amount) { balance = balance + amount; } b) public void deposit(double amount) { balance = balance + amount; transactionCount ++; } c) public void deposit(double amount) { balance = balance + amount; transactionCount = transactionCount + amount; } d) public void deposit(double amount) { balance = transactionCount + amount; }

b) public void deposit(double amount) { balance = balance + amount; transactionCount ++; }

99) Consider the following code snippet: public class Student { private static int lastAssignedStudentNum = 1500; { . . . } } Which of the following statements about this code is correct? a) Each object created from this class will have its own lastAssignedStudentNum variable. b) All objects created from this class will share the lastAssignedStudentNum variable. c) The lastAssignedStudentNum variable's value cannot be changed. d) You cannot assign a value to a static variable.

b) All objects created from this class will share the lastAssignedStudentNum variable

54) If you do not provide a constructor in a class, which of the following is correct? a) The compiler will generate a constructor with arguments for each class instance variable and will initialize all of the class instance variables. b) The compiler will generate a constructor with no arguments. c) The compiler will generate a constructor with arguments for each class instance variable. d) The class code will not compile.

b) The compiler will generate a constructor with no arguments.

97) Which statement illustrates the invocation of a static method? a) mySavings.deposit(100); b) double s = Math.sqrt(100); c) deposit(100).mySavings; d) double s = 100.sqrt();

b) double s = Math.sqrt(100);

83) Insert the missing code in the following code fragment. This fragment is intended to implement a method to set the state of the object. public class Motor { public static final STOPPED = 0; public static final PAUSED = 1; public static final RUNNING = 2; private int motorState; . . . public void stopMotor() { ________ } } a) motorState++; b) motorState = STOPPED; c) motorState = PAUSED; d) motorState = RUNNING;

b) motorState = STOPPED;

96) What type of method does NOT operate on an object? a) private b) static c) final d) instance

b) static

94) Insert the missing code in the following code fragment. This fragment is intended to initialize an instance variable. public class Motor { int motorSpeed; public Motor(int speed) { motorSpeed = speed; } public Motor() { _________; } } Which of the following lines of code will allow the second constructor to call the first constructor? a) this.motorspeed(0); b) this(0); c) this(speed); d) You cannot call a constructor from within another constructor.

b) this(0);

95) Insert the missing code in the following code fragment. This fragment is intended to initialize an instance variable. public class Vehicle { int numAxles; public Vehicle(int axles) { numAxles = axles; } public Vehicle() { _________; } } Which of the following lines of code will allow the second constructor to call the first constructor? a) this. numAxles(0); b) this(0); c) this(axles); d) this(numAxles);

b) this(0);

66) Consider the following class: public class Auto { private String make; private String model; private String year; public Auto(String aMake, String aModel, String aYear) { make = aMake; model = aModel; year = aYear; } public String getInfo() { return year + " " + make + " " + model; } } Which of the following code snippets will correctly create an object of this class and display its information? a) myAuto = new Auto(); System.out.println(myAuto.getInfo()); b) Auto myAuto = new Auto(); System.out.println(myAuto.getInfo()); c) Auto myAuto = new Auto("Ford", "Focus", "2011"); System.out.println(myAuto.getInfo()); d) Auto myAuto = new Auto("2011", "Ford", "Focus"); System.out.println(myAuto.getInfo());

c) Auto myAuto = new Auto("Ford", "Focus", "2011"); System.out.println(myAuto.getInfo());

98) Consider the following code snippet: public static class Triangle { public static double getTriangleArea(double heightLen, double baseLen) { . . . } } You are writing a program that needs to use this method. Which of the following statements correctly calls this method? a) Triangle myTriangle = new Triangle(); double area = myTriangle.getTriangleArea(4.0,7.0); b) double area = new Triangle().getTriangleArea(4.0,7.0); c) double area = Triangle.getTriangleArea(4.0, 7.0); d) This method cannot be used outside of the Triangle class.

c) double area = Triangle.getTriangleArea(4.0, 7.0);

74) You have created a Coin class and a Purse class. Which of the following will correctly allow the Purse class to collect Coin objects? a) public Purse { private Coin ArrayList coins; } b) public Purse { private ArrayList coins; } c) public Purse { private ArrayList<Coin> coins; } d) public Purse { public ArrayList<Coin> coins; }

c) public Purse { private ArrayList<Coin> coins; }

76) You have created a ShoppingCart class and a Fruit class. Which of the following will correctly declare an array list of Fruit objects in the ShoppingCart class? a) public ShoppingCart { private Fruit ArrayList fruits; } b) public ShoppingCart { private ArrayList fruits; } c) public ShoppingCart { private ArrayList<Fruit> fruits; } d) public ShoppingCart { public ArrayList<Fruit> fruits; }

c) public ShoppingCart { private ArrayList<Fruit> fruits; }

82) Which of the following statements about class properties is correct? a) A setter method is used to retrieve the value of a class property. b) A getter method is used to assign the value of a class property. c) A setter method is used to assign the value of a class property. d) You cannot assign values to class properties.

c) A setter method is used to assign the value of a class property.

65) Which of the following statements about testing a class is correct? a) A unit test verifies that only one method within a class works correctly. b) A unit test verifies that a class works correctly when it is in a complete program. c) A unit test verifies that a class works correctly in isolation, outside a complete program. d) A unit test does not need to know what results are expected.

c) A unit test verifies that a class works correctly in isolation, outside a complete program.

61) Consider the following code snippet: public class Employee { private String empID; private boolean hourly; public Employee(String employeeID, boolean isHourly) { . . . } } Which of the following statements can be used to create an object of type Employee? a) Employee anAuto = new Employee(); b) Employee anAuto = new Employee(10548, true); c) Employee anAuto = new Employee("10548", true); d) Employee anAuto = new Employee(10548, "true");

c) Employee anAuto = new Employee("10548", true);

57) Consider the following code snippet: public class Vessel { private String type; public String Vessel(String type) { . . . } } What is wrong with this code? a) The class instance variable type must be initialized when it is declared. b) The constructor must not have any arguments. c) The constructor must not have a return type declared. d) The constructor's return type must be void.

c) The constructor must not have a return type declared.

87) Given the following class: public class Coin { private String coinName; private double coinValue; . . . } Consider the following code snippet that utilizes the Coin class: Coin coin1 = new Coin("dime", 0.10); Coin coin2 = coin1; coin2.setValue(0.15); Which of the following statements is correct? a) coin1 has a value of 0.10 and coin2 has a value of 0.15. b) coin1 and coin2 both have the value of 0.10. c) coin1 and coin2 both have the value of 0.15. d) coin2 does not have any data in the coinName variable

c) coin1 and coin2 both have the value of 0.15.

89) Which choice indicates that a string variable refers to no string? a) nil b) "" c) null d) empty

c) null

79) Insert the missing code in the following code fragment. This fragment is intended to implement a method to get the value stored in an instance variable. public class Employee { private String empID; private boolean hourly; . . . _______ { return hourly; } } a) public void setHourly(String isHourly) b) public void getHourly() c) public boolean getHourly() d) public boolean setHourly(boolean isHourly

c) public boolean getHourly()

93) Insert the missing code in the following code fragment. This fragment is intended to initialize an instance variable. public class Employee { private String empName; . . . public Employee(String empName) { ________ } } a) Employee.empName = empName; b) empName = this.empName; c) this.empName = empName; d) The constructor parameter variable cannot have the same name as an instance variable.

c) this.empName = empName;

92) Insert the missing code in the following code fragment. This fragment is intended to initialize an instance variable. public class Motor { private int motorSpeed; . . . public Motor(int motorSpeed) { ________ } } a) Motor.motorSpeed = motorSpeed; b) motorSpeed = this.motorSpeed; c) this.motorSpeed = motorSpeed; d) The constructor parameter variable cannot have the same name as the instance variable.

c) this.motorSpeed = motorSpeed;

67) Consider the following class: public class Auto { private String make; private String model; private String year; public Auto(String aMake, String aModel, String aYear) { make = aMake; model = aModel; year = aYear; } public String calcMileage(double milesDriven, double gasUsed) { double milage = milesDriven/gasUsed; return mileage.toString(); } } Which of the following code snippets will correctly create an object of type Auto and display its mileage? a) myAuto = new Auto(); System.out.println(myAuto.calcMileage(246.0, 15.8)); b) Auto myAuto = new Auto(); System.out.println(myAuto.calcMileage(246.0, 15.8)); c) Auto myAuto = new Auto("2011", "Ford", "Focus"); System.out.println(myAuto.calcMileage(246.0, 15.8)); d) Auto myAuto = new Auto("Ford", "Focus", "2011"); System.out.println(myAuto.calcMileage(246.0, 15.8));

d) Auto myAuto = new Auto("Ford", "Focus", "2011"); System.out.println(myAuto.calcMileage(246.0, 15.8));

51) Which of the following statements about constructors is correct? a) A class can have only one constructor. b) A constructor should always return a value. c) A class must have at least one constructor supplied by the programmer. d) A constructor must have the same name as the class.

d) A constructor must have the same name as the class.

63) Consider the following code snippet: public class Vehicle { private String type; private int numAxles; public void Vehicle(String vehicleType, int VehicleAxles) { . . . } } What is wrong with this code? a) There must be a default constructor with no arguments. b) The class instance variables must be initialized in the declaration statements. c) The constructor declaration must have a return type of String. d) The constructor declaration must not have any return type.

d) The constructor declaration must not have any return type.

53) Consider the following code snippet: public class Coin { private String coinName; private int coinValue; public Coin() { } . . . } Which statement reflects the action performed when the constructor to Coin is called? a) The variable coinName will be initialized to "" and the variable coinValue will be initialized to 0. b) The variable coinName will be initialized to "null" and the variable coinValue will be initialized to 0. c) The variable coinName will not be initialized but the variable coinValue will be initialized to 0. d) The variable coinName will be initialized to null and the variable coinValue will be initialized to 0.

d) The variable coinName will be initialized to null and the variable coinValue will be initialized to 0.

55) Consider the following code snippet: public class Employee { private String empID; private boolean hourly; public Employee() { } . . . } Which statement reflects the action performed when the constructor to Employee is called? a) The variable empID will be initialized to "" and the variable hourly will be initialized to false. b) The variable empID will be initialized to "null" and the variable hourly will be initialized to true. c) The variable empID will not be initialized but the variable hourly will be initialized to false. d) The variable empID will be initialized to null and the variable hourly will be initialized to false.

d) The variable empID will be initialized to null and the variable hourly will be initialized to false.

52) Consider the following code snippet: public class Vehicle { private String type; private int numAxles; public Vehicle() { } . . . } Which statement reflects the action performed when the constructor to Vehicle is called? a) The variable type will be initialized to "" and the variable numAxles will be initialized to 0. b) The variable type will be initialized to "null" and the variable numAxles will be initialized to 0. c) The variable type will not be initialized but the variable numAxles will be initialized to 0. d) The variable type will be initialized to null and the variable numAxles will be initialized to 0.

d) The variable type will be initialized to null and the variable numAxles will be initialized to 0.

64) Consider the following code snippet: public class Vehicle { private String type; private int numAxles; public Vehicle(String vehicleType, int VehicleAxles) { . . . } public Vehicle(String vehicleType) { . . . } } What is wrong with this code? a) There must be a default constructor with no arguments. b) A class cannot have more than one constructor. c) If a class has more than one constructor, each one should have a unique name. d) There is nothing wrong with this code.

d) There is nothing wrong with this code.

60) Consider the following code snippet: public class Vehicle { private String type; private int numAxles; public Vehicle(String vehicleType, int VehicleAxles) { . . . } } Which of the following statements can be used to create an object of type Vehicle? a) Vehicle anAuto = new Vehicle(); b) Vehicle anAuto = new Vehicle(2, "SUV"); c) Vehicle anAuto = new Vehicle("SUV", "2"); d) Vehicle anAuto = new Vehicle("SUV", 2);

d) Vehicle anAuto = new Vehicle("SUV", 2);

68) Which of the following statements using data values in a class is NOT correct? a) You can use an instance variable's data value in an instance method. b) You can use the argument of an instance method in that method. c) You can use a global constant's value in an instance method. d) You can use the argument of any instance method in any other method.

d) You can use the argument of any instance method in any other method.

86) Consider the following code snippet: Coin coin1 = new Coin("dime", 0.10); Coin coin2 = new Coin("dime", 0.10); Which of the following statements is correct? a) coin1 and coin2 contain references to the same object. b) coin1 and coin2 refer to the same object. c) coin1 and coin2 contain the same memory location. d) coin1 and coin2 each have their own set of instance variables.

d) coin1 and coin2 each have their own set of instance variables

85) Consider the following code snippet: Coin coin1 = new Coin("dime", 0.10); Coin coin2 = coin1; Which of the following statements is NOT correct? a) coin1 and coin2 are object references. b) coin1 and coin2 refer to the same object. c) coin1 and coin2 contain the same memory location. d) coin1 and coin2 each have their own set of instance variables.

d) coin1 and coin2 each have their own set of instance variables.

78) Insert the missing code in the following code fragment. This fragment is intended to implement a method to get the value stored in an instance variable. public class Vehicle { private String model; private double rpm; . . . _______ { return model; } } a) public void setModel(String modelName) b) public void getModel() c) public String getModel() d) public String setModel(String modelName)

d) public String setModel(String modelName)

81) Insert the missing code in the following code fragment. This fragment is intended to implement a method to set the value stored in an instance variable. public class Employee { private String empID; private boolean hourly; . . . _______ { hourly = isHourly; } } a) public void setHourly(String isHourly) b) public void getHourly() c) public boolean getHourly() d) public boolean setHourly(boolean isHourly)

d) public boolean setHourly(boolean isHourly)

91) The this reference refers to ____. a) the explicit parameter b) the method that is running c) the constructor of the object d) the implicit parameter

d) the implicit parameter


Ensembles d'études connexes

Nelson Mandela, South Africa, and Apartheid

View Set

Human Biology Lecture 1: What is Life, Ch. 1

View Set

IS 242 Chapter 7: Sampling and Sampling Distributions

View Set

politics and the english language test (woods)

View Set