Unit 2 Progress Check: MCQ Part B

Ace your homework & exams now with Quizwiz!

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? A. 2 5 -1 B. 2 5 2 C. 2 6 -1 D. 3 6 -1 E. -1 -1 2

A

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? A. 8 8 B. 8 10 C. 10 10 D. 16 10 E. 16 18

D

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? A. CS AP12 B. AP CS3 C. CSAP 12 D. APCS 12 E. APCS 3

D

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? A. 8.0 B. 8.5 C. 9 D. 9.0 E. Nothing is printed because the code does not compile. The actual parameter d1 passed to calculate is a Double, but the formal parameter x is a double.

D

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? A. Math.random() * 4 + 1 B. Math.random() * 8 C. (int) (Math.random() * 4) D. (int) (Math.random() * 4 + 1) E. (int) (Math.random() * 8 + 1)

D

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? A. pow(s, 2); B. s.pow(2); C. Math.pow(s); D. Math.pow(s, 2); E. Math.pow(2, s);

D

Which of the following expressions represents x^|k−j|, where x, k, and j are properly declared and initialized int variables? A. Math.abs(k - j, Math.pow(x)); B. Math.abs(Math.pow(x), k - j); C. Math.pow(x, Math.abs(k - j)); D. Math.pow(Math.abs(k - j), x); E. Math.pow.abs(x, k - j));

C

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? A. strawpieberry B. piestrawpieberry C. strawpieberrypie D. strawberry E. piestrawberry

B

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? A. num is an Integer that contains the value 15, and n is an int that contains the value 0. B. num is an Integer that contains the value 15, and n is an int that contains the value 15. C. num is an Integer that contains the value 15, and n is an Integer that contains the value 15. D. num is an int that contains the value 15, and n is an Integer that contains the value 0. E. num is an int that contains the value 15, and n is an Integer that contains the value 15.

B

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? A. Fdb B. FGdebc C. Gec D. GHefcd E. There is no output due to a compilation error.

C

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!"; A. I only B. II only C. III only D. II and III only E. I, II, and III

E

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? A. epbe B. epber C. Sepbe D. Seper E. Sepber

E


Related study sets

Exploring the Religions of Our World: Chapter 3

View Set