CS Test

¡Supera tus tareas y exámenes ahora con Quizwiz!

Which of the following can be used to replace / missing code / so that the code segment works as intended?

I only x = y; y = temp; int onesDigit = num % 10; int tensDigit = num / 10;

Consider the following code segment, which is intended to find the average of two positive integers, x and y. int x; int y; int sum = x + y; double average = (double) (sum / 2); Which of the following best describes the error, if any, in the code segment?

In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be less than the expected result for odd values of sum.

Which of the following changes to SomeClass will allow other classes to access but not modify the value of myC?

Include the method: public int getC ( ) { return myC; }

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

"ilercom"

int a = 3 + 2 * 3; int b = 4 + 3 / 2 int c = 7 % 4 + 3 What is the value of d after the code segment is executed?

20.0

System.out.println(404/10*10+1)

401

The following declaration appears in another class.SomeClass obj = new SomeClass ( );Which of the following code segments will compile without error? A int x = obj.getA ( ); B int x; obj.getA (x); C int x = obj.myA; D int x = SomeClass.getA ( ); E int x = getA(obj);

A - int x = obj.getA ( );

Consider the following code segment. int one = 1; int two = 2; String zee = "Z"; System.out.println(one + two + zee); What is printed as a result of executing the code segment?

B - 3Z

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?

C - BCEF

Which of the following changes should be made so that the class definition compiles without error In the party class below, the addPeople method is intended to increase the value of the instance variable numOfPeople

Replace line ___ with public void addPeople(int additionalPeople)

Consider the following code segment. int w = 1; int x = w / 2; double y = 3; int z = (int) (x + y); Which of the following best describes the results of compiling the code segment?

The code segment compiles without error.

The following code segment is intended to round val to the nearest integer and print the result. double val = -0.7; int roundedVal = (int) (val + 0.5); System.out.println(roundedVal); Which of the following best describes the behavior of the code segment?

The code segment does not work as intended because the expression (int) (val + 0.5) rounds to the nearest integer only when val is positive.

Consider the following code segment, where k and count are properly declared and initialized int variables. k++; k++; count++; k--; count++; k--; Which of the following best describes the behavior of the code segment?

The code segment leaves k unchanged and increases count by 2.

Which of the following code segments, if located in a method in a class other than Thing, will cause the message "Hello my friend" to be printed?

D Thing a = new Thing(); a.greet();

Which of the following expressions evaluate to 7 ? 9 + 10 % 12 (9 + 10) % 12 9 - 2 % 12 A I only B II only C I and III D II and III E I, II, and III

D - II and III

What is printed as a result of the call fido.act()? a. run eat b. run eat sleep c. run eat sleep bark d. run eat bark sleep e. Nothing is printed due to infinite recursion

D - run eat bark sleep

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?

E - 7

A code segment (not shown) is intended to determine the number of players whose average score in a game exceeds 0.5. A player's average score is stored in avgScore, and the number of players who meet the criterion is stored in the variable count. Which of the following pairs of declarations is most appropriate for the code segment described?

double avgScore; int count;

Which of the following statements stores the value 3 in x ?

int x = 8 % 5;


Conjuntos de estudio relacionados

PrepU Chp 28: Assessment of Hematologic Function and Treatment Modalities

View Set

Origins and Insertions (Abductor Pollicis Longus)

View Set

Business Management II - VB Management Reading

View Set