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

6
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by JamesCherrill

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.

because assignment operator will assign value.

answer to question 3 is 12.

answer to question 4 is -1

answer to question 5 is 120.

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.

#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.

#3 and #4 you need to read on how a loop is being processed.

#5 you need to understand #3 and #4 first, and then you need to look on recursion as well. Your answer is wrong by the way.

#6 you need to understand #5 and also know what modulo (%) symbol does in mathematics. Your answer is also wrong.

Edited by Taywin

I don't see any ambiguity in Q1
The two || cases don't fit the "if and only if" requirement because there are arrays for which they will evaluate true even when all the numbers are not be in ascending order (just two ascending will be sufficient).
Case d is just a trap based on array indexing from 1 not 0
Only b is true "if and only if" A is sorted in ascending order

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.