Deck 8: Single-Dimensional Arrays
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/63
Play
Full screen (f)
Deck 8: Single-Dimensional Arrays
1
We use the new keyword to instantiate an array using an initialization list.
False
2
Before we can instantiate objects inside an array, we must first instantiate the array.
True
3
In order to find the maximum value of an element in an array, we must use the maxVal variable.
False
4
To avoid encapsulation problems resulting from shared array references, it may be necessary to copy the elements of the array into a new or temporary array.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
5
An Insertion Sort works by implementing a triple loop.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
6
During an Insertion Sort, the value of the element that we are currently inserting (at index i) is stored in some temporary variable.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
7
An Insertion Sort runs much more efficiently than a Binary Search.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
8
During a Selection Sort, a portion of an array is known as a(n) __________.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
9
We want to store the number of referrals to our website from the top four search engines (Google, Yahoo, Bing, and Ask). Which of the following array declarations would you use?
A) int [] searchEngineReferrals;
B) int [4] google, yahoo, bing, ask;
C) int [ ] google, [ ] yahoo, [ ] bing, [ ] ask;
D) None of these is correct.
A) int [] searchEngineReferrals;
B) int [4] google, yahoo, bing, ask;
C) int [ ] google, [ ] yahoo, [ ] bing, [ ] ask;
D) None of these is correct.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
10
When we declare an array, we also allocate memory for the array at the same time.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
11
Considering the functionality of length in an array, which of the following statements is true?
A) length functions as an instance variable.
B) length functions as a method.
C) length is always a double.
D) size and length can be used interchangeably.
A) length functions as an instance variable.
B) length functions as a method.
C) length is always a double.
D) size and length can be used interchangeably.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
12
The last element in an array always has an index of 1 less than the number of elements.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
13
What kind of loop is generally employed to print every element of an array, in order?
A) for loop
B) while loop
C) infinite loop
D) zero loop
A) for loop
B) while loop
C) infinite loop
D) zero loop
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
14
The statement for ( int i = 0; i <= arrayName.length; i++ ) has generated a runtime exception. What would the corrected syntax look like?
A) for ( int i = 0; i <= arrayName.length; ++i )
B) for ( int i = 1; i < arrayName.length; i++ )
C) for ( int i = 0; i < arrayName.length( ); i++ )
D) for ( int i = 0; i < arrayName.length; i++ )
A) for ( int i = 0; i <= arrayName.length; ++i )
B) for ( int i = 1; i < arrayName.length; i++ )
C) for ( int i = 0; i < arrayName.length( ); i++ )
D) for ( int i = 0; i < arrayName.length; i++ )
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
15
Which of the following is not an attribute of arrays when used in user-defined classes?
A) They are an instance variable.
B) They are a parameter to a method.
C) They share references with the client.
D) They are a method.
E) They are a local variable inside a method.
A) They are an instance variable.
B) They are a parameter to a method.
C) They share references with the client.
D) They are a method.
E) They are a local variable inside a method.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
16
When defining a method that takes an array as a parameter, we use the following syntax:
accessModifier returnType methodName( arrayName )
accessModifier returnType methodName( arrayName )
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
17
Which of the following statements is not true of a Selection Sort algorithm?
A) It loops through the elements of an array multiple times.
B) It re-orders the elements of an array in ascending or descending order.
C) It stops looping when the subarray has only one element left.
D) It indicates when it is finished by returning -1.
A) It loops through the elements of an array multiple times.
B) It re-orders the elements of an array in ascending or descending order.
C) It stops looping when the subarray has only one element left.
D) It indicates when it is finished by returning -1.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
18
Parallel arrays are best used when we have lists of items of the same length with different data types.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
19
Suppose we want to track the winning lottery numbers (ranging from 1 to 72) and count the number of times each number comes up over the course of a month. In this scenario, we would be wise to:
A) set up an array of counters with 72 elements.
B) set up 72 counters with if/else statements.
C) set up an array of counters with 72 elements and set up 72 counters with if/else statements.
D) None of these is correct.
A) set up an array of counters with 72 elements.
B) set up 72 counters with if/else statements.
C) set up an array of counters with 72 elements and set up 72 counters with if/else statements.
D) None of these is correct.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
20
In a lottery number tracking scenario, in which the number 0 is not a possible winning number and thus would never be counted, it would be wise to create an array with one additional element and ignore element 0 altogether.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
21
Assume we have a situation where the number of inputs cannot possibly be determined until run time. The Java feature that we would use to specify that the method accept a variable number of arguments is called:
A) varparam.
B) varargs.
C) varinput.
D) varoutput.
A) varparam.
B) varargs.
C) varinput.
D) varoutput.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
22
When we have a parameter specified by a method using the varargs syntax, that parameter must be listed as the first one in the method header.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
23
When we instantiate an array:
A) we allocate memory for the array.
B) the elements are given initial values.
C) we use the keyword new.
D) All of these are correct.
A) we allocate memory for the array.
B) the elements are given initial values.
C) we use the keyword new.
D) All of these are correct.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
24
To compare the elements of an array for equality, which of the following actions must we take first?
A) First, make a backup copy of the two arrays.
B) First, make sure the two arrays are the same length.
C) First, run a for loop to compare each array, element to element.
D) None of these is correct.
A) First, make a backup copy of the two arrays.
B) First, make sure the two arrays are the same length.
C) First, run a for loop to compare each array, element to element.
D) None of these is correct.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
25
Which of the following is not a method used to display array data as a bar chart?
A) setFill method
B) fillRect method
C) barStart method
D) fillText method
A) setFill method
B) fillRect method
C) barStart method
D) fillText method
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
26
At the beginning of a Selection Sort, the entire unsorted array is considered to be a subarray.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
27
In the header public static void main( String [ ] args ), the main method receives a String ____________ as a parameter.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
28
The position of an element within an array is known as its __________.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
29
The compareTo method of the String class compares the values of two ____________.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
30
The sorting algorithm that mirrors inserting cards one at a time into a hand is called __________.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
31
Suppose you are looking for the name of your friend, Mike Jones, in the telephone directory. You open the book in the middle and look at the name at the top of the left page; it starts with an L. You mark that page and look halfway between the first page and that page. As you continue to apply this strategy, you are using the _____________ algorithm.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
32
Where is the error in the array declaration int [5] numbers;?
A) There is no error.
B) The square brackets should be empty.
C) There should be no space between int and [.
D) It should be int numbers[5];.
A) There is no error.
B) The square brackets should be empty.
C) There should be no space between int and [.
D) It should be int numbers[5];.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
33
To access the element at index i of an array named grades, we use:
A) grades.
B) grades[ i ].
C) grades( i ).
D) grades{ i }.
A) grades.
B) grades[ i ].
C) grades( i ).
D) grades{ i }.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
34
If an array has n elements, then the last index is:
A) n.
B) n + 1.
C) n − 1.
A) n.
B) n + 1.
C) n − 1.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
35
Which of the following would we use to access the number of elements in an array named grades?
A) grades
B) grades.size( )
C) grades.size
D) grades.length( )
E) grades.length
A) grades
B) grades.size( )
C) grades.size
D) grades.length( )
E) grades.length
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
36
When processing all the elements of an array named grades using a for loop, what is the for loop header?
A) for( int i = 1; i <= grades.length; i++ )
B) for( int i = 1; i < grades.length; i++ )
C) for( int i = 0; i <= grades.length; i++ )
D) for( int i = 0; i < grades.length; i++ )
A) for( int i = 1; i <= grades.length; i++ )
B) for( int i = 1; i < grades.length; i++ )
C) for( int i = 0; i <= grades.length; i++ )
D) for( int i = 0; i < grades.length; i++ )
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
37
When computing the maximum value in an array of integers, you should initialize the maximum value to:
A) 0.
B) 1.
C) the first element in the array.
D) 100.
A) 0.
B) 1.
C) the first element in the array.
D) 100.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
38
Which of the following is correct regarding the method header public static void main( String [ ] args )?
A) args is an array of Strings
B) args is a String
C) args is a void array
D) main is an array
A) args is an array of Strings
B) args is a String
C) args is a void array
D) main is an array
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
39
Insertion Sort uses a:
A) single loop.
B) double loop.
C) triple loop.
A) single loop.
B) double loop.
C) triple loop.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
40
In an array, we can have a mix of integers and Strings.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
41
When declaring an array, we use a set of square brackets.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
42
We can instantiate an array by assigning values when the array is declared.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
43
We cannot combine the declaration and instantiation of an array into one statement.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
44
It is possible to have an array of objects.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
45
To process all the elements of an array, we use a loop.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
46
To compare two arrays for equality, you call the equals method with the first array and pass the second array as the argument.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
47
An array can be an instance variable of a class.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
48
A method cannot return an array.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
49
We can pass an array parameter to a method.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
50
When we instantiate an array, we use the keyword __________.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
51
Looking for a value in an array by looping through all the elements of an array in order is called a(n) __________.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
52
What happens if you try to access an array element using a negative index or an index greater than or equal to the number of elements in the array?
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
53
Why is a NullPointerException generated after the following statements are executed?
String [] words = new String[10];
System.out.println( words[0].length( ) );
String [] words = new String[10];
System.out.println( words[0].length( ) );
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
54
How do you copy an array into another array?
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
55
How do you compare two arrays for equality?
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
56
Name two algorithms for sorting arrays.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
57
We have made an array declaration of double dailyWindSpeeds [ ]; What is another valid way of writing this same declaration with an alternative syntax?
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
58
When an array is created with the new operator, the elements of the array are automatically assigned default values. Can you identify those values for numeric elements, boolean elements, char elements, and object references?
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
59
What is the primary difference in functionality between .length used in an array and length ( ) used in Strings?
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
60
Under what conditions are ArrayIndexOutOfBounds exceptions generated?
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
61
If we use the following code to make a separate copy of the array called testScores, will it work? Why or why not?
int [ ] testScores = {95, 100, 89, 92, 25};
int [ ] copyOfTestScores = testScores;
int [ ] testScores = {95, 100, 89, 92, 25};
int [ ] copyOfTestScores = testScores;
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
62
Assume that we have an array called propertyTaxes whose elements are objects. Using a for loop, we make a copy of that array called propertyTaxesCopy. When an object in propertyTaxes changes, does it have any effect on the object in propertyTaxesCopy? Why or why not?
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck
63
Explain how the Sequential Search algorithm works within unsorted arrays.
Unlock Deck
Unlock for access to all 63 flashcards in this deck.
Unlock Deck
k this deck