Question: 7. Refer to the following code: private int[] arr; //precondition: arr.length > 0 public void mystery() { int s1 = 0; int s2 = 0;

7. Refer to the following code:

private int[] arr;

//precondition: arr.length > 0 public void mystery() { int s1 = 0; int s2 = 0;

for (int k = 0; k < arr.length; k++) { int num = arr[k]; if ((num > 0) && (num % 2 == 0)) s1 += num; else if (num < 0) s2 += num; } System.out.println (s1); System.out.println (s2);

Which of the following best describes the value of s1 output by the method mystery?

Question 7 options:

The sum of all values greater than 2 in arr

The sum of all positive values in arr

the sum of all positive odd values in arr

The sum of all positive even values in arr

The sum of all values less than 2 in arr

8. Consider the static method selectSort shown below. Method selectSort is intended to sort an array into increasing order; however, it does not always work as intended.

//precondition: numbers.length >0 //postcondition: numbers is sorted in increasing order public static void selectSort(int[] numbers) { int temp; Line1: for (int j = 0; j < numbers.length - 1; j++) { Line 2: int pos = 0; Line 3: for (int k = j + 1; k < numbers.length; k++) { Line 4: if (numbers[k] < numbers [pos]) { Line 5: pos = k; } } temp = numbers[j]; numbers[j] = numbers[pos]; numbers[pos] = temp; } }

Which of the following changes should be made so that selectSort will work as intended?

Question 8 options:

Line 2 should be changed to

int pos = j;

Line 5 should be changed to

k = pos;

Line 4 should be changed to

if (numbers[k] > numbers[pos])

Line 1 should be changed to

for (int j = 0; j < numbers.length - 2; j++)

Line 3 should be changed to

for (int k = 0; k < numbers.length; k++)

9. Consider the following declaration.

int valueOne, valueTwo;

Assume that valueOne and valueTwo have been initialized. Which of the following evaluates to true if valueOne and valueTwo contain the same value?

Question 9 options:

valueOne == valueTwo

valueOne.compareTo((Object) valueTwo) == 0

valueOne.equals(valueTwo)

valueOne.compareTo(valueTwo) == 0

valueOne.equals((Object) valueTwo)

10. When designing classes, which of the following would be the best reason to use inheritance?

Question 10 options:

Inheritance allows the creation of a subclass that can use the methods of its superclass without rewriting the code for those methods.

Inheritance reduces the number of polymorphic structures encapsulated in applications.

Inheritance guarantees that the applications will compile and execute much more quickly.

Inheritance allows for data encapsulation, while noninherited classes do not allow for data encapsulation.

Inheritance allows you to write applications that require fewer base and super classes.

11. Assume that the methods f and g are defined as follows.

public int f(int x) { if (x <= 0) { return 0; } else { return g (x - 1); } }

public int g(int x) { if (x <=0 ) { return 0; } else { return (f(x - 1) + x); } }

What value is returned as a result to the call f(6)?

Question 11 options:

9

0

12

6

3

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!