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]?
my answer is -1
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