Loops, Data Structures, Recursion & Big-O
CSCE 146 Practice Exam Review
Computer Science • Active-Learning Edition
How to use this workbook
1. Read the concept explanation. 2. Study the worked example. 3. Try the practice problems yourself.
4. Check your work against the Answer Key. 5. Revisit anything you missed.
, 1. Loops and Arrays
● The outer loop controls the number of iterations (rows/rounds); the inner loop controls how many
items print per iteration.
● Loop variables can increment in different ways (e.g. i += 2), which changes the output and iteration
count.
● Array indexing (e.g. i * 2) can lead to skipped values or out-of-bounds errors — track indices carefully.
● Beginner rule: write out each iteration in a table (loop variable | array index | output) rather than
solving in your head.
WORKED EXAMPLE — Tracing a Nested Loop
for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { System.out.print(i + "," + j + " "); } }
i=0: j=0 → print 0,0 | j=1 → print 0,1
i=1: j=0 → print 1,0 | j=1 → print 1,1
i=2: j=0 → print 2,0 | j=1 → print 2,1
Final output: 0,0 0,1 1,0 1,1 2,0 2,1
✏ TRY IT YOURSELF
1. Trace: for (int i = 0; i < 4; i += 2) { System.out.println(i); } — what prints?
2. Given int[] a = {5,10,15,20,25}; what does a[i*2] give when i=1? When does this go out of bounds?
2. Linked Lists
● A linked list is made of nodes; each node holds data and a reference to the next node, starting from the
head.
● Add to the front: head = new Node(x, head); — this updates the head reference.
● Setting a link to null removes all nodes after that point.
● To traverse: start at head, follow .next until you hit null.
WORKED EXAMPLE — Adding Nodes and Printing
Node head = new Node(1, null); head = new Node(2, head); → list is now 2 → 1
To print: Node current = head; while (current != null) { print(current.data); current = current.next; }
✏ TRY IT YOURSELF
1. Draw the list after: head = new Node(5, null); head = new Node(3, head); head = new Node(9, head);
2. What happens if you call current.next on a node whose next is null?