These are original Marlbridge practice questions, not past exam questions. All code is invented for these questions and was compiled and run before publication. The rubrics are a suggested Marlbridge rubric to help you check your work; they are not an official scoring guideline. No calculator is allowed on the exam, so work everything by hand. Code fragments are assumed to be inside a main method.
Question 1 (multiple choice · foundation)
What is the value of x after the following code segment?
int x = 8;
x += 4;
- (A)
4 - (B)
8 - (C)
12 - (D)
32
Answer and explanation
Answer: (C). x += 4; adds 4 to the current value: 8 + 4 = 12.
- (A) subtracts the wrong way round, as if the statement were
x -= 4;. - (B) assumes that the statement had no effect on
x. - (D) multiplies instead of adding.
Question 2 (multiple choice · foundation)
What is the value of x after the following code segment?
int x = 4;
x *= 2 + 3;
- (A)
9 - (B)
11 - (C)
24 - (D)
20
Answer and explanation
Answer: (D). The right side 2 + 3 is evaluated first, giving 5, then 4 * 5 = 20. The statement means x = x * (2 + 3);.
- (A) adds all three numbers.
- (B) reads the statement as
x = x * 2 + 3;, which is (4 * 2) + 3. The right side of a compound assignment is evaluated in full first. - (C) is 4 * 2 * 3: it multiplies by both numbers instead of adding them first.
Question 3 (multiple choice · core)
What is the value of a after the following code segment?
int a = 17;
a %= 5;
a++;
- (A)
2 - (B)
3 - (C)
4 - (D)
18
Answer and explanation
Answer: (B). 17 % 5 is 2 (17 = 3 * 5 + 2), so a is 2. Then a++ adds 1, giving 3.
- (A) forgets the increment.
- (C) uses the quotient 3 instead of the remainder, then adds 1.
- (D) ignores the
%=and applies only the increment to 17.
Question 4 (multiple choice · core)
What is the value of n after the following code segment?
int n = 9;
n /= 2;
n--;
- (A)
3.5 - (B)
4 - (C)
3 - (D)
3.0
Answer and explanation
Answer: (C). n is an int, so n /= 2; is integer division: 9 / 2 is 4. Then n--; subtracts 1, giving 3.
- (A) keeps a fraction, which an
intcannot hold. - (B) forgets the decrement.
- (D) would be a
doublevalue;nis anintand prints as3.
Question 5 (multiple choice · core)
Which statement is equivalent to total -= discount + 1;?
- (A)
total = total - (discount + 1); - (B)
total = total - discount + 1; - (C)
total = (total - discount) + 1; - (D)
total = discount + 1 - total;
Answer and explanation
Answer: (A). A compound assignment evaluates the whole right side first, so discount + 1 is a single value that is subtracted from total.
- (B) and (C) both subtract only
discountand then add 1. The two are the same thing written differently, and neither subtracts the 1. - (D) reverses the order of the subtraction, so it works out
discount + 1minustotal.
Question 6 (multiple choice · core)
What is printed by the following code segment?
double d = 9;
d /= 2;
d += 1;
System.out.println(d);
- (A)
5.5 - (B)
5 - (C)
5.0 - (D)
4.5
Answer and explanation
Answer: (A). d is a double holding 9.0. d /= 2; is real division: 4.5. Then d += 1; gives 5.5.
- (B) assumes integer division and an
intresult. - (C) assumes that the division truncated to 4.0 and then 1 was added.
- (D) forgets the final addition.
Question 7 (tracing · core)
Trace the following code segment and state the final values of p and q.
int p = 12;
int q = 5;
p -= q;
q *= p;
p /= 2;
q %= p + 1;
p++;
q--;
Worked solution
| After statement | p | q | Working |
|---|---|---|---|
int p = 12; |
12 | - | |
int q = 5; |
12 | 5 | |
p -= q; |
7 | 5 | 12 - 5 |
q *= p; |
7 | 35 | 5 * 7 |
p /= 2; |
3 | 35 | integer division 7 / 2 |
q %= p + 1; |
3 | 3 | right side first: 3 + 1 = 4; 35 % 4 = 3 (35 = 8 * 4 + 3) |
p++; |
4 | 3 | |
q--; |
4 | 2 |
The final values are p = 4 and q = 2.
Suggested Marlbridge rubric (5 marks):
| Mark | Awarded for |
|---|---|
| 1 | p is 7 and q is 35 after the first two updates |
| 1 | p is 3 (integer division) |
| 1 | q is 3 (the right side p + 1 evaluated first, then the remainder) |
| 1 | p is 4 |
| 1 | q is 2 |
Question 8 (writing code · core)
A program has stored a duration of 7384 seconds in the int variable s. Using / and %= only (no new calculation methods), write statements that leave the hours in h, the minutes in mi and the leftover seconds in s, and state their values.
Worked solution
int s = 7384;
int h = s / 3600;
s %= 3600;
int mi = s / 60;
s %= 60;
System.out.println(h + " " + mi + " " + s);
7384 / 3600 is 2 (2 * 3600 = 7200), so h is 2 and s %= 3600 leaves 7384 - 7200 = 184. Then 184 / 60 is 3 (180), so mi is 3, and s %= 60 leaves 184 - 180 = 4. The final values are h = 2, mi = 3 and s = 4, so the line printed is 2 3 4. Check: 2 * 3600 + 3 * 60 + 4 = 7384.
Suggested Marlbridge rubric (5 marks):
| Mark | Awarded for |
|---|---|
| 1 | Hours found with s / 3600 (value 2) |
| 1 | s %= 3600; leaves 184, after the hours were taken |
| 1 | Minutes found with s / 60 (value 3) |
| 1 | s %= 60; leaves 4 |
| 1 | Final values 2, 3, 4 (order of statements preserved) |
Question 9 (a quiet conversion · stretch)
A programmer writes the following. The code compiles.
int n = 5;
n *= 1.5;
System.out.println(n);
(a) State what is printed.
(b) Explain why this compiles, although 1.5 is a double, while the statement n = n * 1.5; in the same place would not.
(c) The programmer wanted n to hold 7.5. What should the type of n be?
Worked solution
(a) 7 is printed. The product 5 * 1.5 is 7.5, and it is converted to an int as it is stored, which truncates the fraction.
(b) A compound assignment converts the result of the operation back to the type of the variable on the left, so a double result stored in an int variable is allowed. In the ordinary form n = n * 1.5;, the right side is a double and the compiler rejects storing a double in an int (it reports a possible lossy conversion).
(c) n should be a double, declared as double n = 5;; then n *= 1.5; leaves 7.5.
Suggested Marlbridge rubric (4 marks):
| Mark | Awarded for |
|---|---|
| 1 | Output 7 |
| 1 | Notes that 7.5 is truncated (not rounded) when stored |
| 1 | Explains that the compound form stores the result back in the variable’s type, while the plain form needs a cast |
| 1 | Says n should be a double |
How did you do?
- Questions 1 to 3: re-read “Updating a variable from its own value” in the study guide.
- Questions 2 and 5: these test the idea that the whole right side is evaluated first.
- Questions 4 and 6: the variable’s type decides whether
/=is an integer division. - Questions 7 and 8: practise tracing with a table, one row per statement.
- Question 9: revisit “A detail to notice”.
Then try the checklist.