CS unit 2
Consider the following code segment. String str = "AP"; str += "CS " + 1 + 2; System.out.println(str); What is printed as a result of executing the code segment?
APCS 3
Consider the following code segment. Integer num = new Integer(15); int n = num.intValue(); Which of the following statements best describes the type and contents of num and n after the code segment executes?
num is an Integer that contains the value 15, and n is an int that contains the value 15.
Consider the following code segment. String dessert = "pie"; dessert += "straw" + dessert + "berry"; What is the value of dessert after the code segment has been executed?
piestrawpieberry
The code segment below is intended to randomly print one of the values 2, 4, 6, or 8 with equal probability. int val = /* missing code */ ; val *= 2; System.out.print(val); Which of the following can be used to replace /* missing code */ so that the code segment works as intended?
(int) (Math.random() * 4 + 1)
Consider the following code segment. Integer original = new Integer(8); Integer first = new Integer(original.intValue() * 2); Integer second = new Integer(original.intValue() + 2); System.out.println(first.intValue() + " " + second.intValue()); What is printed when the code segment is executed?
16 10
Consider the following code segment. String one = "ABC123"; String two = "C"; String three = "3"; System.out.println(one.indexOf(two)); System.out.println(one.indexOf(three)); System.out.println(two.indexOf(one)); What is printed when the code segment is executed?
2 5 -1
Consider the following method. public double calculate(double x) { return x + 1.5; } The following code segment calls the method calculate in the same class. Double d1 = new Double(7.5); System.out.println(calculate(d1)); What, if anything, is printed when the code segment is executed?
9.0
Consider the following code segment. String s1 = "ABCDEFGHI"; String s2 = s1.substring(6, 7); String s3 = new String("abcdefghi"); String s4 = s3.substring(4, 5); String s5 = s3.substring(2, 3); System.out.print(s2 + s4 + s5); What, if anything, is printed when the code segment is executed?
Gec
The method below is intended to return the area of a square whose side length is s units. The area of a square is its side length times itself. public double squareArea(double s) { return /* missing code */ } Which of the following can be used to replace /* missing code */ so that the method works as intended?
Math.pow(s, 2);
Which of the following code segments can be used to set the value of the string str to "Good morning, sunshine!" ? I. String str = "Good " + "morning," + " sunshine!"; II. String str = "Good"; str += " morning, sunshine!"; III. String str = " morning, "; str = "Good" + str + "sunshine!";
I, II, and III
Which of the following expressions represents x|k−j|, where x, k, and j are properly declared and initialized int variables?
Math.pow(x, Math.abs(k - j));
Consider the following code segment. String word = "September"; String str1 = word.substring(0, 3); String str2 = word.substring(word.length() - 3); System.out.println(str1 + str2); What is printed when the code segment is executed?
Sepber