Chapter 7 Flashcards
What is required for method overloading in Java?
Method overloading requires that methods differ either by the number of parameters or the types of parameters. The return type alone cannot be used to distinguish between overloaded methods.
How can you fix a method overloading error where two methods have the same name and parameters in Java?
You can either change the parameter type (e.g., use double for one method) or add a different number of parameters. The return type alone cannot distinguish overloaded methods.
How do you declare an array in Java?
Arrays are declared using a reference type and square brackets:
java
Copy code
int[] numbers;
How do you create an array in Java?
After declaring an array reference, you can create an array like this:
java
Copy code
numbers = new int[6];
Or in one statement:
java
Copy code
int[] numbers = new int[6];
Can arrays hold different types of data in Java? Give an example.
Yes, arrays can hold any type of data, including primitive types or objects. Example:
java
Copy code
float[] temperatures = new float[100];
What is the size of an array in Java and how is it specified?
The size of an array is a non-negative number and can be a literal, constant, or variable:
java
Copy code
final int ARRAY_SIZE = 6;
int[] numbers = new int[ARRAY_SIZE];
How do you access an element in an array in Java?
You access an array element using its reference name and index. Indexes start at zero:
java
Copy code
int value = numbers[0];
What is the purpose of the length field in an array?
The length field stores the size of the array. It can be used in loops to avoid errors:
java
Copy code
int size = numbers.length;
How do you initialize an array with specific values in Java?
You can initialize an array using an initialization list:
java
Copy code
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
How do you copy the elements of one array to another in Java?
You can copy an array by iterating through and assigning each element:
java
Copy code
for (int i = 0; i < firstArray.length; i++) {
secondArray[i] = firstArray[i];
}
What is a two-dimensional array in Java?
A two-dimensional array is an array of arrays, where each element is another array. Example:
java
Copy code
double[][] scores = new double[3][4];
How do you declare and initialize a ragged array in Java?
A ragged array has rows with varying numbers of columns. Declare and initialize like this:
java
Copy code
int[][] ragged = new int[4][];
ragged[0] = new int[3];
ragged[1] = new int[4];
Can arrays hold objects in Java? Give an example.
Yes, arrays can hold objects. For example, an array of String objects:
java
Copy code
String[] names = { “Bill”, “Susan”, “Steven”, “Jean” };
How do you use array elements in a loop?
You can loop through array elements with a for loop:
java
Copy code
for (int i = 0; i < temperatures.length; i++) {
System.out.println(“Temperature “ + i + “: “ + temperatures[i]);
}
How do you return an array from a method in Java?
You can return an array by specifying the return type as an array of the appropriate type:
java
Copy code
public static double[] getArray() {
double[] array = { 1.2, 2.3, 4.5, 6.7, 8.9 };
return array;
}