AP BowlABExam2005

advertisement
AP Bowl
Practice Exam AB
April 16, 2005
Georgia Tech
Question 1
• A bookstore is working on
an on-line ordering
system. For each type of
published material
(books, movies, audio
tapes) they need to track
the id, title, author(s),
date published, and price.
Which of the following
would be the best
design?
a) Create one class
PublishedMaterial with the
requested fields plus type
b) Create classes Book, Movie,
and AudioTape and each class
has the requested fields
c) Create the class
PublishedMaterial and have
Book, Movie, and AudioTape
inherit from it all the listed fields
d) Create one class BookStore
with the requested fields plus
type
e) Create classes for
PublishedMaterial, Books,
Movies, AudioTape, Title, Price,
ID, Authors, DatePublished
Question 1 Answer
• A bookstore is
working on an on-line
ordering system. For
each type of
published material
(books, movies, audio
tapes) they need to
track the id, title,
author(s), date
published, and price.
Which of the following
would be the best
design?
• Answer c:
• Create the class
PublishedMaterial and
have Book, Movie, and
AudioTape inherit from it
all the listed fields
• We will need to get
objects based on their
type so we should create
classes for Book, Movie,
and AudioTape. They
have common fields so
we should put these in a
common superclass
PublishedMaterial.
Question 2
private ArrayList nums;
// precondition: nums.size() > 0;
// nums contains Integer objects
public void numQuest() {
int k = 0;
Integer zero = new Integer(0);
while (k < nums.size()) {
if (nums.get(k).equals(zero))
nums.remove(k);
else
k++; }
}
• ArrayList nums = [0, 0, 4, 2, 5, 0,
3, 0]
• What will ArrayList nums contain
as a result of executing
numQuest?
a)
b)
c)
d)
e)
[0, 4, 2, 5, 3]
[3, 5, 2, 4, 0, 0, 0, 0]
[0, 0, 0, 0, 4, 2, 5, 3]
[4, 2, 5, 3]
[0, 0, 4, 2, 5, 0, 3, 0]
Question 2 Answer
private ArrayList nums;
// precondition: nums.size() > 0;
// nums contains Integer objects
public void numQuest() {
int k = 0;
Integer zero = new Integer(0);
while (k < nums.size()) {
if (nums.get(k).equals(zero))
nums.remove(k);
else
k++; }
}
• ArrayList nums = [0, 0, 4, 2, 5,
0, 3, 0] What will ArrayList
nums contain as a result of
executing numQuest ?
• Answer d:
• [4, 2, 5, 3]
• This code will loop
through the elements
of the list and if the
current element is
zero it will remove it
from the list. This
moves down all
remaining elements
so the current index
isn’t incremented.
Question 3
• Consider the following
method:
public int m1 (int a)
{
if (a == 1)
return 10;
else
return 10 +
m1 (a – 1);
}
• What is the output when
m1(5) is called?
a)
b)
c)
d)
e)
50
20
60
10
30
Question 3 Answer
• Consider the following
method:
public int m1 (int a)
{
if (a == 1)
return 10;
else
return 10 +
m1 (a – 1);
}
• What is the output when
m1(5) is called?
• Answer a: 50 = 10 * 5
m1(5)
return 10 + m1(4)
return 10 + m1(3)
return 10 + m1(2)
return 10+ m1(1)
return 10
return 20
return 30
return 40
return 50
Question 4
private int [] myStuff;
//precondition: myStuff contains
// integers in no particular order
public int mystery(int num)
{
for (int k = myStuff.length - 1;
k >= 0; k--)
{
if (myStuff[k] > num)
return k;
}
return -1;
}
• Which of the following best
describes the contents of
myStuff after the following
statement has been executed?
int m = mystery(n);
a) All values in positions m+1
through myStuff.length-1 are
greater than or equal to n.
b) All values in position 0 through
m are less than n.
c) All values in position m+1
through myStuff.length-1 are
less than or equal to n.
d) The smallest value is at
position m.
e) The largest value that is
smaller than n is at position m.
Question 4 Answer
private int [] myStuff;
//precondition: myStuff contains
// integers in no particular order
public int mystery(int num)
{
for (int k = myStuff.length - 1;
k >= 0; k--)
{
if (myStuff[k] > num)
return k;
}
return -1;
}
• Which of the following best
describes the contents of
myStuff after the following
statement has been executed?
int m = mystery(n);
• Answer c: All values in position
m+1 through myStuff.length-1
are less than or equal to n.
• This loops backwards through
the array and returns the index
where it finds the value at the
index in the array greater than
the passed number. So once it
returns, all values in the array
passed that index must be less
than or equal to the passed
num (n).
Question 5
//precondition: x >=0
public void mystery (int x)
{
System.out.print(x % 10);
if ((x / 10) != 0)
{
mystery(x / 10);
}
System.out.print(x % 10);
}
•
What is the output from
mystery(4321)?
a)
b)
c)
d)
e)
43211234
1234
4321
12344321
32144123
Question 5 Answer
//precondition: x >=0
public void mystery (int x)
{
System.out.print(x % 10);
if ((x / 10) != 0)
{
mystery(x / 10);
}
System.out.print(x % 10);
}
•
•
What is the output from
mystery(4321)?
Answer d: 12344321
mystery(4321)
prints 1 (4321 % 10 = 1)
mystery(432) (4321 / 10)
prints 2 (432 % 10 = 2)
mystery(43) (432 / 10)
prints 3 (43 % 10 = 3)
mystery(4) (43 / 10)
prints 4 (4 % 10 = 4)
no recursive call
prints 4 (4 % 10 = 4)
prints 3 (43 % 10 = 3)
prints 2 (432 % 10 = 2)
prints 1 (4321 %10 = 1)
Question 6
a) A doubly linked list with
• Which of the
references to the first and
following best
last nodes only.
describes the data
structure represented b) A singly linked list with a
reference to the first node
by
only.
java.util.LinkedList?
c) A singly linked list with
references to the first and
last nodes.
d) A doubly linked list with a
reference to the first node
only.
e) A doubly linked list with
reference to the first,
middle, and last nodes.
Question 6 Answer
• Which of the
following best
describes the
data structure
represented by
java.util.LinkedLi
st?
•
•
Answer a:
A doubly linked list
with references to
the first and last
nodes only.
Question 7
public int mystery(int n)
{
if (n == 0) return 1;
else return 2 *
mystery (n - 1);
}
• What value is
returned as the result
of mystery(7)?
a)
b)
c)
d)
e)
128
256
64
0
2
Question 7 Answer
public int mystery(int n)
{
if (n == 0) return 1;
else return 2 *
mystery (n - 1);
}
• What value is returned as
the result of mystery(7)?
• Answer a: 128
• This calculates 2 to the
given n
mystery(7)
2 * mystery(6)
2 * mystery(5)
2 * mystery(4)
2 * mystery(3)
2 * mystery(2)
2 * mystery(1)
2 * mystery(0) = 1
Question 8
int [][] mat = new int [4][3];
for (int row = 0;
row < mat.length; row++) {
for (int col = 0;
col < mat[0].length;
col++) {
if (row < col)
mat[row][col] = 1;
else if (row == col)
mat[row][col] = 2;
else
mat[row][col] = 3; } }
• What are the contents of mat
after the code segment has
been executed?
a)
b)
c)
d)
e)
{{2 3 3},
{1 2 3},
{1 1 2},
{1 1 1}}
{{2 1 1},
{3 2 1},
{3 3 2},
{3 3 3}}
{{2 1 1 1},
{3 2 1 1},
{3 3 2 1}}
{{2 3 3 3},
{1 2 3 3},
{1 1 2 3}}
{{1 1 1 1},
{2 2 2 2},
{3 3 3 3}}
Question 8 Answer
int [][] mat = new int [4][3];
for (int row = 0;
row < mat.length; row++) {
for (int col = 0;
col < mat[0].length;
col++) {
if (row < col)
mat[row][col] = 1;
else if (row == col)
mat[row][col] = 2;
else
mat[row][col] = 3; } }
• What are the contents of mat
after the code segment has
been executed?
•
•
•
Answer b:
{{2 1 1},
{3 2 1},
{3 3 2},
{3 3 3}}
This creates an array of 4
rows and 3 columns. This
code will put a 1 in the array
when the row index is less
than the column index and a
2 in the array when the row
and column index are the
same, and a 3 in the array
when the row index is greater
than the column index.
Question 9
public class Point2D {
public int x;
public int y;
public Point2D() {}
public Point2D(int x,int y) {
this.x = x; this.y = y;
}
// other methods
}
public class Point3D extends
Point2D
{
public int z;
// other code
}
•
Which of the following constructors
would be valid for Point3D?
I. public Point3D() {}
II. public Point3D(int x, int y, int z) {
super(x,y);
this.z = z;
}
III. public Point3D(int x, int y) {
this.x = x;
this.y = y;
this.z = 0;
}
a) I only
b) II only
c) III only
d) I and II only
e) I, II, and III
Question 9 Answer
public class Point2D {
public int x;
public int y;
public Point2D() {}
public Point2D(int x,int y) {
this.x = x; this.y = y;
}
// other methods
}
public class Point3D extends Point2D
{
public int z;
// other code
}
•
Which of the following constructors
would be valid for Point3D?
I. public Point3D() {}
II. public Point3D(int x, int y, int z) {
super(x,y);
this.z = z;
}
III. public Point3D(int x, int y) {
this.x = x;
this.y = y;
this.z = 0;
}
•
•
Answer e: I, II, and III
Point2D does have a no-arg
constructor so I is okay. Point2D
does have a constructor that takes
x and y so II is okay. And x and y
are public so III is okay.
Question 10
Queue que = new ListQueue();
// ListQueue implements Queue
Object obj;
que.enqueue("a");
que.enqueue("b");
que.enqueue("c");
obj = que.peekFront();
que.enqueue(obj + "d");
while (! que.isEmpty()) {
System.out.print(que.dequeue()
+ " ");
}
• What is printed as a result of
executing this code segment?
a)
b)
c)
d)
e)
a b c bd
a b c ad
a b c cd
ad c b a
a b bd
Question 10 Answer
Queue que = new ListQueue();
// ListQueue implements Queue
Object obj;
que.enqueue("a");
que.enqueue("b");
que.enqueue("c");
obj = que.peekFront();
que.enqueue(obj + "d");
while (! que.isEmpty()) {
System.out.print(que.dequeue()
+ " ");
}
• What is printed as a result of
executing this code segment?
• Answer b:
• a b c ad
• The method enqueue
adds items to the end of
the queue. The method
peekFront returns the first
element in the queue.
And dequeue removes
and returns the first
element in the queue.
Question 11
private static void sort(List theList)
{
Iterator itr = theList.iterator();
PriorityQueue pq = new
PriorityQueueImpl();
// PriorityQueueImpl implements
//
PriorityQueue
while (itr.hasNext()) {
pq.add(itr.next())
itr.remove(); // removes last
// item just seen by itr
}
while ( !pq.isEmpty())
theList.add(pq.removeMin());
}
•
If the parameter is an
ArrayList, and the
PriorityQueueImpl is
implemented as a min-heap,
which data structure
operation dominates the
running time of the sort?
a)
b)
c)
d)
e)
pq.add
pq.isEmpty
pq.removeMin
itr.remove
theList.add
Question 11 Answer
private static void sort(List theList)
{
Iterator itr = theList.iterator();
PriorityQueue pq = new
PriorityQueueImpl();
// PriorityQueueImpl implements
//
PriorityQueue
while (itr.hasNext()) {
pq.add(itr.next())
itr.remove(); // removes last
// item just seen by itr
}
while ( !pq.isEmpty())
theList.add(pq.removeMin());
}
•
•
•
•
If the parameter is an
ArrayList, and the
PriorityQueueImpl is
implemented as a min-heap,
which data structure
operation dominates the
running time of the sort?
Answer d:
itr.remove
When you remove from the
front of the array list all the
values past it must be moved
down one. This is O(n).
Question 12
public class Student {
public String getFood() {
return “Pizza”;
}
public String getInfo() {
return this.getFood();
}
}
public class GradStudent extends
Student {
public String getFood() {
return “Taco”;
}
}
• What is the output from this:
Student s1 = new GradStudent();
s1.getInfo();
a) Won’t compile since
GradStudent doesn’t have a
getInfo method
b) Pizza
c) Won’t compile since you are
creating a GradStudent, not a
Student
d) Won’t compile since you use
this.getFood()
e) Taco
Question 12 Answer
public class Student {
public String getFood() {
return “Pizza”;
}
public String getInfo() {
return this.getFood();
}
}
public class GradStudent extends
Student {
public String getFood() {
return “Taco”;
}
}
• What is the output from this:
Student s1 = new GradStudent();
s1.getInfo();
• Answer e: Taco
• Objects know what class they
are created as and all methods
are resolved starting with that
class. If the method isn’t
found in that Class the parent
class is checked (and so on
until it is found). This will
execute the getInfo in Student
and then execute the getFood
in GradStudent and print Taco.
Question 13
•
The following is intended to
return an integer array sum
such that for all i, sum[i] is
equal to arr[0] + arr[1] + ... +
arr[i]. For instance, if arr
contains the values { 1, 4, 1, 3
}, the array sum will contain
the values { 1, 5, 6, 9 }.
private int[] arr;
public int[] partialSum() {
int[] sum = new int[arr.length];
for (int j = 0; j < sum.length; j++)
sum[j] = 0;
/* missing code */
return sum;
}
•
Here are two ways to replace
/* missing code */
1. for (int j = 0; j < arr.length; j++)
sum[j] = sum[j - 1] + arr[j];
•
Or
2. for (int j = 0; j < arr.length; j++)
for (int k = 0; k <= j; k++)
sum[j] = sum[j] + arr[k];
•
a)
b)
c)
d)
e)
Which of the following statements is
true?
Implementation 1 will cause an
ArrayIndexOutOfBoundsException.
Both work but 1 is faster than 2
Both work but 2 is faster than 1
Both work and are equally fast
Implementation 2 will cause an
ArrayIndexOutOfBoundsException
Question 13 Answer
• The following is intended to
return an integer array sum
such that for all i, sum[i] is
equal to arr[0] + arr[1] + ... +
arr[i]. For instance, if arr
contains the values { 1, 4, 1, 3
}, the array sum will contain the
values { 1, 5, 6, 9 }.
private int[] arr;
public int[] partialSum() {
int[] sum = new int[arr.length];
for (int j = 0; j < sum.length; j++)
sum[j] = 0;
/* missing code */
return sum;
}
• Here are two ways to replace
/* missing code */
1. for (int j = 0; j < arr.length; j++)
sum[j] = sum[j - 1] + arr[j];
• Or
2. for (int j = 0; j < arr.length; j++)
for (int k = 0; k <= j; k++)
sum[j] = sum[j] + arr[k];
• Answer a:
• Implementation 1 will cause an
ArrayIndexOutOfBoundsExcep
tion.
• When j is 0 sum[j-1] will be
sum[-1] which will cause an
ArrayIndexOutOfBoundsExcep
tion.
Question 14
• What are the values
of a and b after the for
loop finishes?
int a = 10, b = 3, temp;
for (int i=1; i<=6; i++)
{
temp = a;
a = i + b;
b = temp – i;
}
a)
b)
c)
d)
e)
a = 13 and b = 0
a = 6 and b = 7
a = 9 and b = 3
a = 8 and b = 3
a = 0 and b = 13
Question 14 Answer
• What are the values
of a and b after the for
loop finishes?
int a = 10, b = 3, t;
for (int i=1; i<=6; i++)
{
t = a;
a = i + b;
b = t – i;
}
•
Answer a:
a = 13 and b = 0
•
The variable i loops
from 1 to 6
i = 1, t = 10, a = 4, b = 9
i = 2, t = 4, a = 11, b =2
i = 3, t = 11, a = 5, b = 8
i = 4, t = 5, a = 12, b = 1
i = 5, t = 12, a = 6, b = 7
i = 6, t = 6, a = 13, b = 0
Question 15
•
Susan is 5 years older than
Matt. Three years from now
Susan’s age will be twice
Matt’s age.
for (int s = 1; s <= 100; s++) {
for (int m = 1; m <= 100; m++) {
if (<condition>)
System.out.println(“Susan is
“ + s + “ and Matt is “ + m);
}
}
•
What should be in
<condition> to solve this
problem?
a) (s == m – 5) &&
(s – 3 == 2 * (m – 3))
b) (s == (m + 5)) && ((s + 3) ==
(2 * m + 3))
c) s == (m – 5) && (2 * s – 3) ==
(m – 3)
d) s == m + 5 && s + 3 == 2 * m
+6
e) None of the above is correct
Question 15 Answer
• Susan is 5 years older than
Matt. Three years from now
Susan’s age will be twice
Matt’s age.
for (int s = 1; s <= 100; s++) {
for (int m = 1; m <= 100; m++) {
if (<condition>)
System.out.println(“Susan is
“ + s + “ and Matt is “ + m);
}
}
• What should be in <condition>
to solve this problem?
• Answer d:
s == m + 5 && s + 3 == 2 * m + 6
• Susan is 5 years old than
Matt so s == m + 5 should be
true and in 3 years she will be
twice as old so s + 3 = 2 * (m +
3) = 2 * m + 6.
Question 16
Stack s = new ListStack();
// ListStack implements Stack
Queue q = new ListQueue();
// ListQueue implements Queue
• Assume that s is initially empty
and that q initially contains the
following strings: W X Y Z The
front points to the W and the
back points to the Z. Consider
the following code segment:
while (!q.isEmpty())
s.push(q.dequeue());
while (!s.isEmpty())
q.enqueue(s.pop());
•
a)
b)
c)
d)
e)
After execution which is true:
Stack s is empty and queue q
contains W, X, Y, Z, in that order,
with W at the front of the queue.
Stack s contains Z, Y, X, W, in
that order, with Z at the top of
the stack, and queue q is empty.
Stack s contains Z, Y, X, W, in
that order, with Z at the top of
the stack; and queue q contains
W, X, Y, Z, in that order, with W
at the front of the queue.
Stack s is empty and queue q
contains Z, Y, X, W, in that order,
with Z at the front of the queue.
Stack s contains Z, Y, X, W, in
that order, with Z at the top of
the stack; and queue q contains
Z, Y, X, W, in that order, with Z at
the front of the queue.
Question 16 Answer
Stack s = new ListStack();
// ListStack implements Stack
Queue q = new ListQueue();
// ListQueue implements Queue
• Assume that s is initially empty
and that q initially contains the
following strings: W X Y Z The
front points to the W and the
back points to the Z. Consider
the following code segment:
while (!q.isEmpty())
s.push(q.dequeue());
while (!s.isEmpty())
q.enqueue(s.pop());
• Answer d:
• Stack s is empty and
queue q contains Z, Y, X,
W, in that order, with Z at
the front of the queue.
• The method enqueue
adds to the back of a
queue and dequeue
removes from the front of
a queue. Push adds to
the top of a stack and pop
removes from the top of a
stack.
Question 17
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= n;
k = k * 2) {
System.out.println(j + " "
+ k);
}
}
• Of the following, which
best characterizes the
running time of the code
segment?
a)
b)
c)
d)
e)
O(log n)
O(n)
O(n*n) (n squared)
O(n log n)
O(n!)
Question 17 Answer
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= n;
k = k * 2) {
System.out.println(j + " " + k);
}
}
• Of the following, which
best characterizes the
running time of the code
segment?
•
Answer d:
• O(n log n)
• The outer loop is n
and the inner loop is
log n due to k
changing by a
multiple of 2 each
time
Question 18
• What is the output
from the following
code?
String s = “Georgia Tech”;
String s1 = s.substring(0,7);
String s2 = s1.substring(2);
String s3 = s2.substring(0,3);
System.out.println(s3);
a)
b)
c)
d)
e)
eor
eorg
org
orgi
You will get an index
out of bounds
exception
Question 18 Answer
• What is the output from
the following code?
String s = “Georgia Tech”;
String s1 = s.substring(0,7);
String s2 = s1.substring(2);
String s3 = s2.substring(0,3);
System.out.println(s3);
• Answer c: org
• Substring(a,b) means
start at a and stop
before b.
• substring(a) means start
at a and go to the end of
the string.
• The first character is at
index 0.
– Georgia
– orgia
– org
Question 19
public class Person
implements
Comparable
{ code for class }
public class Player
extends Person
{ code for class }
• Which declaration will
result in a compile
error?
a) Person p = new
Person();
b) Person p = new
Player();
c) Comparable c = new
Person();
d) Player p = new
Person();
e) Comparable c = new
Player();
Question 19 Answer
public class Person
implements
Comparable
{ code for class }
public class Player
extends Person
{ code for class }
• Which declaration will
result in a compile
error?
•
Answer d:
Player p = new Person();
• A variable can hold the
declared type and any
child of that type
• A class that implements
an interface (or inherits
from a class that
implements the
interface) can be
declared to be of the
interface type
Question 20
public boolean check(String s)
{
return s.length() >= 2 &&
(s.charAt(0) ==
s.charAt(1) ||
check(s.substring(1)));
}
• This will return true if and
only if:
a) s contains two or more
of the same characters
b) s contains two or more
of the same characters
in a row
c) s starts with two or
more of the same
characters
d) s ends with two or more
of the same characters
e) s.charAt(0) ==
s.charAt(1)
Question 20 Answer
public boolean check(String s)
{
return s.length() >= 2 &&
(s.charAt(0) ==
s.charAt(1) ||
check(s.substring(1)));
}
• This will return true if and
only if:
•
•
•
Answer b:
s contains two or more
of the same characters
in a row
This will return true
when s has at least 2
characters in it and the
first two characters are
the same or the
recursive call to check
with the substring
starting with the 2nd
character returns true
Question 21
• Which will cause the
shortest execution of
a binary search
looking for a value in
an array of integers
sorted in ascending
order? The array has
an odd number of
integers.
1. The value is the first
in the array
2. The value is in the
middle of the array
3. The value is the last
in the array
4. The value is not in
the array
5. The value is the third
element in the array
Question 21 Answer
• Which will cause the
shortest execution of
a binary search
looking for a value in
an array of integers
sorted in ascending
order? The array has
an odd number of
integers.
• Answer b:
• The value is in the
middle of the array
• A binary search starts
at the middle of a
sorted array so if that
is the value you are
searching for
execution can stop
then.
Question 22
• Which will cause the
longest execution of a
sequential search looking
for a value in an array of
10 integers?
a) The value is the first one
in the array
b) The value is in the
middle of the array
c) The value is at position
3 in the array
d) The value isn’t in the
array
e) The value is at position
6 in the array
Question 22 Answer
• Which will cause the
longest execution of
a sequential search
looking for a value in
an array of 10
integers?
•
•
•
Answer d:
The value isn’t in the
array
If the value isn’t in
the array it will have
to check every
element in the array
to be sure that it isn’t
in the array
Question 23
• If you have a parent class
Animal that has a method
speak() which returns
“Awk” and you have
children classes that do
the following:
– Cat has a speak method
that returns “Meow”
– Bird doesn’t have a speak
method
– Dog has a speak method
that returns “Woof”
– Pig doesn’t have a speak
method
– Cow has a speak method
that returns “Moo”
•
What is the output from
looping through this
array of animals and
asking each to speak()?
Animal[] a = { new Cat(), new
Cow(), new Dog(), new
Pig(), new Bird() }
a) Awk Awk Awk Awk Awk
b) This won’t compile
c) Meow Moo Woof Awk
Awk
d) This will have runtime
errors
e) Meow Moo Woof Oink
Awk
Question 23 Answer
• If you have a parent class
Animal that has a method
speak() which returns
“Awk” and you have
children classes that do
the following:
– Cat has a speak method
that returns “Meow”
– Bird doesn’t have a speak
method
– Dog has a speak method
that returns “Woof”
– Pig doesn’t have a speak
method
– Cow has a speak method
that returns “Moo”
•
•
•
Answer c:
Meow Moo Woof Awk
Awk
Objects keep a
reference to the class
that created them. So,
even if you put them in
an array of Animals they
know what they are and
all methods are resolved
starting with the class of
the object. Bird and Pig
do not override speak so
the speak method in
Animal will execute.
Question 24
public class Person {
private String name;
public Person(String name)
{ this.name = name; }
public void setName(String
name) {
this.name = name;
}
public String getName()
{ return name; }
}
public class Student extends
Person {
<constructors>
}
•
I.
II.
Which are legal constructors?
public Student() {}
public Student(String name) {
this.name = name;
}
III. public Student(String name) {
super(name);
}
IV. public Student(String name) {
this.setName(name);
}
a)
b)
c)
d)
e)
III only
III and IV
I, III, and IV
IV only
I, II, III, and IV
Question 24 Answer
public class Person {
private String name;
public Person(String name)
{ this.name = name; }
public void setName(String
name) {
this.name = name;
}
public String getName()
{ return name; }
}
public class Student extends
Person {
<constructors>
}
• Answer a: III only
• The field name is private to
Person and can’t be directly
set in Student so II can’t be
correct. There is no noargument constructor in
Person so I and IV can’t be
correct. In IV there is no
explicit call to super so one will
be added by the compiler to
the no-arg constructor but
since there is not a no-arg
constructor in Person this will
not be allowed. Only III has
the correct call to the
constructor in Person that
takes a name.
Question 25
• The following integers are
inserted into an empty
binary search tree in the
following order:
• 26 20 37 31 22 18 25 29
19
• Which traversal of the
tree would produce the
following output?
• 26 20 18 19 22 25 37 31
29
a)
b)
c)
d)
e)
Level-by-level
Preorder
Inorder
Postorder
Reverse postorder
Question 25 Answer
• The following integers are
inserted into an empty
binary search tree in the
following order:
• 26 20 37 31 22 18 25 29
19
• Which traversal of the
tree would produce the
following output?
• 26 20 18 19 22 25 37 31
29
• Answer b:
• Preorder
• A preorder traversal prints
the node value then goes
left and then right
26
20
37
18 22
31
19 25 29
Question 26
• Question: Consider the
following declarations.
Integer valueOne, valueTwo;
• Assume that valueOne
and valueTwo have been
properly initialized.
• Which of the following is
equivalent to the
expression below?
valueOne.intValue() ==
valueTwo.intValue()
a) valueOne == valueTwo
b) valueOne.compareTo(
valueTwo)
c) valueOne.equals(valueTwo)
== 0
d) valueOne.intValue().equals(
valueTwo.intValue())
e) valueOne.compareTo(
valueTwo) == 0
Question 26 Answer
• Question: Consider the
following declarations.
Integer valueOne, valueTwo;
• Assume that valueOne
and valueTwo have been
properly initialized.
• Which of the following is
equivalent to the
expression below?
valueOne.intValue() ==
valueTwo.intValue()
• Answer e:
• valueOne.compareTo(
valueTwo) == 0
• valueOne.intValue() ==
valueTwo.intValue()
• This will return true when
the int values in valueOne
and valueTwo are the
same and false
otherwise.
• The method compareTo
returns 0 when the values
are equal and not 0 when
they are not so it will have
the same result
Question 27
• Consider the following data
field and method.
private int[] arr;
// precondition: arr.length 0
public int checkArray()
{
int loc arr.length / 2;
for (int k 0; k arr.length; k)
{
if (arr[k] arr[loc])
loc k;
}
return loc;
}
•
a)
b)
c)
d)
e)
Which of the following is the
best postcondition for
checkArray?
Returns the index of the first
element in array arr whose
value is greater than arr[loc]
Returns the index of the last
element in array arr whose
value is greater than arr[loc]
Returns the largest value in
array arr
Returns the index of the
largest value in the second
half of array arr
Returns the index of the
largest value in array arr
Question 27 Answer
• Consider the following data
field and method.
private int[] arr;
// precondition: arr.length 0
public int checkArray()
{
int loc = arr.length / 2;
for (int k = 0; k < arr.length; k++)
{
if (arr[k] > arr[loc])
loc = k;
}
return loc;
}
•
•
Answer e: Returns the index of
the largest value in array arr
This code sets the loc to the
middle of the array and then
loops through all the array
elements. If the value at the
current index is greater than
the value at the stored loc then
it changes loc to the index. It
returns the loc which is the
index to the largest value in
the array.
Question 28
private int[] arr;
public int mystery(int low, int high, int
num) {
int mid (low+high) / 2;
if (low > high) {
return -1;
}
else if (arr[mid] < num) {
return mystery(mid +1, high, num);
}
else if (arr[mid] > num) {
return mystery(low, mid - 1, num);
}
else
return mid;
}
• What is returned by the
call:
mystery(0, 4, 5)
when arr = {1, 2, 3, 5, 7}?
a) 5
b) -1
c) 3
d) 0
e) 4
Question 28 Answer
private int[] arr;
public int mystery(int low, int high, int
num) {
int mid (low+high) / 2;
if (low > high) {
return -1;
}
else if (arr[mid] < num) {
return mystery(mid +1, high, num);
}
else if (arr[mid] > num) {
return mystery(low, mid - 1, num);
}
else
return mid;
}
• What is returned by the call:
mystery(0, 4, 5) ;
when arr = {1, 2, 3, 5, 7}?
• Answer c: 3
• This returns the index of the
num if it is found in the array
or else -1 if it isn’t found in
the array. This is a binary
search.
Question 29
Base
Child1
Child2
printTypes(Object o)
{
if (o instanceof Object)
System.out.print(“Object, “);
if (o instanceof Base)
System.out.print(“Base, ”);
if (o instanceof Child1)
System.out.print(“Child1, “);
if (o instanceof Child2)
System.out.print(“Child2“);
}
• What is printed from
printTypes(new Child2())?
a) Child2
b) Base, Child2
c) Object, Base, Child1, Child2
d) Object, Base, Child1
e) Object, Base, Child2
Question 29 Answer
Base
Child1
Child2
printTypes(Object o)
{
if (o instanceof Object)
System.out.print(“Object, “);
if (o instanceof Base)
System.out.print(“Base, ”);
if (o instanceof Child1)
System.out.print(“Child1, “);
if (o instanceof Child2)
System.out.print(“Child2“);
}
• Answer e:
Object, Base, Child2
• The method printTypes is
classed with an object of the
class Child2. From the UML
class diagram we can see that
Child2 is a child of Base so it
will also be an instance of
Base. All objects inherit from
Object so it will also be an
Object.
Question 30
• In the Marine Biology
Case Study what is
the minimum number
of steps it takes for a
fish to change
location to the
location directly
behind it?
a)
b)
c)
d)
e)
1
2
3
4
5
Question 30 Answer
• In the Marine Biology
Case Study what is
the minimum number
of steps it takes for a
fish to change
location to the
location directly
behind it?
• Answer c: 3
• A fish can not go
backwards in the case
study. So it would have
to go left (or right) three
times to get to the
location behind the
current location
2
3
1
0
Question 31
•
In the Marine
Biology Case Study
which of the
following are
interfaces?
I. Fish
II. Environment
III. Location
IV. Direction
a)
b)
c)
d)
e)
I only
II only
I and III
II and III
I, III and IV
Question 31 Answer
•
In the Marine
Biology Case Study
which of the
following are
interfaces?
I. Fish
II. Environment
III. Location
IV. Direction
• Answer b: II only
• Only Environment is
an interface. Location
is a class that
implements the
Locatable interface.
Question 32
• What is the best
explanation for what is
meant by overriding a
method?
a)
b)
c)
d)
e)
Defining another method with
the same name as another
method but with a different
number of parameters
Defining another method with
the same name as another
method but with different
types for the parameters
Defining a method with the
same name as an inherited
method but with a different
number of parameters
Defining a method with the
same name and parameter
list as an inherited method
Defining a method with the
same precondition
Question 32 Answer
• What is the best
explanation for
what is meant by
overriding a
method?
•
Answer d: Defining
a method with the
same name and
parameter list as an
inherited method
Question 33
I.
II.
III.
IV.
public class Timer {
public void start();
public void stop();
public int getTime();
}
public interface Timer {
private void start();
private void stop();
private int getTime();
}
public class Timer {}
public interface Timer {
public void start();
public void stop();
public int getTime();
}
•
a)
b)
c)
d)
e)
Which of the these
correctly defines an
interface?
I only
IV only
II and IV
III only
I and IV
Question 33 Answer
I.
II.
III.
IV.
public class Timer {
public void start();
public void stop();
public int getTime();
}
public interface Timer {
private void start();
private void stop();
private int getTime();
}
public class Timer {}
public interface Timer {
public void start();
public void stop();
public int getTime();
}
• Which of the these
correctly defines an
interface?
• Answer b: IV only
• An interface is declared
using the keyword
interface and all the
methods must be public.
They also have to be
abstract methods but you
don’t have to use the
abstract keyword.
Question 34
• Given the following
declaration of a field in a
class:
public static final String
GREETING = “Hi”;
• Which of these
statements is FALSE?
a) Each object of this class
can access GREETING
b) The value of greeting
can’t be changed in any
methods
c) Each object of this class
has it’s own copy of
GREETING
d) GREETING.length() = 2
e) GREETING.toUpperCas
e() = “HI”
Question 34 Answer
• Given the following
declaration of a field in a
class:
public static final String
GREETING = “Hi”;
• Which of these
statements is not true?
• Answer c:
a) Each object of this class
has it’s own copy of
GREETING
• This is the only false
statement. Static
variables are not
created for each object.
They are created in the
object that defines the
class (class Class). All
objects keep a
reference to this Class
object and can access
fields in it.
Question 35
public abstract class Shape {
// code for Shape
}
public class Oval extends Shape {
// code for Oval
}
Which is valid?
I.
II.
III.
IV.
Shape s = new Oval();
Oval o = new Shape();
Shape s1 = new Shape();
Oval o = new Oval();
a)
b)
c)
d)
e)
I only
I, III, and IV
IV only
I and IV
III only
Question 35 Answer
public abstract class Shape {
// code for Shape
}
public class Oval extends Shape {
// code for Oval
}
Which is valid?
I.
Shape s = new Oval();
II.
Oval o = new Shape();
III.
Shape s1 = new Shape();
IV.
Oval o = new Oval();
• Answer d: I and IV
• Shape is an abstract
class and you can not
create objects of an
abstract class.
• You can declare an
object to be of a type and
it can refer to an object of
that type or a subclass of
that type.
Question 36
• Method hasItem should return
true if item is found in myList;
otherwise, it should return
false.
private List myList;
public boolean hasItem(Object
item) {
Iterator itr = myList.iterator();
while ( /* condition */ ) {
if (itr.next().equals(item))
return true;
}
return false;
}
•
Which of the following
expressions can be
used to replace /*
condition */ so that
hasItem will work as
intended?
a)
b)
c)
d)
e)
myList.hasNext()
itr.hasNext()
! itr.hasNext()
! myList.hasNext()
itr != null
Question 36 Answer
• Method hasItem should return
true if item is found in myList;
otherwise, it should return
false.
private List myList;
public boolean hasItem(Object
item) {
Iterator itr = myList.iterator();
while ( /* condition */ ) {
if (itr.next().equals(item))
return true;
}
return false;
}
• Which of the following
expressions can be used
to replace /* condition */
so that hasItem will work
as intended?
• Answer b:
• itr.hasNext()
• Iterators have a method
hasNext that returns true
while the iterator has
more elements.
Question 37
• See the following mail
groups
– Family:
myMom@business.com,
myDad@isp.net,
mySis@school.edu
– Friends: sue@yahoo.com,
mary@aol.com
• Which of the following
describes the best choice
of data structures for
representing mail groups
in an electronic mail
application?
a)
b)
c)
d)
e)
Use a 2-d array of strings
with the group names in the
first column and the
addresses in the second
Use an array of linked lists
with the group name the first
element in the linked list
Use an ArrayList of Maps,
where each Map has one
key, the group name, and a
set of mail addresses
Use a map with the group
name as the key and a set of
addresses as the value
Use two arrays, one with the
group name and the other
the mail addresses as a
String
Question 37 Answer
• See the following mail
groups
– Family:
myMom@business.com,
myDad@isp.net,
mySis@school.edu
– Friends: sue@yahoo.com,
mary@aol.com
• Which of the following
describes the best choice
of data structures for
representing mail groups
in an electronic mail
application?
• Answer d:
• Use a map with the group
name as the key and a
set of addresses as the
value
• You will need to lookup
the group and a map is
very quick for looking up
the location given a
unique key. You can
store the mail addresses
in a set because there
shouldn’t be any
duplicates.
Question 38
• In the MBCS which is
FALSE about
SlowFish?
a) It has a .2 chance of
moving
b) It can move to 3 empty
neighboring locations
c) It inherits the move
method from Fish
d) It overrides
generateChild from Fish
e) If it can’t move it
reverses direction
Question 38 Answer
• In the MBCS which is
FALSE about
SlowFish?
• Answer e:
• If it can’t move it
reverses direction
• Only objects of the
DarterFish class do
this, not objects of the
SlowFish class
Question 39
• In the MBCS which of
the following is
FALSE about
DarterFish?
a) When both spaces are full in
front of it, it will reverse
direction
b) When both spaces are empty
in front of it, it will move 2
spaces forward
c) When one space is empty in
front of it but the 2nd is blocked
it will turn left
d) When one space is empty in
front of it but the 2nd is blocked
it will move 1 space forward
e) It overrides the move method
that it inherited from Fish
Question 39 Answer
• In the MBCS which of
the following is
FALSE about
DarterFish?
•
•
•
Answer c:
When one space is
empty in front of it
but the 2nd is
blocked it will turn
left
No, it will just move
forward one space
when the 2nd space
is blocked
Question 40
public List process1(int n) {
ArrayList someList = new
ArrayList();
for (int k = 0; k < n; k++)
someList.add(new Integer(k));
return someList;
}
public List process2(int n) {
ArrayList someList = new
ArrayList();
for (int k = 0; k < n; k++)
someList.add(k, new Integer(k));
return someList;
a) Both methods produce the
same result and take the
same amount of time.
b) Both methods produce the
same result, and process1 is
faster than process2.
c) The two methods produce
different results and take the
same amount of time.
d) The two methods produce
different results, and process1
is faster than process2.
e) The two methods produce
different results, and process2
is faster than process1.
Question 40 Answer
public List process1(int n) {
ArrayList someList = new
ArrayList();
for (int k = 0; k < n; k++)
someList.add(new Integer(k));
return someList;
}
public List process2(int n) {
ArrayList someList = new
ArrayList();
for (int k = 0; k < n; k++)
someList.add(k, new
Integer(k));
return someList;
}
•
•
•
Answer a:
Both methods produce the
same result and take the
same amount of time.
The method process1 adds
to the end of the ArrayList
each time through the loop.
The method process2 also
adds to the end of the
ArrayList each time through
the loop. The only difference
would be if there were values
in the ArrayList in process2.
Any existing values would be
moved to the right. But, there
are no existing values in the
ArrayList at that index or past
it.
Download