Use this diagnostic to find which Unit 1 topics to revisit: one short question per topic, in the order of the topics.
These are original Marlbridge practice questions, not past exam questions. The diagnostic is not calibrated and gives no predicted score. Where a question uses a class, its code is shown just below.
How to take it. Allow 40 minutes; answer everything before opening any answer. No calculator is allowed on the exam, so work by hand, and write down trace tables. Code fragments are assumed to be inside a main method. Two classes are used in Questions 12 to 14.
class Counter {
private int count;
public Counter() {
count = 0;
}
public void increment() {
count++;
}
public int getCount() {
return count;
}
}
class Cup {
private int ml;
public Cup() {
ml = 250;
}
public Cup(int amount) {
ml = amount;
}
public int getMl() {
return ml;
}
}
Question 1 (multiple choice · 1.1)
A program compiles and runs to the end, with no error message, but it displays 120 when the correct answer is 80. Which kind of error is this?
- (A) A syntax error
- (B) A run-time error
- (C) A compile-time error
- (D) A logic error
Answer and explanation
Answer: (D). The program behaves incorrectly without stopping: a logic error, found by testing with data whose answer is known.
- (A) and (C) would stop the program being compiled, so it would not run at all.
- (B) would stop or disturb the program while it runs, usually with an exception message.
If you missed this: Topic 1.1 study guide.
Question 2 (multiple choice · 1.2)
Which declaration compiles?
- (A)
double rate = 4; - (B)
int count = 3.0; - (C)
boolean open = "true"; - (D)
int 2nd = 2;
Answer and explanation
Answer: (A). An int value can be stored in a double variable, so rate holds 4.0.
- (B) stores a
doublein anint. - (C) stores a
Stringin aboolean. - (D) uses a name that starts with a digit.
If you missed this: Topic 1.2 study guide.
Question 3 (multiple choice · 1.3)
What is printed by System.out.println(17 / 5 + 17 % 5 * 2.0);?
- (A)
7 - (B)
7.0 - (C)
5.8 - (D)
3.4
Answer and explanation
Answer: (B). 17 / 5 is 3 (integer division). 17 % 5 is 2, and 2 * 2.0 is 4.0. Then 3 + 4.0 is 7.0.
- (A) forgets that a
doubleoperand makes the result adouble. - (C) uses 17 / 5 = 3.4 as a real division, which it is not.
- (D) is 17 / 5.0.
If you missed this: Topic 1.3 study guide.
Question 4 (multiple choice · 1.4)
What is printed by the following code segment?
int a = 4;
int b = a * 2;
a = b - a;
b = a + b;
System.out.println(a + " " + b);
- (A)
4 8 - (B)
8 12 - (C)
4 12 - (D)
12 4
Answer and explanation
Answer: (C). b is 8. Then a = b - a is 8 - 4 = 4. Then b = a + b uses the new a: 4 + 8 = 12.
- (A) forgets the last statement.
- (B) leaves
aas 8, as ifa = b - a;had not run. - (D) swaps the order of the two values.
If you missed this: Topic 1.4 study guide.
Question 5 (multiple choice · 1.5)
What is the value of (int) 3.9 + (int) -2.5?
- (A)
0 - (B)
2 - (C)
1 - (D)
1.4
Answer and explanation
Answer: (C). The casts truncate toward zero: (int) 3.9 is 3 and (int) -2.5 is -2. The sum is 1.
- (A) treats
(int) -2.5as -3, moving away from zero; truncation moves toward zero. - (B) rounds 3.9 up to 4; a cast truncates and never rounds.
- (D) adds the two
doublevalues without casting.
If you missed this: Topic 1.5 study guide.
Question 6 (multiple choice · 1.6)
What is the value of n after the following code segment?
int n = 10;
n -= 3;
n *= 2 + 1;
n %= 8;
- (A)
7 - (B)
5 - (C)
21 - (D)
2
Answer and explanation
Answer: (B). n -= 3 gives 7. n *= 2 + 1 means n = n * (2 + 1), which is 21. Then 21 % 8 is 5.
- (A) stops after the first update.
- (C) stops after the second update.
- (D) reads
%=as a division: 21 / 8 is 2. The operator stores the remainder.
If you missed this: Topic 1.6 study guide.
Question 7 (multiple choice · 1.7)
Which statement is true?
- (A) The
Scannerclass needs animportstatement, butStringandMathdo not - (B) All library classes are available without an import
- (C) A library is documentation that explains how to use classes
- (D) Attributes are the methods of a class
Answer and explanation
Answer: (A). String and Math are in java.lang, which is available by default. Scanner is in java.util and must be imported.
- (B) is wrong: only
java.langis available by default. - (C) describes the API specification; a library is a collection of classes.
- (D) mixes up attributes (data in variables) with behaviors (methods).
If you missed this: Topic 1.7 study guide.
Question 8 (multiple choice · 1.8)
A method static int quotient(int a, int b) has the precondition b != 0. A caller passes 0 for b. Which statement is correct?
- (A) The compiler reports an error
- (B) The method is expected to check and return -1
- (C) The postcondition is still guaranteed
- (D) The caller broke the precondition, so the postcondition is not guaranteed
Answer and explanation
Answer: (D). A precondition is the caller’s responsibility. If it is broken, nothing is promised about the result.
- (A) is wrong: the compiler does not check preconditions.
- (B) is wrong: there is no expectation that a method checks its precondition.
- (C) is wrong: the postcondition depends on the precondition holding.
If you missed this: Topic 1.8 study guide.
Question 9 (multiple choice · 1.9)
What is the signature of public static double mean(int a, int b, int c)?
- (A)
double mean(int, int, int) - (B)
mean(a, b, c) - (C)
mean - (D)
mean(int, int, int)
Answer and explanation
Answer: (D). The signature is the name plus the ordered list of parameter types.
- (A) includes the return type.
- (B) uses parameter names.
- (C) leaves out the parameter list.
If you missed this: Topic 1.9 study guide.
Question 10 (multiple choice · 1.10)
A class Calc has the class method static int half(int n), which returns n / 2. Which statement, in a different class, stores the value of half(9) in an int variable h?
- (A)
int h = Calc.half(9); - (B)
int h = half(9); - (C)
int h = new Calc().half; - (D)
int h = Calc(9).half();
Answer and explanation
Answer: (A). A class method is called with the class name and the dot operator. The result is 4, stored in h.
- (B) omits the class name, which is needed from another class.
- (C) is not a valid call.
- (D) is not valid syntax.
If you missed this: Topic 1.10 study guide.
Question 11 (multiple choice · 1.11)
Which expression produces a random int from 10 to 19, both inclusive?
- (A)
(int) (Math.random() * 19) - (B)
(int) (Math.random() * 10) + 10 - (C)
(int) Math.random() * 10 + 10 - (D)
(int) (Math.random() * 20)
Answer and explanation
Answer: (B). There are 10 values (10 to 19), so scale by 10, cast the product, and shift by 10.
- (A) gives 0 to 18.
- (C) casts
Math.random()alone, so the result is always 10. - (D) gives 0 to 19.
If you missed this: Topic 1.11 study guide.
Question 12 (short answer · 1.12)
Use the Counter class above.
Counter a = new Counter();
Counter b = a;
b.increment();
Counter c = new Counter();
System.out.println(a.getCount() + " " + c.getCount());
(a) State the output. (b) How many Counter objects exist, and what does b = a copy?
Answer and explanation
(a) The output is 1 0.
(b) Two Counter objects exist. The statement b = a; copies the reference, so a and b refer to the same object; incrementing through b changes the count seen through a. The object that c refers to is separate and still holds 0.
Check yourself: one mark each for 1 0, for two objects, and for saying the reference (not the object) is copied.
If you missed this: Topic 1.12 study guide.
Question 13 (short answer · 1.13)
Use the Cup class above, which has two constructors.
(a) State the value returned by getMl() for new Cup() and for new Cup(330).
(b) Say why new Cup(1, 2) does not compile.
Answer and explanation
(a) new Cup() calls the constructor with no parameters, so getMl() returns 250. new Cup(330) calls the constructor with one int, so it returns 330.
(b) No constructor of Cup has two parameters, so no signature matches the two arguments.
Check yourself: one mark each for 250, 330 and the reason for the error.
If you missed this: Topic 1.13 study guide.
Question 14 (multiple choice · 1.14)
What happens when the following code segment is compiled and run?
Counter c = null;
c.increment();
System.out.println("done");
- (A) It prints
done - (B) It does not compile
- (C) It compiles, then throws a
NullPointerExceptionand does not printdone - (D) It compiles, then throws an
ArithmeticException
Answer and explanation
Answer: (C). An instance method call on a null reference results in a NullPointerException at run time, which stops the program before done.
- (A) assumes that the call does nothing.
- (B) is wrong: the compiler cannot tell what the variable will refer to.
- (D) is the exception for integer division by zero.
If you missed this: Topic 1.14 study guide.
Question 15 (short answer · 1.15)
The variable s holds "banana bread". State the value of each expression.
(a) s.length() (b) s.substring(7) (c) s.indexOf("na") (d) s.substring(0, 6) + s.length()
Answer and explanation
The characters are b(0) a(1) n(2) a(3) n(4) a(5), a space at index 6, then b(7) r(8) e(9) a(10) d(11).
- (a)
12. - (b)
"bread": from index 7 to the end. - (c)
2: the first"na"starts at index 2. - (d)
"banana12":substring(0, 6)is"banana", and theint12 is joined as text.
Check yourself: one mark for each part.
If you missed this: Topic 1.15 study guide.
What next?
- Missed one question? Read that topic’s study guide and redo its practice questions.
- Missed several in Topics 1.3 to 1.6? Work through the expressions and assignment topics again: the later topics rely on them.
- Missed Questions 12 to 15? Revisit objects and strings, then try the Unit 1 mixed review.
- Tick off the Topic 1.15 checklist once you can do everything on it.