Test Study Weeks 4-7 Flashcards
(57 cards)
What are the two types of methods in Java?
Void methods perform a task and terminate, while value-returning methods return a value to the caller.
What are the key components of a method declaration?
Modifiers, return type, method name, and parameter list.
What is a method signature?
A method’s name and parameter list (number, type, and order of parameters).
How do you call a method in Java?
By using its name followed by parentheses: methodName();
What is the difference between arguments and parameters?
Arguments are values passed to a method; parameters are the variables that receive those values.
How does Java pass primitive data types to a method?
By value, meaning a copy of the value is passed, and modifications inside the method do not affect the original variable.
What happens when an object reference is passed to a method?
The memory address of the object is passed, so changes made inside the method affect the original object.
Why are Strings immutable in Java?
Once created, String objects cannot be modified. Changing a String creates a new object.
What is a local variable?
A variable declared inside a method that is accessible only within that method.
How do you return a value from a method?
Using the return statement followed by a value. Example: return number * 2;
What is an array in Java?
A collection of elements of the same data type, stored in contiguous memory locations.
How do you declare and initialize an array?
int[] numbers = new int[5]; or int[] numbers = {1, 2, 3, 4, 5};
What is zero-based indexing?
Arrays start indexing at 0, so the first element is at index 0.
Can you change the size of an array after creation?
No, arrays have a fixed size once initialized.
How do you access an array element?
Using its index: numbers[0] = 10; assigns 10 to the first element.
What happens if you access an array out of bounds?
Java throws an ArrayIndexOutOfBoundsException.
How do you find the length of an array?
Using the length property: array.length;
What is an off-by-one error?
A common mistake where a loop iterates one index too far, causing an exception.
How do you iterate over an array?
Using a for loop: for (int i = 0; i < array.length; i++) or an enhanced for loop: for (int num : array).
How do you copy an array correctly?
Using a loop: for (int i = 0; i < arr1.length; i++) arr2[i] = arr1[i];
What happens when you assign one array reference to another?
Both references point to the same array, not a copy.
How do you compare two arrays for equality?
Using Arrays.equals(arr1, arr2) for content comparison (not ==, which checks references).
What is a partially filled array?
An array where some elements are unused, tracked with a separate counter variable.
What is a two-dimensional array?
An array of arrays, arranged in rows and columns. Example: int[][] matrix = new int[3][4];