1/87
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
What is a compile-time error?
An error the compiler catches before the program runs, so no bytecode is produced.
Name 3 common causes of compile-time errors.
Syntax errors (missing semicolon), type mismatches (int x = "hi";), using an undeclared variable.
What is a runtime error?
An error that happens while a program that compiled successfully is running, because it tries an operation that can't be done.
What is a logic error?
The program compiles and runs without crashing but produces the wrong result.
Compile or runtime error? int x = "hello";
Compile error. Type mismatch: a String can't be assigned to an int.
Compile or runtime error? int[] a = new int[3]; a[3] = 1;
Runtime error: ArrayIndexOutOfBoundsException. Valid indices are 0 to 2.
Compile or runtime error? int z = 0; int x = 10 / z;
Runtime error: ArithmeticException (integer division by zero).
Compile or runtime error? String s = null; s.length();
Runtime error: NullPointerException.
What is a wrapper class?
A class that wraps a primitive so it can be used as an object. int → Integer, double → Double, char → Character, boolean → Boolean.
What is autoboxing?
Java automatically converting a primitive into its wrapper object. Example: Integer x = 5;
What is unboxing?
Java automatically converting a wrapper object back into its primitive. Example: int y = x; where x is an Integer.
Why can't you write ArrayList
Generics only accept reference types, so you use the wrapper: ArrayList
What happens? Integer x = null; int y = x;
Runtime NullPointerException, because Java tries to unbox null.
What is a recursive method?
A method that calls itself to solve a smaller version of the same problem.
What is the base case in recursion?
The condition where the method returns directly without calling itself. It stops the recursion. A method can have more than one.
What is the recursive case?
The part where the method calls itself on a smaller input that moves toward the base case.
What happens if a recursive method never reaches a base case?
It keeps calling itself until the call stack runs out: StackOverflowError.
What is the depth of recursion?
The number of times a method calls itself before reaching the base case.
Why does recursion use more memory than a loop?
Every call adds a new frame (its parameters and local variables) to the call stack, and it stays there until that call returns.
Is recursion ever required to solve a problem?
No. Any recursive solution can also be written iteratively with a loop.
Iteration vs recursion: how does each one repeat work?
Iteration repeats a block with a loop until its condition is false. Recursion repeats by the method calling itself until it hits the base case.
What does factorial(3) return? static int factorial(int n) { if (n <= 1) return 1; return n * factorial(n - 1); }
factorial(3) = 3 * factorial(2) = 3 * 2 * factorial(1) = 3 * 2 * 1.
What is a generic class?
A class written with a type parameter so the same code works with different reference types.
What is the main benefit of generics?
Compile-time type safety: the compiler catches the wrong types, and you don't need casts.
Type parameter vs type argument?
Type parameter: the placeholder in the declaration (T in class Box
Write the header of a generic class Box with one type parameter.
public class Box
Class vs object?
A class is the blueprint. An object is an instance created from that blueprint with new.
What does it mean for a class to be public?
Any other class can access it, including classes in other packages.
Can a class be private?
Only if it's nested inside another class. Then only the enclosing class can use it. Top-level classes can only be public or package-private (no modifier).
What does static mean?
The member belongs to the class itself, not to any one object. There is one shared copy for all instances.
How do you call a static method?
ClassName.methodName(), with no object needed. Example: Math.sqrt(16).
Why can you call Math.max(3, 5) without writing new Math()?
max is a static method, so it belongs to the Math class, not to an object.
Can a static method directly use an instance variable?
No. A static method has no object (no this), so it can only directly access static members.
3 objects of a class share a static int count. How many copies of count exist?
One, shared by all objects.
What does void mean as a return type?
The method doesn't return a value. You can still use return; to exit early.
Array vs ArrayList: size
An array's size is fixed when created (new int[5]). An ArrayList grows and shrinks automatically as you add and remove.
Array vs ArrayList: what types can they hold?
An array can hold primitives or objects. An ArrayList holds only objects, so primitives go in as wrappers (ArrayList
How do you get the size of an array, an ArrayList, and a String?
arr.length (a field, no parentheses), list.size(), str.length().
What is an exception?
An object representing an error event at runtime that disrupts the normal flow of the program.
What happens when an exception is not handled?
The program terminates and prints the exception and its stack trace.
What does exception handling let you do?
Catch the exception and decide how to respond, so the program can keep running instead of crashing.
What are the 3 parts of exception handling?
Declaring (throws in the method header), throwing (throw new SomeException()), and catching (try/catch).
throw vs throws?
throw actually throws an exception object inside a method. throws in the method header declares that the method might throw it.
What is a checked exception?
An exception the compiler forces you to deal with: catch it with try/catch or declare it with throws, otherwise it won't compile. Example: IOException.
What is an unchecked exception?
A RuntimeException or one of its subclasses. Handling is optional. Examples: NullPointerException, ArrayIndexOutOfBoundsException, ArithmeticException.
Does this compile? public void read() { throw new IOException(); }
No. IOException is checked, so it must be caught or the method needs "throws IOException" in its header.
Does this compile? public void get(int[] a) { int x = a[10]; }
Yes. ArrayIndexOutOfBoundsException is unchecked, so the compiler doesn't require handling. It may fail at runtime.
What is try-catch used for?
Write the code
Try catch is a general method for EH
If it throws an exception it automatically just jumps to a predefined catch block that has the right solution alr built in
If no exceptions occur the catch blocks are just ignored
