Chapter 7 Practice Questions Flashcards

1
Q

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.

A

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.

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

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.

A

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.

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

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.

A

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.

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

Arrays in Java can hold:

A: Only primitive types.
B: Only objects.
C: Both primitive types and objects.
D: Strings only

A

C
Explanation: Arrays can hold both primitive types (e.g., int, double) and objects (e.g., String, custom objects).

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

Q5: Arrays in Java are zero-indexed.

True
False

A

True
Explanation: Arrays in Java start with an index of 0, meaning the first element is accessed with index 0.

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

The return type can be used to distinguish overloaded methods in Java.

True
False

A

False
Explanation: The return type alone cannot be used to distinguish overloaded methods. The parameter list must differ.

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

A two-dimensional array in Java is an array of arrays.

True
False

A

True
Explanation: A two-dimensional array in Java is implemented as an array where each element is itself an array.

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

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.

A

B
Explanation: The array names has indices 0, 1, 2. Trying to access names[3] exceeds its bounds, resulting in an exception.

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

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

A
Explanation: The for loop iterates through all the elements of the array numbers and prints each one.

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

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.

A

B
Explanation: The length field is a constant that gives the size of the array and cannot be modified.

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

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

A
Explanation: The element at numbers[1][2] is 5. Arrays in Java allow jagged arrays, where rows can have different lengths.

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

The __________ field of an array gives its size.

A

length
Explanation: The length field of an array provides the number of elements it contains.

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

A ragged array is a __________ array with rows of different lengths.

A

two-dimensional
Explanation: A ragged array is a two-dimensional array where each row can have a different number of columns.

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

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.

A

B
Explanation: The method getArray creates and returns an array of double values.

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

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.

A

C
Explanation: The length field of an array is a constant and cannot be modified, leading to a compile-time error.

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

In a selection sort, the algorithm stops once the smallest value in the array is moved to the first position.
A) True
B) False

A

B) False
Explanation: The selection sort continues sorting the remaining unsorted portion of the array until all elements are in their proper order.

17
Q

What is required for a binary search to work?
A) The array must be sorted in ascending order.
B) The array must have all distinct elements.
C) The array must contain only integers.
D) The array must contain at least one negative number.

A

A) The array must be sorted in ascending order.
Explanation: A binary search divides the array into halves, which only works when the array is sorted.

18
Q

What will the following code do?

java
Copy code
public static void main(String[] args) {
System.out.println(args[0]);
}
A) It will print the first command-line argument passed to the program.
B) It will always print null.
C) It will cause an error if no command-line arguments are provided.
D) It cannot compile.

A

C) It will cause an error if no command-line arguments are provided.
Explanation: Accessing args[0] without any arguments will throw an ArrayIndexOutOfBoundsException.

19
Q

What is the purpose of the … symbol in a method parameter?
A) To accept a single integer as input.
B) To indicate a variable-length argument (vararg).
C) To declare a new array.
D) To initialize a variable inside the method.

A

B) To indicate a variable-length argument (vararg).
Explanation: The … allows the method to accept zero or more arguments of the specified type.

20
Q

The ArrayList class automatically resizes when an element is added or removed.
A) True
B) False

A

A) True
Explanation: One of the primary advantages of using an ArrayList is that it dynamically resizes.

21
Q

What will this code output?

java
Copy code
ArrayList<String> nameList = new ArrayList<>();
nameList.add("Alice");
nameList.add("Bob");
System.out.println(nameList.size());
A) 0
B) 1
C) 2
D) The code will not compile.</String>

A

C) 2
Explanation: The size() method of ArrayList returns the number of elements currently in the list.

22
Q

How do you replace an element in an ArrayList at a specific index?
A) list.add(index, element)
B) list.set(index, element)
C) list.replace(index, element)
D) list.update(index, element)

A

B) list.set(index, element)
Explanation: The set method is used to replace an element at the specified index.

23
Q

What will this code output?

java
Copy code
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.set(1, "Charlie");
System.out.println(names);
A) [Alice, Bob]
B) [Alice, Charlie]
C) [Charlie, Alice]
D) The code will not compile.</String>

A

B) [Alice, Charlie]
Explanation: The set method replaces the element at index 1 (Bob) with “Charlie”.

24
Q

The ArrayList class has a capacity that determines how many elements it can hold before resizing.
A) True
B) False

A

A) True
Explanation: The default capacity of an ArrayList is 10, but it can resize dynamically.

25
Q

Which of the following statements correctly initializes an ArrayList with a custom capacity of 50?
A) ArrayList<String> list = new ArrayList<>(50);
B) ArrayList<String> list = new ArrayList<String>();
C) ArrayList<String> list = new ArrayList<>();
D) ArrayList<String> list = new ArrayList(50);</String></String></String></String></String>

A

A) ArrayList<String> list = new ArrayList<>(50);
Explanation: This is the correct syntax to initialize an ArrayList with a specific capacity.</String>

26
Q

What will this code do?

java
Copy code
int[] arr = {5, 10, 15, 20};
System.out.println(arr.length);
A) It prints 4.
B) It prints the last element of the array.
C) It prints the sum of all elements.
D) It will not compile.

A

A) It prints 4.
Explanation: The length field of an array returns the number of elements in the array.

27
Q

The length of a string in Java is accessed as a field, not a method.
A) True
B) False

A

B) False
Explanation: The length of a string is accessed using the length() method, unlike arrays that use the length field.

28
Q

What will this code output?

java
Copy code
String[] names = {“Alice”, “Bob”, “Charlie”};
System.out.println(names[1].toUpperCase());
A) BOB
B) Bob
C) Charlie
D) The code will not compile.

A

A) BOB
Explanation: The toUpperCase() method converts the string “Bob” to uppercase.

29
Q

Which of the following is NOT an advantage of using ArrayList over an array?
A) Automatically resizes when needed.
B) Allows duplicate elements.
C) Provides built-in methods for manipulation.
D) Faster than arrays for all operations.

A

D) Faster than arrays for all operations.
Explanation: ArrayList is slower than arrays for certain operations due to overhead like resizing and dynamic memory allocation.

30
Q

What does the following code snippet demonstrate?

java
Copy code
int[][] ragged = new int[3][];
ragged[0] = new int[2];
ragged[1] = new int[4];
ragged[2] = new int[3];
A) A two-dimensional array.
B) A ragged array.
C) A three-dimensional array.
D) A binary tree structure.

A

B) A ragged array.
Explanation: A ragged array is a two-dimensional array where each row can have a different number of columns.