加载中...
加载中...
Assessment for Unit 3: Class Creation
Select the one best answer for each question.
Questions 1-3 refer to the following Robot class.
1. Which of the following best describes the robotsManufactured variable?
2. Consider the following code segment in a client class. Robot r1 = new Robot("T-800", 75); Robot r2 = new Robot("R2-D2", 50); ```java System.out.println(Robot.getRobotsManufactured()); ``` What is printed as a result of executing this code segment?
3. The practice of declaring modelName and batteryLevel as private is an example of which object-oriented programming concept?
4. Consider the following Gadget class. ```java public class Gadget { private String name; private double price; private boolean onSale; // constructor is missing } ``` A programmer intends to create a Gadget object with the following statement in another class: Gadget myGadget = new Gadget(); Which of the following constructors, if added to the Gadget class, would allow the object creation statement to compile successfully? I. ```java public Gadget() { name = "Default"; price = 9.99; onSale = false; } ``` II. ```java public Gadget(String name, double price) { ``` this.name = name; this.price = price; ```java onSale = false; } ``` III. ```java public void Gadget() { name = "Default"; price = 0.0; onSale = false; } ```
5. Consider the following Book class. ```java public class Book { private String title; private int pages; public Book(String title, int pages) { // Line 1 } public void setTitle(String title) { // Line 2 } } ``` Which of the following replacements for Line 1 and Line 2 correctly initializes the instance variables?
6. Consider the following method from a Game class. ```java public class Game { private int score; // ... constructor not shown public void updateScore(int points) { int bonus = 5; if (points > 10) { points += bonus; } score += points; } } ``` A Game object myGame is created with an initial score of 100. The following call is made: myGame.updateScore(8); What is the value of myGame.score after the call?
Questions 7-8 refer to the following Lamp class and a client code segment.
7. When the modifyLamp method is called, what is true about the parameter myLamp and the argument livingRoomLamp?
8. What is printed when the LightTester class is run?
9. Consider the following Thermometer class. ```java public class Thermometer { private double currentTemp; public Thermometer(double temp) { currentTemp = temp; } public void adjustTemp(double change) { double newTemp = currentTemp + change; // Line X currentTemp = newTemp; } } ``` Which statement best describes the scope of the variable newTemp declared at Line X?
10. A software team is designing a system to manage a library. They decide to create a Book class. Which of the following best represents the data (attributes) and behaviors (methods) for this class, following the principles of data abstraction?
11. Consider the Account class below. ```java public class Account { private String owner; private double balance; public Account(String name) { owner = name; } // other methods not shown } ``` What is the state of an Account object immediately after the following line of code is executed? Account myAcct = new Account("Sam");
12. The following method is intended to calculate and return the area of a rectangle. /* missing method header */ ```java { return length * width; } ``` The method takes two integer parameters, length and width. Which of the following is the correct method header?
13. A programmer designs a class to handle sensitive user data, such as passwords and personal information. To improve system reliability and security, the programmer makes all instance variables private and provides public methods to access or modify them only after performing validation checks. Why is this a good program design practice?
14. Consider the following method. ```java public void mystery(int x) { x = x + 5; } ``` And the following code segment that calls it. ```java int val = 10; ``` mystery(val); ```java System.out.println(val); ``` What is printed when this code segment is executed?
15. Consider the Pen class. ```java public class Pen { private String inkColor; public Pen(String inkColor) { ``` this.inkColor = inkColor; ```java } public boolean isSameColor(Pen other) { return this.inkColor.equals(other.inkColor); } } ``` Which of the following statements about the use of the this keyword in the isSameColor method is true?