加载中...
加载中...
Assessment for Unit 2: Selection and Iteration
Select the one best answer for each question.
Questions 1-3 refer to the following code segment.
1. What is printed as a result of executing the following code? TicketChecker tc = new TicketChecker(17, true); ```java System.out.println(tc.getTicketPrice()); ```
2. What is printed as a result of executing the following code? TicketChecker tc = new TicketChecker(19, true); ```java System.out.println(tc.getTicketPrice()); ```
3. Consider the following modification to the getTicketPrice method. ```java public String getTicketPrice() { String price = "20 dollars"; if (age >= 18) { if (isStudent) { price = "15 dollars"; } } else { price = "12 dollars"; } return price; } ``` Which of the following constructor calls will result in "20 dollars" being returned?
4. Consider the following code segment. ```java int x = 10; int y = 5; boolean result = (x / y == 2) && (x * y > 40); System.out.println(result); ``` What is printed when this code segment is executed?
5. Consider the following expression. (a > b) || (c < d && a == c) Which of the following sets of values for a, b, c, and d would cause the expression to evaluate to true?
6. Which of the following Boolean expressions is equivalent to !(x > y && z <= 10)?
7. Consider the following code segment. ```java int k = 0; int sum = 0; while (k < 5) { sum += k; k++; } System.out.println(sum); ``` What is printed when this code segment is executed?
8. Consider the following code segment. ```java int count = 0; for (int i = 1; i <= 10; i += 2) { count++; } System.out.println(count); ``` What is printed when this code segment is executed?
9. Consider the following code segment. ```java int total = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { total++; } } System.out.println(total); ``` What is printed when this code segment is executed?
10. Consider the following method. ```java public String mystery(String str) { String result = ""; for (int i = 0; i < str.length(); i++) { if (i % 2 == 0) { result += str.substring(i, i + 1); } } return result; } ``` What is returned by the call mystery("computer")?
11. Consider the following method, which is intended to return the number of integers between start and end, inclusive, that are divisible by d. ```java public int countDivisible(int start, int end, int d) { int count = 0; for (int i = start; i <= end; i++) { ``` /* missing code */ ```java } return count; } ``` Which of the following can be used to replace /* missing code */ so that the method works as intended?
12. Consider the following code segment. ```java int n = /* some positive integer */; int count = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { count++; } } ``` How many times will the statement count++; be executed?
13. Consider the following method. ```java public void checkValue(int x) { if (x > 100 || x / 0 == 5) { System.out.println("Success"); } else { System.out.println("Failure"); } } ``` What happens when the call checkValue(200) is executed?
14. Consider the following String objects. ```java String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); ``` Which of the following expressions evaluates to false?
15. Consider the following code segment. ```java int num = 24; while (num > 1) { if (num % 2 == 0) { num = num / 2; } else { num = num + 1; } System.out.print(num + " "); } ``` What is printed to the console?
16. The following code segment is intended to print the sum of the even integers from 2 to 100, inclusive. ```java int sum = 0; for (int i = 2; i <= 100; i += 2) { sum += i; } System.out.println(sum); ``` Which of the following while loops produces the same output? I. ```java int i = 2; int sum = 0; while (i <= 100) { sum += i; i += 2; } ``` II. ```java int i = 2; int sum = 0; while (i < 101) { if (i % 2 == 0) { sum += i; } i++; } ``` III. ```java int i = 0; int sum = 0; while (i <= 100) { i += 2; sum += i; } ```
17. Consider the following code segment. ```java int score = 75; String grade = "F"; if (score >= 60) { grade = "D"; if (score >= 70) { grade = "C"; if (score >= 80) { grade = "B"; if (score >= 90) { grade = "A"; } } } } System.out.println(grade); ``` What is printed when this code segment is executed?
18. Consider the following method. ```java public static String reverse(String str) { String reversed = ""; for (int i = str.length() - 1; i >= 0; i--) { reversed = reversed + str.substring(i, i + 1); } return reversed; } ``` What is returned by the call reverse("flow")?
19. A coffee machine algorithm is described as follows: Check if the user selected "latte". If so, check if they selected "extra shot". If "extra shot" was selected, add espresso. Add steamed milk. Otherwise (if "latte" was not selected), just add hot water. This algorithm demonstrates which two fundamental programming concepts?
20. What is the value of the boolean expression 15 % 4 != 3 || 10 / 4 == 2.5?
21. Consider the following code segment. ```java for (int r = 1; r <= 3; r++) { for (int c = 1; c <= r; c++) { System.out.print("*"); } System.out.println(); } ``` What is the output of this code segment?
22. The following method is intended to find the smallest integer in a sequence of positive integers entered by the user. The user enters -1 to signal the end of the sequence. ```java public int findMin() { ``` Scanner console = new Scanner(System.in); ```java int min = 1000; // Assume inputs are less than 1000 int input = console.nextInt(); while (input != -1) { if (input < min) { min = input; } input = console.nextInt(); } return min; } ``` If the user enters the sequence 30 50 12 45 -1, what value is returned by findMin()?
23. Consider the following while loop. ```java int x = 100; while (x > 0) { System.out.println(x); x = x / 2; } ``` Which of the following for loops is equivalent to this while loop?
Questions 24-25 refer to the following method.
24. Which of the following method calls will return true?
25. A proposed change to the method is to make it more efficient. The loop should stop as soon as a digit is found. Which change accomplishes this?
26. Consider the following code segment. ```java for (int k = 20; k > 5; k--) { k -= 3; System.out.print(k + " "); } ``` What is printed when the code segment is executed?
27. Consider the following code segment. ```java int a = 5; int b = 10; if (a * 2 == b) a = 0; b = 0; ``` What are the final values of a and b after this code is executed?
28. Consider three algorithms that process an input of size n. Algorithm A has a key statement that executes times. Algorithm B has a key statement that executes times. Algorithm C has a key statement that executes times. Which of the following correctly orders the algorithms from most efficient to least efficient for large values of n?
29. Which of the following statements is true about the expressions !(a || b) and !a && !b in Java, where a and b are boolean variables?
30. Consider the following code segment. ```java int val = 1; while (val < 100) { System.out.print(val + " "); val = val * 2; } ``` How many numbers are printed?
31. Consider the following code segment. ```java int temp = 80; boolean isRaining = false; if (temp > 75) { System.out.print("Warm "); if (isRaining) { System.out.print("and Humid"); } } else { System.out.print("Cool "); if (isRaining) { System.out.print("and Damp"); } } ``` What is printed when this code segment is executed?
32. Consider the following method check(): ```java public boolean check(int num, String str) { return str.length() > num && num > 0; } ``` Which of the following calls to check() will return false?
33. Consider the following method countChar, which is intended to count the occurrences of a character c in a string str. ```java public int countChar(String str, String c) { int count = 0; for (int i = 0; i < str.length(); i++) { if (str.substring(i, i + 1).equals(c)) { count++; } } return count; } ``` What is returned by the call countChar("banana", "a")?
34. How many times does the following loop execute its body? ```java for (int i = 10; i >= 1; i--) { // loop body } ```
35. Consider the following code segment. ```java int check = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (i == j) { check++; } } } System.out.println(check); ``` What is printed when this code segment is executed?
36. The following method is intended to return true if num is a prime number and false otherwise. A prime number is a number greater than 1 that has no positive divisors other than 1 and itself. ```java public boolean isPrime(int num) { if (num <= 1) { return false; } for (int i = 2; i < num; i++) { if (num % i == 0) { ``` /* location X */ ```java } } ``` /* location Y */ ```java } ``` Which of the following statements should be placed at /* location X */ and /* location Y */ for the method to work correctly?
37. Which of the following expressions correctly checks if an integer x is an even number between 50 and 100, inclusive?
38. Consider the following code segment. ```java int count = 0; int num = 12345; while (num > 0) { num = num / 10; count++; } System.out.println(count); ``` What is printed when this code segment is executed?
39. Consider the following method. ```java public void process(int val) { if (val < 10) { System.out.print("A"); } else if (val < 20) { System.out.print("B"); } else { System.out.print("C"); } if (val % 2 == 0) { System.out.print("D"); } } ``` What is printed by the call process(15)?