Hey
triviajeon,
Let's read the program together.
Initially, the program starts off setting the default values of W, Y to the first element in the array, and sets the X, Z to 1 (which is the index of the array). Notice that the variable
i was declared and set to 2, which we can assume that this must be quite an optimised sort/search algorithm (because there's no point of comparing element at index 1 to itself as that's a waste of time, so we start off from the next element. This approach will however introduce the problem of index overflow (runtime error) if the list is of length 1 and there is no element at index 2, but let's just say the question is legit).
Then, the program loops with incrementing
i at the end of each loop. Let's see what the loop is doing to our variables.
Lines 8 to 11 compares the List(i) to W, and if the former is greater, it will set the variable W to the value of List(i) and X to the current index, i. As W and X are not referenced again in the loop, we can safely say that, after looping through the entire array, lines 8 to 11 will set the biggest value List(nMax) to variable W and its index in the array to X.
Similarly, Lines 12-15 sets the smallest value List(nMin) to variable Y and its index to Z.
After the loop, the program swaps the value at X and Z, i.e. swaps the min/max value in the list. So, since the max value of the list is 6 (index=3) and min value is 1 (index=2), the value of X is 3 and Y is 2 giving option C.
For Question 16, the correct answer is B. What we are trying to do is to
Swap the elements at index (a, b), not the index itself. Looking at A), it is swapping the values of the a and b, which are the indices (integers), not the element at List(a) and List(b). For option C, it will not work as it has the same issue as A and also it will set both a and b to the value of b. Option D does not work as it will set both the elements at indices a and b to the value at index b, i.e. effectively List(a) = List(b).
Hope it helps. Good luck with your SDD exam tomorrow (and good luck to myself too haha)