These are original Marlbridge practice questions, not past exam questions. All data is fictional. Mark points are a suggested Marlbridge rubric, not an official scoring guideline. No calculator is allowed. The array used is int[] a = {3, 8, 1, 6}; unless stated, and the class Counter is in the study guide. Every output below was produced by running the code.
Question 1 (multiple choice · foundation)
Which loop header visits every element of the array nums exactly once, from first to last?
- (A)
for (int i = 1; i <= nums.length; i++) - (B)
for (int i = 0; i <= nums.length; i++) - (C)
for (int i = 0; i < nums.length; i++) - (D)
for (int i = 0; i < nums.length - 1; i++)
Answer and explanation
Answer: (C). Indices run from 0 to length - 1, so start at 0 and continue while i < nums.length.
- (A) skips index 0 and tries index
length, which throws an exception. - (B) visits index
length, which does not exist. - (D) stops one element early and misses the last.
Question 2 (multiple choice · core)
int altSum = 0;
for (int i = 0; i < a.length; i += 2)
{
altSum += a[i];
}
System.out.println(altSum);
What is printed?
- (A)
18 - (B)
9 - (C)
4 - (D)
14
Answer and explanation
Answer: (C). i takes the values 0 and 2, so the sum is a[0] + a[2] = 3 + 1 = 4.
- (A) is the sum of all four elements.
- (B) is 8 + 1, which uses indices 1 and 2.
- (D) is 8 + 6, the elements at indices 1 and 3.
Question 3 (multiple choice · core)
for (int x : a)
{
x = x * 2;
}
System.out.println(a[0] + " " + a[1] + " " + a[2] + " " + a[3]);
What is printed?
- (A)
6 16 2 12 - (B)
3 8 1 6 - (C)
0 0 0 0 - (D) The code does not compile.
Answer and explanation
Answer: (B). The loop variable x is a copy of each element, so doubling it does not change the array.
- (A) is what an indexed loop assigning
a[i] = a[i] * 2would produce. - (C) has no basis in the code.
- (D) is false: assigning to the loop variable is legal.
Question 4 (multiple choice · core)
Counter has an instance variable ticks, a method bump() that adds 1 to it, and getTicks().
Counter[] cs = {new Counter(0), new Counter(5), new Counter(9)};
for (Counter c : cs)
{
c.bump();
}
for (Counter c : cs)
{
c = new Counter(100);
}
System.out.println(cs[0].getTicks() + " " + cs[1].getTicks() + " " + cs[2].getTicks());
What is printed?
- (A)
0 5 9 - (B)
100 100 100 - (C)
1 6 10 - (D)
101 101 101
Answer and explanation
Answer: (C). The first loop calls bump() on each object through the copied reference, which changes the objects. The second loop reassigns only the loop variable, so the array still holds the same objects. Output: 1 6 10.
- (A) ignores the first loop’s effect.
- (B) and (D) assume the second loop replaces the stored objects.
Question 5 (multiple choice · core)
int j = 0;
int big = 0;
while (j < a.length)
{
if (a[j] > 5)
{
big++;
}
j++;
}
System.out.println(big);
What is printed?
- (A)
1 - (B)
2 - (C)
3 - (D)
4
Answer and explanation
Answer: (B). The elements greater than 5 are 8 and 6.
- (A) counts only one of them.
- (C) includes the value 3 or 1 wrongly.
- (D) counts every element.
Question 6 (multiple choice · stretch)
Which statement about enhanced for loops is true?
- (A) The loop variable is an index into the array.
- (B) Any enhanced
forloop can be rewritten using an indexedforloop. - (C) An enhanced
forloop can be used to change the values stored in anintarray. - (D) An enhanced
forloop can traverse an array backward.
Answer and explanation
Answer: (B). An enhanced for loop can be rewritten with an indexed for loop (or a while loop).
- (A) is false: the variable holds a copy of an element.
- (C) is false: assigning to the variable does not alter the array.
- (D) is false: it always goes first to last.
Question 7 (constructed response · core)
For int[] d = {5, 3, 8, 1}:
(a) Write an indexed for loop that stores in evenSum the sum of the elements at even indices.
(b) Write the same traversal with a while loop.
(c) State the value of evenSum and explain why an enhanced for loop is a poor fit.
Answer and explanation
(a)
int evenSum = 0;
for (int i = 0; i < d.length; i += 2)
{
evenSum += d[i];
}
(An equivalent loop tests i % 2 == 0 for every index.)
(b)
int evenSum = 0;
int i = 0;
while (i < d.length)
{
evenSum += d[i];
i += 2;
}
(c) evenSum is 5 + 8 = 13 (confirmed by running the loop). An enhanced for loop gives each element but not its position, so it cannot easily select the even indices without keeping a separate counter.
Suggested Marlbridge rubric (6 marks).
| Mark | Requirement |
|---|---|
| 1 | Part (a): evenSum initialised to 0 before the loop |
| 1 | Part (a): correct loop bounds and step (or index test) |
| 1 | Part (a): adds d[i] |
| 1 | Part (b): index initialised, tested with < d.length and updated inside the loop |
| 1 | Part (b): same additions as (a) |
| 1 | Part (c): 13, with the reason about the missing index |
Question 8 (constructed response · core)
Using Counter and Counter[] cs = {new Counter(0), new Counter(5), new Counter(9)};:
(a) State the three tick counts after for (Counter c : cs) { c.bump(); }.
(b) State the three tick counts after the additional loop for (Counter c : cs) { c = new Counter(100); }.
(c) Explain both results using the words reference and copy.
Answer and explanation
(a) 1 6 10.
(b) 1 6 10: unchanged.
(c) In each iteration the loop variable c holds a copy of the reference to an element. Calling bump() through it follows the reference to the same object, so the object’s state changes. Assigning new Counter(100) to c only changes the copy, so the references stored in the array are not altered and the array still holds the original objects.
Suggested Marlbridge rubric (5 marks).
| Mark | Requirement |
|---|---|
| 1 | Part (a): 1 6 10 |
| 1 | Part (b): 1 6 10 |
| 1 | Part (c): the loop variable is a copy of the reference |
| 1 | Part (c): a mutator call changes the shared object |
| 1 | Part (c): assignment to the variable does not change the array’s references |
Question 9 (constructed response · stretch)
For int[] b = {2, 4, 6} each loop has a flaw relative to its stated goal. Describe the effect of each and give a corrected header.
(i) Goal: print every element. for (int i = 0; i <= b.length; i++) { System.out.print(b[i] + " "); }
(ii) Goal: add every element. for (int i = 1; i < b.length; i++) { s += b[i]; }
(iii) Goal: count the elements. for (int i = 0; i < b.length - 1; i++) { count++; }
Answer and explanation
(i) It prints 2 4 6 and then throws ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3, because i reaches b.length. Fix: i < b.length.
(ii) It skips b[0], so s is 10 instead of 12. Fix: start at int i = 0.
(iii) It runs for i = 0 and 1 only, so count is 2 instead of 3. Fix: i < b.length.
The first two effects were confirmed by running the code; (iii) follows from the same arithmetic (for the four-element a the equivalent loop ran 3 times).
Suggested Marlbridge rubric (6 marks). One mark for the correct effect and one for a correct fix, for each of (i), (ii) and (iii).
How did you do?
- Questions 1 to 6 right and the rubrics mostly met: move on to the Implementing Array Algorithms study guide.
- Missed Questions 1, 2 or 5: reread “The indexed for loop” and “The while loop” in the study guide.
- Missed Questions 3, 4, 6 or 8: reread “The enhanced for loop” and Figure 1.
- Missed Questions 7 or 9: reread Worked examples 1 and 2, then tick the topic checklist.