CSE 123 Spring 2025 Practice Quiz 2
Name of Student: __________________________________________________________________________________
Section (e.g., AA):_____________________ Student UW ID Number: ________________________
Do not turn the page until you are instructed to do so.
Rules/Guidelines:
● You must not begin working before time begins, and you must stop working promptly when time is called. Any
modifications to your quiz (writing or erasing) before time begins or after time is called will be reported as
academic misconduct to the university.
● You are allowed one page of notes, no larger than 8.5 x 11 inches. You may not access any other resources or
use any electronic devices (including calculators, phones, or smart watches, among others) during the quiz. Using
unauthorized resources or devices will be reported as academic misconduct to the university.
● We will only scan your exam packet. We will not scan your scratch paper or your notes sheet. We have
included a blank page in the exam packet. If you run out of space while answering a question, write your answer
on that blank page and clearly indicate that we should look for your answer there.
● In general, you are limited to Java concepts or syntax covered in class. You may not use break, continue, a
return from a void method, try/catch, or Java 8 stream/functional features.
● You are limited to the standard Java classes and methods listed on the provided reference sheet. You do not need
to write import statements.
● If you abandon one answer and write another, clearly cross out the answer(s) you do not want graded and draw
a circle or box around the answer you do want graded. When in doubt, we will grade the answer that appears in
the space indicated, and the first such answer if there is more than one.
● If you require scratch paper, raise your hand and we will bring some to you.
● Answers must be written as proper Java code. Pseudocode or comments will not be graded.
● The quiz is not graded on code quality. You are not required to include comments.
● You are also allowed to abbreviate System.out.print and System.out.println as S.o.p and S.o.pln
respectively. You may NOT use any other abbreviations.
Grading:
● There are three problems. Each problem will receive a single E/S/N grade.
● Minor syntax errors will be ignored as long as it is unambiguous what was intended (e.g. forgetting a semicolon,
misspelling a variable name where there is only one close option). Major syntax errors, or errors where it is
unclear what was intended, may have an impact on your grade.
Advice:
● Read all questions carefully. Be sure you understand the question before you begin your answer.
● The questions are not necessarily in order of difficulty. Be sure you at least attempt every question.
● Write clearly and legibly. We cannot award credit for answers we cannot read.
● If you have questions, raise your hand to ask. The worst that can happen is we will say "I can’t answer that."
● Ask questions as soon as you have them. Do not wait until you have several questions.
Initial here to indicate you have read and agreed to these rules:
This page intentionally left blank
Nothing written on this page will be graded
1. Recursion Tracing
Part A - Mystery
Consider the following mystery code:
public int mystery(int num) {
if (num < 10) {
return num;
}
return mystery(num / 10) + (num % 10);
}
What would the outputs be for the following inputs?
Input Output
mystery(7)
mystery(35)
mystery(123)
mystery(1123)
In 1-2 sentences, provide a high-level, plain-English description of what the mystery method does. You’re
welcome to format this like a standard method comment you’d leave on an assignment if you’d wish.
Part B - Completion
Fill in the blanks to correctly complete the code below so that the method returns the minimum value in arr
between the given indices left (inclusive) and right (inclusive). You may assume left <= right.
public int findMin(int[] arr, int left, int right) {
if (left == right) {
return _________________________________________________;
} else {
int mid = (left + right) / 2;
int leftMin = findMin(arr, left, mid);
int rightMin = _________________________________________;
return _________________________________________________; // overall Min
}
2. LinkedLists w/ Recursion
Below is an iterative solution to removeFromRange written within the LinkedIntList class without a size field.
This method removes all of the values within the provided range (inclusive). For example, calling
[5, 2, 1, 9, 3, 9, 0, 4, 6, 2].removeRange(2, 5)
results in the following change:
[1, 9, 9, 0, 6]
Notice that all of the values >= 2 and <= 5 have been removed:
public void removeRange(int lo, int hi) {
if (lo > hi) {
throw new IllegalArgumentException();
}
while (front != null && front.data >= lo && front.data <= hi) {
front = front.next;
}
ListNode curr = front;
while (curr != null && curr.next != null) {
if (curr.next.data >= lo && curr.next.data <= hi) {
curr.next = curr.next.next;
} else {
curr = curr.next;
}
}
}
Write an equivalent, recursive version of this method. You may not use any iteration to solve this problem.
Write your solution on the next page.
3. Binary Trees
Part A: Traversals
Based on the binary tree shown above, identify the traversal order used to produce each sequence of nodes.
Traversal Order Sequence
duck, robin, jay, eagle, crow, hawk, quail, goose
jay, hawk, crow, eagle, robin, goose, quail, duck
jay, robin, eagle, crow, hawk, duck, quail, goose
Part B: Tracing
Consider the following method to be added to the IntTree class from the reference sheet:
public void mystery(int n) {
overallRoot = mystery(overallRoot, n);
}
private IntTreeNode mystery(IntTreeNode root, int n) {
if (root != null) {
root.left = mystery(root.left, n);
root.right = mystery(root.right, n);
if (root.left != null || root.right != null || root.data > n) {
return root;
}
}
return null;
}
For each of the following trees, draw the result after mystery(10) has been executed on that tree:
Input Output
In 1-2 sentences, provide a high-level, plain-English description of what the mystery method does. You’re
welcome to format this like a standard method comment you’d leave on an assignment if you’d wish.
CSE 123 Quiz 2 Reference Sheet
(DO NOT WRITE ANY WORK YOU WANTED GRADED ON THIS REFERENCE SHEET. IT WILL NOT BE GRADED)
Math Methods
abs(x) Returns the absolute value of x
max(x, y) Returns the larger of x and y
min(x, y) Returns the smaller of x and y
pow(x, y) Returns the value of x to the y power
random() Returns a random number between 0.0 and 1.0
round(x) Returns x rounded to the nearest integer
String Methods
charAt(i) Returns the character in this String at a given index
contains(str) Returns true if this String contains the other's characters inside it
endsWith(str) Returns true if this String ends with the other's characters
equals(str) Returns true if this String is the same as str
equalsIgnoreCase(str) Returns true if this String is the same as str, ignoring capitalization
indexOf(str) Returns the first index in this String where str begins (-1 if not found)
lastIndexOf(str) Returns the last index in this String where str begins (-1 if not found)
length() Returns the number of characters in this String
isEmpty() Returns true if this String is the empty string
startsWith(str) Returns true if this String begins with the other's characters
substring(i, j) Returns the characters in this String from index i (inclusive) to j (exclusive)
substring(i) Returns the characters in this String from index i (inclusive) to the end
toLowerCase() Returns a new String with all this String’s letters changed to lowercase
toUpperCase() Returns a new String with all this String’s letters changed to uppercase
compareTo(str) Returns a negative number if this comes lexicographically (alphabetically) before
other, 0 if they’re the same, positive if this comes lexicographically after other.
LinkedIntList Class
public class LinkedIntList extends AbstractIntList {
private ListNode front;
public LinkedIntList() {...}
public LinkedIntList(int[] nums) {...}
public void add(int index, int value) {...}
public int remove(int targetIndex) {...}
public int size() {...}
public int get(int index) {...}
public static class ListNode {
public final int data;
public ListNode next;
public ListNode(int data) {...}
public ListNode(int data, ListNode next) {...}
}
}
IntTree Class
public class IntTree {
private IntTreeNode overallRoot;
public IntTree() {...}
public IntTree(int[] arr) {...}
public boolean contains(int value) {...}
public String toString() {...}
public void replace(int toReplace, int newValue) {...}
private static class IntTreeNode {
public final int data;
public IntTreeNode left;
public IntTreeNode right;
public IntTreeNode(int data) {...}
public IntTreeNode(int data, IntTreeNode left, IntTreeNode right) {...}
}
}