Difficulty: Intermediate to Advanced | Prerequisites: Loops, methods, basic OOP, String methods
Arrays, data processing, and ArrayList questions make up the bulk of the CS312 credit-by-exam. Five of the eight questions test these skills directly. The problems require you to create and manipulate arrays, process text with Scanner, use ArrayList methods correctly, and handle 2D array indexing with bounds checks. You should be comfortable with loop patterns (while with Scanner, for with arrays, nested for with 2D arrays) and understand the difference between arrays and ArrayList at a method level.
These questions test your ability to loop through data structures, map characters to array indices, copy array subsets without mutating the original, find minimum values with nested loops, remove elements from an ArrayList by value, and clamp values in a 2D sub-region. The exam rewards clean structure and penalises shortcuts like indexOf, contains, or treating an ArrayList like an array.
Scanner
A class for reading tokens from an input source. sc.hasNext() returns true if another token is available. sc.next() returns the next whitespace-delimited token as a String.
Think of it as a cursor that walks through text one word at a time.
Character-to-index mapping (ch - 'A')
Subtracting 'A' from an uppercase character gives its position in the alphabet as an integer: 'A' - 'A' = 0, 'B' - 'A' = 1, and so on up to 'Z' - 'A' = 25. This is how you map a letter to an array index without using a lookup table.
Counter array
An int[] where each index represents a category and the value at that index is a count. For the alphabet, a 26-element array tracks how many times each letter appears.
ArrayList
A resizable list of String objects. Key methods: size() returns the number of elements, get(i) returns the element at index i, remove(i) removes and returns the element at index i (shifting subsequent elements left), add(e) appends an element.
In simple terms, it is an array that can grow and shrink, but you must use method calls instead of bracket notation.
2D array (int[][])
An array of arrays. mat[row][col] accesses the element at a given row and column. mat.length gives the number of rows. mat[0].length gives the number of columns (assuming a rectangular matrix).
Bounds checking
Ensuring that indices do not go below 0 or above the array length. In 2D problems, you must check both row and column bounds, especially when a sub-region extends beyond the edges of the matrix.
The capitalLettersPresent pattern (17 points)
Given a Scanner and an int[] of required letter counts (index 0 = A, index 1 = B, etc.), determine whether the scanned text contains at least those many of each capital letter.
Algorithm:
Outer loop: while (sc.hasNext()) to read each token.
For each token, loop through its characters with charAt(i).
Check if the character is an uppercase letter: 'A' <= ch && ch <= 'Z'.
Map the character to an index: int index = ch - 'A'.
If required[index] > 0, decrement it. (The > 0 check is not strictly required for correctness but avoids going negative.)
After processing all tokens, loop through the required array. If any value is still greater than 0, return false. Otherwise return true.
Grading breakdown:
Loop with hasNext(): 2 points
Get next token: 2 points
Loop through characters: 2 points
Check for capital letter: 4 points
Update counter correctly: 3 points
Final check and return: 4 points
Key penalties:
Looping through a string of capital letters or using indexOf instead of the ch - 'A' mapping: lose 4 points
Putting the Scanner loop inside another loop (scanning the file multiple times): lose 4 points
Concatenating strings to create a modified copy of input: lose 3 points
Problem (16 points):
Given an int[], a start index, and a stop index, return a new array that contains all elements except those from index start (inclusive) to stop (exclusive).
Algorithm:
Calculate the new length: vals.length - (stop - start).
Create a new int[] of that length.
First loop: copy elements from index 0 up to (but not including) start.
Second loop: copy elements from stop to the end. Use a separate index variable (indexResult) to track where you are writing in the result array, starting at start.
Return the new array.
Why two loops, not one?
A single loop with an if statement works but is considered less efficient and loses 2 points. The two-loop approach is cleaner and maps directly to the two segments being copied.
Key penalties:
Returning the original array when no elements are removed (instead of a copy): lose 4 points
Modifying the values in the parameter array vals: lose 4 points
Using one loop with if: lose 2 points
Problem (17 points):
Given an array of Point objects, find the minimum distance between any two points.
Algorithm:
Initialise min to the distance between the first two points (pts[0].distance(pts[1])) or Double.MAX_VALUE.
Outer loop: i from 0 to pts.length.
Inner loop: j from i + 1 to pts.length. Starting j at i + 1 avoids comparing a point to itself and avoids checking the same pair twice.
Calculate p1.distance(p2). If it is less than min, update min.
Return min.
Why j = i + 1?
Distance is symmetric: the distance from A to B equals the distance from B to A. Starting j at i + 1 ensures each pair is checked exactly once. Starting j at 0 and guarding with if (i != j) works but is inefficient and loses 2 points.
Key penalties:
No inner loop (only checking adjacent pairs): lose 7 points. This is the single largest deduction on the exam.
Using Integer.MAX_VALUE or an arbitrary initial value instead of a proper double initialisation: lose 2 points.
Problem (16 points):
Given a Scanner and an ArrayList, for each word from the Scanner, remove the first occurrence of that word from the list. Return how many removals were made.
Algorithm:
Outer loop: while (sc.hasNext()) reads each word.
For each word, search through the ArrayList using a while loop with an index and a boolean flag.
Compare with .equals(s), not ==. Using == compares references, not content.
When found, call list.remove(i), increment the count, and set the flag to stop searching (you only remove the first occurrence).
Return the count after all words are processed.
Why not use indexOf or contains?
The exam explicitly forbids these convenience methods. The point is to demonstrate that you can search manually. Using them loses 4 points.
Why use list.get(i) and not list[i]?
ArrayList is not an array. Bracket notation does not work. Using [index] instead of get(index) loses 4 points.
Key penalties:
Scanner loop inside a list loop (instead of list loop inside Scanner loop): lose 5 points
Removing all occurrences instead of just the first: lose 5 points
Using indexOf or contains: lose 4 points
Using bracket notation [i]: lose 4 points
Problem (16 points):
Given a 2D int[][], a lower-right corner (r, c), a width w, a height h, and a target value tgt, set every element in that sub-region to tgt if it is currently less than tgt. The sub-region may extend beyond the top or left edges of the matrix, and those out-of-bounds portions are ignored.
Algorithm:
Calculate the starting row: r - h + 1. If this is less than 0, clamp it to 0.
Calculate the starting column: c - w + 1. If this is less than 0, clamp it to 0.
Nested loops: outer loop from startRow to r (inclusive), inner loop from startCol to c (inclusive).
For each element, if mat[row][col] < tgt, set it to tgt.
Why r, c is the lower-right corner:
The problem defines the region by its lower-right corner, width, and height. You work backwards to find the upper-left corner. This is the opposite of the more common pattern where you start from the upper-left.
Key penalties:
Array index out of bounds (AIOBE): lose 4 points
Off-by-one error (OBOE): lose 2 for the first, 1 for each subsequent
Infinite loop: lose 6 points
Using disallowed methods: lose 5 points
Students often compare Strings with == instead of .equals(). In Java, == checks whether two references point to the same object, not whether the strings contain the same characters.
Students often use list[i] syntax on an ArrayList. ArrayList is not an array. Use list.get(i), list.set(i, val), and list.remove(i).
Students often forget that remove(i) shifts all subsequent elements left. If you remove the element at index 3, the element that was at index 4 is now at index 3. If you then increment i, you skip it.
Students often only check adjacent pairs when looking for the minimum distance between points. You must check every pair, which requires a nested loop.
Students often initialise a minimum-distance variable to Integer.MAX_VALUE when the result is a double. Use Double.MAX_VALUE or initialise to the distance between the first two elements.
⚠️ The five array/list questions total 82 points out of the full exam, making them the majority of your score.
⚠️ Structural errors (wrong loop nesting, using forbidden methods) carry heavier penalties than small logic mistakes.
⚠️ Always create a new array when the problem asks you to return one. Never modify and return the input array unless the problem says to.
⚠️ The ch - 'A' mapping pattern appears frequently. Know it cold.
True or False: "hello" == "hello" is always true in Java. (False. It may be true due to string interning, but you should never rely on it. Use .equals().)
Fill in the blank: To map the character 'C' to index 2 in an array, compute 'C' - ____. ('A')
True or False: After calling list.remove(0) on an ArrayList with 5 elements, list.size() returns 4. (True.)
True or False: In the minDistance problem, checking only adjacent pairs in the array is sufficient. (False. The two closest points may be anywhere in the array.)
Fill in the blank: For a 2D array mat, the number of columns is given by mat[0].____. (length)
Q: Given int[] vals = {10, 20, 30, 40, 50}, what does copyWithOutRange(vals, 1, 3) return?
A: {10, 40, 50}. Elements at indices 1 and 2 (values 20 and 30) are removed. Index 3 (stop) is exclusive.
Q: A Scanner reads the text "Hello WORLD". How many capital letters does it contain, and what are they?
A: Six: H, W, O, R, L, D. The H in "Hello" counts. All five letters in "WORLD" count.
Q: Why is it wrong to write list[2] when list is an ArrayList?
A: ArrayList does not support bracket notation. You must use list.get(2). Bracket notation is only for arrays.
Q: In the clampValue problem, if r = 3, h = 5, and the matrix has rows indexed 0 to 4, what is startRow?
A: 0. The calculation r - h + 1 gives 3 - 5 + 1 = -1, which is less than 0, so it is clamped to 0.
Q: What is the time complexity of the minDistance algorithm with the j = i + 1 inner loop?
A: O(n^2), where n is the number of points. You check roughly n(n-1)/2 pairs.
The character-mapping technique (ch - 'A') connects to how Java handles char as a numeric type, which is tested directly in the expressions questions. The nested-loop pattern in minDistance is the same structure used in 2D array problems, just applied to a 1D array of objects. ArrayList operations connect to inheritance because the exam tests whether you understand that ArrayList is a class with methods, not a primitive structure with bracket access.
Java arrays, int array, 2D array, multidimensional array, ArrayList, ArrayList methods, get, set, remove, size, Scanner, hasNext, next, charAt, character mapping, ch minus A, counter array, frequency array, copyWithOutRange, array copying, nested loops, minimum distance, Point class, distance method, clampValue, bounds checking, off by one, AIOBE, ArrayIndexOutOfBoundsException, equals vs ==, String comparison, CS312 UT Austin, credit by exam, data processing