加载中...
加载中...
Assessment for Unit 1: Using Objects and Methods
Select the one best answer for each question.
Questions 1-3 refer to the following code snippet.
1. What is the value of the expression a / b?
2. What is the value of the expression (double) a / b?
3. Which of the following expressions evaluates to 12.5?
4. A program is being written to track player statistics for a basketball game. Which of the following is the most appropriate data type for a variable that stores the number of points a player has scored?
5. Consider the following code segment. ```java int score = 50; score = score + 10; score = score * 2; ``` What is the value of score after this code segment has been executed?
6. Which of the following code segments is equivalent to the statement balance = balance - withdrawal;?
7. Consider the following code segment. ```java System.out.print("Result: "); System.out.println(5 + 3 * 2); ``` What is printed to the console when this code segment is executed?
8. Consider the expression 17 % 5. What is the value of this expression?
9. A programmer is writing a method to calculate the area of a circle. The method requires that the radius provided as input must be a non-negative number. Which of the following best describes this requirement?
10. What is the value of the expression Math.pow(2, 4)?
11. Consider the following code segment. ```java int x = -15; double y = Math.abs(x); ``` What is the value of y after this code segment is executed?
12. Which of the following statements best describes the relationship between a class and an object in Java?
13. Which of the following is the correct way to create a String object with the value "AP CSA"?
14. Consider the following code segment. ```java String str1 = "Computer"; String str2 = "Science"; String result = str1 + " " + str2; System.out.println(result); ``` What is printed to the console?
15. What is the value of the expression "Hello".length()?
Questions 16-17 refer to the following code snippet.
16. What is the value of message.indexOf("Pro")?
17. What is the value of message.substring(5, 13)?
18. A programmer writes code in a .java file. Before the program can be run, it must be processed by a tool that translates the high-level Java code into a lower-level format called bytecode. What is this tool called?
19. Consider the following code segment. ```java double val = 7.85; int num = (int) val; ``` What is the value of num after this code segment is executed?
20. Consider the following code segment. ```java int k = 10; k++; k *= 2; ``` What is the final value of k?
21. An Application Programming Interface (API) is best described as:
22. Which expression generates a random double value r such that ?
23. Which of the following expressions correctly generates a random integer between 1 and 6, inclusive, to simulate the roll of a standard six-sided die?
24. Consider the following code segment. ```java String s1 = "apple"; String s2 = new String("apple"); String s3 = s1; ``` Which of the following is true after the code executes?
25. What is printed by the following statement? ```java System.out.println("1" + 2 + 3); ```
26. What is printed by the following statement? ```java System.out.println(1 + 2 + "3"); ```
27. Methods that are part of a class itself rather than an instance of the class are known as static methods. How are static methods typically called?
28. Consider a class Robot that has an instance method void moveForward(). Assume bot1 is a properly created Robot object. Which of the following is a valid call to the moveForward method?
29. Consider the following code segment. ```java String city = null; System.out.println(city.length()); ``` What is the result of attempting to compile and run this code?
30. A method signature is used by the compiler to uniquely identify a method. Which of the following are components of a method signature in Java? I. The method name II. The return type III. The types of its parameters in order
31. Consider the following declarations. ```java int numItems = 20; double price = 9.99; boolean inStock = true; ``` Which of the following System.out.println statements will cause a compile-time error?
32. The String class is said to be immutable. What does this mean?
33. Consider the following code segment. ```java String phrase = "change"; ``` phrase.substring(0, 3); ```java System.out.println(phrase); ``` What is printed to the console?
34. Consider the following code segment. ```java int x = 5; int y = 10; x = y; y = x; System.out.println(x + " " + y); ``` What is printed to the console?
35. The int data type in Java has a maximum value, represented by Integer.MAX_VALUE. What happens when the following code is executed? ```java int val = Integer.MAX_VALUE; val = val + 1; ```
36. A Scanner object is used to get input from the user. Which line of code correctly creates a Scanner object named input that reads from the standard keyboard input?
37. What is the value of (int) Math.sqrt(20)?
38. A programmer finds that their code compiles successfully but does not produce the correct output for certain inputs. For example, a program designed to calculate the average of two numbers instead calculates their sum. What type of error is this?
39. Which of the following declares a variable that can hold a reference to an object of the Robot class?
40. Consider the following String method from the Java API: public int indexOf(String str) It returns the index within this string of the first occurrence of the specified substring. What is the value of "mississippi".indexOf("issi")?
41. Consider the following code segment. ```java double a = 5.0; int b = 2; System.out.println(a / b * 2); ``` What is printed to the console?
42. Which of the following statements will result in the value 3.5?
43. Consider the following comment block in a Java program. /* * This method calculates the final price of an item. * It applies a discount and then adds sales tax. */ What type of comment is this?
44. The Integer class contains a static method with the signature public static int parseInt(String s). This method converts a string representation of an integer into an int. Which of the following is a correct call to this method?
45. Consider the following code segment. ```java String s1 = "abc"; String s2 = "def"; String s3 = s1 + s2; s1 = s3; ``` Which of the following best describes what has occurred?