Login or Create an Account to view question mark schemes (answers), comment, and download a PDF of this test
Question 1[2 Marks]
Given the nested loop below and without using any kind of loop breaking function / command, state the code to include inside the selection statement such that, if and when the variable k is equal to the number 7 or the number 9, both loops will immediately stop iterating.
function escape
i = 0
j = 0
k = 0
loop while i < 4
i = i + 1
k = 2 * k
loop while j <= 3
j = j + 1
k = k + 1
if k = 7 or k = 9 then
// insert code here
end if
end loop
j = 0
end loop
end function
Question 2[16 Marks]
(a).
Using the table below as a guide, trace the following algorithm by filling out the trace table below row by row (left to right).
[4]
INPUT = [2, -2, -5, 7, 0] // A collection
A=0
B=0
C=0
loop while INPUT.hasNext()
A = INPUT.getnext()
if (A => 0) then
B = INPUT.getnext()
end if
C = A+B
output C
end loop
(b).
Explain why binary search is almost always more efficient than linear search on ordered values.
[4](c).
In the context of programming, define a flag.
[1](d).
Given two arrays, ALPHA which contains 10 elements and BETA which contains 20 elements, construct an algorithm to determine whether or not ALPHA is a subset of BETA. That is, if all the items in ALPHA are also in BETA then output “SUBSET” otherwise output “NOT SUBSET”
[7]Question 3[1 Mark]
What is the value of N after the while loop has completed?
N = 7
loop while N > 3
N = N - 3
A = N mod 2
N = N + A + 1
end loop
(a).
1
(b).
3
(c).
5
(d).
The while loop runs infinitely. Therefore N will hold whatever value it happens to have reached when the program runs out of resources and crashes.
Question 4[1 Mark]
A = 7
SUM = 10
loop while A > 2
A = A - 1
SUM = SUM + A
end loop
ouput SUM
Choose the correct output for the pseudocode above.
(a).
30
(b).
20
(c).
31
(d).
21
4 Questions20 MarksShared + Premium
0 Uses12 Views0 Likes