CompuScholar Chapter 18

Ace your homework & exams now with 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

Which answer best describes the looping structure needed by an algorithm to count the number of consecutive, duplicate values in an array?

A single loop that iterates over the entire array sequentially

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

Which of the following statements about arrays are true?

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

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

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

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

Which of the following is a limitation of the enhanced for() loop?

Enhanced for() loops do not allow you to change the original array element values Enhanced for() loops will only iterate through the entire array sequentially, from first to last element

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

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?

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

Which of the following tools may help you design an array algorithm?

Pseudocode & Flowchart

Which answer best describes the looping structure in an algorithm to count the number of duplicates of a specific value within an array?

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

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?

String hobby = hobbies[3];

Which of the following best defines the term "algorithm"?

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

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 of the following statements about algorithms is true?

There are often multiple ways to write each algorithm

You can best use an enhanced for() loop to iterate over an array in which of the following scenarios?

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?

You should declare an array index variable outside the loop You should change the array index variable inside the loop body to avoid an infinite loop You should create a while() logical expression that ends the loop when the index becomes too large

Which of the following statements correctly declares and initializes an array of 6 double values?

double[] myRaceTimes = new double[6];

Given the following array, which 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 vairable 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];


Related study sets

Lec 3, pt 1: HA Assessment Techniques

View Set

ch. 13 Medical Math, ch. 7:4 Skeletal System, ch. 7 Anatomy and Physiology

View Set

Ap Euro Semester 1 Multiple Choice

View Set

Kyle and Carman: Essentials of Pediatric Nursing

View Set

Intro to African History Final Exam

View Set

Phase II: Quiz 4 (100 Questions)

View Set