Chapter 7 Flashcards

1
Q

What is required for method overloading in Java?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you fix a method overloading error where two methods have the same name and parameters in Java?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you declare an array in Java?

A

Arrays are declared using a reference type and square brackets:
java
Copy code
int[] numbers;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you create an array in Java?

A

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];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can arrays hold different types of data in Java? Give an example.

A

Yes, arrays can hold any type of data, including primitive types or objects. Example:
java
Copy code
float[] temperatures = new float[100];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the size of an array in Java and how is it specified?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you access an element in an array in Java?

A

You access an array element using its reference name and index. Indexes start at zero:
java
Copy code
int value = numbers[0];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the purpose of the length field in an array?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you initialize an array with specific values in Java?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you copy the elements of one array to another in Java?

A

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];
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a two-dimensional array in Java?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you declare and initialize a ragged array in Java?

A

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];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can arrays hold objects in Java? Give an example.

A

Yes, arrays can hold objects. For example, an array of String objects:
java
Copy code
String[] names = { “Bill”, “Susan”, “Steven”, “Jean” };

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you use array elements in a loop?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you return an array from a method in Java?

A

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;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the purpose of a sequential search in an array?

A

A sequential search checks each array element sequentially to find a specific value. It stops once the value is found or the array is fully checked.

17
Q

What is the difference between an array’s length field and a String’s length() method?

A

The length field gives the size of the array, while String’s length() is a method that returns the number of characters in the string.

18
Q

How do you initialize a two-dimensional array in Java?

A

You initialize it using nested braces:
java
Copy code
int[][] numbers = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

19
Q

What does a selection sort do?

A

It finds the smallest value in the array and moves it to the first position, then repeats for the next smallest values until the array is sorted.

20
Q

In a binary search, what is the prerequisite for the array?

A

The array must be sorted in ascending order.

21
Q

Describe the process of a binary search.

A

Check the middle element.
If it matches the desired value, the search ends.
If it’s greater than the desired value, search the first half.
If it’s less, search the second half.
Repeat with adjusted start and end points until the value is found or the range is empty.

22
Q

What is the header of the main method that allows Java to receive command-line arguments?

A

public static void main(String[] args)

23
Q

How can arguments passed to the main method be accessed?

A

They can be accessed via the args array. For example, args[0], args[1], etc.

24
Q

What does the … symbol in a method parameter mean in Java?

A

It indicates a vararg (variable-length argument) parameter.

25
Q

Provide an example of a method that uses a vararg parameter.

A

public static int sum(int… numbers) {
int total = 0;
for (int val : numbers) {
total += val;
}
return total;
}

26
Q

What are the advantages of using an ArrayList over an array in Java?

A

Automatically resizes when items are added or removed.
Provides methods like add, remove, get, and size.

27
Q

How do you declare and initialize an ArrayList in Java?

A

ArrayList<String> nameList = new ArrayList<>();</String>

28
Q

How do you add an element to an ArrayList?

A

Use the add method:

java
Copy code
nameList.add(“James”);

29
Q

How do you get the size of an ArrayList?

A

nameList.size();

30
Q

How do you access an element in an ArrayList?

A

Use the get method:

java
Copy code
nameList.get(index);

31
Q

How do you remove an element from an ArrayList by index?

A

nameList.remove(index);

32
Q

How do you replace an element in an ArrayList?

A

Use the set method:

java
Copy code
nameList.set(index, “NewValue”);

33
Q

What is the default capacity of an ArrayList in Java?

A

10 items.

34
Q

How do you specify a custom capacity for an ArrayList?

A

ArrayList<String> list = new ArrayList<>(100);</String>

35
Q

What does the toString method of an ArrayList do?

A

Returns a string representation of all items in the list.

36
Q

What is the diamond operator (<>) in Java?

A

A shorthand introduced in Java 7 for simpler ArrayList declarations:
ArrayList<String> list = new ArrayList<>();</String>