Test Study Weeks 4-7 Flashcards
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];
What is a ragged array?
A 2D array where rows have different lengths.
How do you sum all elements in a 2D array?
Using nested loops: for (int row = 0; row < matrix.length; row++) for (int col = 0; col < matrix[row].length; col++) total += matrix[row][col];
What is a class in Java?
A blueprint for creating objects, containing fields (data) and methods (behavior).
What is an object?
An instance of a class with unique values for its fields.
What are instance variables?
Variables that belong to each object of a class and store its state.
What are instance methods?
Methods that operate on an object’s instance variables.
What is data hiding?
A principle where instance variables are kept private and accessed through methods.
What are accessor and mutator methods?
Accessors (getters) return field values, and mutators (setters) modify them.
What is a constructor?
A special method that initializes an object when it is created.
What are the characteristics of a constructor?
1) Same name as the class. 2) No return type. 3) Called automatically when an object is created.
What is a no-arg constructor?
A constructor with no parameters that sets default values.
What is constructor overloading?
Defining multiple constructors with different parameter lists.
What is method overloading?
Defining multiple methods with the same name but different parameters.
What is the this keyword used for?
Refers to the current object, useful for distinguishing between instance variables and parameters.
What is the toString method?
A method that returns a string representation of an object.
What happens when toString is not overridden?
It prints the memory address of the object.
What is the equals method used for?
Compares object contents instead of memory addresses.
What is a copy constructor?
A constructor that creates a new object as a copy of an existing one.
What is object aggregation?
When an object contains a reference to another object (“has-a” relationship).
What is method chaining?
Calling multiple methods on the same object in one statement: obj.method1().method2();
What is the static keyword used for?
Declares class-level variables and methods shared among all instances.
How do you call a static method?
Using the class name: Math.sqrt(25);
What is garbage collection?
Automatic memory management that removes unreferenced objects.
What is a null reference?
A reference that does not point to any object.
How do you check if a reference is null?
if (obj != null) { … }
What is shadowing in Java?
When a local variable has the same name as an instance variable, hiding it.
What is a package in Java?
A way to organize related classes in separate namespaces.
How do you import a package?
import java.util.Scanner; or import java.util.*;
What is the difference between == and .equals() in Java?
== compares memory addresses, while .equals() compares actual content.
What is a deep copy vs. a shallow copy?
A deep copy creates a new object with copied values; a shallow copy copies the reference.
What is the purpose of the super keyword?
Calls a parent class’s constructor or method.
What is polymorphism?
The ability of different classes to use the same method names, often through method overriding.
What is an interface in Java?
A collection of abstract methods that a class can implement.