Chapter 7 Practice Questions Flashcards
What is required for method overloading in Java?
A: Methods must have the same return type.
B: Methods must differ in the number or type of parameters.
C: Methods must have the same name but different access modifiers.
D: Methods must differ only in the return type.
B
Explanation: Method overloading requires that methods have the same name but differ in the number or type of parameters. The return type alone is not enough to distinguish overloaded methods.
Which of these is a correct way to declare and initialize an array in Java?
A: int[] numbers = {1, 2, 3, 4, 5};
B: int numbers[] = new int[5];
C: int[] numbers; numbers = new int[5];
D: All of the above.
D
Explanation: All these options are valid ways to declare and initialize arrays in Java. You can use direct initialization, separate declaration and instantiation, or place the square brackets after the variable name.
What will the following code do?
java
Copy code
int[] numbers = new int[3];
System.out.println(numbers[3]);
A: Prints 0.
B: Throws an ArrayIndexOutOfBoundsException.
C: Prints garbage value.
D: The code will not compile.
B
Explanation: The array numbers has indices 0, 1, 2. Trying to access numbers[3] exceeds the bounds of the array, resulting in an ArrayIndexOutOfBoundsException.
Arrays in Java can hold:
A: Only primitive types.
B: Only objects.
C: Both primitive types and objects.
D: Strings only
C
Explanation: Arrays can hold both primitive types (e.g., int, double) and objects (e.g., String, custom objects).
Q5: Arrays in Java are zero-indexed.
True
False
True
Explanation: Arrays in Java start with an index of 0, meaning the first element is accessed with index 0.
The return type can be used to distinguish overloaded methods in Java.
True
False
False
Explanation: The return type alone cannot be used to distinguish overloaded methods. The parameter list must differ.
A two-dimensional array in Java is an array of arrays.
True
False
True
Explanation: A two-dimensional array in Java is implemented as an array where each element is itself an array.
What does the following code do?
java
Copy code
String[] names = new String[3];
names[0] = “Alice”;
names[1] = “Bob”;
names[2] = “Charlie”;
System.out.println(names[3]);
A: Prints null.
B: Throws an ArrayIndexOutOfBoundsException.
C: Prints Charlie.
D: The code will not compile.
B
Explanation: The array names has indices 0, 1, 2. Trying to access names[3] exceeds its bounds, resulting in an exception.
What does the following code do?
java
Copy code
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
A: Prints the elements of the array.
B: Throws an exception.
C: The code will not compile.
D: The output depends on user input.
A
Explanation: The for loop iterates through all the elements of the array numbers and prints each one.
Which of these is true for the length field of an array?
A: It is a method that returns the number of elements in the array.
B: It is a constant field that stores the array size.
C: It can be modified after the array is created.
D: It is used only for multi-dimensional arrays.
B
Explanation: The length field is a constant that gives the size of the array and cannot be modified.
What will this code do?
java
Copy code
int[][] numbers = { {1, 2}, {3, 4, 5}, {6} };
System.out.println(numbers[1][2]);
A: Prints 5.
B: Throws an exception.
C: Prints 4.
D: The code will not compile.
A
Explanation: The element at numbers[1][2] is 5. Arrays in Java allow jagged arrays, where rows can have different lengths.
The __________ field of an array gives its size.
length
Explanation: The length field of an array provides the number of elements it contains.
A ragged array is a __________ array with rows of different lengths.
two-dimensional
Explanation: A ragged array is a two-dimensional array where each row can have a different number of columns.
What does the following code do?
java
Copy code
public static double[] getArray() {
double[] array = {1.2, 2.3, 4.5};
return array;
}
A: Compiles but will not run.
B: Returns an array of doubles.
C: Throws an exception.
D: The code will not compile.
B
Explanation: The method getArray creates and returns an array of double values.
What will happen if you attempt to modify the length field of an array?
A: It will throw a NullPointerException.
B: It will compile but throw an exception at runtime.
C: It will result in a compile-time error.
D: It will successfully change the array size.
C
Explanation: The length field of an array is a constant and cannot be modified, leading to a compile-time error.