Deck 8: 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
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/70
Play
Full screen (f)
Deck 8: Arrays
1
For the questions below, assume values is an int array that is currently filled to capacity, with the following values:

What is the value of values.length?
A) 0
B) 5
C) 6
D) 7
E) 18

What is the value of values.length?
A) 0
B) 5
C) 6
D) 7
E) 18
D
Explanation: D) The length operator for an array returns the size of the array. The above picture shows that that values stores 7 elements and since it is full, the size of values is 7.
Explanation: D) The length operator for an array returns the size of the array. The above picture shows that that values stores 7 elements and since it is full, the size of values is 7.
2
An int array stores the following values. Use the array to answer the questions below.

Which of the following lists of numbers would accurately show the array after the second pass of the Selection Sort algorithm?
A) 9, 4, 12, 2, 6, 8, 18
B) 2, 4, 9, 6, 12, 8, 18
C) 2, 4, 12, 9, 6, 8, 18
D) 2, 4, 6, 8, 9, 12, 18
E) 2, 4, 12, 6, 8, 9, 18

Which of the following lists of numbers would accurately show the array after the second pass of the Selection Sort algorithm?
A) 9, 4, 12, 2, 6, 8, 18
B) 2, 4, 9, 6, 12, 8, 18
C) 2, 4, 12, 9, 6, 8, 18
D) 2, 4, 6, 8, 9, 12, 18
E) 2, 4, 12, 6, 8, 9, 18
C
Explanation: C) After one pass, the array would be 2, 4, 12, 9, 6, 8, 18. The second pass would look to swap the item in array index 1 (4) with the smallest value after 2 (4). So, 4 would swap with 4 and the array would stay the same as it was after the first pass.
Explanation: C) After one pass, the array would be 2, 4, 12, 9, 6, 8, 18. The second pass would look to swap the item in array index 1 (4) with the smallest value after 2 (4). So, 4 would swap with 4 and the array would stay the same as it was after the first pass.
3
Consider the array declaration and instantiation: int[ ] arr = new int[5]; Which of the following is True about arr?
A) It stores 5 elements with legal indices between 1 and 5
B) It stores 5 elements with legal indices between 0 and 4
C) It stores 4 elements with legal indices between 1 and 4
D) It stores 6 elements with legal indices between 0 and 5
E) It stores 5 elements with legal indices between 0 and 5
A) It stores 5 elements with legal indices between 1 and 5
B) It stores 5 elements with legal indices between 0 and 4
C) It stores 4 elements with legal indices between 1 and 4
D) It stores 6 elements with legal indices between 0 and 5
E) It stores 5 elements with legal indices between 0 and 5
B
Explanation: B) Arrays are instantiated with an int value representing their size, or the number of elements that they can store. So, arr can store 5 elements. Further, all arrays start at index 0 and go to index size - 1, so arr has legal indices of 0 through 4.
Explanation: B) Arrays are instantiated with an int value representing their size, or the number of elements that they can store. So, arr can store 5 elements. Further, all arrays start at index 0 and go to index size - 1, so arr has legal indices of 0 through 4.
4
What does the following method do?
Public int question15( )
{
Int value1 = 0;
Int value2 = 0;
For (int j = 0; j < 12; j++)
If (candy[j] > value1)
{
Value1 = candy[j];
Value2 = j;
}
Return value2;
}
A) It returns the total number of candy bars sold
B) It returns the total number of children who sold 0 candy bars
C) It returns the total number of children who sold more than 0 candy bars
D) It returns the number of candy bars sold by the child who sold the most candy bars
E) It returns the index of the child who sold the most candy bars
Public int question15( )
{
Int value1 = 0;
Int value2 = 0;
For (int j = 0; j < 12; j++)
If (candy[j] > value1)
{
Value1 = candy[j];
Value2 = j;
}
Return value2;
}
A) It returns the total number of candy bars sold
B) It returns the total number of children who sold 0 candy bars
C) It returns the total number of children who sold more than 0 candy bars
D) It returns the number of candy bars sold by the child who sold the most candy bars
E) It returns the index of the child who sold the most candy bars
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
5
An int array stores the following values. Use the array to answer the questions below.

How many passes will it take in all for Selection Sort to sort this array?
A) 2
B) 4
C) 5
D) 6
E) 7

How many passes will it take in all for Selection Sort to sort this array?
A) 2
B) 4
C) 5
D) 6
E) 7
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
6
For the questions below, assume an int array, candy, stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all.
What does the following code do?
Scanner scan = Scanner.create(System.in);
Int value1 = scan.nextInt( );
Int value2 = scan.nextInt( );
Bars[value1] += value2;
A) adds 1 to the number of bars sold by child value1 and child value2
B) adds 1 to the number of bars sold by child value1
C) adds value1 to the number of bars sold by child value2
D) adds value2 to the number of bars sold by child value1
E) inputs a new value for the number of bars sold by both child value1 and child value2
What does the following code do?
Scanner scan = Scanner.create(System.in);
Int value1 = scan.nextInt( );
Int value2 = scan.nextInt( );
Bars[value1] += value2;
A) adds 1 to the number of bars sold by child value1 and child value2
B) adds 1 to the number of bars sold by child value1
C) adds value1 to the number of bars sold by child value2
D) adds value2 to the number of bars sold by child value1
E) inputs a new value for the number of bars sold by both child value1 and child value2
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
7
The statement System.out.println(values[7]); will
A) output 7
B) output 18
C) output nothing
D) cause an ArrayOutOfBoundsException to be thrown
E) cause a syntax error
A) output 7
B) output 18
C) output nothing
D) cause an ArrayOutOfBoundsException to be thrown
E) cause a syntax error
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
8
The "off-by-one" error associated with arrays arises because
A) the first array index is 0 and programmers may start at index 1, or may use a loop that goes one index too far
B) the last array index is at length + 1 and loops may only iterate to length, missing one
C) the last array element ends at length - 1 and loops may go one too far
D) programmers write a loop that goes from 0 to length - 1 whereas the array actually goes from 1 to length
E) none of the above, the "off-by-one" error has nothing to do with arrays
A) the first array index is 0 and programmers may start at index 1, or may use a loop that goes one index too far
B) the last array index is at length + 1 and loops may only iterate to length, missing one
C) the last array element ends at length - 1 and loops may go one too far
D) programmers write a loop that goes from 0 to length - 1 whereas the array actually goes from 1 to length
E) none of the above, the "off-by-one" error has nothing to do with arrays
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
9
If int[ ] x = new int[15]; and the statement x[-1] = 0; is executed, then which of the following Exceptions is thrown?
A) IndexOutOfBoundsException
B) ArrayIndexOutOfBoundsException
C) NegativeArraySizeException
D) NullPointException
E) ArithmeticException
A) IndexOutOfBoundsException
B) ArrayIndexOutOfBoundsException
C) NegativeArraySizeException
D) NullPointException
E) ArithmeticException
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
10
What does the following code do? Assume list is an array of int values, temp is some previously initialized int value, and c is an int initialized to 0.
For (int j = 0; j < list.length; j++)
If (list[j] < temp) c++;
A) It finds the smallest value and stores it in temp
B) It finds the largest value and stores it in temp
C) It counts the number of elements equal to the smallest value in list
D) It counts the number of elements in list that are less than temp
E) It sorts the values in list to be in ascending order
For (int j = 0; j < list.length; j++)
If (list[j] < temp) c++;
A) It finds the smallest value and stores it in temp
B) It finds the largest value and stores it in temp
C) It counts the number of elements equal to the smallest value in list
D) It counts the number of elements in list that are less than temp
E) It sorts the values in list to be in ascending order
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
11
If an int array is passed as a parameter to a method, which of the following would adequately define the parameter list for the method header?
A) (int[ ])
B) (int a[ ])
C) (int[ ] a)
D) (int a)
E) (a[ ])
A) (int[ ])
B) (int a[ ])
C) (int[ ] a)
D) (int a)
E) (a[ ])
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
12
In Java, arrays are
A) primitive data types
B) objects
C) interfaces
D) primitive data types if the type stored in the array is a primitive data type and objects if the type stored in the array is an object
E) Strings
A) primitive data types
B) objects
C) interfaces
D) primitive data types if the type stored in the array is a primitive data type and objects if the type stored in the array is an object
E) Strings
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
13
For the questions below, assume values is an int array that is currently filled to capacity, with the following values:
![<strong>For the questions below, assume values is an int array that is currently filled to capacity, with the following values: What is returned by values[3]?</strong> A) 9 B) 12 C) 2 D) 6 E) 3](https://d2lvgg3v3hfg70.cloudfront.net/TB4648/11ea4e28_c821_5f9d_b59b_fbbcbfa674be_TB4648_00_TB4648_00.jpg)
What is returned by values[3]?
A) 9
B) 12
C) 2
D) 6
E) 3
![<strong>For the questions below, assume values is an int array that is currently filled to capacity, with the following values: What is returned by values[3]?</strong> A) 9 B) 12 C) 2 D) 6 E) 3](https://d2lvgg3v3hfg70.cloudfront.net/TB4648/11ea4e28_c821_5f9d_b59b_fbbcbfa674be_TB4648_00_TB4648_00.jpg)
What is returned by values[3]?
A) 9
B) 12
C) 2
D) 6
E) 3
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
14
We compare sorting algorithms by examining
A) the number of instructions executed by the sorting algorithm
B) the number of instructions in the algorithm itself (its length)
C) the types of loops used in the sorting algorithm
D) the amount of memory space required by the algorithm
E) whether the resulting array is completely sorted or only partially sorted
A) the number of instructions executed by the sorting algorithm
B) the number of instructions in the algorithm itself (its length)
C) the types of loops used in the sorting algorithm
D) the amount of memory space required by the algorithm
E) whether the resulting array is completely sorted or only partially sorted
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
15
Which of the following is a legal way to declare and instantiate an array of 10 Strings?
A) String s = new String(10);
B) String[10] s = new String;
C) String[ ] s = new String[10];
D) String s = new String[10];
E) String[ ] s = new String;
A) String s = new String(10);
B) String[10] s = new String;
C) String[ ] s = new String[10];
D) String s = new String[10];
E) String[ ] s = new String;
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
16
An int array stores the following values. Use the array to answer the questions below.

Which of the following lists of numbers would accurately show the array after the first pass through the Selection Sort algorithm?
A) 9, 4, 12, 2, 6, 8, 18
B) 4, 9, 12, 2, 6, 8, 18
C) 2, 4, 12, 9, 6, 8, 18
D) 2, 4, 6, 8, 9, 12, 18
E) 2, 4, 9, 12, 6, 8, 18

Which of the following lists of numbers would accurately show the array after the first pass through the Selection Sort algorithm?
A) 9, 4, 12, 2, 6, 8, 18
B) 4, 9, 12, 2, 6, 8, 18
C) 2, 4, 12, 9, 6, 8, 18
D) 2, 4, 6, 8, 9, 12, 18
E) 2, 4, 9, 12, 6, 8, 18
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
17
Which of the following loops would adequately add 1 to each element stored in values?
A) for (j=1; jB) for (j=0; jC) for (j=0; j<=values.length; j++) values[j]++;
D) for (j=0; jE) for (j=1; j
A) for (j=1; j
D) for (j=0; j
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
18
An int array stores the following values. Use the array to answer the questions below.

Which of the following lists of numbers would accurately show the array after the fourth pass of the Selection Sort algorithm?
A) 9, 4, 12, 2, 6, 8, 18
B) 2, 4, 6, 9, 12, 8, 18
C) 2, 4, 6, 8, 9, 12, 18
D) 2, 4, 6, 9, 8, 12, 18
E) 2, 4, 6, 8, 12, 9, 18

Which of the following lists of numbers would accurately show the array after the fourth pass of the Selection Sort algorithm?
A) 9, 4, 12, 2, 6, 8, 18
B) 2, 4, 6, 9, 12, 8, 18
C) 2, 4, 6, 8, 9, 12, 18
D) 2, 4, 6, 9, 8, 12, 18
E) 2, 4, 6, 8, 12, 9, 18
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
19
Both the Insertion Sort and the Selection Sort algorithms have efficiencies on the order of ________ where n is the number of values in the array being sorted.
A) n
B) n * log n
C) n²
D) n³
E) Insertion sort has an efficiency of n and Selection Sort has an efficiency of n²
A) n
B) n * log n
C) n²
D) n³
E) Insertion sort has an efficiency of n and Selection Sort has an efficiency of n²
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
20
For the questions below, assume an int array, candy, stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all.
Which of the following code could be used to compute the total number of bars sold by the children?
A) for (int j=0; j<12; j++) sum += candy[j];
B) for (int j=0; j<12; j++) candy[j] = sum;
C) for (int j=0; j<12; j++) sum = candy[j];
D) for (int j=0; j<12; j++) sum += [j];
E) for (int j=0; j<12; j++) [j] += sum;
Which of the following code could be used to compute the total number of bars sold by the children?
A) for (int j=0; j<12; j++) sum += candy[j];
B) for (int j=0; j<12; j++) candy[j] = sum;
C) for (int j=0; j<12; j++) sum = candy[j];
D) for (int j=0; j<12; j++) sum += [j];
E) for (int j=0; j<12; j++) [j] += sum;
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
21
Assume that BankAccount is a predefined class and that the declaration BankAccount[ ] firstEmpireBank; has already been performed. Then the following instruction reserves memory space for firstEmpireBank = new BankAccount[1000];
A) a reference variable to the memory that stores all 1000 BankAccount entries
B) 1000 reference variables, each of which point to a single BankAccount entry
C) a single BankAccount entry
D) 1000 BankAccount entries
E) 1000 reference variables and 1000 BankAccount entries
A) a reference variable to the memory that stores all 1000 BankAccount entries
B) 1000 reference variables, each of which point to a single BankAccount entry
C) a single BankAccount entry
D) 1000 BankAccount entries
E) 1000 reference variables and 1000 BankAccount entries
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
22
A Java main method uses the parameter (String[ ] variable) so that a user can run the program and supply "command-line" parameters. Since the parameter is a String array, however, the user does not have to supply any parameters.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
23
An array index cannot be a float, double, boolean or String.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
24
The Mouse Events include
A) mousePressed, mouseReleased, mouseClicked, mouseEntered, mouseExited
B) mousePressed, mouseReleased, mouseClicked, mouseEntered, mouseExited, mouseMoved, mouseDragged
C) mousePressed, mouseReleased, mouseClicked, mouseMoved, mouseDragged
D) mouseClicked, mouseEntered, mouseExited, mouseMoved, mouseDragged
E) mouseMoved, mouseDragged
A) mousePressed, mouseReleased, mouseClicked, mouseEntered, mouseExited
B) mousePressed, mouseReleased, mouseClicked, mouseEntered, mouseExited, mouseMoved, mouseDragged
C) mousePressed, mouseReleased, mouseClicked, mouseMoved, mouseDragged
D) mouseClicked, mouseEntered, mouseExited, mouseMoved, mouseDragged
E) mouseMoved, mouseDragged
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
25
The statement int[ ] list = {5, 10, 15, 20};
A) adds 4 int values to array list
B) initializes list to have 20 int values
C) initializes list to have 4 int values
D) declares list but does not initialize it
E) causes a syntax error because it does not include "new int[4]" prior to the list of values
A) adds 4 int values to array list
B) initializes list to have 20 int values
C) initializes list to have 4 int values
D) declares list but does not initialize it
E) causes a syntax error because it does not include "new int[4]" prior to the list of values
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
26
A polyline in Java is
A) an object
B) a shape defined as a collection of lines
C) a shape defined as a collection of Polygons
D) an array of shapes
E) an shape defined by a collection of points
A) an object
B) a shape defined as a collection of lines
C) a shape defined as a collection of Polygons
D) an array of shapes
E) an shape defined by a collection of points
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
27
Arrays have a built in toString method that returns all of the elements in the array as one String with "\n" inserted between each element.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
28
A Polygon object in Java can be
A) built up by using a sequence of addPoint method calls
B) defined by an array of x-y coordinates
C) defined by a polyline
D) A, B, and C all are True
E) only A and B are True
A) built up by using a sequence of addPoint method calls
B) defined by an array of x-y coordinates
C) defined by a polyline
D) A, B, and C all are True
E) only A and B are True
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
29
To initialize a String array names to store the three Strings "Huey", "Duey" and "Louie", you would do
A) String names = {"Huey", "Duey", "Louie"};
B) String[ ] names = {"Huey", "Duey", "Louie"};
C) String[ ] names = new String{"Huey", "Duey", "Louie"};
D) String names[3] = {"Huey", "Duey", "Louie"};
E) String names; names[0] = "Huey"; names[1] = "Duey"; names[2] = "Louie";
A) String names = {"Huey", "Duey", "Louie"};
B) String[ ] names = {"Huey", "Duey", "Louie"};
C) String[ ] names = new String{"Huey", "Duey", "Louie"};
D) String names[3] = {"Huey", "Duey", "Louie"};
E) String names; names[0] = "Huey"; names[1] = "Duey"; names[2] = "Louie";
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
30
To declare a three-dimensional int array called threeD, which of the following would you use?
A) int[3] threeD;
B) int[ , , ] threeD;
C) int[ ][ ][ ] threeD;
D) int [ [ [ ] ] ] threeD;
E) int[ ] threeD[3];
A) int[3] threeD;
B) int[ , , ] threeD;
C) int[ ][ ][ ] threeD;
D) int [ [ [ ] ] ] threeD;
E) int[ ] threeD[3];
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
31
The following code accomplishes which of the tasks written below? Assume list is an int array that stores positive int values only.
Int foo = 0;
For (int j =0 ; j < list.length; j++)
If (list[j] > foo) foo = list[j];
A) It stores the smallest value in list (the minimum) in foo
B) It stores the largest value in list (the maximum) in foo
C) It stores every value in list, one at a time, in foo, until the loop terminates
D) It counts the number of elements in list that are greater than foo
E) It counts the number of elements in list that are less than foo
Int foo = 0;
For (int j =0 ; j < list.length; j++)
If (list[j] > foo) foo = list[j];
A) It stores the smallest value in list (the minimum) in foo
B) It stores the largest value in list (the maximum) in foo
C) It stores every value in list, one at a time, in foo, until the loop terminates
D) It counts the number of elements in list that are greater than foo
E) It counts the number of elements in list that are less than foo
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
32
The Key Events include
A) keyPressed, keyReleased
B) keyPressed, keyReleased, keyTyped
C) keyPressed, keyReleased, keyTyped, keyDown, keyUp
D) keyDown, keyUp, keyTyped
E) none of the above
A) keyPressed, keyReleased
B) keyPressed, keyReleased, keyTyped
C) keyPressed, keyReleased, keyTyped, keyDown, keyUp
D) keyDown, keyUp, keyTyped
E) none of the above
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
33
Java arrays can store primitive types and Strings, but cannot store any other type of Object other than Strings.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
34
A Polygon object in Java is
A) a shape defined as a collection of x, y points
B) a shape defined as a collection of lines
C) a shape defined as a collection of polylines
D) an array of shapes
E) 2 lines connected by 1 common point
A) a shape defined as a collection of x, y points
B) a shape defined as a collection of lines
C) a shape defined as a collection of polylines
D) an array of shapes
E) 2 lines connected by 1 common point
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
35
As described in the Software Failure, a race condition bug in a computer program is one in which
A) a race is held to see which team can finish a computer program first
B) two or more processes that start at the same time finish at different times
C) two or more processes race to create an error
D) two or more processes that share the same data continue running under wrong assumptions after one of the processes changes the data
E) none of the above
A) a race is held to see which team can finish a computer program first
B) two or more processes that start at the same time finish at different times
C) two or more processes race to create an error
D) two or more processes that share the same data continue running under wrong assumptions after one of the processes changes the data
E) none of the above
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
36
If x is a char, and values is an int array, then values[x]
A) causes a syntax error
B) causes an Exception to be thrown
C) casts x as an int based on x's position in the alphabet (for instance, if x is 'a' then it uses 0 and if x is 'z' then it uses 25)
D) casts x as an int based on x's ASCII value (for instance, if x is 'a' then it uses 97 and if x is 'z' then it uses 122)
E) casts x as an int based on the digit that is stored in x (for instance, if x is '3' it uses 3) but throws an exception if x does not store a digit
A) causes a syntax error
B) causes an Exception to be thrown
C) casts x as an int based on x's position in the alphabet (for instance, if x is 'a' then it uses 0 and if x is 'z' then it uses 25)
D) casts x as an int based on x's ASCII value (for instance, if x is 'a' then it uses 97 and if x is 'z' then it uses 122)
E) casts x as an int based on the digit that is stored in x (for instance, if x is '3' it uses 3) but throws an exception if x does not store a digit
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
37
If a and b are both int arrays, then a = b; will
A) create an alias
B) copy all elements of b into a
C) copy the 0ᵗʰ element of b into the 0ᵗʰ element of a
D) return True if each corresponding element of b is equal to each corresponding element of a (that is, a[0] is equal to b[0], a[1] is equal to b[1] and so forth) and return false otherwise
E) return True if a and b are aliases and return false otherwise
A) create an alias
B) copy all elements of b into a
C) copy the 0ᵗʰ element of b into the 0ᵗʰ element of a
D) return True if each corresponding element of b is equal to each corresponding element of a (that is, a[0] is equal to b[0], a[1] is equal to b[1] and so forth) and return false otherwise
E) return True if a and b are aliases and return false otherwise
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
38
Which of the following GUI components operates as a group so that only one of the options can be selected at a time?
A) JCheckBox
B) JButton
C) JRadioButton
D) JButtonGroup
E) all of the above
A) JCheckBox
B) JButton
C) JRadioButton
D) JButtonGroup
E) all of the above
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
39
The Mouse Motion Events include
A) mousePressed, mouseReleased, mouseClicked, mouseEntered, mouseExited
B) mousePressed, mouseReleased, mouseClicked, mouseEntered, mouseExited, mouseMoved, mouseDragged
C) mousePressed, mouseReleased, mouseClicked, mouseMoved, mouseDragged
D) mouseClicked, mouseEntered, mouseExited, mouseMoved, mouseDragged
E) mouseMoved, mouseDragged
A) mousePressed, mouseReleased, mouseClicked, mouseEntered, mouseExited
B) mousePressed, mouseReleased, mouseClicked, mouseEntered, mouseExited, mouseMoved, mouseDragged
C) mousePressed, mouseReleased, mouseClicked, mouseMoved, mouseDragged
D) mouseClicked, mouseEntered, mouseExited, mouseMoved, mouseDragged
E) mouseMoved, mouseDragged
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
40
Given the following declarations, which of the following variables are arrays?
Int[ ] a, b;
Int c, d[ ];
A) a
B) a and b
C) a and d
D) a, b and d
E) a, b, c and d
Int[ ] a, b;
Int c, d[ ];
A) a
B) a and b
C) a and d
D) a, b and d
E) a, b, c and d
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
41
Write code fragment to swap the two Strings stored by variables a and b.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
42
Demonstrate how the following array is sorted using Selection Sort. Show the array after each pass of the outer loop.
[16, 3, 12, 13, 8, 1, 18, 9]
[16, 3, 12, 13, 8, 1, 18, 9]
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
43
So long as one is only accessing the elements of an ArrayList, its efficiency is about the same as that of an array. It's only when one begins to insert or remove elements towards the front portion of an ArrayList that its efficiency deteriorates.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
44
Write a method to compute the average of an int array and return the value as a double. The int array and the number of elements in the array are both passed as parameters to the method in that order.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
45
The length operator can be used to control a for-loop that iterates through each element of an array, as in
for (int j=0; jHowever, this is not necessarily safe. Why not?
for (int j=0; j
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
46
A ragged array is a multidimensional array whose initial indices don't all start at zero.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
47
Mouse Events deal with detecting the actions of the buttons on a mouse, among other things, while Mouse Motion Events deal with the movement of the mouse with/without buttons being depressed.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
48
It is possible to sort an array of int, float, double or String, but not an array of an Object class such as a CD class.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
49
Regarding the Software Failure: In the 2003 Northeast Blackout, the backup system crashed for a different reason than the main system, which failed due to a race condition bug in the alarm system software.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
50
Write a method to compute and return the value of max - min where max is the largest element of an int array and min is the smallest element of an int array. The method is passed the int array and an int value that denotes the number of elements in the array. You may assume that the int array has at least 1 element in it.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
51
Just as arrays can only have a fixed number of elements, set at the time the array is declared, a parameter list also can only have a fixed number of elements, set at the time the method is declared.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
52
In Java, an array can only store one type of data. For instance, you cannot create an array that stores both double and String values.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
53
Write a code fragment to create a two-dimensional 10x10 array and initialize every element to be the value of i * j where i and j are the two indices (for instance, element [5][3] is 5 * 3 = 15).
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
54
An array, when instantiated, is fixed in size, but an ArrayList can dynamically change in size when new elements are added to it.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
55
The class Name consists of 4 instance data, String title, String first, String middle, String last. The class has a constructor that is passed all 4 String values and initializes the class appropriately, and has 4 methods, each returns one of the four instance data, called returnTitle( ), returnFirst( ), returnMiddle( ) and returnLast( ). Name[ ] addressBook = new Name[100]; is performed. Write a method that is passed 4 String parameters corresponding to a Name's title, first, middle, and last, and finds this Name in addressBook and returns the index of where this Name was found, or -1 if the Name was not found. Assume that addressBook stores n entries (n is an int).
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
56
To swap the 3ʳᵈ and 4ᵗʰ elements in the int array values, you would do:
values[3] = values[4];
values[4] = values[3];
values[3] = values[4];
values[4] = values[3];
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
57
In a two-dimensional array, both dimensions must have the same number of elements, as in[10][10].
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
58
If the following statement is performed: CD[ ] mycollection = new CD[200]; where CD is a previously defined class, then mycollection[5] is a CD object.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
59
Although methods may be declared with a variable length parameter list, class constructors cannot.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
60
Demonstrate how the following array is sorted using Insertion Sort. Show the array after each pass of the outer loop.
[16, 3, 12, 13, 8, 1, 18, 9]
[16, 3, 12, 13, 8, 1, 18, 9]
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
61
Write an insertion sort method to sort an array of String values (instead of an array of int values). Assume the array is an instance data of the current class called list, and number is an int instance data that stores the number of elements currently stored in list.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
62
In the RubberLinesPanel example in the text a single LineListener class was defined, implementing both MouseListener and MouseMotionListener. What are the advantages and disadvantages of this approach compared to defining two separate classes one for each kind of Mouse Events?
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
63
Explain how to alter the Selection Sort algorithm so that it sorts in descending order instead of ascending order.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
64
Write a method to output all elements of a two-dimensional array of int values as a table of rows and columns. Use "\t" to get elements to line up properly. The method is passed the two-dimensional array and two int values that denote the number of both dimensions (rows followed by columns).
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
65
Implement an ItemListener so that a new value is output by the second JLabel every time a JCheckBox is clicked on.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
66
Say that you want to construct a tiny drawing program, where the objects are defined using line segments. One might use the rubber-banding ideas presented in this chapter to define the endpoints of line segments, hooking them together into a polyline. One might provide a button that turns a polyline into a polygon. If this approach is taken, what would be a good way to represent the data involved in this program: the points, the line segments, the polylines, the polygons?
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
67
Write code to create a JPanel with 4 JCheckBox GUI components and two JLabels for output, one that says "Current cost is" and the other that outputs the computed cost based on which of the food items has been selected.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
68
Write a method that takes an array of Strings as a parameter and the number of elements currently stored in the array, and returns a single String that is the concatenation of all Strings in the array.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
69
Assume xArray and yArray are equal length arrays of int values storing num items (num is also an int). Write a paint method for an Applet that will draw a Polygon shape using this information. Assume xArray, yArray and num are all instance data of the Applet.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck
70
Write a method public static int[ ][ ] matMult(int[ ][ ] x, int[ ][ ] y) which accepts two matrices, x and y, and yields their product x * y as its result. Use the normal rules for matrix multiplication. Be certain to test to see that x and y are compatible before beginning to compute the matrix product.
Unlock Deck
Unlock for access to all 70 flashcards in this deck.
Unlock Deck
k this deck