Compuscholar Chapter 18 Quiz Questions

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

If you want to create a loop that traverses the following array, what are the minimum and maximum valid array index numbers? char[] letters = {'A','B','C','D'};

0 and 3

Given the following code, what are the contents of the letters array after the code runs? char[] letters = {'A','B','C','D'}; for (char value : letters) { value = 'X'; }

A, B, C, D

Given the following code, what is printed to the screen? char[] letters = {'A','B','C','D'}; for (char value : letters { System.out.print( value ); }

ABCD

In an algorithm to find the average value in a numeric array, what key step happens inside the main loop body?

Add the current array element value to the sum that holds the total value of all array elements

What will be printed to the screen by the following code? char[] letters = {'A','B','C','D'}; for (int i=letters.length -1; i>=0; i--) { System.out.print(letters[i]); }

DCBA

In an algorithm to find the minimum value in an array, what key step is taken inside the main loop?

If the current array element is less than the current minimum, replace the current minimum with that lower value

In an algorithm to search an array and report if "at least one" element matches a target value, what key step happens inside the loop body?

If the current element value matches the target value, report the match and exit the loop immediately

Given the following code, what happens on the last loop iteration? char[] letters = {'A','B','C','D'}; for (int i=0; i<= letters.length; i++) { System.out.print(letters[i]); }

The program will throw an ArrayIndexOutOfBoundsException

Which answer best describes the looping structure needed by an algorithm to count the number of consecutive, duplicate values in an array? a. A single loop that iterates over the entire array sequentially b. Nested loops that both iterate over the entire array c. A single loop that iterates over the first half of the array d. Nested loops where the outer loop iterates over the first half and the inner loop iterates over the second half

a. A single loop that iterates over the entire array sequentially

Which of the following tools may help you design an array algorithm? a. Both of these may be useful b. Neither of these can be useful c. Flowchart d. Pseudocode

a. Both of these may be useful

If an array named "hobbies" has been initialized to hold 4 String values, which of the following statements will correctly read the last element in the array?

a. String hobby = hobbies[3];

Which of the following best defines the term "algorithm"? a. The exact sequence of steps a program takes to solve a problem b. The choice you have to make between for(), while(), and enhanced for() loops c. The art of drawing useful diagrams to illustrate your code d. The process of setting up a loop to correctly iterate over an array

a. The exact sequence of steps a program takes to solve a problem

You can best use an enhanced for() loop to iterate over an array in which of the following scenarios? a. You need to iterate from beginning to end, without a numeric index and without changing any element values b. You only want to iterate over part of the array c. You want to iterate over the entire array backward, from the last element to the first d. You need to use the loop's numeric index for other calculations, in addition to accessing the array element

a. You need to iterate from beginning to end, without a numeric index and without changing any element values

If you want to use a while() loop to iterate over an array from first to the last element, which of the following statements is true? a. You should change the array index variable inside the loop body to avoid an infinite loop b. All of these are true c. You should create a while() logical expression that ends the loop when the index becomes too large d. You should declare an array index variable outside the loop

b. All of these are true

Which of the following is a limitation of the enhanced for() loop? a. Enhanced for() loops will only iterate over the entire array sequentially, from first to last element b. Enhanced for() loops do not allow you to change the original array element values c. Both of these are limitations d. Neither of these are limitations

c. Both of these are limitations

If a Frog class has been defined and an array of Frogs has been defined as shown below, then after the statement runs, which of the following is true? Frog[] myPets = new Frog[4]; a. This statement will not run, because you don't declare arrays of objects this way b. One Frog object has been created and that reference was copied to each of the array elements c. Each array element initially contains null, and you still need to create a new Frog object to assign to each element d. Each array element contains a valid Frog object

c. Each array element initially contains null, and you still need to create a new Frog object to assign to each element

If you want to write an efficient algorithm to reverse the contents of an array "in place" (by modifying the original array), which of the following best describes an overall algorithm that will work? a. Iterate over the entire array, swapping each element with the element to the right b. Iterate from the middle to the end, swapping each element with the very first element in the array c. Iterate from the start to the middle, swapping each element with the matching position calculated from the end to the middle d. Iterate over the entire array backward, swapping each element with the element to the left

c. Iterate from the start to the middle, swapping each element with the matching position calculated from the end to the middle

Which of the following statements about arrays are true? a. One array can hold different types of data in each element b. Arrays can hold up to 256 elements c. Each element in the array is uniquely identified by a letter starting with 'A' d. An array can hold any type of data, including primitive data and object references, as long as all elements are of the same type

d. An array can hold any type of data, including primitive data and object references, as long as all elements are of the same type

Which answer best describes the looping structure in an algorithm to count the number of duplicates of a specific value within an array? a. Set up nested loops, with the outer loop iterating from first to last and the inner loop iterating from last to first b. Set up nested loops, with both loops iterating over the entire array sequentially from first to last c. Set up nested loops, with the outer loop iterating over the first half of the array and the inner loop iterating over the last half of the array d. Set up nested loops, with the outer loop iterating over all elements and the inner loop iterating from the "next" element to the end of the array

d. Set up nested loops, with the outer loop iterating over all elements and the inner loop iterating from the "next" element to the end of the array

Which of the following statements about algorithms is true? a. All array algorithms must iterate over the entire array, sequentially from first to last element b. There is only one correct way to write any algorithm c. All algorithms must be defined by a flowchart before being implemented in Java code d. There are often multiple ways to write each algorithm

d. There are often multiple ways to write each algorithm

Which of the following statements correctly declares and initializes an array of 6 double values? a. double myRaceTimes[6]; b. double[6] myRaceTimes; c. double myRaceTimes = new double(6); d. double[] myRaceTimes = new double[6];

d. double[] myRaceTimes = new double[6];

Given the following array, which answer correctly sets up an enhanced for() loop to traverse the array? char[] letters = {'A','B','C','D'};

for (char value : letters)

Given the following array, which answer correctly sets up a for() loop that will iterate over every array element, from first to last? char[] letters = {'A','B','C','D'};

for (int i=0; i<letters.length; i++)

Given the code below, what phrase would replace the ??? marks to initialize the numHobbies variable to the number of elements in the array? String[] hobbies = {"Basketball","Video Games","Running","Singing","Poetry"};int numHobbies = ????;

hobbies.length

In an algorithm to shift array elements to the right, what key step happens inside the main loop body? Assume "index" is a valid array index, the array name is "targetArray", and the loop is iterating from right to left towards the beginning of the array.

targetArray[index+1] = targetArray[index];


Kaugnay na mga set ng pag-aaral

Chapter 45 Pathophysiology NCLEX-Style Review Questions

View Set

Advanced Project Management Assessment 1

View Set

DECA- Business Economics Worksheet 1

View Set