Common Java API classes (Math, Random, ArrayList). They are pre-built tools that save you from writing hundreds of lines of code. For the 1Z0-811 exam, you need to understand when to use each one and how to call their methods correctly, because they appear in multiple-choice questions that test your ability to spot the right tool for the job.
Jump to a section
A simple way to picture Common Java API Classes (Math, Random, ArrayList)
Ever stood in your kitchen wondering which gadget to use for the job? You need a precise measurement, a random snack pick, or a flexible way to store leftovers. That is exactly what the Java API classes Math, Random, and ArrayList do for you in code.
Think of Math as your kitchen scale. It gives exact, predictable results: weight conversions, temperature adjustments, square roots of ingredients. You call Math.sqrt(25) and you get exactly 5.0, every single time. It is reliable, never random, and perfect when you need a calculated number.
Now, Random is like a lucky dip bowl filled with numbered tickets. You reach in and pull out a ticket blindly. Next time you reach in, you might get a different number. You control the possible numbers by setting the range — just like you control the ticket options. That is what new Random().nextInt(10) does: gives you a random ticket between 0 and 9.
Finally, ArrayList is your expandable fridge shelf. A normal array is a fixed egg carton — only holds 12 eggs and cannot grow. ArrayList is a shelf that magically extends when you add more food. You can add items, remove them, and rearrange them without pre-planning the exact count. When you write ArrayList<String> shoppingList = new ArrayList<>(); and then shoppingList.add("milk"); the shelf expands to hold it.
These three tools — the scale, the lucky dip, and the expandable shelf — solve common problems in programming without you building the logic from scratch.
Java, like any language, gives you a set of ready-made tools called APIs (Application Programming Interfaces). An API is a collection of pre-written classes and methods that solve common problems. Instead of writing your own code to calculate a square root or generate a random number, you can use these public APIs that come built into Java. The 1Z0-811 exam tests your knowledge of three specific ones: Math, Random, and ArrayList.
Let us start with the Math class. Math is in the java.lang package, which means it is automatically available in every Java program without needing an import statement. It contains only static methods — methods you call directly on the class name, not on an object. You cannot create an instance of Math because its constructor is private. You write Math.methodName(parameters).
Key methods in Math that appear on the exam:
Math.abs(int a) or Math.abs(double a): returns the absolute (positive) value of a number. Math.abs(-7) returns 7.
Math.ceil(double a): rounds up to the nearest whole number. Math.ceil(4.2) returns 5.0.
Math.floor(double a): rounds down to the nearest whole number. Math.floor(4.9) returns 4.0.
Math.round(double a): rounds to the nearest whole number using standard rounding (0.5 rounds up). Math.round(4.5) returns 5.
Math.max(int a, int b) and Math.min(int a, int b): return the larger or smaller of two numbers.
Math.pow(double a, double b): returns a raised to the power of b. Math.pow(2, 3) returns 8.0.
Math.sqrt(double a): returns the square root. Math.sqrt(16) returns 4.0.
Math.random(): returns a double between 0.0 (inclusive) and 1.0 (exclusive). Be careful — this is often confused with the Random class, but Math.random() is a simple one-off random call that returns a decimal.
Now, the Random class. Unlike Math, Random is in the java.util package, so you must write import java.util.Random; at the top of your file. You create an instance of Random using the constructor: Random rand = new Random(); Then you call methods on that object. The key methods for the exam:
rand.nextInt(): returns a random integer from the entire range of int values (including negatives).
rand.nextInt(int bound): returns a random integer from 0 (inclusive) up to bound (exclusive). For example, rand.nextInt(10) gives a number between 0 and 9. This is the most commonly tested version.
rand.nextDouble(): returns a random double between 0.0 and 1.0 (similar to Math.random()).
rand.nextBoolean(): returns true or false randomly.
A critical detail: every time you create a new Random object, it uses the current system time as a seed. If you create two Random objects in the same millisecond, they could generate the same sequence of numbers. But for the exam, assume each new Random() gives a different series.
Finally, the ArrayList class. ArrayList is a resizable array. A standard array in Java has a fixed size once created — you cannot add or remove elements. ArrayList solves that. It is in the java.util package, so you need import java.util.ArrayList;. You create an ArrayList like this: ArrayList<String> names = new ArrayList<>(); The angle brackets <String> specify the type of elements the list will hold. This is called a generic type.
Key methods for ArrayList:
add(E element): adds an element to the end of the list. names.add("Alice") puts "Alice" at index 0.
add(int index, E element): inserts an element at a specific position, shifting subsequent elements to the right.
get(int index): retrieves the element at that index. names.get(0) returns "Alice".
set(int index, E element): replaces the element at a specific index. names.set(0, "Bob") changes the first element to "Bob".
remove(int index): removes the element at that index and shifts remaining elements left.
remove(Object o): removes the first occurrence of the specified object.
size(): returns the number of elements (not the capacity). This is different from the length property of arrays.
clear(): removes all elements.
isEmpty(): returns true if the list contains no elements.
ArrayList is backed by an internal array. When the internal array becomes full, Java automatically creates a new, larger array and copies everything over. This happens behind the scenes, so you do not need to worry about it.
Why do these three classes matter for the 1Z0-811 exam? The exam tests your ability to recognise correct syntax and method signatures. They love to ask questions where the wrong answer uses a non-existent method (like Math.square() or ArrayList.push()) or uses the wrong class (like using Math where Random is needed). They also test whether you remember that ArrayList methods return specific types: get() returns the element type, remove() returns the removed element (when using index), and size() returns an int.
Import the required class
Write import java.util.ArrayList; and/or import java.util.Random; at the top of your Java file. Math is in java.lang and is automatically available. Without the import, the code will not compile because ArrayList and Random are not recognised.
Declare and instantiate the object
For ArrayList, write ArrayList<String> list = new ArrayList<>();. The left side declares the variable with the generic type, and the right side creates the actual list object. For Random, write Random rand = new Random();. This allocates memory for the object and initialises it with a seed.
Choose the correct method for your task
Decide whether you need to add, get, set, remove, or check size (for ArrayList) or generate a random int, double, or boolean (for Random). For Math, decide which function (abs, ceil, floor, round, max, min, pow, sqrt, random) fits your calculation. The exam expects you to know which method matches which task.
Call the method with correct parameters
Use the exact syntax shown in the documentation. For example, rand.nextInt(10) not rand.nextInt[10]. For ArrayList, list.add(0, "hello") inserts at index 0, while list.add("hello") appends. Using wrong parameters (like list.add(0) to add integer 0) is a common mistake.
Store or use the return value appropriately
Most methods return a value. For example, Math.abs(-5) returns 5 — you can store it in a variable or print it directly. ArrayList.get(0) returns the element at that index. Ignoring the return value is often incorrect unless the method is designed to be used for its side effect (like list.clear()).
Handle index bounds carefully
ArrayList indices start at 0 and go up to size()-1. Trying to access an index equal to size() throws IndexOutOfBoundsException. After a remove operation, indices shift. Always use list.size() to get the current number of elements before accessing by index.
Imagine you are a junior developer working for a company called "QuizMaster" that builds online quizzes for schools. Your task is to write a feature that randomly selects ten questions from a pool of fifty, shuffles their order, calculates the final score, and stores the results.
First, you need a way to store the pool of questions. A standard array would be too rigid because the number of questions might change. You use ArrayList. You write:
ArrayList<String> questionPool = new ArrayList<>(); questionPool.add("What is the capital of France?"); questionPool.add("What is 2+2?");
Now you have a flexible list. Next, you need to randomly select ten questions. You use the Random class:
Random rand = new Random(); ArrayList<String> selectedQuestions = new ArrayList<>(); for (int i = 0; i < 10; i++) { int index = rand.nextInt(questionPool.size()); selectedQuestions.add(questionPool.remove(index)); }
This loop picks a random index, removes that question from the pool, and adds it to the selected list. Using questionPool.size() ensures the random number is within the current bounds.
Now, after the quiz, you must calculate the final score. You have a variable correctAnswers. You use Math to convert it to a percentage:
double percentage = (double) correctAnswers / 10 * 100; long roundedScore = Math.round(percentage);
Math.round gives you a neatly rounded whole number.
Finally, the company wants a "double points" round where scores are multiplied by two. You use Math.pow to determine the bonus multiplier, but a simpler approach is just multiplication. However, you might need to find the maximum score between two players using Math.max.
An IT professional uses these APIs daily. A data analyst uses Math functions to transform data. A game developer uses Random to spawn enemies in different positions. A web developer uses ArrayList to manage items in a shopping cart. The core pattern is always the same: you import the correct class, instantiate it if needed, and call the appropriate method. The exam tests whether you remember which class provides which method and whether you use the correct syntax.
Common real-world tasks include:
Using Math.round() to display currency values without long decimals.
Using Random.nextInt(100) to generate a random discount code.
Using ArrayList to store user inputs from a form.
Using ArrayList.remove() to delete a completed task from a to-do list.
Using Math.min() and Math.max() to clamp values within a range.
The 1Z0-811 exam tests your knowledge of Math, Random, and ArrayList through multiple-choice questions that typically present a code snippet and ask what the output is, or ask which line of code correctly uses a method. You will not be asked to write a full program, but you must recognise correct syntax.
Here are the exact concepts they love to test:
The difference between Math.random() and Random.nextInt(). Math.random() returns a double between 0.0 and 1.0. Random.nextInt(int bound) returns an int between 0 and bound-1. A common trap question shows code like (int)(Math.random() * 10) and asks for the range — the answer is 0 to 9.
The return type of Math methods. Math.sqrt returns double, Math.round returns long (or int if the input is float). They might test that Math.round(4.5) returns 5, not 5.0.
ArrayList generic syntax. The exam expects you to know that ArrayList<String> list = new ArrayList<>(); is correct, but ArrayList list = new ArrayList(); without the generic type is also legal (but gives a warning). They may test that you cannot use primitives like int directly — you must use the wrapper class ArrayList<Integer>.
ArrayList index management. After remove(int index), the list size decreases and elements shift. For example, if list contains [A, B, C] and you remove index 0, the list becomes [B, C] with B now at index 0.
The fact that ArrayList.size() returns the number of elements, not the capacity. There is no length property on ArrayList (unlike arrays).
The difference between add(E) and add(int, E). The first appends; the second inserts.
The fact that Random objects are created with new Random(), not Random r = Random(); (missing the constructor call).
Common traps they set:
Asking what Math.round(4.5) returns and offering 4 as an option. Standard rounding rounds 4.5 up to 5.
Asking the outcome of creating two Random objects in the same millisecond — but this is rarely tested because it is implementation-dependent.
Asking whether Math.random() can return 1.0. It cannot — it returns numbers from 0.0 inclusive up to but not including 1.0.
Asking you to identify the correct import statement. ArrayList is in java.util, not java.lang.
Asking what happens when you use an index that is out of bounds in ArrayList — you get an IndexOutOfBoundsException.
Asking whether ArrayList can store different types in the same list without generics. It can, but when you retrieve them, you get Object references, not the original type.
Key definitions to memorise: - static method: a method that belongs to the class itself, not to an instance. - parameterised type: the type inside angle brackets, like <String>. - autoboxing: Java automatically converting a primitive int to an Integer object when adding to ArrayList<Integer>. - seed: the starting value for a pseudorandom number generator.
The best way to prepare is to write small test programs that call each method and print the result. Then change the parameters to see how the output changes. This builds the muscle memory you need for the exam.
The Math class contains only static methods, so you call them directly like Math.abs(-5) without creating an object.
Math.random() returns a double between 0.0 (inclusive) and 1.0 (exclusive) — never 1.0 itself.
The Random class requires importing java.util.Random and creating an instance with new Random().
ArrayList stores objects only — use wrapper classes like Integer for primitive numbers.
ArrayList.size() returns the number of elements, and after a remove operation, elements shift left and indices change.
When you need a random integer in a specific range, use rand.nextInt(bound) which returns 0 to bound-1.
Math.round(4.5) rounds 4.5 up to 5 using standard rounding rules, not down.
Always import java.util.ArrayList and java.util.Random before using them in your code.
These come up on the exam all the time. Here's how to tell them apart.
Math.random()
Returns a double between 0.0 and 1.0
Static method — no object needed
You must cast to get an integer
Random.nextInt(bound)
Returns an int between 0 and bound-1
Instance method — need new Random()
Directly gives an integer without casting
ArrayList.size()
A method call with parentheses: size()
Returns the current number of elements
Changes dynamically as you add/remove
Array.length
A field with no parentheses: length
Returns the fixed capacity of the array
Does not change after array creation
Math.round(double)
Returns a long (nearest whole number)
Rounds .5 upwards
Can round up or down depending on value
Math.floor(double)
Returns a double (largest integer ≤ value)
Always rounds down
Always returns a double value, not an integer type
ArrayList.add(E element)
Appends the element to the end of the list
Does not shift any existing elements
Takes one parameter
ArrayList.add(int index, E element)
Inserts the element at a specific index
Shifts all subsequent elements to the right
Takes two parameters
Random.nextInt()
Returns any int from full range (including negatives)
No bound parameter
Result can be as low as -2147483648
Random.nextInt(bound)
Returns int from 0 up to bound-1
Requires a positive bound parameter
Result is always non-negative
Mistake
Math.random() returns an integer.
Correct
Math.random() returns a double between 0.0 and 1.0. To get an integer, you must multiply and cast, e.g., (int)(Math.random() * 10) gives an integer from 0 to 9.
The word 'random' makes beginners think of whole numbers like dice rolls, but the method is designed for fine-grained decimal values.
Mistake
You can use ArrayList<int> directly.
Correct
ArrayList only works with reference types (objects), not primitives. You must use the wrapper class: ArrayList<Integer>.
Arrays (like int[]) work with primitives, so beginners assume ArrayList does too. Java requires wrapper classes for generics.
Mistake
ArrayList has a length property like arrays.
Correct
ArrayList uses the size() method to return the number of elements. Arrays use the length field. They are different syntax.
Arrays are a special language feature with length as a field; ArrayList is a regular class with methods. Beginners mix them up because both store sequences.
Mistake
After you remove an element from an ArrayList, the remaining elements keep their original indices.
Correct
When you remove an element at index i, all elements after i shift left by one position, and their indices decrease by one.
People think of removal like deleting a page from a binder, leaving the other page numbers unchanged. Java physically moves the elements.
Mistake
Math.round() always returns a long.
Correct
Math.round() is overloaded: if you pass a float, it returns an int; if you pass a double, it returns a long.
Most teaching examples use double, so beginners think the return type is always long. The exam tests awareness of the overloaded versions.
Mistake
Creating a new Random object every time you need a random number gives better randomness.
Correct
Creating many Random objects in quick succession can produce identical sequences because they are seeded by the system time. It is better to create one instance and reuse it.
Beginners think 'new' means 'fresh and random', but the seed mechanism can cause duplicates if created too fast.
Reveal each answer, then mark whether you got it right. Score 60%+ to unlock the next chapter.
Math.random() returns a double between 0.0 and 1.0. Random.nextInt() returns an integer from the full int range, and Random.nextInt(bound) returns an integer from 0 to bound-1. Use Math.random() for simple decimal randoms and Random for integers.
No. The Math class is in java.lang, which is automatically imported in every Java program. You can use Math.abs() directly without any import statement.
No, ArrayList only stores objects. To store primitives, use their wrapper classes: ArrayList<Integer> for int, ArrayList<Boolean> for boolean, ArrayList<Double> for double. Java automatically converts (autoboxes) primitives to their wrapper objects when you add them.
ArrayList uses the method size() to return the number of elements. Arrays use a field named length (with no parentheses). They are different syntax: list.size() versus array.length.
You get an IndexOutOfBoundsException at runtime. The exception message will say something like 'Index 10 out of bounds for length 5'. Always check that the index is less than list.size().
Math.round(4.5) returns 5. It uses standard rounding where .5 or above rounds up. If you pass a double, the return type is long (so 5L), and if you pass a float, it returns int (so 5).
Use list.remove(object). For example, list.remove("Alice") removes the first occurrence of the string "Alice". If the object is not found, the list remains unchanged and remove returns false (for object version).
You've finished Common Java API Classes (Math, Random, ArrayList). Continue through the 1Z0-811 study guide to build a complete picture of the exam.
Done with this chapter?