## dantheman4

1.Which of the expressions below is true if and only if an array A containing three different integers is sorted in ascending order
a. A[1] < A[2] || A[2] < A[3]
b. A[0] < A[1] && A[1] < A[2]
c. A[0] < A[1] || A[1] < A[2]
d. A[1] < A[2] && A[2] < A[3]

my answer is b because of the && and it seems to be going in ascending order.

2.Suppose that A is the array {8, 2, 7, 9, 5}.

Suppose that these two statements are executed:

A[0] = A[4];
A[4] = A[0];

What is now the value of A[4]?

3.

``````public static int f (int[] A) {
int s = 0;
for (int i = 1; i < A.length; i = i+2) {
s = s + A[i];
}
return s;
}
``````

If the array {1, 2, 3, 4, 5, 6, 7} is passed to f, what does it return?
I dont know how to do this

4.

``````private static int g (int[] B) {
for (int i = B.length-1; i >= 0; i = i-1) {
if (B[i] % 3 == 0) {
return B[i];
}
}
return -1;
}
``````

If the array {1, 3, 2, 6, 8} is passed to g, what does it return?
Similar to 4 I dont know how to do this

5.

``````public static int f (int n) {
if (n <= 0) {
return 1;
}
else {
return n * f(n-1);
}
}
``````

What is returned by f(5)?
i think this is 20 because it is 5*(5-1)

6.

``````public static int g (int a, int b) {
if (b == 0) {
return a;
}
else {
return g(b, a%b);
}
}
``````

What is returned by g(15, 6)?
i think is 6/15

commented: dont post your homework here... +0

## NormR1 563

Have you written any test programs and added these code segments to them to see how they execute?

Compile and execute the code to see what the results are.

## jalpesh_007 -3

because assignment operator will assign value.

answer to question 3 is 12.

answer to question 4 is -1

answer to question 5 is 120.

## stultuske 1,092

what I do wonder about, is how you come up with the value '-1' as an answer for question 2. even if you don't know how arrays work (in Java), it should be pretty clear that the only possible values that can be returned, are the values that are stored in the array to begin with.

## Taywin 312

#1 You are correct if and only if the question is not about writing a program. In other words, true && true is the same as (true || false OR false || true) in programming. The question ask which expression is true and that is ambiguous to me.