Progress Check 2B

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

Consider the processWords method. Assume that each of its two parameters is a String of length two or more. public void processWords(String word1, String word2) { String str1 = word1.substring(0, 2); String str2 = word2.substring(word2.length() - 1); String result = str2 + str1; System.out.println(result.indexOf(str2)); } Which of the following best describes the value printed when processWords is called?

The value 0 is always printed.

Consider the following method. What value is returned as a result of the call scramble("compiler", 3)?

"ilercom"

Consider the following code segment. String str = "CompSci"; System.out.println(str.substring(0, 3)); int num = str.length(); What is the value of num when the code segment is executed?

The value of num is 7. In the first code line, str is assigned a string of length 7. In the second code line, the application of the substring method on str does not affect the value of str, so in the third code line, num is assigned the value 7.

Consider the following method, which is intended to calculate and return the expression (x+y)2|a−b|−−−−−√(x+y)2|a−b|. public double calculate(double x, double y, double a, double b) { return /* missing code */; } Which of the following can replace /* missing code */ so that the method works as intended?

Math.pow(x + y, 2) represents the expression (x+y)2(x+y)2; Math.abs(a - b) represents the expression |a−b||a−b|; and Math.sqrt(Math.pow(x + y, 2) / Math.abs(a - b)) represents the expression shown here.

Consider the following code segment. String oldStr = "ABCDEF"; String newStr = oldStr.substring(1, 3) + oldStr.substring(4); System.out.println(newStr); What is printed as a result of executing the code segment?

The call oldStr.substring(1, 3) returns the substring of oldStr starting at index 1 and ending at index 2, or "BC"; the call oldStr.substring(4) returns the substring of oldStr starting at index 4 and going to the end of oldStr, or "EF". Therefore, as the result of the concatenation operation "+", newStr gets assigned the value "BCEF".

Consider the following code segment, which is intended to assign to num a random integer value between min and max, inclusive. Assume that min and max are integer variables and that the value of max is greater than the value of min. double rn = Math.random(); int num = /* missing code */; Which of the following could be used to replace /* missing code */ so that the code segment works as intended?

The expression max - min + 1 represents the number of possible values in the desired range. Multiplying rn by the expression max - min + 1 and casting that product to an int produces a value in the range from 0 to max - min. Adding min to that int value produces a value in the desired range of min to max, inclusive...........(int) (rn * (max - min + 1)) + min

Consider the following code segment. Assume that a is greater than zero. int a = /* value not shown */; int b = a + (int) (Math.random() * a); What best describes the value assigned to b when the code segment is executed?

The random method returns a value between 0.0, inclusive, and 1.0, exclusive. Multiplying that value by a and casting to an int produces a result between 0 and a - 1, inclusive. The sum of a and a value between 0 and a - 1, inclusive, is a value between a and 2 * a - 1, inclusive.

A pair of number cubes is used in a game of chance. Each number cube has six sides, numbered from 1 to 6, inclusive, and there is an equal probability for each of the numbers to appear on the top side (indicating the cube's value) when the number cube is rolled. The following incomplete statement appears in a program that computes the sum of the values produced by rolling two number cubes. int sum = / * missing code * / ; Which of the following replacements for /* missing code */ would best simulate the value produced as a result of rolling two number cubes?

2 + (int) (Math.random() * 6) + (int) (Math.random() * 6)

Consider the following class. public class SomeMethods {public void one(int first) { / * implementation not shown * / } public void one(int first, int second) { / * implementation not shown * / } public void one(int first, String second) { / * implementation not shown * / } } Which of the following methods can be added to the SomeMethods class without causing a compile-time error? 1. public void one(int value){ / * implementation not shown * / } 2. public void one (String first, int second) { / * implementation not shown * / } 3. public void one (int first, int second, int third) { / * implementation not shown * / }

Big Idea And Learning Objective MOD: Modularity MOD-1: Some objects or concepts are so frequently represented that programmers can draw upon existing code that has already been tested, enabling them to write solutions more quickly and with a greater degree of confidence. MOD-1.F.1: A method signature for a method with parameters consists of the method name and the ordered list of parameter types. Methods are said to be overloaded when there are multiple methods with the same name but a different signature. MOD-1.F.3: Methods are said to be overloaded when there are multiple methods with the same name but a different signature. 2 and 3 are the only methods can be added to the SomeMethods class without causing a compile-time error.

Consider the following method. public int timesTwo (int n) { return n * 2; } The following code segment appears in a method in the same class as the timesTwo method. Integer val = 10; int result1 = timesTwo(val); Integer result2 = result1; System.out.print(result2); What, if anything, is printed as a result of executing the code segment?

The Java compiler automatically handles conversions between Integer and int, so the second line of the code segment assigns the value 20 to result1 and the third line assigns that same value to result2.

Which of the following statements assigns a random integer between 1 and 10, inclusive, to rn ?

The Math.random() method returns a double that is greater than or equal to 0 and less than 1. When that value is multiplied by 10, it evaluates to a double that is greater than or equal to 0 and less than 10. Casting this value to an int results in an int value from 0 to 9, inclusive. Adding 1 to that int produces a value in the desired range 1 to 10, inclusive.........int rn = (int) (Math.random() * 10) + 1;

In the code segment below, assume that the int variable n has been properly declared and initialized. The code segment is intended to print a value that is 1 more than twice the value of n. /* missing code */ System.out.print(result); Which of the following can be used to replace /* missing code */ so that the code segment works as intended? 1. int result = 2 * n; result = result + 1; 2. int result = n + 1; result = result * 2; 3. int result = (n + 1) * 2;

The correct answer is 1 only. Statement 1 is correct because the value of result at the end of the code segment is 2 * n + 1, which is the intended value. Statements II and III are incorrect because the value of result at the end of each code segment is (n + 1) * 2, which is different than the intended value 2 * n + 1.

Consider the following code segment. String temp = "comp"; System.out.print(temp.substring(0) + " " + temp.substring(1) + " " + temp.substring(2) + " " + temp.substring(3)); What is printed when the code segment is executed?

The four substring method calls return "comp", "omp", "mp", and "p", respectively. The print statement concatenates those return values, separated by spaces, and the string "comp omp mp p" is printed.

2: Using Objects 2.7: String Methods Skill 5.A: Describe the behavior of a given segment of program code.

VAR-1.E.12: The following String methods and constructors-including what they do and when they are used-are part of the Java Quick Reference- * String(String str) -Constructs a new String object that represents the same sequence of characters as str * int length()-returns the number of characters in a String object * String substring(int from, int to)-returns the substring beginning at index from and ending at index to - 1 * String substring(int from) -returns substring(from, length()) * int indexOf(String str) -returns the index of the first occurrence of str; returns -1 if not found * boolean equals(String other) -returns true if this is equal to other; returns false otherwise * int compareTo(String other) -returns a value < 0 if this is less than other; returns zero if this is equal to other; returns a value > 0 if this is greater than other

Consider the following method, which is intended to return true if at least one of the three strings s1, s2, or s3 contains the substring "art". Otherwise, the method should return false. Which of the following method calls demonstrates that the method does not work as intended?

containsArt ("rattrap", "similar", "today")

Assume that the following variable declarations have been made. double d = Math.random(); double r; What expression assigns a value to r from the uniform distribution over the range 0.5 ≤ r < 5.5 ?

r = d * 5.0 + 0.5;


Ensembles d'études connexes

Chapter 5: The Integumentary System Study Questions

View Set

Language arts 8A- Words and how to use them~ STANDARD AND NONSTANDARD USAGE!!

View Set

Chapter 12 (Pre-quiz) : Lymphatic system

View Set

Read Theory - Novels and Novelists (Grade 12, 1240L)

View Set

Interpersonal Skills and I-Messages

View Set

Chapter 10: Health Inequalities, Environmental Racism, and Environmental Justice

View Set