Practice Questions and Detailed Solutions Latest Update
2026/2027 | Classes, Methods, Arrays, Verified Answers - 167
Questions
This midterm exam assesses foundational knowledge of object-oriented programming in Java, focusing on class
design, method implementation, arrays, and program logic. It requires critical thinking and application of
concepts to non-trivial coding scenarios. It contains 167 multiple-choice questions, each with four distractors and
a fully worked rationale that explains why the keyed answer is correct. Questions are organized into clearly
labelled sections that mirror the major content areas of the course. Targeted learning outcomes include: Design
and implement Java classes with appropriate encapsulation and visibility modifiers.; Analyze and trace method
calls, including recursion and parameter passing.; Manipulate arrays and ArrayLists to solve complex
data-processing problems.; Identify and fix common coding errors and anti-patterns in Java code.. Every item has
been reviewed for clinical accuracy, current guidelines, and clarity so that students can study with confidence and
self-correct as they work through the bank. Use it as a high-yield review immediately before the exam, or as a
structured practice tool during the unit - the rationales double as concise teaching notes. The recommended
writing time is 2 hours 30 minutes, with a passing score of 70%. Aligned with Aligned with ACM/IEEE Computer
Science Curricula 2023 and ABET computing accreditation standards. standards and reflects the question style
commonly seen on accredited program examinations. Students consistently achieving above the cut score on this
bank have historically gone on to earn A+ on the corresponding course exam. Read every stem carefully -
Section 1: General (Questions 1-167)
1 Consider the following class definitions. What is the output when
the main method is executed?
A) 2 4
B) 4 2
C) 2 2
D) 4 4
Answer: B
Rationale: The method setVals is called on obj1 with x=1, y=2. Inside
setVals, a new A object is created and assigned to the parameter obj,
so obj now refers to a different object. Changes to obj.x and obj.y
affect this new object, not the caller's obj1. Therefore, obj1 remains
with x=2, y=4. The output is '4 2' because the getX() and getY()
methods are called on obj1, which has x=2 and y=4.
2 Which of the following statements best explains the behavior of the
`finalize()` method in Java?
,A) It is guaranteed to be called exactly once before an object is
garbage collected.
B) It is called by the garbage collector on an object when the object
is no longer reachable, but the timing is non-deterministic.
C) It can be used to force immediate garbage collection by calling
`System.gc()`.
D) It is deprecated and should never be used in modern Java code.
Answer: B
Rationale: The `finalize()` method is invoked by the garbage collector
when an object is deemed unreachable, but the exact time is not
deterministic. It is not guaranteed to run exactly once, and it is indeed
deprecated for general use, but it is still a valid concept. Option B
correctly describes the core behavior without overgeneralizing.
3 Given the following array declaration: `int[] arr = {3, 1, 4, 1, 5, 9, 2,
6};`. Which code snippet correctly rotates the array elements to the
left by two positions?
A) int temp = arr[0]; for (int i = 0; i < arr.length-1; i++) arr[i] =
arr[i+1]; arr[arr.length-1] = temp;
B) int[] temp = {arr[0], arr[1]}; for (int i = 0; i < arr.length-2; i++)
arr[i] = arr[i+2]; arr[arr.length-2] = temp[0]; arr[arr.length-1] =
temp[1];
C) int[] temp = new int[arr.length]; for (int i = 0; i < arr.length; i++)
temp[(i+2)%arr.length] = arr[i]; arr = temp;
D) int[] temp = new int[arr.length]; for (int i = 2; i < arr.length; i++)
temp[i-2] = arr[i]; temp[arr.length-2] = arr[0]; temp[arr.length-1] =
arr[1]; arr = temp;
Answer: C
Rationale: Option C correctly shifts each element to the left by two
positions using modulo arithmetic to wrap around. Option A rotates
by one position only. Option B incorrectly overwrites the first two
elements and then tries to place temporary values at the end, but it
loses the original values. Option D incorrectly places the first two
,elements at the end but does not preserve the order of the rest correctly
(it shifts them left by two, which is correct, but the assignment to
temp[arr.length-2] and temp[arr.length-1] overwrites the values that
should be there from the loop, causing errors).
4 What is the output of the following recursive method when called
with `mystery(5)`?
A) 15
B) 10
C) 20
D) 5
Answer: A
Rationale: The method `mystery` returns the sum of integers from 1 to
n. For n=5, it returns 5 + mystery(4) = 5 + (4 + mystery(3)) = ... =
5+4+3+2+1 = 15. The correct answer is 15.
5 Which of the following statements about the `==` operator and the
`equals()` method in Java is correct?
A) The `==` operator compares object references, while `equals()` is
always overridden to compare object contents.
B) The `equals()` method can be used to compare primitive values,
but `==` cannot.
C) For any two objects `a` and `b`, if `a == b` is true, then
`a.equals(b)` must be true.
D) The `==` operator compares object contents for String objects, so
`"abc" == "abc"` is always true.
Answer: C
Rationale: If `a == b` is true, it means they refer to the same object, so
`a.equals(b)` will also return true (since the same object is equal to
itself). Option A is incorrect because `equals()` is not always
overridden; it uses identity by default. Option B is incorrect because
`equals()` is for objects, not primitives. Option D is incorrect because
`==` compares references for Strings, not contents, unless they are
, interned or compile-time constants.
6 Consider the following code snippet. Which exception is thrown
and why?
A) ArrayIndexOutOfBoundsException because the loop condition `i
<= arr.length` accesses an out-of-bounds index.
B) NullPointerException because the array is not initialized.
C) ArithmeticException because of division by zero.
D) No exception; the code runs without error.
Answer: A
Rationale: The loop `for (int i = 0; i <= arr.length; i++)` will iterate
from 0 to arr.length inclusive, and when i equals arr.length, accessing
arr[i] causes an ArrayIndexOutOfBoundsException. The array is
initialized, so no NullPointerException. There is no division by zero.
The code will throw an exception.
7 Which of the following is the correct way to create a
two-dimensional array in Java with 3 rows and 4 columns, all
initialized to 0?
A) int[][] arr = new int[3][4];
B) int[][] arr = new int[3][];
C) int[][] arr = { {0,0,0,0}, {0,0,0,0}, {0,0,0,0} };
D) Both A and C.
Answer: D
Rationale: Option A creates a 2D array with 3 rows and 4 columns,
and all elements are initialized to 0 by default. Option C explicitly
initializes a 2D array with the same dimensions and all zeros. Both are
valid. Option B creates a jagged array with 3 rows but no columns
specified.
8 What is the primary difference between an abstract class and an
interface in Java?