Built around the two study methods with the strongest research behind them: testing yourself and spacing it out. Take the test first, then use the notes to fix what you missed.
Why this order: a large review of study techniques (Dunlosky et al., 2013) rated practice testing and spaced practice as the two most effective methods, and rated rereading and highlighting as low value. The test also mixes lessons together instead of going in order, because the real test won't tell you which lesson a question comes from.
32 questions, lessons mixed. Tap an answer to check it.
Write the code yourself. Do it on paper if your test is on paper; typing with autocomplete hides mistakes. Then open the sample solution and check off each rubric point you earned. Any correct solution counts, even if it doesn't match the sample.
add(3, 4), 3 and 4 are arguments; in add(int a, int b), a and b are parameters.calc(int, double). Return type and parameter names are not part of it.int f(int x) and double f(int x) have the same signature, so that's a compile error.static in the header and belong to the class, not to an object.public static returnType name(parameters)ClassName.method(). Inside the same class, the class name is optional.void method: System.out.println(sayHi()); is a compile error if sayHi is void.int x = methodReturningDouble(); is an error.Math is in java.lang, so no import is needed. All its methods are static: Math.method().
| Method | Returns | Example |
|---|---|---|
Math.abs(int) / Math.abs(double) | Absolute value, same type as input | Math.abs(-3) → 3 |
Math.pow(double, double) | double: first to the power of second | Math.pow(2, 3) → 8.0 |
Math.sqrt(double) | double: positive square root | Math.sqrt(16) → 4.0 |
Math.random() | double ≥ 0.0 and < 1.0 | never exactly 1.0 |
Random int from min to max (inclusive):
(int)(Math.random() * range) + min // range = max - min + 1
(int)(Math.random() * 6) + 5 // 5 to 10 (range = 10 - 5 + 1 = 6)
(int)(Math.random() * 20) + 10 // 10 to 29 (biggest is 19 + 10)
pow and sqrt always return a double, so they print with .0 and can't be stored in an int without a cast. Also: the parentheses in (int)(Math.random() * 6) matter. (int) Math.random() * 6 casts first, which is always 0.Dog pet = new Dog(); → Dog is the class, pet is the object (instance).null.extends a superclass and gets its attributes and behaviors. class Poodle extends Dog → Poodle is the subclass. Every class is a subclass of Object.new: Movie m = new Movie("Up", "Docter", 8.3);new Cat().public Cat() // no-arg
public Cat(String c, String b, boolean h) // 3 args
new Cat(); // ✓
new Cat("orange", "Tabby", false); // ✓
new Cat("Shorthair", true); // ✗ no 2-arg constructor
Cat("orange", "Tabby", false); // ✗ missing new
static) are called on an object with the dot operator: myParty.inviteFriend();Cat.meow() doesn't work if meow is an instance method.null → NullPointerException (run-time error).| Class (static) method | Instance method | |
|---|---|---|
| Header | public static void go() | public void go() |
| Call | ClassName.go() | obj.go() |
| Needs an object? | No | Yes |
"hi" or new String("hi"). String is in java.lang.substring return a new String; the original never changes.0 to length() - 1. Going outside → IndexOutOfBoundsException.+ concatenates. A primitive joined with a String becomes text. Evaluated left to right..equals, not ==.| Method (on Quick Reference) | Example with s = "computer" | Result |
|---|---|---|
length() | s.length() | 8 |
substring(from, to)to is not included | s.substring(2, 5) | "mpu" |
substring(from) | s.substring(3) | "puter" |
substring(i, i + 1) | s.substring(0, 1) | "c" |
indexOf(str) | s.indexOf("t") / s.indexOf("z") | 5 / -1 |
equals(other) | s.equals("Computer") | false |
compareTo(other) | "apple".compareTo("banana") | negative (a comes first) |
// index: c=0 o=1 m=2 p=3 u=4 t=5 e=6 r=7
1 + 2 + "3" + 4 + 5 // 3 → "33" → "334" → "3345"
"1" + 2 + 3 // "12" → "123"
"Java".substring(4) // "" (empty; from == length is allowed)
"Java".substring(1, 5) // IndexOutOfBoundsException (5 > length)
substring(a, b) gives b - a characters.