Array Basics in AP Computer Science A
If an int
array contains {4, 5, 6, 7}, what will be the output after running an enhanced for loop that prints each element multiplied by two?
The original elements since they cannot be changed in an enhanced for loop.
8 10 12 14
An error message because multiplication is not allowed inside an enhanced for loop.
No output as enhanced for loops do not support integer arrays.
In the enhanced for loop, what is the role of 'type' in the general enhanced for loop header 'for (type element: arrayName)'?
There is no 'type' role in the general enhanced for loop header.
It defines the data type that the programmer wants the array elements to be converted to.
It determines the type of enhanced for loop the loop is.
It represents the data type of the elements in the array.
What is the purpose of the 'continue' keyword in Java's enhanced for loop?
Skips the current iteration and continues with the next one
Stops execution of the current method and returns control to the caller
Exits the current enhanced for loop immediately
Rewrites over the whole loop from the beginning
How does Java determine the number of iterations in an enhanced for loop operating on a String[] colors = {"red", "green", "blue"}
?
Three times because the string is lowercase.
Length of colors array dictates the number of iterations.
Relies on user input to specify how many times to repeat.
Uses a fixed value set at the beginning of the execution of the program.
What will be the content of result after executing the following code snippet if arr contains [5, 10, 15, 20]?
result will contain the value of 2.
result will contain the value of 4.
result will contain the value of 50.
result will contain the value of -1.
Which of the following is true about rewriting an enhanced for loop as another type of loop?
Any enhanced for loop can be rewritten as a for loop or a while loop.
Enhanced for loops cannot be rewritten another type of loop.
Enhanced for loops can only be rewritten as for loops, not while loops.
Enhanced for loops can only be rewritten as while loops, not for loops.
Which statement accurately describes iterating through a double[] arr using an enhanced for-loop?
Iteration order depends on explicitly defined criteria within our code.
It allows direct assignment to alter any double values within arr.
Each iteration provides both value and index from arr.
Each iteration gives us access to a single double value from arr.

How are we doing?
Give us your feedback and let us know how we can improve
Which action allows a programmer to avoid a possible IndexOutOfBoundsException when using an enhanced for loop to iterate through an array?
Wrapping each iteration in a try-catch block specifically for this exception type.
Initializing all elements with non-null values before starting the loop.
Ensuring that all elements accessed within the loop are valid array elements.
Checking each element's index against array length before accessing it.
When implementing a queue that requires frequent enqueueing and dequeueing operations from both ends, which Java Collection Framework data structure offers optimized performance for such tasks?
LinkedList
ArrayDeque
ArrayList
Vector
Which of the following enhanced for loop implementations would correctly sum up all the even numbers in an integer array named nums
?
int sum = 0; for(int num : nums){ int temp = num <<1 ; if(temp == num){sum+=num;}}
for(int i=0; i < nums.length(); i++) { if(nums[i] % 2 == 0) { sum += nums[i]; }}
int sum = 0; for(int num : nums) { if(num / 2 == 0) { sum += num; }}
int sum = 0; for(int num : nums) { if(num % 2 == 0) { sum += num; }}