Common Mistakes That Lose Points
common mistakes
Part of: Exam Workshop: FRQ Strategies
The Small Errors That Cost the Most A handful of mistakes show up on almost every exam and quietly drain points. Knowing them by name lets you scan your own code for them before you move on. Four stand out: comparing objects with ==, off-by-one loop bounds, boundary conditions, and printing when you should return. == Versus .equals For String and other objects, == tests whether two references point to the same object, not whether they hold the same text. To compare content, use .equals. Use == only for primitives like int and boolean. For anything built with new or returned as an object, reach for .equals. Off-by-One and Boundaries Loop bounds are the second big trap. Valid array indices run from 0 to length - 1, so the correct bound is i < arr.length. Writing i <= arr.length reads one past the end and throws an out-of-bounds error. Whenever a loop touches the first or last element, trace those two passes to be sure. Return Versus Print The third trap is doing the wrong action with your answer. If the prompt says the method returns a value, a System.out.println earns nothing, because the caller gets no value back. Return the result and let the caller decide whether to print it. Key
Challenge: Count Matching Words