These are original Marlbridge practice questions, not past exam questions. Mark points are a suggested Marlbridge rubric, not an official scoring guideline. Every code fragment was compiled and run, and the outputs quoted come from those runs.
Question 1 (multiple choice · foundation)
Which line uses autoboxing?
- (A)
int n = Integer.parseInt("5"); - (B)
Integer k = 12; - (C)
int m = k + 1;wherekis anInteger - (D)
String s = "12";
Answer and explanation
Answer: (B). An int value is assigned to an Integer variable, so the compiler boxes it.
- (A)
parseIntreturns anintdirectly; no wrapper is involved. - (C) Here a wrapper is converted to a primitive, which is unboxing, not boxing.
- (D) A
Stringhas no wrapper class involved.
Question 2 (multiple choice · core)
What is printed?
Integer x = 10;
Integer y = 3;
double r = x / y;
System.out.println(r);
- (A) 3
- (B) 3.0
- (C) 3.3333333333333335
- (D) The code does not compile
Answer and explanation
Answer: (B). x and y unbox to int, so x / y is integer division: 3. That int is then widened to the double 3.0, and println shows 3.0 (run and confirmed).
- (A) The variable is a
double, so the output has a decimal point. - (C) Needs a cast before dividing, as in
(double) x / y. - (D) The code compiles; unboxing and widening are automatic.
Question 3 (multiple choice · core)
What is printed?
System.out.println(Integer.parseInt("42") + Integer.parseInt("8"));
- (A) 428
- (B) 50
- (C) 42 8
- (D) A
NumberFormatExceptionis thrown
Answer and explanation
Answer: (B). Both Strings convert to int, and + adds them: 50. For comparison, "42" + "8" joins two Strings into "428" (run and confirmed).
- (A) is the result of joining Strings, not adding numbers.
- (C)
printlnreceives one number and prints one value. - (D) Both Strings are valid whole numbers.
Question 4 (multiple choice · core)
Which declaration does not compile?
- (A)
double v = 7; - (B)
Double w = 7.0; - (C)
Double u = 7; - (D)
Integer t = 7;
Answer and explanation
Answer: (C). The literal 7 is an int, and an int cannot be boxed into a Double. The compiler message was incompatible types: int cannot be converted to Double.
- (A)
intwidens todoubleas an ordinary conversion. - (B) A
doubleboxes toDouble. - (D) An
intboxes toInteger.
Question 5 (multiple choice · core)
What is printed?
Integer n = 5;
Integer m = n;
n++;
System.out.println(n + " " + m);
- (A)
6 6 - (B)
5 5 - (C)
6 5 - (D)
5 6
Answer and explanation
Answer: (C). Integer objects are immutable. n++ unboxes 5, adds 1 and boxes a new Integer 6 into n. m still refers to the original object holding 5.
- (A) would be right only if the object could be changed in place.
- (B) forgets that
n++changesn. - (D) swaps the values.
Question 6 (multiple choice · core)
What happens when this code runs?
Integer z = null;
int w = z;
System.out.println(w);
- (A) It prints 0
- (B) It prints
null - (C) It does not compile
- (D) It throws a
NullPointerException
Answer and explanation
Answer: (D). Unboxing null means asking a missing object for its value, which throws NullPointerException (run and confirmed).
- (A) Java does not substitute a default value.
- (B)
wis anintand cannot holdnull. - (C) The code compiles, because
nullmay be assigned to anInteger.
Question 7 (multiple choice · core)
Which call returns the double 12.5 from the String s, where s is "12.5"?
- (A)
Integer.parseInt(s) - (B)
s.parseDouble() - (C)
Double.parseDouble(s) - (D)
Double(s)
Answer and explanation
Answer: (C). parseDouble is a class method called on Double.
- (A)
parseInt("12.5")throws aNumberFormatException. - (B)
parseDoubleis not an instance method ofString. - (D) This is not a method call at all and does not compile.
Question 8 (multiple choice · stretch)
What is the output?
Integer k = 7;
Double q = 3.0;
System.out.println(k + 2.5);
System.out.println((int) (q * 2));
- (A)
9.5then6 - (B)
9then6 - (C)
9.5then6.0 - (D)
72.5then6
Answer and explanation
Answer: (A). k unboxes to 7, then 7 + 2.5 is the double 9.5. q unboxes to 3.0, q * 2 is 6.0 and the cast to int gives 6. The run printed 9.5 and 6.
- (B) drops the decimals from the first line;
7 + 2.5is adoublesum. - (C) forgets that
(int)removes the decimal part. - (D) treats the
+as String joining; neither operand is a String.
Question 9 (constructed response · core)
Write sumStrings(String[] vals), which returns the sum of the whole numbers held as Strings in vals. For example {"12", "007", "-4"} gives 15. Assume every String is a valid whole number.
Answer and explanation
public static int sumStrings(String[] vals)
{
int sum = 0;
for (String s : vals)
{
sum += Integer.parseInt(s);
}
return sum;
}
For the example the sum is 12 + 7 + (-4) = 15 (run and confirmed).
Suggested Marlbridge rubric (4 points)
| Point | Award for |
|---|---|
| 1 | Correct header returning int and taking String[] |
| 2 | Initialises an accumulator and traverses every element |
| 3 | Converts each String with Integer.parseInt (not + on Strings) |
| 4 | Adds to the accumulator and returns it |
An indexed for loop is equally acceptable.
Question 10 (constructed response · stretch)
(a) Write meanOf(String[] vals), which returns the mean of the decimal numbers held as Strings in vals, or 0.0 if the array is empty.
(b) State the value returned for {"3.5", "4.25", "2"} and explain why Integer.parseInt could not be used.
Answer and explanation
(a)
public static double meanOf(String[] vals)
{
if (vals.length == 0)
{
return 0.0;
}
double total = 0;
for (String s : vals)
{
total += Double.parseDouble(s);
}
return total / vals.length;
}
(b) The total is 3.5 + 4.25 + 2.0 = 9.75 and 9.75 / 3 = 3.25 (run and confirmed). Integer.parseInt("3.5") would throw a NumberFormatException, because an int String may not contain a decimal point.
Suggested Marlbridge rubric (6 points)
| Point | Award for |
|---|---|
| 1 | Returns 0.0 (and does not divide by zero) for an empty array |
| 2 | Accumulates in a double |
| 3 | Traverses every element |
| 4 | Uses Double.parseDouble on each String |
| 5 | Divides by the number of elements and returns a double; the sum must not be an int |
| 6 | Part (b): 3.25 and a correct reason (parseInt rejects a String with a decimal point) |
How did you do?
Questions 1 to 8 are one mark each. If you slipped on boxing rules, re-read the boxing section of the Topic 4.7 study guide; then tick off the checklist.