Deck 7: Arrays and Arraylists
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/41
Play
Full screen (f)
Deck 7: Arrays and Arraylists
1
Consider array items,which contains the values 0,2,4,6 and 8.If method changeArray is called with the method call changeArray( items,items[ 2 ] ),what values are stored in items after the method has finished executing? public static void changeArray( int[] passedArray,int value )
{
PassedArray[ value ] = 12;
Value = 5;
} // end method changeArray
A)0,2,5,6,12.
B)0,2,12,6,8.
C)0,2,4,6,5.
D)0,2,4,6,12.
{
PassedArray[ value ] = 12;
Value = 5;
} // end method changeArray
A)0,2,5,6,12.
B)0,2,12,6,8.
C)0,2,4,6,5.
D)0,2,4,6,12.
D
2
A programmer must do the following before using an array:
A)declare then reference the array.
B)create then declare the array.
C)create then reference the array.
D)declare then create the array.
A)declare then reference the array.
B)create then declare the array.
C)create then reference the array.
D)declare then create the array.
D
3
Consider the code segment below.Which of the following statements is false? int[] g;
G = new int[ 23 ];
A)The first statement declares an array reference.
B)The second statement creates the array.
C)g is a reference to an array of integers.
D)The value of g[ 3 ] is -1.
G = new int[ 23 ];
A)The first statement declares an array reference.
B)The second statement creates the array.
C)g is a reference to an array of integers.
D)The value of g[ 3 ] is -1.
D
4
Which of the following tasks cannot be performed using an enhanced for loop?
A)Calculating the product of all the values in an array.
B)Displaying all even element values in an array.
C)Comparing the elements in an array to a specific value.
D)Incrementing the value stored in each element of the array.
A)Calculating the product of all the values in an array.
B)Displaying all even element values in an array.
C)Comparing the elements in an array to a specific value.
D)Incrementing the value stored in each element of the array.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
5
Attempting to access an array element out of the bounds of an array,causes a(n) .
A)ArrayOutOfBoundsException.
B)ArrayElementOutOfBoundsException.
C)ArrayIndexOutOfBoundsException.
D)ArrayException.
A)ArrayOutOfBoundsException.
B)ArrayElementOutOfBoundsException.
C)ArrayIndexOutOfBoundsException.
D)ArrayException.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
6
Consider the array: s[ 0 ] = 7
S[ 1 ] = 0
S[ 2 ] = -12
S[ 3 ] = 9
S[ 4 ] = 10
S[ 5 ] = 3
S[ 6 ] = 6
The value of s[ s[ 6 ] - s[ 5 ] ] is:
A)0.
B)3.
C)9.
D)0.
S[ 1 ] = 0
S[ 2 ] = -12
S[ 3 ] = 9
S[ 4 ] = 10
S[ 5 ] = 3
S[ 6 ] = 6
The value of s[ s[ 6 ] - s[ 5 ] ] is:
A)0.
B)3.
C)9.
D)0.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
7
Which of the following will not produce a compiler error?
A)Changing the value of a constant after it is declared.
B)Changing the value at a given index of an array after it is created.
C)Using a final variable before it is initialized.
D)All of the above will produce compiler errors.
A)Changing the value of a constant after it is declared.
B)Changing the value at a given index of an array after it is created.
C)Using a final variable before it is initialized.
D)All of the above will produce compiler errors.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
8
Which statement correctly passes the array items to method takeArray? Array items contains 10 elements.
A)takeArray( items[] ).
B)takeArray( items ).
C)takeArray( items[ 9 ] ).
D)Arrays cannot be passed to methods-each item must be sent to the method separately.
A)takeArray( items[] ).
B)takeArray( items ).
C)takeArray( items[ 9 ] ).
D)Arrays cannot be passed to methods-each item must be sent to the method separately.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
9
What do the following statements do? double[] array;
Array = new double[ 14 ];
A)Create a double array containing 13 elements.
B)Create a double array containing 14 elements.
C)Create a double array containing 15 elements.
D)Declare but do not create a double array.
Array = new double[ 14 ];
A)Create a double array containing 13 elements.
B)Create a double array containing 14 elements.
C)Create a double array containing 15 elements.
D)Declare but do not create a double array.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
10
Consider integer array values,which contains 5 elements.Which statements successfully swap the contents of the array at index 3 and index 4?
A)values[ 3 ] = values[ 4 ]; values[ 4 ] = values[ 3 ];
B)values[ 4 ] = values[ 3 ]; values[ 3 ] = values[ 4 ];
C)int temp = values[ 3 ]; values[ 3 ] = values[ 4 ];
Values[ 4 ] = temp;
D)int temp = values[ 3 ]; values[ 3 ] = values[ 4 ];
Values[ 4 ] = values[ 3 ];
A)values[ 3 ] = values[ 4 ]; values[ 4 ] = values[ 3 ];
B)values[ 4 ] = values[ 3 ]; values[ 3 ] = values[ 4 ];
C)int temp = values[ 3 ]; values[ 3 ] = values[ 4 ];
Values[ 4 ] = temp;
D)int temp = values[ 3 ]; values[ 3 ] = values[ 4 ];
Values[ 4 ] = values[ 3 ];
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
11
Which flag in a format specifier indicates that values with fewer digits than the field width should begin with a leading 0?
A)p.
B)l.
C)w.
D)0.
A)p.
B)l.
C)w.
D)0.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
12
In this question,assume a class,Book,has been defined.Which set of statements creates an array of Book objects?
A)Book[] books; books = new Book[ numberElements ];
B)Book[] books]; books = new Book()[ numberElements ];
C)new Book()books[]; books = new Book[ numberElements ];
D)All of the above.
A)Book[] books; books = new Book[ numberElements ];
B)Book[] books]; books = new Book()[ numberElements ];
C)new Book()books[]; books = new Book[ numberElements ];
D)All of the above.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
13
Which expression adds 1 to the element of array arrayName at index i?
A)++arrayName[ i ].
B)arrayName++[ i ].
C)arrayName[ i++ ].
D)None of the above.
A)++arrayName[ i ].
B)arrayName++[ i ].
C)arrayName[ i++ ].
D)None of the above.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
14
Which of the following initializer lists would correctly set the elements of array n?
A)int[] n = { 1,2,3,4,5 };.
B)array n[ int ] = { 1,2,3,4,5 };.
C)int n[ 5 ] = { 1;2;3;4;5 };.
D)int n = new int( 1,2,3,4,5 );.
A)int[] n = { 1,2,3,4,5 };.
B)array n[ int ] = { 1,2,3,4,5 };.
C)int n[ 5 ] = { 1;2;3;4;5 };.
D)int n = new int( 1,2,3,4,5 );.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
15
Which of the following statements is false?
A)An exception indicates a problem that occurs while a program executes.
B)Exception handling enables you to create fault-tolerant programs that can resolve (or handle)exceptions-in many cases,this allows a program to continue executing as if no problems were encountered.
C)The catch block contains the code that might throw an exception,and the try block contains the code that handles the exception if one occurs.
D)Inside the catch block,you can use the parameter's identifier to interact with a caught exception object.
A)An exception indicates a problem that occurs while a program executes.
B)Exception handling enables you to create fault-tolerant programs that can resolve (or handle)exceptions-in many cases,this allows a program to continue executing as if no problems were encountered.
C)The catch block contains the code that might throw an exception,and the try block contains the code that handles the exception if one occurs.
D)Inside the catch block,you can use the parameter's identifier to interact with a caught exception object.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
16
Which of the following statements about creating arrays and initializing their elements is false?
A)The new keyword should be used to create an array.
B)When an array is created,the number of elements must be placed in square brackets following the type of element being stored.
C)The elements of an array of integers have a value of null before they are initialized.
D)A for loop is commonly used to set the values of the elements of an array.
A)The new keyword should be used to create an array.
B)When an array is created,the number of elements must be placed in square brackets following the type of element being stored.
C)The elements of an array of integers have a value of null before they are initialized.
D)A for loop is commonly used to set the values of the elements of an array.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
17
Assume array items contains the integer values 0,2,4,6 and 8.Which of the following set of statements uses the enhanced for loop to display each value in array items?
A)for ( int i = 0;i < items.length;i++ ) System.out.prinf( "%d\n",items[ i ] );
B)for ( int i : items ) System.out.prinf( "%d\n",items[ i ] );
C)for ( int i : items ) System.out.prinf( "%d\n",i );
D)for ( int i = 0 : items.length ) System.out.prinf( "%d\n",items[ i ] );
A)for ( int i = 0;i < items.length;i++ ) System.out.prinf( "%d\n",items[ i ] );
B)for ( int i : items ) System.out.prinf( "%d\n",items[ i ] );
C)for ( int i : items ) System.out.prinf( "%d\n",i );
D)for ( int i = 0 : items.length ) System.out.prinf( "%d\n",items[ i ] );
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
18
Arrays are:
A)variable-length entities.
B)fixed-length entities.
C)data structures that contain up to 10 related data items.
D)used to draw a sequence of lines,or "rays."
A)variable-length entities.
B)fixed-length entities.
C)data structures that contain up to 10 related data items.
D)used to draw a sequence of lines,or "rays."
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
19
Constant variables also are called .
A)write-only variables.
B)finals.
C)named constants.
D)All of the above.
A)write-only variables.
B)finals.
C)named constants.
D)All of the above.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
20
Invalid possibilities for array indices include .
A)Positive integers.
B)Negative integers.
C)Zero.
D)None of the above.
A)Positive integers.
B)Negative integers.
C)Zero.
D)None of the above.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
21
Which statement below initializes array items to contain 3 rows and 2 columns?
A)int[][] items = { { 2,4 },{ 6,8 },{ 10,12 } };.
B)int[][] items = { { 2,6,10 },{ 4,8,12 } };.
C)int[][] items = { 2,4 },{ 6,8 },{ 10,12 };.
D)int[][] items = { 2,6,10 },{ 4,8,12 };.
A)int[][] items = { { 2,4 },{ 6,8 },{ 10,12 } };.
B)int[][] items = { { 2,6,10 },{ 4,8,12 } };.
C)int[][] items = { 2,4 },{ 6,8 },{ 10,12 };.
D)int[][] items = { 2,6,10 },{ 4,8,12 };.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
22
In array items,which expression below accesses the value at row 3 and column 4?
A)items[ 3 ].[ 4 ].
B)items[ 3[ 4 ] ].
C)items[ 3 ][ 4 ].
D)items[ 3,4 ].
A)items[ 3 ].[ 4 ].
B)items[ 3[ 4 ] ].
C)items[ 3 ][ 4 ].
D)items[ 3,4 ].
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
23
What kind of application tests a class by creating an object of that class and calling the class's methods?
A)Pseudo application.
B)Debugger.
C)Tester.
D)Test harness.
A)Pseudo application.
B)Debugger.
C)Tester.
D)Test harness.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
24
Class Arrays methods sort,binarySearch,equals and fill are overloaded for primitive-type arrays and Object arrays.In addition,methods __________ and __________ are overloaded with generic versions.
A)sort,binarySearch.
B)sort,fill.
C)binarySearch,equals.
D)binarySearch,fill.
A)sort,binarySearch.
B)sort,fill.
C)binarySearch,equals.
D)binarySearch,fill.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
25
Which command below runs TestProgram,and passes in the values files.txt and 3?
A)java TestProgram files.txt 3.
B)java TestProgram files.txt,3.
C)java TestProgram "files.txt","3".
D)java TestProgram (the arguments files.txt and 3 were passed in when the application was compiled).
A)java TestProgram files.txt 3.
B)java TestProgram files.txt,3.
C)java TestProgram "files.txt","3".
D)java TestProgram (the arguments files.txt and 3 were passed in when the application was compiled).
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
26
Class Arrays provides method __________ for comparing arrays.
A)compare.
B)compares.
C)equal.
D)equals.
A)compare.
B)compares.
C)equal.
D)equals.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
27
Which method call converts the value in variable stringVariable to an integer?
A)Convert.toInt( stringVariable ).
B)Convert.parseInt( stringVariable ).
C)Integer.parseInt( stringVariable ).
D)Integer.toInt( stringVariable ).
A)Convert.toInt( stringVariable ).
B)Convert.parseInt( stringVariable ).
C)Integer.parseInt( stringVariable ).
D)Integer.toInt( stringVariable ).
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
28
Which set of statements totals the items in each row of two-dimensional array items,and displays each total?
A)int total = 0;
For ( int row = 0;row < items.length;row++ )
{
Total = 0;
For ( int column = 0;column < a[ row ].length;column++ )
Total += a[ row ][ column ];
System.out.printf( "%d\n",total );
}
B)int total = 0;
For ( int row = 0;row < items.length;row++ )
{
For ( int column = 0;column < a[ row ].length;column++ )
Total += a[ row ][ column ];
System.out.printf( "%d\n",total );
}
C)int total = 0;
For ( int row = 0;row < items.length;row++ )
{
For ( int column = 0;column < a[ column ].length;column++ )
Total += a[ row ][ column ];
System.out.printf( "%d\n",total );
}
D)int total = 0;
For ( int row = 0;row < items.length;row++ )
{
Total = 0;
For ( int column = 0;column < a[ column ].length;column++ )
Total += a[ row ][ column ];
System.out.printf( "%d\n",total );
}
A)int total = 0;
For ( int row = 0;row < items.length;row++ )
{
Total = 0;
For ( int column = 0;column < a[ row ].length;column++ )
Total += a[ row ][ column ];
System.out.printf( "%d\n",total );
}
B)int total = 0;
For ( int row = 0;row < items.length;row++ )
{
For ( int column = 0;column < a[ row ].length;column++ )
Total += a[ row ][ column ];
System.out.printf( "%d\n",total );
}
C)int total = 0;
For ( int row = 0;row < items.length;row++ )
{
For ( int column = 0;column < a[ column ].length;column++ )
Total += a[ row ][ column ];
System.out.printf( "%d\n",total );
}
D)int total = 0;
For ( int row = 0;row < items.length;row++ )
{
Total = 0;
For ( int column = 0;column < a[ column ].length;column++ )
Total += a[ row ][ column ];
System.out.printf( "%d\n",total );
}
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
29
Which of the following statements creates a multidimensional array with 3 rows,where the first row contains 1 element,the second row contains 4 elements and the final row contains 2 elements?
A)int[][] items = { { 1,null,null,null },{ 2,3,4,5 },{ 6,7,null,null } };.
B)int[][] items = { { 1 },{ 2,3,4,5 },{ 6,7 } };.
C)int[][] items = { { 1 },{ 2,3,4,5 },{ 6,7 },{} );.
D)int[][] items = { { 1 },{ 4 },{ 2 } };.
A)int[][] items = { { 1,null,null,null },{ 2,3,4,5 },{ 6,7,null,null } };.
B)int[][] items = { { 1 },{ 2,3,4,5 },{ 6,7 } };.
C)int[][] items = { { 1 },{ 2,3,4,5 },{ 6,7 },{} );.
D)int[][] items = { { 1 },{ 4 },{ 2 } };.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
30
An argument type followed by a(n) in a method's parameter list indicates that the method receives a variable number of arguments of that particular type.
A)square brackets ([]).
B)ellipsis (…).
C)varargs keyword.
D)All of the above are acceptable to indicate a variable number of arguments.
A)square brackets ([]).
B)ellipsis (…).
C)varargs keyword.
D)All of the above are acceptable to indicate a variable number of arguments.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
31
In Java,multidimensional arrays:
A)are not directly supported.
B)are implemented as arrays of arrays.
C)are often used to represent tables of values.
D)All of the above.
A)are not directly supported.
B)are implemented as arrays of arrays.
C)are often used to represent tables of values.
D)All of the above.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
32
Which of the following is false?
A)The size of an ArrayList can be determined via its length instance variable.
B)The size of an ArrayList can be determined via its size method.
C)You can add a new item to the end of an ArrayList with its add method.
C)You can get an item from a specified index in an ArrayList with its get method.
A)The size of an ArrayList can be determined via its length instance variable.
B)The size of an ArrayList can be determined via its size method.
C)You can add a new item to the end of an ArrayList with its add method.
C)You can get an item from a specified index in an ArrayList with its get method.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
33
An array with m rows and n columns is not: A.An m-by-n array.
B)An n-by-m array.
C)A two-dimensional array.
D)A dual-transcripted array.
A)A and C.
B)A and D.
C)B and D.
D)B and C.
B)An n-by-m array.
C)A two-dimensional array.
D)A dual-transcripted array.
A)A and C.
B)A and D.
C)B and D.
D)B and C.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
34
The preferred way to traverse a two-dimensional array is to use .
A)a do while statement.
B)a for statement.
C)two nested for statements.
D)three nested for statements.
A)a do while statement.
B)a for statement.
C)two nested for statements.
D)three nested for statements.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
35
When an argument is passed by reference:
A)a copy of the argument's value is passed to the called method.
B)changes to the argument do not affect the original variable's value in the caller.
C)the called method can access the argument's value in the caller directly and modify that data.
D)the original value is removed from memory.
A)a copy of the argument's value is passed to the called method.
B)changes to the argument do not affect the original variable's value in the caller.
C)the called method can access the argument's value in the caller directly and modify that data.
D)the original value is removed from memory.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
36
Which set of statements totals the values in two-dimensional int array items?
A)int total = 0;
For ( int subItems : items )
For ( int item : subItems )
Total += item;
B)int total = 0;
For ( int item: int[] subItems : items )
Total += item;
C)int total = 0;
For ( int[] subItems : items )
For ( int item : items )
Total += item;
D)int total = 0;
For ( int[] subItems : items )
For ( int item : subItems )
Total += item;
A)int total = 0;
For ( int subItems : items )
For ( int item : subItems )
Total += item;
B)int total = 0;
For ( int item: int[] subItems : items )
Total += item;
C)int total = 0;
For ( int[] subItems : items )
For ( int item : items )
Total += item;
D)int total = 0;
For ( int[] subItems : items )
For ( int item : subItems )
Total += item;
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
37
Class ________ represents a dynamically resizable array-like data structure.
A)Array.
B)ArrayList.
C)Arrays.
D)None of the above.
A)Array.
B)ArrayList.
C)Arrays.
D)None of the above.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
38
Which of the following sets of statements creates a multidimensional array with 3 rows,where the first row contains 1 value,the second row contains 4 items and the final row contains 2 items?
A)int[][] items; items = new int[ 3 ][ ? ];
Items[ 0 ] = new int[ 1 ];
Items[ 1 ] = new int[ 4 ];
Items[ 2 ] = new int[ 2 ];
B)int[][] items; items = new int[ 3 ][ ];
Items[ 0 ] = new int[ 1 ];
Items[ 1 ] = new int[ 4 ];
Items[ 2 ] = new int[ 2 ];
C)int[][] items; items = new int[ ? ][ ? ];
Items[ 0 ] = new int[ 1 ];
Items[ 1 ] = new int[ 4 ];
Items[ 2 ] = new int[ 2 ];
D)int[][] items; items[ 0 ] = new int[ 1 ];
Items[ 1 ] = new int[ 4 ];
Items[ 2 ] = new int[ 2 ];
A)int[][] items; items = new int[ 3 ][ ? ];
Items[ 0 ] = new int[ 1 ];
Items[ 1 ] = new int[ 4 ];
Items[ 2 ] = new int[ 2 ];
B)int[][] items; items = new int[ 3 ][ ];
Items[ 0 ] = new int[ 1 ];
Items[ 1 ] = new int[ 4 ];
Items[ 2 ] = new int[ 2 ];
C)int[][] items; items = new int[ ? ][ ? ];
Items[ 0 ] = new int[ 1 ];
Items[ 1 ] = new int[ 4 ];
Items[ 2 ] = new int[ 2 ];
D)int[][] items; items[ 0 ] = new int[ 1 ];
Items[ 1 ] = new int[ 4 ];
Items[ 2 ] = new int[ 2 ];
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
39
For the array in the previous question,what is the value returned by items[ 1 ][ 0 ]?
A)4.
B)8.
C)12.
D)6.
A)4.
B)8.
C)12.
D)6.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
40
Which method sets the background color of a JPanel?
A)setBack.
B)setBackground.
C)setBackgroundColor.
D)setColor.
A)setBack.
B)setBackground.
C)setBackgroundColor.
D)setColor.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck
41
Which of the following statements about an arc is false?
A)An arc is a section of an oval.
B)The sweep is the amount of arc to cover.
C)Method drawArc draws the edges of an arc.
D)The fillArc method draws an oval,with the section that is an arc filled in.
A)An arc is a section of an oval.
B)The sweep is the amount of arc to cover.
C)Method drawArc draws the edges of an arc.
D)The fillArc method draws an oval,with the section that is an arc filled in.
Unlock Deck
Unlock for access to all 41 flashcards in this deck.
Unlock Deck
k this deck