Name More Array Practice Consider the line of code shown here

advertisement
Name ______________________________________________________________
More Array Practice
Consider the line of code shown here & answer true/false questions 1  5 (write answer on answer sheet):
int[] a = new int[10];
1.
2.
3.
4.
5.
T
T
T
T
T
F
F
F
F
F
The length of this array is 10.
The index of the last element of this array is 10.
Every value of this array is currently 0.
The line of code a[4] = “three”; would cause a syntax error.
The line of code a[a.length/2] = a.length; would cause a syntax error.
Consider the line of code shown here & answer true/false questions 6  10 (write answer on answer sheet):
String[] teachas = {“Mike”, “Chris”, “Maria”, “Ken”, “Paulette”, “Sue”,
“Tom”, “Jeanne”, “Bob”, “Lea”, “Jeff”, “Sharon”};
6.
7.
8.
9.
T
T
T
T
F
F
F
F
10. T
F
teachas[1] refers to “Mike”.
teachas[teachas.length – 1] refers to “Sharon”.
teachas.length is 12.
The code below successfully swaps teachas[3]and teachas[4]:
String temp = teachas[3];
teachas[3] = teachas[4];
teachas[4] = temp;
Assuming that gen is a valid Random object, teachas[gen.nextInt(13)] has no
chance of causing an out-of-bounds error.
Multiple Choice Questions – Write answer on answer sheet
11. Consider the line of code shown here:
String[] dogList = new String[12];
Which of the following lines of code will correctly store the next-to-last element of dogList to a
variable called temp?
A. String temp = dogList[12];
B. String temp = dogList[11];
C. String temp = dogList[10];
12. Consider the array of Strings shown here:
String[] nums = {“one”, “two”, “three”, “four”, “five”, “six”, “seven”};
Determine the output (if any) of System.out.println(nums[nums.length/2]);
A. two
B.
three
C.
four
D.
five
E. no output – error in accessing an index that doesn’t exist
13. To assign a random integer from 10  99 (inclusive) to the variable x, one could use the code:
A.
Random g = new Random();
int x = g.nextInt(100) + 10;
B.
Random g = new Random();
int x = g.nextInt(99) + 10;
C.
Random g = new Random();
int x = g.nextInt(90) + 10;
D.
Random g = new Random();
int x = g.nextInt(89) + 10;
E.
None of these
14. Consider the line of code shown here:
int x = gen.nextInt(50) + 1
How can you best describe the value of x?
A.
B.
C.
D.
E.
F.
A random integer in the set {0, 1, 2, … , 49}
A random integer in the set {0, 1, 2, … , 50}
A random integer in the set {0, 1, 2, … , 51}
A random integer in the set {1, 2, 3, … , 49}
A random integer in the set {1, 2, 3, … ,50}
A random integer in the set {1, 2, 3, … , 51}
15. Consider the code segment here:
int[] x = new int[5];
for (int j = 0; j < x.length; j++)
x[j] = j*j;
Which of the following correctly lists the current contents of x?
A.
B.
C.
D.
E.
{0,
{1,
{0,
{1,
{1,
0,
4,
1,
4,
4,
0,
9,
4,
9,
9,
0, 0}
16, 25}
9, 16}
16}
16, 25, 36}
16. Which of the following statements regarding arrays is FALSE ?
A. Once an array has been initialized, you cannot increase its capacity later on in the program.
B. An array can be used to store individual grades that can later be printed out, averaged, searched,
and sorted.
C. An array of length n is indexed from 0 to n–1 .
D. The same array can store multiple data types, so, for instance, it is feasible that the contents of the
array junkPile could be {4, true, “my stuff”, “Fido”, 8.8}.
E. An out-of-bounds error when dealing with arrays is considered to be a run-time error because it
causes a problem when you run the program, not when you compile the program.
17. Consider the partial code segment here:
int total = 0;
int[] nums = {3, 8, 11, 15, 19, 94};
for (int i = 0; i < nums.length; i++)
// code not shown
System.out.println(total);
Which of the following correctly replaces // code not shown so that the code segment will print
the sum of the integers in nums?
A.
B.
C.
D.
E.
total
total
total
total
total
=
=
=
=
=
total + nums[i];
nums[i];
total + i;
i;
total + nums;
18. Consider the code segment here & determine its output:
int[] nums = {3, 84, 112, 52, 37, 94};
int mysteryNumber = nums[0];
for (int i = 1; i < nums.length; i++)
{
if (nums[i] > mysteryNumber)
mysteryNumber = nums[i];
}
Sysem.out.println(mysteryNumber);
A.
B.
C.
D.
E.
F.
3
84
112
52
37
94
G.
H.
I.
J.
K.
L.
M.
0
1
2
3
4
5
6
Wow! This is could be a
new record for number of
multiple choice options!
19. Consider the code shown here & describe the output:
int[] nums = {3, 84, 112, 52, 37, 94};
System.out.println(nums);
A.
B.
C.
D.
E.
The output is not very meaningful to the user – just a memory location.
The output is {3, 84, 112, 52, 37, 94} (including the commas and brackets}
The output is 3, 84, 112, 52, 37, 94 (commas, no curly brackets)
The output is 3 84 112 52 37 94 (no commas, no curly brackets)
The output is {3 84 112 52 37 94} (curly brackets, no commas)
20. Which of the following for loops can be used to print the elements of an array called arr backwards?
A. for(int k = arr.length; k >= 0; k--)
System.out.print(arr[k] + “ ”);
B. for(int k = arr.length; k > 0; k--)
System.out.print(arr[k] + “ ”);
C. for(int k = arr.length – 1; k >= 0; k--)
System.out.print(arr[k] + “ ”);
D. for(int k = arr.length – 1; k > 0; k--)
System.out.print(arr[k] + “ ”);
Determine the output:
1. Output
String [] jedis = {“Luke”, “Yoda”, “Ani”, “Obiwan”,
“Qui-Gon”, “Mace”};
for (int j = 0; j < jedis.length; j+=2)
{
System.out.println( (j + 1) + jedis[j]);
}
2. Output
int[] x = new int[10];
for (int j = 0; j < x.length; j++)
x[j] = 10 – j;
for (int j = 0; j < x.lenth; j++)
System.out.print(x[j] + “ ”);
Download