Multiple Choice Multiple Choice Section 3.1 A Review of Java Arrays Suppose I ha
ID: 3537393 • Letter: M
Question
Multiple Choice
Multiple ChoiceSection 3.1
A Review of Java Arrays Suppose I have int b = new int[42]. What are the highest and lowest legal array indexes for b? A. 0 and 41 B. 0 and 42 C. 1 and 41 D. 1 and 42 Consider the following statements: 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? A. p[99] = 75; B. p[100] = 75; C. s[99] = 75; D. s[100] = 75; E. Two or more of the answers will change i to 75. Consider the following statements: 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? A. p[99] = 75; B. p[100] = 75; C. s[99] = 75; D. s[100] = 75; E. Two or more of the answers will change i to 75. Which is the best approach to copying elements from part of one array b to another array c? A. Use a for loop. B. Use b.clone( ). C. Use the assignment operator c = b. D. Use System.arraycopy. Suppose we have this method: 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]); A. 0 B. 1 C. 2 D. 3 Suppose you have the following function prototype and variable declaration: 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: A. goop(x); B. goop(x[ ]); C. goop(x[10]); D. goop(&x); E. goop(&x[ ]); Multiple Choice Section 3.2 The Bag ADT Which statement is true for the Chapter 3 bag (with an array)? A. A programmer who uses the bag must call ensureCapacity as items are added, otherwise the bag might not be big enough for more items. B. A programmer may optionally call ensureCapacity, but if he doesn't then the bag will grow as more items are added. Who needs to know about the invariant of an ADT? A. Only the programmer who implements the class for the ADT. B. Only the programmer who uses the class for the ADT. C. Both the programmer who implements the class and the programmer who uses the class. D. Neither the programmer who implements the class nor the programmer who uses the class. Suppose that the Bag is implemented with an array. Which of these operations will always have a constant worst-case time? A. add B. countOccurrences C. remove D. None of (A), (B), and (C) have a constant worst-case time E. TWO of (A), (B), and (C) have a constant worst-case time F. ALL of (A), (B), and (C) have a constant worst-case time Suppose that the Bag is implemented with an array and that you have two Bags b1 (with size n1) and b2 (with size n2). The capacity of b1 is currently n1+n2, and the capacity of b2 is currently just n2. What are the time requirements for b1.addAll(b2)? What A. O(n1) B. O(n2) C. O(n1+n2) D. O(n1*n2) Suppose that the Bag is implemented with an array and that you have two Bags b1 (with size n1) and b2 (with size n2). The capacity of b1 is currently n1+n2, and the capacity of b2 is currently just n2. What are the time requirements for b2.addAll(b1)? What A. O(n1) B. O(n2) C. O(n1+n2) D. O(n1*n2) Suppose that the Bag class is efficiently implemented with an array with a capacity of 4000, as in Chapter 3 of the class notes. Choose the best description of b's instance variables after we execute these statements: 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? A. b.manyItems is 3, b.data[0] is 4, b.data[1] is 5, and b.data[2] is 6 B. b.manyItems is 3, b.data[0] is 5, b.data[1] is 4, and b.data[2] is 6 C. b.manyItems is 3, b.data[0] is 6, b.data[1] is 4, and b.data[2] is 5 D. b.manyItems is 3, b.data[0] is 6, b.data[1] is 5, and b.data[2] is 4 Suppose that the Bag class is efficiently implemented with an array with a capacity of 4000, as in Chapter 3 of the class notes. We execute these 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? A. b.manyItems is 2, b.data[0] is 4, b.data[1] is 6 B. b.manyItems is 2, b.data[0] is 6, b.data[1] is 4 C. b.manyItems is 3, b.data[0] is 4, b.data[1] is 6 D. b.manyItems is 3, b.data[0] is 6, b.data[1] is 4 I have an array named data, which contains n integers. I want to print all of the numbers, starting at data[0]. BUT if the number 42 occurs, then I want to stop my printing just before the 42 (without printing the 42!) Here is most of my for-loop to accomplish my goal: 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.) A. (data[i] != 42) && (i < n) B. (data[i] != 42) || (i < n) C. (i < n) && (data[i] != 42) D. (i < n) || (data[i] != 42) E. More than one of the above answers is correct. Here is a small function that uses the Bag class from Chapter 3: 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? A. During the execution of Line 2. B. During the execution of Line 3. C. Just after Line 4 and before line 5. D. After Line 5. Multiple Choice Section 3.3 The Sequence ADT In Chapter 3, there was a suggested implementation of the Sequence class with these private member variables: 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? A. The first activation of addAfter or addBefore initializes the entire array. B. The array initialization was part of the project that was left to the student. C. The programmer who uses the Sequence is responsible for initializing the array. D. When a Sequence is first created, none of the array is being used. This question is appropriate if you implemented the Sequence class. The Sequences's addBefore member method normally puts a new item before the current item. What does addBefore do if there is no current item? A. The addBefore precondition is violated. B. The new item is put at the beginning of the list. C. The new item is put at the end of the list. D. None of the above. Suppose that the Sequence is implemented with an array. Which of these operations are likely to have a constant worst-case time? A. addBefore B. countOccurrences C. remove D. None of (A), (B), and (C) have a constant worst-case time E. TWO of (A), (B), and (C) have a constant worst-case time F. ALL of (A), (B), and (C) have a constant worst-case time Suppose the Bag and Sequence are both implemented with an array as in Chapter 3. What is the difference between the private instance variables of the Bag and the private instance variables of the Sequence? A. The Bag has an extra array of integers. B. The Bag has one extra int instance variable. C. The Sequence has an extra array of integers. D. The Sequence has one extra int instance variable. Suppose that the Bag and the Sequence have both been implemented with partially filled arrays. Which statement is true about the worst-case run time of the remove operations. A. Both removal methods have constant worst-case run times. B. The Bag's removal is constant time, but the Sequence's removal is not. C. The Sequence's removal is constant time, but the Bag's removal is not. D. Neither removal method has constant worst-case run time. Multiple Choice
Section 3.1
A Review of Java Arrays
Explanation / Answer
Suppose I have int b = new int[42]. What are the highest and lowest legal array indexes for b?
A. 0 and 41
Consider the following statements:
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?
E. Two or more of the answers will change i to 75.
Consider the following statements:
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?
A. p[99] = 75;
Which is the best approach to copying elements from part of one array b to another array c?
D. Use System.arraycopy.
Suppose we have this method:
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]);
D. 3
Suppose you have the following function prototype and variable declaration:
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:
A. goop(x);
Which statement is true for the Chapter 3 bag (with an array)?
A. A programmer who uses the bag must call ensureCapacity as items are added, otherwise the bag might not be big enough for more items.
Who needs to know about the invariant of an ADT?
A. Only the programmer who implements the class for the ADT.
Suppose that the Bag is implemented with an array. Which of these operations will always have a constant worst-case time?
D. None of (A), (B), and (C) have a constant worst-case time
Suppose that the Bag is implemented with an array and that you have two Bags b1 (with size n1) and b2 (with size n2). The capacity of b1 is currently n1+n2, and the capacity of b2 is currently just n2. What are the time requirements for b1.addAll(b2)? What
B. O(n2)
Suppose that the Bag is implemented with an array and that you have two Bags b1 (with size n1) and b2 (with size n2). The capacity of b1 is currently n1+n2, and the capacity of b2 is currently just n2. What are the time requirements for b2.addAll(b1)? What
C. O(n1+n2)
Suppose that the Bag class is efficiently implemented with an array with a capacity of 4000, as in Chapter 3 of the class notes. Choose the best description of b's instance variables after we execute these statements:
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?
B. b.manyItems is 3, b.data[0] is 5, b.data[1] is 4, and b.data[2] is 6
Suppose that the Bag class is efficiently implemented with an array with a capacity of 4000, as in Chapter 3 of the class notes. We execute these 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?
A. b.manyItems is 2, b.data[0] is 4, b.data[1] is 6
I have an array named data, which contains n integers. I want to print all of the numbers, starting at data[0]. BUT if the number 42 occurs, then I want to stop my printing just before the 42 (without printing the 42!) Here is most of my for-loop to accomplish my goal:
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.)
E. More than one of the above answers is correct.
Here is a small function that uses the Bag class from Chapter 3:
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?
A. During the execution of Line 2.
In Chapter 3, there was a suggested implementation of the Sequence class with these private member variables:
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?
D. When a Sequence is first created, none of the array is being used.
This question is appropriate if you implemented the Sequence class. The Sequences's addBefore member method normally puts a new item before the current item. What does addBefore do if there is no current item?
A. The addBefore precondition is violated.
Suppose that the Sequence is implemented with an array. Which of these operations are likely to have a constant worst-case time?
D. None of (A), (B), and (C) have a constant worst-case time
Suppose the Bag and Sequence are both implemented with an array as in Chapter 3. What is the difference between the private instance variables of the Bag and the private instance variables of the Sequence?
D. The Sequence has one extra int instance variable.
Suppose that the Bag and the Sequence have both been implemented with partially filled arrays. Which statement is true about the worst-case run time of the remove operations.
D. Neither removal method has constant worst-case run time.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.