A program stores a student's ID number (never a decimal), their GPA, the single letter of their course section, and whether their permission slip is turned in. Which set of data types fits those four values, in order?
A. int, int, String, int
B. double, double, char, String
C. int, double, char, boolean
D. String, double, String, int
What is C?
An ID with no decimal component is an integer, a GPA needs a fractional part so it must be floating point, a single letter is exactly what char exists for, and a yes/no condition is a Boolean. A stores the GPA as an int and discards everything after the decimal point, then fakes a Boolean with an integer flag. B wastes a floating-point type on a whole number and stores a true/false value as text that has to be re-parsed every time it is checked. D stores a single character as a String, which works but is the wrong tool, and repeats the integer-as-flag mistake.
A program needs the square root of a number, and then needs the position of the first comma in a line of text. Which libraries provide these?
A. String library for the square root, math library for the comma
B. Math library for the square root, string library for the comma
C. Math library for the square root, math library for the comma
D. Neither one; both must be written by hand
What is B?
Square root is a mathematical operation; searching inside text is a string operation, even though the index it returns is an integer. A is the exact mirror image and misfiles both. C is the trap worth naming out loud: the return type of a method does not tell you which library it belongs to, the operation does. D is false, and rewriting routines that ship with the language wastes time and introduces defects the tested library version does not have.
What distinguishes an object data type from a primitive, and what is one cost of using objects?
A. Objects hold one raw value; they require no extra memory
B. Objects cannot be placed in arrays; they slow down compiling
C. Objects always run faster than primitives; they use less memory
D. Objects bundle data with methods; they use more memory than primitives
What is D?
An object groups related data together with the operations that act on it, which is what makes encapsulation, inheritance, and polymorphism possible in the first place. The tradeoff is real: each object carries overhead beyond the raw value it stores, and it is reached indirectly through a reference rather than sitting in the variable itself. A describes a primitive, not an object. B is false, since arrays of objects are routine. C reverses the performance tradeoff; the advantage of objects is organization and reuse, not speed.
Using integer variables, what are the values of 17 / 5 and 17 % 5?
A. 3 and 2
B. 3.4 and 2
C. 3 and 3.4
D. 3.4 and 3.4
What is A?
When both operands are integers, division discards the remainder and yields 3, and the modulus operator returns precisely that discarded remainder, 2. B and D expect a decimal result, which only happens when at least one operand is a floating-point value; casting one side first, as in (double) 17 / 5, is what produces 3.4. C swaps the two operators, giving the quotient a remainder's job and the remainder a quotient's. This is also why double r = 7 / 2 stores 3.0 rather than 3.5: the division finishes in integer arithmetic before the widening ever happens.
Why might a binary search fail to find a value that is present in an array?
A. The array contains duplicate values
B. The array holds Strings rather than numbers
C. The array is too large for the method
D. The array is not in sorted order
What is D?
Binary search compares the target to the middle element and throws away the half that cannot contain it. That decision is only valid when the data is sorted; on unsorted data it will confidently discard the half holding the target. A is not a correctness problem: duplicates may change which matching index comes back, but a match is still found. B is false, since anything with a defined ordering can be searched. C is backward, because large arrays are exactly where binary search earns its keep, since the work grows logarithmically rather than linearly.
What is the decimal value of the binary number 1011?
A. 11
B. 13
C. 7
D. 1011
What is A?
Reading right to left, the place values are 1, 2, 4, and 8, so the number is 8 + 0 + 2 + 1, or 11. B is what you get by reading the digits backward as 1101. C is the value of 0111, which happens when the place values slide one position and the leading 1 is treated as the 4s place instead of the 8s. D treats a binary numeral as though it were already a decimal number, the most common error when base notation is unfamiliar.
This segment is meant to total the numbers 0 through 4, but it will not compile:
for (int i = 0; i < 5; i = i + 1)
int sum = sum + i
end for
print sum
What is wrong?
A. Nothing is wrong; the segment prints 10
B. sum should have been declared as a constant
C. The loop variable i is out of scope in the body
D. sum goes out of scope at the end of each iteration
What is D?
A variable's scope is the block in which it is declared. Declaring sum inside the loop body creates a brand-new local variable on every pass, each one trying to initialize itself from a value that does not yet exist, and nothing survives to reach the print statement. The fix is to declare and initialize sum to 0 before the loop so a single variable persists across all five iterations. A assumes an accumulator works without living in an enclosing scope. B confuses a constant with a persistent variable; a constant cannot be reassigned at all, which is precisely what accumulation requires. C reverses the situation: i is in scope throughout the loop and out of scope after it.
Which class design best encapsulates its data?
A. Public fields, no methods, changed directly by other classes
B. Private fields, plus a public field holding the same value
C. Private fields, with public methods that read and update them
D. Public fields, with private methods other classes cannot call
What is C?
Encapsulation means the data is hidden and every access runs through methods the class itself controls, so the class can validate changes, keep its state consistent, and rewrite its internals later without breaking any code that calls it. A exposes everything and leaves the class no control over its own data. B defeats the private field by publishing a duplicate of the same value. D inverts the design entirely: the data is open and the behavior is locked away, which is the opposite of the goal.
Using integer arithmetic, what is the value of 4 + 6 / 2 * 3?
A. 15
B. 13
C. 21
D. 5
What is B?
Multiplication and division sit at the same precedence level, above addition, and are evaluated left to right: 6 / 2 gives 3, then 3 * 3 gives 9, then adding 4 gives 13. A evaluates strictly left to right from the start, performing the addition first. C performs the addition before the multiplication, as though the expression were (4 + 6 / 2) * 3. D treats 2 * 3 as a single denominator, which the expression would need parentheses to mean.
A sorted array holds 1, 3, 5, 7, 9, 11, 13. A binary search looks for 3. Which element is compared first, and how many comparisons are needed?
A. 1, and three comparisons
B. 3, and one comparison
C. 7, and two comparisons
D. 7, and four comparisons
What is C?
Binary search begins at the middle of the range, index 3, which holds 7. Because 3 is smaller, the entire upper half is discarded, and the middle of what remains is index 1, which holds 3, found on the second comparison. A describes a linear search starting from the front. B assumes the target is reached directly, which is the one thing binary search never does. D counts as though discarding half the range did not shrink the work.
Why are byte values written in hexadecimal, and what is hex 2F in binary?
A. Hex is the only base a processor runs; 2F is 0010 1110
B. Hex compresses the stored byte; 2F is 0011 1111
C. Hex avoids negative values; 2F is 1111 0010
D. Each hex digit equals four bits; 2F is 0010 1111
What is D?
Sixteen is two to the fourth power, so every hex digit maps to exactly one four-bit group: 2 is 0010 and F is 1111. That clean mapping is why a single byte is written as two hex digits instead of eight binary ones, and why hex is the default for memory addresses and color values. A is wrong because processors execute binary, hex is a human-readable shorthand, and 0010 1110 is 2E. B confuses notation with compression; the stored byte is identical either way, and 0011 1111 is 3F. C invents a property hex does not have, and 1111 0010 is F2, the two halves reversed.
A program currently reads test scores typed at the keyboard. The teacher wants it to read the same scores from a saved file. What has to change?
A. The file must be opened, read to end-of-file, and closed
B. Nothing changes; the statements are identical for both
C. No validation is needed, since files cannot hold bad data
D. The file must be converted to keyboard input first
What is A?
Both are input streams, but a file is a named external resource with a lifecycle: you open it, read until the end-of-file condition, and close it to release it. Keyboard input arrives interactively and is bounded by the user or a sentinel value. B skips the open, close, and end-of-file handling that file access requires. C is both false and dangerous; files are one of the most common sources of malformed data, and input validation matters just as much there as at a keyboard. D describes something that does not exist.
A subclass defines a method with the same name, parameters, and return type as one in its superclass. A separate class defines two methods with the same name but different parameter lists. Which pairing is correct?
A. The first is overloading; the second is overriding
B. The first is overriding; the second is overloading
C. Both are overriding, since both reuse a method name
D. Both are overloading, since both reuse a method name
What is B?
Overriding replaces an inherited method with a new version that has an identical signature, so the subclass's version is the one that runs. Overloading provides several methods that share a name within a class and are told apart by their parameter lists. A reverses the two. C and D collapse a distinction the exam tests directly: reusing a name is not what decides it; what matters is whether the signature matches and whether an inheritance relationship is involved.
A program evaluates if (count != 0 AND total / count > 10) when count is 0. What happens?
A. A division error occurs, since both sides are evaluated
B. The condition is true, since the first test is checked first
C. The condition is false and the division never runs
D. A syntax error occurs, since AND cannot join comparisons
What is C?
With a logical AND, the whole expression cannot possibly be true once the left side is false, so evaluation stops immediately and the right side is never reached. That behavior is short-circuit evaluation, and guarding a division this way is the standard idiom for avoiding division by zero. A describes what would happen in a language that evaluated both operands eagerly. B has the logic backward, since count != 0 is false when count is 0. D is wrong because joining two comparisons is exactly what a logical operator is for. With OR the short circuit happens on the opposite condition: evaluation stops as soon as the left side is true.
An array starts as 5, 2, 9, 1. After one step of sorting it reads 2, 5, 9, 1. Which sort is running?
A. Insertion sort, because the front elements are now ordered
B. Selection sort, because the smallest value moved to the front
C. Bubble sort, because the largest value moved to the end
D. Merge sort, because the array was split into halves
What is A?
Insertion sort grows a sorted region at the front of the array, taking the next element and sliding it back into position, so early on only the leading elements are in order and the tail is untouched, exactly the pattern shown. B is wrong because selection sort scans for the true minimum, 1, and moves it to index 0 on its first pass. C is wrong because a bubble sort's first pass drives the largest value, 9, to the final position. D is wrong because merge sort divides the array and does not produce this kind of partially sorted state after one step.
Consider this code:
int[] a = {1, 2, 3}
int[] b = a
b[0] = 99
print a[0]
What prints, and what does it demonstrate?
A. 1, because arrays are primitives and b got a copy
B. 99, because a and b refer to the same array
C. 99, because assignment always copies the contents
D. An error, because arrays cannot be assigned
What is B?
A primitive variable holds a value; a reference variable holds the address of an object. The assignment copies the address, not the three elements, so a and b become two names for one array and a change made through either is visible through the other. A treats arrays as primitives, which is the misconception this item targets. C reaches the right output by the wrong route: assignment does copy the value on the right, but for a reference type that value is the address. D is false; this is a legal assignment. The same distinction explains why comparing two Strings with == can be false even when the text matches, which is worth connecting back to the locateWord code from earlier in the night.
Trace this:
procedure tweak(int n, int[] arr)
n = n + 1
arr[0] = arr[0] + 1
end tweak
int x = 5
int[] list = {5}
tweak(x, list)
print x, list[0]
What prints, and why?
A. 6 and 6; both arguments are passed by reference
B. 5 and 5; both arguments are passed by value
C. 5 and 6; the int is copied, the array reference is not
D. 6 and 5; primitives are by reference, arrays by value
What is C?
The procedure receives a copy of the integer, so incrementing n leaves x untouched. It also receives a copy of the array's address, but following that address leads to the one and only array, so the element change persists after the procedure ends. This is exactly why a swap procedure can reorder a caller's array during a bubble sort without returning anything. A and D get the direction of the distinction wrong. B applies correct reasoning to the wrong argument: passing a reference by value still hands the procedure a working route to the original object.
A Library class contains a list of Book objects. A Textbook class extends Book and adds an edition field. Which describes the two relationships?
A. Library has-a Book (composition); Textbook is-a Book (inheritance)
B. Library is-a Book (inheritance); Textbook has-a Book (composition)
C. Both are inheritance, since both classes depend on Book
D. Both are composition, since both classes refer to Book
What is A?
Composition is a has-a relationship in which one class holds instances of another as part of its own state; inheritance is an is-a relationship in which a subclass is a specialized kind of its superclass. B reverses them, which would make a library a kind of book. C and D each flatten two genuinely different relationships into one: depending on a class is not the same as being one, and the has-a versus is-a test is the quickest way to decide which design you are looking at.
The values 1, 2, and 3 are added to a stack and also to a queue, in that order. Each structure then removes one item. Which pairing is correct?
A. Stack removes 1; queue removes 1
B. Stack removes 1; queue removes 3
C. Stack removes 2; queue removes 2
D. Stack removes 3; queue removes 1
What is D?
A stack is last-in, first-out, so the most recently pushed value, 3, comes off the top first; think of a stack of plates. A queue is first-in, first-out, so the earliest value, 1, leaves first, the way a line at a counter works. A applies queue behavior to both structures. B reverses the two definitions. C assumes removal happens somewhere in the middle, which neither structure allows.
An O(n) algorithm takes about one second on 1,000,000 items. On the same data, what should you expect from O(log n) and O(n^2)?
A. Both finish in well under a second
B. O(log n) is nearly instant; O(n^2) is impractically slow
C. O(log n) takes a second; O(n^2) takes two seconds
D. O(n^2) is fastest, since squaring reduces the work
What is B?
Big-O describes how the work grows as the input grows, not how long any single run takes. A logarithmic algorithm needs roughly twenty steps for a million items; a quadratic one needs on the order of a trillion. That is the difference between instantaneous and unusable, and it is why the growth rate matters far more than machine speed. A is right about the logarithmic case and badly wrong about the quadratic one. C treats the notations as small constant differences rather than different growth curves. D misreads the exponent as a reduction in work rather than an explosion of it.
A program runs double price = 9.99, then int dollars = (int) price. The value in dollars must then be added to a list that holds only objects. What is the value of dollars, and what has to happen before it can be added?
A. 10, and nothing else is needed
B. 9, and it must be changed back to a double
C. 9, and it must be placed in its wrapper class, Integer
D. 9, and it must be changed into a String
What is C?
Two separate conversions are happening here, and the question is really asking whether you can tell them apart.
The first is the cast. Going from a double to an int is a narrowing conversion, and it cuts off the decimal portion rather than rounding, so 9.99 becomes 9, not 10. Narrowing always requires the explicit (int) cast, because data is being thrown away and the language makes you say so on purpose. The opposite direction, int to double, is widening and happens automatically: double d = 9 is legal with no cast at all, because every int fits inside a double without loss.
The second is wrapping, and it has nothing to do with the cast. Java splits its types into two families. Primitives, such as int, double, char, and boolean, hold a raw value directly and have no methods; you cannot call anything on the number 9. Objects, such as String, arrays, and every class, hold a reference to something in memory that does have methods, which is why s.substring(0, 6) works on a String. Collection classes such as ArrayList, Stack, Queue, and HashMap were built to store references, so they cannot hold a primitive at all. For each primitive, Java supplies a matching class whose only job is to carry that value inside an object: int has Integer, double has Double, char has Character, boolean has Boolean. These are the wrapper classes, and putting a primitive into one is called wrapping or boxing.
Example: int dollars = 9; then ArrayList of Integer cart = new ArrayList of Integer; then cart.add(dollars); - the int is wrapped into an Integer.
Modern Java boxes automatically, so students may never type the word Integer in that line and still be tested on it. Wrappers also carry useful methods a bare primitive cannot have; Integer.parseInt("42") is the bridge between text typed at a keyboard and numbers you can do arithmetic with.
A makes the rounding mistake and also treats a primitive as if it were already an object. B undoes the conversion the program just performed and still leaves a primitive. D turns the number into text, which may display correctly but can no longer be used in a calculation.
Given String s = "Domain III", what do s.substring(0, 6), s.indexOf("I"), and s + "!" produce, in that order?
A. "Domain", 7, and "Domain III!"
B. "Domain ", 7, and "Domain III!"
C. "Domain", 4, and "Domain III!"
D. "omain", 7, and "Domain III !"
What is A?
The ending index of a substring is exclusive, so characters 0 through 5 are returned and the space at index 6 is left out. indexOf is case-sensitive, so it skips the lowercase i at index 4 and finds the first capital I at index 7. Concatenation joins exactly what it is given and adds no spacing of its own. Each wrong option isolates a single error: B treats the ending index as inclusive and picks up the space, C matches case-insensitively and lands on index 4, and D starts the substring at index 1 while inventing a space during concatenation.
Several unrelated classes must each guarantee a draw() method. Separately, a family of shape classes should share stored fields and partially written code. Which choice fits?
A. An interface for the shapes; an abstract class for the guarantee
B. An abstract class for both, since it can do either job
C. An interface for both, since it can store fields and code
D. An interface for the guarantee; an abstract class for the shapes
What is D?
An interface states a contract that any class can agree to, regardless of where it sits in the hierarchy, which is exactly what lets unrelated classes all promise a draw() method. An abstract class lives inside an inheritance hierarchy and can hold shared fields and partially implemented behavior for a family of related classes. A reverses the two. B overlooks the fact that a class can inherit from only one abstract class, which breaks the moment the classes are unrelated. C credits interfaces with something they do not provide: they define the contract, not shared stored state.
A binary tree has root B, with left child A and right child C. Which traversal visits the nodes in the order A, B, C?
A. Preorder
B. Inorder
C. Postorder
D. Level order
What is B?
An inorder traversal visits the left subtree, then the node itself, then the right subtree, producing A, B, C; and on a binary search tree that order is always the sorted order of the values, which is the property worth remembering. Preorder visits the node before its children and gives B, A, C. Postorder visits the node after both children and gives A, C, B. Level order reads the tree by depth, top to bottom, and also gives B, A, C, so it is distinguished from preorder only on larger trees.
Consider this method:
int mystery(int n)
if (n <= 1)
return 1
end if
return n * mystery(n - 1)
end mystery
What does mystery(4) return, and which line is the base case?
A. 10, and the line returning n * mystery(n - 1)
B. 24, and the line returning n * mystery(n - 1)
C. 24, and the line returning 1 when n <= 1
D. An infinite loop, because the method calls itself
What is C?
The calls stack up as 4 x 3 x 2 x 1 and unwind to 24. The base case is the condition that ends the recursion by returning a value without calling the method again: here, returning 1 once n reaches 1. Every recursive method needs both parts, a base case that stops and a recursive case that moves toward it. A performs addition instead of multiplication. B gets the arithmetic right but names the recursive case as the base case, reversing the two. D would only be true if the base case were missing or unreachable, which is the failure mode to watch for when students write their first recursive method.