Short Answers Section 3.1 A Review of Java Arrays |
int[ ] m = new int[2];
m[0] = 0;
m[1] = 1;
int[ ] p = m;
p[0] = 4;
p[0] = 5;
int[ ] m = new int[2];
m[0] = 0;
m[1] = 1;
int[ ] p = (int[ ]) m.clone( );
p[0] = 4;
p[0] = 5;
Short Answers
Section 3.2
The Bag ADT
new
but the heap is out of memory?
public void triple_capacity( )
// Postcondition: The capacity of this Bag's array has been
// tripled. This Bag still contains the same items that it previously
// had.
Do not
use the ensureCapacity method. Do use System.arraycopy.
Short Answers
Section 3.3
The Sequence ADT
int[ ] data = new int[100];
int i;
Write a small segment of Java code that will shift data[50]...data[98] up
one spot to the locations data[51]...data[99]. Then insert the number 42
at location data[50]. Use a for loop (not System.arraycopy).
int[ ] data = new int[100];
int i;
Write a small segment of Java code that will shift data[51]...data[99] down
one spot to the locations data[50]...data[98]. The value of data[99]
remains at its original value. Use a for loop (not System.arraycopy).
Multiple Choice Section 3.1 A Review of Java Arrays |
int b = new int[42]
. What are the highest and
lowest legal array indexes for b?
int[ ] p = new int[100];
int[ ] s = p;
After these statements, which of the following statements will change the
last value of p to 75?
p[99] = 75;
p[100] = 75;
s[99] = 75;
s[100] = 75;
int[ ] p = new int[100];
int[ ] s = (int[ ]) p.clone( );
After these statements, which of the following statements will change the
last value of p to 75?
p[99] = 75;
p[100] = 75;
s[99] = 75;
s[100] = 75;
public static foo(int[ ] b)
{
b[0]++;
}
What is printed by these statements?
int[ ] x = new int[100];
x[0] = 2;
foo(x);
System.out.println(x[0]);
public void goop(int[ ] z)
int[ ] x = new int[10];
Which is the correct way to call the goop method with x as the argument:
Multiple Choice
Section 3.2
The Bag ADT
Bag b;
b.add(5);
b.add(4);
b.add(6);
What will be the values of b.manyItems and b.data after the statements?
Bag b;
b.add(5);
b.add(4);
b.add(6);
b.remove(5);
What will be the values of b.manyItems and b.data after the statements?
for (i = 0; ___________________________________________; i++)
System.out.println(data[i]);
What is the correct way to fill in the blank?
(If there is more than one correct answer, please select E.)
(data[i] != 42) && (i < n)
(data[i] != 42) || (i < n)
(i < n) && (data[i] != 42)
(i < n) || (data[i] != 42)
public static void quiz( )
{
int i; // Line 1
IntArrayBag b = new IntArrayBag( ); // Line 2
b.add(42); // Line 3
i = b.size( ); // Line 4
System.out.println(i); // Line 5
}
When is the Bag's array created?
Multiple Choice
Section 3.3
The Sequence ADT
class Sequence
{
int[ ] data;
int manyItems;
int currentIndex;
};
The Sequence's constructor creates a new array for data to refer to, but does not place any values
in the data array. Why?