Java Exam Cheat Sheet: Syntax, OOP Concepts, and the Mistakes That Cost Marks
A one-page review for intro Java courses and Java final exams.
How to use this: This Java exam cheat sheet covers what most intro-level Java courses test: types, strings, control flow, arrays and ArrayLists, object-oriented programming, and exceptions. If your instructor allows a reference sheet on your Java final exam, follow their size and format rules.
Java exams mix code-reading, code-writing, and concept questions. This Java exam cheat sheet keeps the syntax and concepts you need in one place.
Java Exam Cheat Sheet: Data Types and Strings
| Type | Holds | Example |
|---|---|---|
| int | Whole numbers | int count = 5; |
| double | Decimals | double price = 9.99; |
| boolean | true / false | boolean done = false; |
| char | Single character | char grade = 'A'; |
| String | Text (an object, not a primitive) | String name = "Sam"; |
Useful String methods: length(), charAt(i), substring(a, b) (b is excluded), indexOf(s), equals(s), toUpperCase(), trim().
Control Flow
- if / else if / else:
if (x > 0) { ... } else { ... } - for loop:
for (int i = 0; i < n; i++) { ... } - enhanced for:
for (String s : list) { ... } - while loop:
while (condition) { ... }— make sure the condition eventually becomes false. - switch: Remember
breakin classic switch statements to avoid fall-through. - Logical operators:
&&(and),||(or),!(not) — both use short-circuit evaluation.
Arrays and ArrayLists
| Array | ArrayList | |
|---|---|---|
| Create | int[] a = new int[5]; | ArrayList<Integer> list = new ArrayList<>(); |
| Size | a.length | list.size() |
| Get | a[i] | list.get(i) |
| Set | a[i] = 7; | list.set(i, 7); |
| Add / remove | Fixed size | list.add(x), list.remove(i) |
OOP Concepts
Encapsulation
Keep fields private and access them through public getters and setters.
Inheritance
A subclass extends a superclass and inherits its members. Use super(...) to call the parent constructor.
Polymorphism
Overriding: same method signature in a subclass. Overloading: same name, different parameters, in the same class.
Abstraction
Abstract classes can’t be instantiated. Interfaces define methods a class must provide using implements.
Exceptions: Use try { ... } catch (ExceptionType e) { ... } finally { ... }. Checked exceptions must be handled or declared with throws.
Common Mistakes That Cost Marks
- Using == on Strings: Use
.equals()to compare contents. - Integer division:
7 / 2is3, not 3.5. Use7 / 2.0. - Off-by-one errors: Valid indexes run from 0 to length − 1.
- Missing return statements in non-void methods.
- Forgetting to initialize objects before using them (NullPointerException).
- Removing from an ArrayList while looping forward: Loop backward or use an iterator.
Key Takeaways
- Know the primitive types and key String methods.
- Master loop syntax and watch for infinite loops.
- Compare arrays and ArrayLists: length vs. size(), [] vs. get().
- Understand encapsulation, inheritance, polymorphism, and abstraction.
- Avoid == on Strings, integer division surprises, and off-by-one errors.
Frequently Asked Questions
What should be on a Java final exam cheat sheet?
Include data types, String and ArrayList methods, loop syntax, OOP concepts like inheritance and polymorphism, exception handling, and the mistakes you make most often.
What is the difference between overloading and overriding in Java?
Overloading means methods with the same name but different parameters in the same class. Overriding means a subclass provides its own version of a method with the same signature as the parent.
Why shouldn’t I use == to compare Strings in Java?
The == operator checks whether two references point to the same object. To compare the text inside Strings, use the equals() method.
How do I get better at Java code-tracing questions?
Trace code on paper with a table showing each variable’s value after every line or loop iteration, and practice with past exam questions.
The Bottom Line
This Java exam cheat sheet covers the syntax and concepts intro courses test most. Write code by hand, trace it on paper, and you’ll be ready for your Java final.
Need Help With Java?
MyFastEdu offers practice problems, clear explanations, and expert guidance to help you master programming concepts before your exam.
Explore Our Exam Prep Solutions