Java Exam Deep Study Notes
1. Understanding Numbers in Java (Integer vs Double)
One of the most important foundations in Java is understanding how numbers behave. Java treats
integers and decimals very differently. When you divide two integers, Java removes the decimal part
completely. For example, 7 / 2 gives 3, not 3.5. This is called integer division.
To force Java to give a decimal answer, at least one number must be a double. For example, 7 / 2.0
gives 3.5. Always watch for this in exams because it is one of the most common mistakes students
make.
Another key concept is floating-point precision. Values like 0.1 or 0.3 cannot be stored exactly in binary.
This means comparing doubles using == can fail even if they look equal. Instead, compare using a
tolerance such as [Link](a - b) < 0.0001.
2. Boolean Logic and Conditions
Boolean logic controls decision-making in programs. You will frequently use operators like AND (&&),
OR (||), and NOT (!). AND requires both conditions to be true, while OR only needs one.
Java evaluates AND before OR. This means expressions like A || B && C are interpreted as A || (B &&
C). To avoid confusion, always use parentheses.
Short-circuit evaluation is very important. In OR, if the first condition is true, Java does not check the
second. In AND, if the first condition is false, Java skips the second. This prevents errors such as
dividing by zero.
3. Loops (While and For)
Loops allow repeated execution of code. A while loop runs as long as its condition is true. It is your
responsibility to update the variable inside the loop, otherwise it may run forever.
For example, dividing a number inside a loop reduces it until the loop stops. This is often tested in
exams where you must trace outputs step-by-step.
A for loop is more structured. It has initialization, condition, and update in one line. For example, for (int
i = 0; i < 10; i = i + 3) will produce 0, 3, 6, 9.
Variables declared inside a for loop only exist inside that loop. This is called scope.
4. Break and Control Flow
The break statement immediately exits a loop regardless of the condition. This is useful when a certain
condition is met early.
In exams, you may see loops that appear to run many times but stop early because of a break. Always
trace carefully.
5. Methods, Parameters, and Return Values
Methods are blocks of reusable code. Parameters are variables in the method definition, while
arguments are the values passed when calling the method.
A method must return a value if its return type is not void. Forgetting a return statement will cause an
error.
Void methods do not return anything. They are typically used for printing or modifying data.
6. Constructors and Objects
A constructor is used to initialize objects. It runs automatically when an object is created.
The keyword this() allows one constructor to call another within the same class. This helps reduce
repeated code.
Static variables belong to the class, not individual objects. This means all objects share the same value.
7. Arrays
Arrays store multiple values in a single variable. When you create an array with new int[10], it has 10
elements indexed from 0 to 9.
Accessing an index outside this range causes an error. Always remember: valid indices are from 0 to
length - 1.
When assigning values like a = b, Java copies the value, not the memory location. This is why changing
one does not affect the other.
To find the minimum value in an array, start with the first element and compare all others to it.
8. Switch Statements
Switch statements allow multiple conditions based on a single variable. Each case must include a break
statement to prevent fall-through.
If break is missing, Java continues executing the next cases. This behavior is often tested.
9. Random Numbers
The method nextInt(n) generates values from 0 to n - 1. You can shift this range by adding or
subtracting a number.
For example, nextInt(9) - 4 produces values from -4 to 4.
10. Pass-by-Value
Java uses pass-by-value. This means when you pass variables into a method, copies are used.
Changes inside the method do not affect the original variables.
This is why swap methods do not work unless you use arrays or objects.
Final Exam Strategy
Always trace code step by step. Do not guess. Pay attention to data types, especially int vs double.
Watch for off-by-one errors in loops and arrays.
Most exam questions are not about writing code, but understanding how code behaves. If you
understand these concepts deeply, you will perform very well.