Chapter 1: Introduction
Multiple Choice · Digital Data
Question 1: Representing Digital Data
Which statement about digital data is false?
13 = 8 + 4 + 1 → 00001101; 00001011 is 11. The others are true: JSON and XML are tagged, semi-structured data, not schema-bound tables.
Short Answer · Introduction to Data Structures
Question 2: Primitive Types and Division
What does this code print? Explain each value by its type's size and behavior.
byte b = 127; b++; int x = 7 / 2; double y = 7 / 2; double z = 7 / 2.0; System.out.println(b + " " + x + " " + y + " " + z);
Show answer
Output: -128 3 3.0 3.5
- A
bytehas 8 bits (−128 … 127); 127 + 1 overflows and wraps to −128. 7 / 2on twoints is integer division → 3 (fraction dropped).y: the division is stillint(3), then widened todouble→ 3.0.z: one operand is adouble, so the division is floating-point → 3.5.
A data type fixes both its values and how operations behave.
Multiple Choice · Introduction to Data Structures
Question 3: Type Safety
Which line causes a compile error in Java?
* is not defined for String, and the compiler rejects it before the program runs (type safety). D is a distractor but legal: the char is promoted to a number, so d = 66.
Code Trace · Introduction to Data Structures
Question 4: Passing an Array and an int
What does the program print? Which assignments inside modify affect a and n?
The array is created on the heap; a holds only a reference to it.
n is a primitive; its value sits in the frame.
New frame: arr gets a copy of a's address, k a copy of n's value.
arr[0] = 99 changes the shared object itself, so a sees [99, 2] too.
k = 99 changes only the local copy; n is still 1.
arr now points to a new array; the array a points to is untouched.
Only the new array changes; a's array stays [99, 2].
Return: the frame is discarded; the new array is now unreachable, so the garbage collector reclaims it.
Output: 99 2 1. Java passes by value; for a reference, that value is an address.
static void modify(int[] arr, int k) { arr[0] = 99; k = 99; arr = new int[]{0, 0}; arr[1] = 50; } // main: int[] a = {1, 2}; int n = 1; modify(a, n); System.out.println(a[0] + " " + a[1] + " " + n);
Short Answer · Introduction to Data Structures
Question 5: A Generic Box
What is a? What happens if s.put(7) is uncommented? Why is Box<int> not allowed?
Box<Integer> b = new Box<>(); b.put(5); int a = b.get() + 1; Box<String> s = new Box<>(); s.put("data"); // s.put(7);
Show answer
a = 6: put(5) boxes the int into an Integer; b.get() + 1 unboxes it and adds.
s.put(7) is a compile error: a Box<String>'s put takes only a String. Types are checked at compile time, before the program runs.
T must be a reference type: use the wrapper class Integer, not the primitive int.
Short Answer · Introduction to Data Structures
Question 6: ADT, Interface, and Class
Which is the ADT and which the data structure: this interface or the class ArrayStack<E>? Why does the client declare s as a Stack<Integer>?
public interface Stack<E> { int size(); boolean isEmpty(); void push(E e); E top(); E pop(); } // client code: Stack<Integer> s = new ArrayStack<>(10);
Show answer
- Interface = the ADT's contract: which operations exist (what); no implementation details.
- ArrayStack = the data structure: implements the ADT with an array and an index
t(how); memory and performance details live here. - With the interface as its type, the client depends only on the contract. To switch to a linked-list
LinkedStack, change onlynew ArrayStack<>(10); the rest of the client code stays the same.
One ADT (the stack), many implementations.
Multiple Choice · Introduction to Data Structures
Question 7: The State of an ArrayStack
An empty ArrayStack<Integer> with capacity 4 receives push(3), push(7), pop(), push(9), top(), in order. What are the top index t and the value of top() at the end?
push(3): t = 0; push(7): t = 1; pop() returns 7 and clears the cell, t = 0; push(9): t = 1, data[1] = 9. top() returns 9 without removing it, so t stays 1. B forgets that pop decrements t.
Multiple Choice · Fundamental Data Structures
Question 8: Matching Needs to Structures
Which pairing of requirement and data structure is wrong?
Reaching the i-th node of a linked list means walking i nodes from the head → O(n); O(1) access by index is an array's strength. The rest are right: “back” returns to the latest page (LIFO), key lookup suits a map, a network suits a graph.
Multiple Choice · Fundamental Data Structures
Question 9: Stack or Queue?
Which of these uses a stack (LIFO) rather than a queue (FIFO)?
Undo reverses the latest action first (LIFO); recursion, backtracking (say, in a maze), and bracket matching also use stacks. A, C, and D serve first come, first served → queue. C is the trap: BFS uses a queue; DFS uses a stack.
Multiple Choice · Fundamental Data Structures
Question 10: How Maps and Sets Behave
What does this code print?
Map<String, Integer> m = new HashMap<>(); m.put("apple", 3); m.put("pear", 5); m.put("apple", 7); Set<Integer> s = new HashSet<>(m.values()); s.add(5); System.out.println(m.size() + " " + m.get("apple") + " " + m.get("cherry") + " " + s.size());
Map keys are unique: the second put("apple", 7) replaces the value instead of adding a pair → 2 pairs. get returns null for a missing key. Adding 5 to the set {7, 5} again changes nothing → 2. B is the trap: the old value is overwritten, not kept.
Open-Ended Question · Summary
Question 11: Designing a Library System
A library system must find books by ISBN instantly, queue borrowers of a popular book in arrival order, let users browse a category/subcategory hierarchy, and handle a collection that changes often. Which data structures would you pick? Justify each by operation costs and the structure-selection questions.
Sample answer
- ISBN → book: a map (hash table, HashMap): unique keys, O(1) average lookup, insertion, and deletion. A sorted array with binary search finds in O(log n) but needs O(n) shifting per insertion.
- Waiting list: a queue (FIFO) per book; enqueue/dequeue O(1).
- Category hierarchy: a tree; each subcategory has one parent (one-to-many).
- Changing collection: frequent insertions and deletions call for dynamic structures; the size is not known in advance, so a fixed array won't do.
Optional: a graph for “similar books”; a set for unique tags.
Multiple Choice · Introduction to Data Structures
Question 12: Arrangement and Relationship
Which classification by arrangement is wrong?
A user follows many people and has many followers; there is no single parent, and cycles can occur → a graph (many-to-many). C is right: each employee has one manager. In A the items have no order or links; in D each car has one predecessor and one successor.
Short Answer · Introduction to Data Structures
Question 13: Identifying the Arrangement
Give the arrangement (unordered, linear, tree, graph) and the relationship for each case:
- Folders and files on a computer
- Items in a shopping cart (order doesn't matter)
- Songs to be played in order from a playlist
- Flight routes between cities
A shortcut is added that points to a folder from elsewhere. What does the folder structure become?
Show answer
| Case | Arrangement | Relationship |
|---|---|---|
| 1. Folders | tree | one-to-many |
| 2. Cart | unordered | none |
| 3. Playlist | linear | one-to-one |
| 4. Flight routes | graph | many-to-many |
- Each file or folder has exactly one parent folder; the tree branches out from the root.
- A city has flights to many cities and from many; there is no single parent.
- Shortcut: the folder is now reachable by two paths, so a node has two “parents.” The structure stops being a tree and becomes a graph (a shortcut to an ancestor folder even creates a cycle).