Part 1: ArrayList
n
Assume that variable myList is an ArrayList containing at least two
objects.
Which of the following code segments moves the first object in the
list to the end of the list?
A.
myList.add(myList.get(0));
B.
myList.add(myList.remove(0));
ANSWER
C. myList.add(0,
myList.remove(0));
D.
myList.add(myList.size(), myList.get(0));
E.
myList.add(myList.remove(myList.size()-1));
n
Consider the following code segment:
ArrayList<Integer> L = new ArrayList<Integer();
for(int k = 0; k<9; k++)
{
L.add(new Integer(k))
}
for (int k = 0; k<5; k++)
{
Object tmp = L.get(k);
L.set(k, L.get(8-k));
L.set(8-k, tmp);
}
for (int k = 0; k<9; k++)
{
System.out.print (L.get(k) + " ");
}
What is printed when this
code executes?
A. 0 1 2 3 4 5 6 7 8
B. 8 7 6 5 4 3 2 1 0
ANSWER
C. 0 1 2 2 4 3 2 1 0
D. 0 0 0 0 0 0 0 0 0
E. 8 8 8 8 8 8 8 8 8
n
Assume that variable L is an ArrayList and that the method call
L.size() returns 10.
Which of the following method calls
does not cause a run time error?
A. L.add(10, "hello");
ANSWER
B. L.add(20, "hello");
C. L.get(10);
D. L.set(10, "hello");
E. L.remove(10);
n
Consider the following code. Note that this uses a "plain"
ArrayList instead of specifying the
type of items that will be stored in
the list.
ArrayList L = New ArrayList();
L.add("one");
L.add("two");
L.add("three);
System.out.println((Integer) L.get(L.size()-1));
Which of the following statements about this code is true?
A. The code will not
compile because the last line casts the result of the call to get to an
Integer, but the values stored in the list are of type String.
B. The code will compile but there will be a runtime error because the last line casts the result of the call to get to an Integer, but the values stored in the list are of type
String.
ANSWER
C. The code will compile
but there will be a runtime error because the last line calls get with a
value that is too large.
D. The code will compile
and run without error and will print 3.
E. The code will compile
and print without error and will print three.
n Assume that the type of variable strList is ArrayList<String>. Consider the following two code segments, both of which are intended to add an exclamation point to the end of each
string in the list.
Segment 1
for (int k =
0, k<strList.size(); k++)
{
Str tmp =
strList.remove(k);
strList.add(k, tmp + "!");
} |
Segment 2
for (int k =
0; k<strList.size(); k++)
{
String tmp
= strList.get(k);
strList.set(k, tmp + "!");
} |
Which of the following statements about these two code segments is
true?
A. Both will work as
intended, and they will be equally efficient.
B. Both will work as
intended; Segment 1 will be more efficient
than Segment 2.
C. Both will work as
intended; Segment 2 will be more efficient than segment 1.
ANSWER
D. Only Segment 1 will
work as intended.
E. Only Segment 2 will
work as intended.
n
Which of the following declarations will cause an error?
I.
ArrayList<String> stringList = new ArrayList<String>();
II. ArrayList<int>
intList = new ArrayList<int>();
III. ArrayList<Comparable> compList = new ArrayList<Comparable>();
A. I only
B. II only
ANSWER
C. III only
D. I and III only
E. II and III only
n
Consider these declarations
ArrayList<String>
stringList = new ArrayList<String>();
String ch = " ";
Integer intOb = new
Integer(5);
Which
statement will cause an error?
A.
strList.add(ch);
B.
strList.add(new String("handy andy"));
C.
strList.add(intOb.toString());
D.
strList.add(ch + 8);
E.
strList.add(intOb + 8);
ANSWER
n
Let list be an ArrayList<Integer> containing these elements
2
5 7
6 0
1
Which of the following statements would
not cause an error to occur?
Assume that each statement applies to the given list, independent of
other statements.
A. Object ob =
list.get(6);
B. Integer intOb =
list.add(3,4);
C. list.add(6,9);
ANSWER
D. Object x =
list.remove(6);
E. Object y =
list.set(6,8);
Part 2: One-D Arrays
n
Consider the following code segment. The line numbers are included
for reference.
1 |
Int [ ] A =
new int[3]; |
2 |
Int [ ] B =
new int[10]; |
3 |
B[9] = 30; |
4 |
A = B; |
5 |
A[9] = 20; |
6 |
B[9] = 10; |
7 |
System.out.println(A[9]); |
What happens when this code is compiled and executed?
A. Line 5 will cause a
compile-time error because of an out-of-bounds array index.
B. Line 5 will cause a
runtime error because of an out-of-bounds array index.
C. The code will compile
and execute without error. The output will be 10.
ANSWER
D. The code will compile
and execute without error. The output will be 20.
E. The code will compile
and execute without error. The output will be 30.
n
Which of the following statements
is not true?
A. Every object has an
equals method.
B. If a programmer does
not write an equals method for a new class, the default method will return
true if an only if all fields of the two instances of the class contain the
same values.
ANSWER
C. The equals method for Strings returns true if and only if the two
Strings contain the same sequence of characters.
D. The equals method for arrays returns true if and only if the two
arrays point to the same chunk of memory.
E. The equals method for Strings will return false if one String is
shorter than the other.
n
Consider the following code segment:
String s1 = "abc";
String s2 = s1;
String s3 = s2;
After this code executes, which of the following expressions would
evaluate to true?
I. s1.equals(s3)
II. s1 == s2
III. s1 == s3
A. I only
B. II only
C. III only
D. I and II only
E. I, II, and III
ANSWER
n
Consider the following code segment:
int [ ] A = {1, 2, 3};
int [ ] B = {1, 2, 3};
int [ ] C = A;
After this code executes, which of the following expressions would
evaluate to true?
I.
A.equals(B)
II. A == B
III. A == C
A. I only
B. II only
C. III only
ANSWER
D. I and III only
E. I, II, and III
n
Which of the following correctly initializes an array arr to
contain four elements each with
value 0?
I. int [ ] arr =
{0, 0, 0, 0};
II. int [ ] arr = new
int[4];
III. int[ ] arr = new int[4];
for(int i = 0; i<arr.length; i++)
arr[i] = 0;
A. I only
B. III only
C. I and III only
D. II and III only
E. I, II, and III
ANSWER
n
The following program segment is intended to find the index of the
first negative integer in
arr[0]...arr[N-1], where arr is an
array of N integers.
int i = 0;
while (arr[i] >= 0)
{
i++
}
location = i;
This program will work as intended
A. always
B. never
C. whenever arr contains
at least one negative number
ANSWER
D. whenever arr contains
at least one nonnegative integer
E. whenever arr contains
no negative integers
n
Refer to the following code segment. You may ssume that arr is an
array of int values.
int sum = arr[0], i = 0;
while (i < arr.length)
{
i ++;
sum += arr[i];
}
Which of the following will be the result of executing the segment?
A. Sum of arr[0], arr[1],
..., arr[arr.length-1] will be stored in sum.
B. Sum of arr[1], arr[2],
..., arr[arr.length-1] will be stored in sum.
C. Sum of arr[0], arr[1],
..., arr[arr.length] will be stored in sum.
D. An infinite loop will
occur.
E. A run-time error will
occur.
ANSWER
n
The following code fragment is intended to find the smallest value
in arr [0]...arr[n-1].
//Precondition:
arr[0]...arr[n-1] initialized with integers, arr is an array, arr.length = n
//Postcondition: min = smallest value in arr[0]...arr[n-1].
int min = arr[0];
int i = 1;
while (i < n)
{
i++;
if (arr[i] < min)
min = arr[i};
}
This code is incorrect. For the segment to work as indicated, which
of the following modifications could be made?
I Change the line
int i = 1;
to
int i = 0;
//make no other changes.
II Change the body of the
while loop to
{
if (arr[i] < min)
min = arr[i];
i++;
}
//make no other changes
III Change the test for the while loop as follows
while (i <= n)
//make no other changes.
A. I only
B. II only
ANSWER
C. III only
D. I and II only
E. I, II, and III
Part 3: GridWorld Case Study
n
Assume that variable grid is a properly initialized Grid. Which of the
following expressions evaluate the number of occupied locations in grid?
A.
grid.numRows() * grid.numCols()
B.
grid.getOccupiedLocations()
C.
grid.getOccupiedLocations().size()
ANSWER
D.
grid.getOccupiedAdjacentLocations().size()
E.
grid.toString().size()
n
Assume that variable oneBug is facing north in its grid. Under which of the
following conditions would the method call oneBug.canMove() return false?
I.
The location to the north of the bug is empty.
II.
The location to the north of the bug contains a flower.
III. The location
to the north of the bug contains a rock.
A.
I only
B.
II only
C.
III only
D.
I and II
ANSWER
E.
II and III
n
Which of the following methods is defined in the Bug class because it is
required to correctly extend the Actor class?
A.
act
B.
turn
C.
move
D.
canMove
E.
No methods are required to be implementd in the Bug class to
correctly extend the Actor class
ANSWER