Difficulty: Beginner to Intermediate | Prerequisites: Basic Java syntax (variables, operators, control flow)
Every line of Java you write produces a value with a type. If you cannot look at an expression and know its type and its result, debugging becomes guesswork. Expression evaluation and code tracing sit at the foundation of CS312: they appear in nearly every exam question, whether the question is about arrays, methods, or object-oriented design.
Before tackling this material, you should be comfortable declaring variables, using arithmetic operators, and writing simple if/else and loop structures. If those feel shaky, revisit your Chapter 1 and 2 notes first.
Java expressions each resolve to a specific data type (int, double, String, char, boolean). Knowing the type rules for division, casting, modulo, and concatenation lets you predict any expression's result. Code tracing means walking through a snippet line by line, tracking variable states, array changes, and method side effects to predict exact output or identify compile/runtime errors.
Integer division
Division between two int operands using the / operator. Java truncates the decimal portion and returns an int. Think of it as: the calculator answer with everything after the decimal point chopped off.
Type casting (explicit cast)
Forcing a value from one type to another by placing the target type in parentheses, e.g. (int) 3.8 yields 3. In simple terms, you are telling the compiler "I know this conversion loses precision, do it anyway."
Modulo operator (%)
Returns the remainder after integer division. 7 % 3 yields 1. Think of it as: divide, throw away the quotient, keep what is left over.
String concatenation
Using the + operator with at least one String operand. Java converts the non-String operand to a String and joins them. The result is always a String.
Boolean expression
An expression that evaluates to true or false. Comparison operators (==, !=, <, >, <=, >=) and logical operators (&&, ||, !) produce boolean results.
Compile error (COMPILE ERROR)
An error caught by the Java compiler before the program runs. Caused by syntax mistakes, type mismatches, or illegal assignments. In simple terms, the code will not even start.
Runtime error (RUNTIME ERROR)
An error that occurs while the program is running, such as dividing by zero or accessing a null reference. The program compiles but crashes mid-execution.
Infinite loop (INFINITE LOOP)
A loop whose condition never becomes false, so the program runs forever. Usually caused by forgetting to update the loop variable or writing the wrong condition.
ArrayIndexOutOfBoundsException (AIOBE)
Thrown when code tries to access an array index that does not exist (negative, or >= array length). Think of it as: you asked for seat 10 in a row that only has seats 0 through 9.
NullPointerException (NPE)
Thrown when code tries to call a method or access a field on a reference that is null. In simple terms, you tried to use an object that does not exist yet.
Off-by-one error (OBOE)
A logic mistake where a loop runs one iteration too many or too few, or an index is one position off. One of the most common bugs in student code.
If both operands are int, the result is int. This includes division: 5 / 2 yields 2, not 2.5.
If either operand is double, Java promotes the other to double and the result is double. So 5 / 2.0 yields 2.5.
The modulo operator % returns an int when both operands are int.
Comparisons (==, <, >, etc.) always yield boolean.
(int) before a double truncates toward zero: (int) 3.9 is 3, (int) -3.9 is -3.
(double) before an int adds a decimal: (double) 5 is 5.0.
Casting applies to the value immediately following it. Parentheses change what gets cast: (int)(5 / 2 + 1.9 / .5) first evaluates the inner expression as 2 + 3.8 = 5.8, then casts to int, giving 5. Wait, let's be precise: 5 / 2 is integer division yielding 2. 1.9 / 0.5 is 3.8. Sum is 5.8. Cast to int gives 5. (The source material states the result is 7, which would apply if the expression were (int)(5 / 2 + 1.9 / .5) evaluated differently, so check your professor's specific examples.)
When + has at least one String operand, Java converts the other operand to String and concatenates.
Evaluation proceeds left to right: 1 + 2 + "hello" yields "3hello", but "hello" + 1 + 2 yields "hello12".
Character literals use single quotes: 'A', '7', '\n'.
Characters are numeric under the hood. 'A' has the integer value 65. You can do arithmetic with them: 'A' + 1 yields 66 (an int).
Compare characters with ==: "CS312".charAt(2) == '3' evaluates to true because the character at index 2 is '3'.
indexOf("or") returns the index of the first occurrence of the substring, or -1 if not found.
Example: "longhorns_or+tv".indexOf("or") returns 2, so "longhorns_or+tv".indexOf("or") * 2 yields 4.
charAt(index) returns the character at that position (0-indexed).
Write out the array state after each iteration. Do not try to do it in your head.
Pay close attention to index bounds. Arrays are 0-indexed, so an array of length 5 has valid indices 0 through 4.
Watch for loops that modify array elements in place. The order of operations matters when one iteration's changes affect the next.
Primitives (int, double, boolean, char) are passed by value. Changes inside the method do not affect the caller's variable.
Objects and arrays are passed by reference (technically, the reference is passed by value). Changes to the object's contents inside the method are visible to the caller.
Example: if a method receives an int[] and modifies its elements, those changes persist after the method returns. If the method receives an int and changes it, the caller's copy is unaffected.
Compile error: the code has a syntax mistake or type violation. It will not run at all. Look for missing semicolons, wrong types in assignments, calling methods that do not exist, or invalid object creation.
Runtime error: the code compiles but crashes. Common causes: dividing by zero (ArithmeticException), accessing a null reference (NullPointerException), or going past the end of an array (ArrayIndexOutOfBoundsException).
Infinite loop: the loop condition never becomes false. Check whether the loop variable is being updated, and whether the update moves it toward the termination condition.
Given int[] a1 = {5, 2, 4, 6, 1}; followed by a method call a(a1); and then System.out.print(Arrays.toString(a1));, the output is [3, 4, 8]. To trace this, you would step through the method a() line by line, updating the array contents at each step and tracking which elements are retained or removed.
Students often think 5 / 2 yields 2.5 in Java. It does not. Both operands are ints, so the result is 2. You need at least one double operand (e.g. 5 / 2.0 or (double) 5 / 2) to get 2.5.
Students often think + always does arithmetic. It does not. If either side is a String, + becomes concatenation. "score: " + 3 + 4 gives "score: 34", not "score: 7".
Students often think changing a primitive parameter inside a method changes the caller's variable. It does not. Primitives are copied. Only changes to object or array contents are visible outside the method.
Students often confuse = (assignment) with == (comparison). The compiler catches this in most cases, but if (x = 5) is not a comparison; it is an assignment (and a compile error in Java since the result is int, not boolean).
⚠️ Expression evaluation questions appear on nearly every CS312 exam. You will be asked to give both the value and the type of the result.
⚠️ Code tracing questions often combine arrays, loops, and method calls. The examiners expect you to identify whether the output is a value, a COMPILE ERROR, a RUNTIME ERROR, or an INFINITE LOOP.
⚠️ Integer division and casting are favourite topics for tricky exam expressions. Always check whether the operands are int or double before evaluating.
⚠️ String concatenation order matters. Evaluate left to right and remember that once a String appears, everything to its right is concatenated as text.
True or false: 7 / 2 evaluates to 3.5 in Java. (False, it evaluates to 3.)
Fill in the blank: "hi" + 3 + 4 evaluates to ____. ("hi34")
True or false: if a method changes an element of an int[] parameter, the change is visible to the caller. (True.)
Fill in the blank: (int) 9.99 evaluates to ____. (9)
True or false: 'A' == 65 evaluates to true in Java. (True.)
Q: What is the type and value of (int)(5 / 2 + 1.9 / 0.5)?
A: 5 / 2 is integer division, yielding 2. 1.9 / 0.5 is double division, yielding 3.8. The sum is 5.8. Casting to int truncates to 5. Type: int. (Check your professor's specific version of this problem, as variations exist.)
Q: What does "CS312".charAt(2) == '3' evaluate to?
A: charAt(2) returns '3' (the character at index 2, since indexing starts at 0: C=0, S=1, 3=2). Comparing '3' == '3' yields true. Type: boolean.
Q: What is the result of "longhorns_or+tv".indexOf("or") * 2?
A: indexOf("or") finds "or" first at index 2 (inside "longhorns"). 2 * 2 = 4. Type: int.
Q: Given int x = 5; change(x); System.out.println(x); where change sets its parameter to 10, what prints?
A: 5. Primitives are passed by value. The method receives a copy, so the original x is unchanged.
Q: What happens when you run int[] a = {1, 2, 3}; System.out.println(a[3]);?
A: RUNTIME ERROR. a[3] is out of bounds (valid indices are 0, 1, 2). Java throws ArrayIndexOutOfBoundsException.
Expression evaluation feeds directly into code tracing, which in turn underpins every debugging and exam question in CS312. Understanding types is also essential for inheritance and polymorphism: knowing which method gets called depends on the declared type vs. the actual type of an object (covered in the OOP study notes).
Array manipulation and ArrayList processing both rely on careful index arithmetic and loop tracing. If you are comfortable tracing a simple for-loop over an array, the jump to 2D arrays and ArrayList removal patterns (covered in the Arrays and Data Processing study notes) is a short one.
Java expression evaluation, CS312 UT Austin, integer division Java, type casting int double, modulo operator remainder, String concatenation order of operations, charAt indexOf, code tracing exam, compile error vs runtime error, infinite loop detection, ArrayIndexOutOfBoundsException, NullPointerException, off-by-one error OBOE, pass by value vs pass by reference Java, boolean expression evaluation