com sci

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

When the line of code below is run, what values will be stored in each of the array elements? int[] teamScores = new int[5];

0

What values are stored in the teamScores array elements after the following code is run? int[] teamScores = new int[5]; for (int i=2; i<teamScores.length; i++) { teamScores[i] = i; }

0 0 2 3 4

What are the minimum and maximum valid array index values for a 5-element array?

0 and 4

What will be displayed by the following code? int[] teamScores = {1,2,3,4,5}; for (int score : teamScores) { System.out.print(score + " "); }

1 2 3 4 5

If the Score class has been defined, how many unique Score objects are created by the following code? Score[] teamScores = new Score[4]; teamScores[0] = new Score(); teamScores[1] = new Score(); teamScores[2] = teamScores[1];

2

If the Score class has been defined, what is the value in the size variable after the following code runs? Score[] teamScores = new Score[4]; teamScores[0] = new Score(); teamScores[1] = new Score(); teamScores[2] = teamScores[1]; int size = teamScores.length;

4

Study the code below. What will be displayed to the screen when the code runs? int[] teamScores = {1,2,3,4,5}; for (int i=teamScores.length - 1; i>= 0; i-=2) { System.out.print(teamScores[i] + " "); }

5 3 1

What is the main difference between a singly-linked list and a doubly-linked list?

A singly-lined list can only be traversed from front to back; a doubly-linked list can also be traversed backward

Which situation describes an ideal use for a LinkedList?

All of these are correct

Given a simple array named "simpleClasses" and an ArrayList named "listClasses", which of the following answers correctly pairs the same kind of operation on each data structure?

All of these operations are correctly paired

If your algorithm uses multiple ArrayLists to hold values that are related to each other, which of the following is normally a key assumption?

Both of these are key assumptions

What Java concept defines a group of individual elements without specifying any order of those elements?

Collection

7. Study the following code carefully. What will be displayed on the screen when the code runs? LinkedList<String> myClasses = new LinkedList<String>(); myClasses.add("Math"); myClasses.add("History"); myClasses.addFirst("English"); myClasses.addLast("Gym"); for (int i=myClasses.size()-1; i>= 0; i--) { System.out.print(myClasses.get(i) + ", "); }

Gym, History, Math, English

What will be the order of elements stored in the ArrayList when the following code is run? ArrayList<String> myClasses = new ArrayList<String>();myClasses.add("Math");myClasses.add(0,"History"); myClasses.add(1,"English"); myClasses.add("Gym");

History, English, Math, Gym

The following code uses an enhanced for() loop to traverse an ArrayList<String> named myClasses. What is wrong with the code?for (String value : myClasses){ System.out.println( value ); }

Nothing is wrong; the code will print all of the ArrayList elements

Which answer best describes an overall approach for an algorithm to shift all of the values in an array one element to the right?

Set up a single loop that goes from the first to last element, moving the value at the current index to the element at index+1

What key feature does a Java List add on top of a Collection?

The List adds a sense of order to the elements

The following code uses an Iterator to traverse an ArrayList<String> named myClasses. What is wrong with the code?for (Iterator<String> myIterator=myClasses.iterator(); myIterator.next(); ){ System.out.println( myIterator.hasNext() ); }

The calls to next() and hasNext() are in the wrong places

Given three possible data structures below, what would be true about using enhanced for() loops to traverse each of the data sets?String[] myClasses;LinkedList<String> myClasses; ArrayList<String> myClasses;

The enhanced for() loop would work exactly the same way on all of these data structures

You want to store the String names of your pets in a List. What risk comes from using the following variable declaration?List myPets;

This plain List definition will store any kind of Object reference, not just Strings

What is the purpose of the following pseudocode algorithm? CREATE array OF ints DECLARE result AS 0 FOR index FROM 0 to size - 1 IF array[index] = array[index + 1] Add 1 TO result END IF ENDFOR PRINT result

To count the number of consecutive duplicate values in the array

What is the purpose of the following algorithm? int teamScores[] = {16, 22, 10, 35, 45}; int target = 10; int result = 0; for (int i=0; i<teamScores.length; i++) { if (teamScores[i] == target) { result++; } } System.out.println("Result: " + result);

To count the number of times the target value appears in the teamScores array

What is the purpose of the following algorithm? int teamScores[] = {16, 22, 10, 35, 45}; int value = 0; for (int i=0; i<teamScores.length; i++) { value += teamScores[i]; } int result = value / teamScores.length; System.out.println("Result: " + result);

To find the average value stored in the teamScores array

What is the purpose of the following algorithm? int teamScores[] = {16, 458, 3, 2890, 5687}; int result = teamScores[0]; for (int i = 1; i < teamScores.length; i++) { if (teamScores[i] > result) { result = teamScores[i]; } } System.out.println("Result: " + result);

To find the maximum value stored in the teamScores array

Which of the following statements about adding or removing elements inside a standard for() loop and an enhanced for() loop is true?

You can do it inside a standard for() loop, but be careful. You can't do it at all inside an enhanced for() loop.

When thinking about algorithms using simple arrays and ArrayLists, which of the following statements is true?

You can use either a simple array or an ArrayList for any algorithm

What key limitation of simple Java arrays does not apply to LInkedLIsts or ArrayLists?

You cannot add or remove elements from a simple array after it is created

What is wrong with the following code? List<String> pets = new List<String>(); pets.add("Lizard");

You cannot allocate a List with the "new" keyword; a List is just an interface and not a concrete class

Which situation describes an ideal use for an ArrayList?

You need to frequently access list items by numeric index

When using a while() loop to iterate over an array, what is true about the numeric index variable used to access the array elements?

You need to manually declare a numeric index before the loop, test it in the logical expression, and update it inside the loop body

What will happen when the following code runs? int[] teamScores = {1,2,3,4,5}; teamScores[5] = 100;

You will get an ArrayIndexOutOfBounds exception

Which of the following types of data can be stored in arrays?

all of these can be stored in arrays

If you have an ArrayList<String> named myClasses, which of the following will set up a standard for() loop to iterate over the entire ArrayList?

for (int i=0; i<myClasses.size(); i++)

Given the statement below, which answer correctly sets up an enhanced for() loop to iterate over the array? int[] teamScores = {1,2,3,4,5};

for (int score : teamScores)

If "value" is an integer, which of the following "if()" statements will be true when that value is even?

if (value % 2 == 0)

If the Score class has been defined, what phrases should replace the ??? marks to set up a standard for() loop to iterate over the entire array? Score[] teamScores = new Score[4]; for (???) { teamScores[i] = new Score(); }

int i=0; i<teamScores.length; i++

Which answer will correctly declare and initialize a 5-element array with specific numeric values?

int[] teamScores = {10,5,17,20,3};

What package should you import at the top of your code for easy (short name) access to the ArrayList and LinkedList classes?

java.util

Which of the following are limitations of the enhanced for() loop?

neither of these are true

Given the code below, which statement will correctly set the very first array element equal to 10? int[] teamScores = new int[5];

teamScores[0] = 10;


संबंधित स्टडी सेट्स

Chapter 11 - How Home Ownership is Held

View Set

week 4 carmen quiz: correlation and regression

View Set

Chapter 11: Nutritional Assessment

View Set

Explorer Study Guide - John Cabot

View Set