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
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/44
Play
Full screen (f)
Deck 7: Arrays and Arraylists
1
Consider the code segment below.Which of the following statements is false?
Int[] g;
G = new int[23];
A)The value of g[3] is -1.
B)The first statement declares an array reference.
C)The second statement creates the array.
D)g is a reference to an array of integers.
Int[] g;
G = new int[23];
A)The value of g[3] is -1.
B)The first statement declares an array reference.
C)The second statement creates the array.
D)g is a reference to an array of integers.
A
2
Attempting to access an array element outside 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.
C
3
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.
C
4
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 with operator new,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 with operator new,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 44 flashcards in this deck.
Unlock Deck
k this deck
5
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[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.
Unlock Deck
Unlock for access to all 44 flashcards in this deck.
Unlock Deck
k this deck
6
Assume class Book has been declared.Which set of statements creates an array of Books?
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 44 flashcards in this deck.
Unlock Deck
k this deck
7
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.
Unlock Deck
Unlock for access to all 44 flashcards in this deck.
Unlock Deck
k this deck
8
Which of the following will not produce a compiler error?
A)Changing the value of a constant after it is initialized.
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 initialized.
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 44 flashcards in this deck.
Unlock Deck
k this deck
9
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 44 flashcards in this deck.
Unlock Deck
k this deck
10
An exception object's ________ method returns the exception's error message.
A)String
B)Message
C)Error
D)toString
A)String
B)Message
C)Error
D)toString
Unlock Deck
Unlock for access to all 44 flashcards in this deck.
Unlock Deck
k this deck
11
How many Book objects are created by the following statement?
Book[] books = new Book[10];
A)10
B)0
C)5
D)None of the above.
Book[] books = new Book[10];
A)10
B)0
C)5
D)None of the above.
Unlock Deck
Unlock for access to all 44 flashcards in this deck.
Unlock Deck
k this deck
12
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.
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 44 flashcards in this deck.
Unlock Deck
k this deck
13
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 44 flashcards in this deck.
Unlock Deck
k this deck
14
Which of the following statements about arrays are true?
A)An array is a group of variables containing values that all have the same type.
B)Elements are located by index.
C)The length of an array c is determined by the expression c.length();.
D)The zeroth element of array c is specified by c[0].
A)A,C,D.
B)A,B,D.
C)C,D.
D)A,B,C,D.
A)An array is a group of variables containing values that all have the same type.
B)Elements are located by index.
C)The length of an array c is determined by the expression c.length();.
D)The zeroth element of array c is specified by c[0].
A)A,C,D.
B)A,B,D.
C)C,D.
D)A,B,C,D.
Unlock Deck
Unlock for access to all 44 flashcards in this deck.
Unlock Deck
k this deck
15
Which of the following statements is true?
A)The catch block contains the code that might throw an exception.
B)The try block contains the code that handles the exception if one occurs.
C)You can have many catch blocks to handle different types of exceptions.
D)When a try block terminates,any variables declared in the try block are preserved.
A)The catch block contains the code that might throw an exception.
B)The try block contains the code that handles the exception if one occurs.
C)You can have many catch blocks to handle different types of exceptions.
D)When a try block terminates,any variables declared in the try block are preserved.
Unlock Deck
Unlock for access to all 44 flashcards in this deck.
Unlock Deck
k this deck
16
A(n)________ indicates a problem that occurs while a program executes.
A)syntax error
B)omitted import
C)missing semicolon
D)exception
A)syntax error
B)omitted import
C)missing semicolon
D)exception
Unlock Deck
Unlock for access to all 44 flashcards in this deck.
Unlock Deck
k this deck
17
Exception handling helps you create ________ programs.
A)high-performance
B)logic-error-free
C)fault-tolerant
D)compilation-error-free
A)high-performance
B)logic-error-free
C)fault-tolerant
D)compilation-error-free
Unlock Deck
Unlock for access to all 44 flashcards in this deck.
Unlock Deck
k this deck
18
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 44 flashcards in this deck.
Unlock Deck
k this deck
19
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 44 flashcards in this deck.
Unlock Deck
k this deck
20
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 44 flashcards in this deck.
Unlock Deck
k this deck
21
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)None of the above.
A)java TestProgram files.txt 3.
B)java TestProgram files.txt,3.
C)java TestProgram "files.txt","3".
D)None of the above.
Unlock Deck
Unlock for access to all 44 flashcards in this deck.
Unlock Deck
k this deck
22
Which of the following statements is false?
A)When an argument is passed by reference,the called method can access the argument's value in the caller directly but cannot modify it.
B)All arguments in Java are passed by value.
C)To pass an individual array element to a method,use the indexed name of the array.
D)To pass an object reference to a method,simply specify in the method call the name of the variable that refers to the object.
A)When an argument is passed by reference,the called method can access the argument's value in the caller directly but cannot modify it.
B)All arguments in Java are passed by value.
C)To pass an individual array element to a method,use the indexed name of the array.
D)To pass an object reference to a method,simply specify in the method call the name of the variable that refers to the object.
Unlock Deck
Unlock for access to all 44 flashcards in this deck.
Unlock Deck
k this deck
23
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 44 flashcards in this deck.
Unlock Deck
k this deck
24
For the array that was the correct answer 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 44 flashcards in this deck.
Unlock Deck
k this deck
25
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 44 flashcards in this deck.
Unlock Deck
k this deck
26
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 44 flashcards in this deck.
Unlock Deck
k this deck
27
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 44 flashcards in this deck.
Unlock Deck
k this deck
28
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 44 flashcards in this deck.
Unlock Deck
k this deck
29
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)Declarator.
D)Test harness.
A)Pseudo application.
B)Debugger.
C)Declarator.
D)Test harness.
Unlock Deck
Unlock for access to all 44 flashcards in this deck.
Unlock Deck
k this deck
30
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 44 flashcards in this deck.
Unlock Deck
k this deck
31
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 44 flashcards in this deck.
Unlock Deck
k this deck
32
When you pass an array or an individual array element of a reference type to a method,the called method receives ________.When you pass an individual element of a primitive type,the called method receives ________.
A)a copy of the element's reference,a copy of the element's reference
B)a copy of the element's value,a copy of the element's reference
C)a copy of the element's value,a copy of the element's value
D)a copy of the element's reference,a copy of the element's value
A)a copy of the element's reference,a copy of the element's reference
B)a copy of the element's value,a copy of the element's reference
C)a copy of the element's value,a copy of the element's value
D)a copy of the element's reference,a copy of the element's value
Unlock Deck
Unlock for access to all 44 flashcards in this deck.
Unlock Deck
k this deck
33
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 44 flashcards in this deck.
Unlock Deck
k this deck
34
Class Arrays provides method __________ for comparing arrays to determine whether they have the same contents.
A)compare
B)compares
C)equal
D)equals
A)compare
B)compares
C)equal
D)equals
Unlock Deck
Unlock for access to all 44 flashcards in this deck.
Unlock Deck
k this deck
35
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 44 flashcards in this deck.
Unlock Deck
k this deck
36
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 44 flashcards in this deck.
Unlock Deck
k this deck
37
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.
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.
Unlock Deck
Unlock for access to all 44 flashcards in this deck.
Unlock Deck
k this deck
38
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;
}
A)0,2,5,6,12.
B)0,2,12,6,8.
C)0,2,4,6,5.
D)0,2,4,6,12.
Public static void changeArray(int[] passedArray,int value)
{
PassedArray[value] = 12;
Value = 5;
}
A)0,2,5,6,12.
B)0,2,12,6,8.
C)0,2,4,6,5.
D)0,2,4,6,12.
Unlock Deck
Unlock for access to all 44 flashcards in this deck.
Unlock Deck
k this deck
39
Assume array items contains the integer values 0,2,4,6 and 8.Which of the following 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 44 flashcards in this deck.
Unlock Deck
k this deck
40
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 44 flashcards in this deck.
Unlock Deck
k this deck
41
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 44 flashcards in this deck.
Unlock Deck
k this deck
42
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 44 flashcards in this deck.
Unlock Deck
k this deck
43
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 44 flashcards in this deck.
Unlock Deck
k this deck
44
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 44 flashcards in this deck.
Unlock Deck
k this deck