dataset
stringclasses 1
value | id
stringlengths 21
21
| question
stringlengths 8
5.15k
| choices
listlengths 4
4
| answer
stringclasses 4
values | rationale
stringlengths 1
1.57k
|
|---|---|---|---|---|---|
canterbury_cs
|
canterbury_cs_0000600
|
You've got this code:
<pre lang="text/x-java">TreeMap<String, String> map = new TreeMap<String, String>();
map.put("A", "*");
map.put("AA", "*");
map.put("B", "****");
for (String s : map.keySet()) {
System.out.print(map.get(s) + " ");
}
</pre>
<span style="font-family: arial, helvetica, sans-serif;">What does it print?</span>
|
[
"A A B",
"* * ****",
"A* AA* B****",
"A AA B"
] |
B
|
We iterate through each key {"A", "AA", "B"}, looking up and printing the value of these keys.
|
canterbury_cs
|
canterbury_cs_0000601
|
You see the expression <code>n = null</code> in code that successfully compiles. Which is not a legal type of <code>n</code>?
|
[
"ArrayList<String>",
"String",
"Object",
"boolean"
] |
D
|
Boolean's can only be true or false.
|
canterbury_cs
|
canterbury_cs_0000602
|
The number of elements in a hashtable must be prime
|
[
"to reduce collisions",
"so that quadratic probing will terminate",
"to reduce cluster sizes",
"so that probing with a double hash visits all elements"
] |
D
|
Since the double hash produces a fixed step size for probing, it may be the case that the step size is a factor of the table size. In this case, advancing by the fixed step size will land us in a cycling sequence.
|
canterbury_cs
|
canterbury_cs_0000603
|
You've got a class that holds two ints and that can be compared with other IntPair objects:
<pre lang="text/x-java">class IntPair {
private int a;
private int b;
public IntPair(int a, int b) {
this.a = a;
this.b = b;
}
public int compareTo(IntPair other) {
if (a < other.a) {
return -1;
} else if (a > other.a) {
return 1;
} else {
if (b == other.b) {
return 0;
} else if (b > other.b) {
return -1;
} else {
return 1;
}
}
}
}
</pre>
<span style="font-family: arial, helvetica, sans-serif;">Let's denote new IntPair(5, 7) as [5 7]. You've got a list of IntPairs:</span>
<span style="font-family: arial, helvetica, sans-serif;">[3 7], [4 6], [3 4]</span>
You sort them using IntPair.compareTo. What is their sorted order?
|
[
"[3 4], [3 7], [4 6]",
"[3 7], [3 4], [4 6]",
"[4 6], [3 7], [3 4]",
"[4 6], [3 4], [3 7]"
] |
B
|
The compareTo orders first by IntPair.a in ascending order, and in the case of a tie, but IntPair.b in descending order.
|
canterbury_cs
|
canterbury_cs_0000604
|
What does the following Java code print?
<pre lang="text/x-java">int outer=0;
int inner=0;
for (int i=0; i<6; i++) {
outer++;
for (int j=0; j<=i; j++) {
inner++;
}
}
System.out.println("outer "+outer+", inner "+inner);
</pre>
|
[
"outer 6, inner i",
"outer 6, inner 24",
"outer 6, inner 21",
"outer 24, inner 24"
] |
C
|
The inner loop is based on the current iteration of the outer loop. If we track the values:
outer 0, inner 0
outer 1, inner 0,1
outer 2, inner 0,1,2
and so on up to 6.
If we add up all of the inner values, there are 21 of them.
This is the classic "triangle" pattern, because the inner values proceed like this:
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 2 3 4 4 5
|
canterbury_cs
|
canterbury_cs_0000605
|
What does the following Java code print?
<pre lang="text/x-java">int outer=0;
for (int i=0; i<12; i++) {
if (i % 2 == 0) {
outer++;
}
}
System.out.println(outer);
</pre>
|
[
"12",
"6",
"3",
"1"
] |
B
|
i % 2 == 0 is true only when i is 0. This loop counts the number of even values between 0 and 12, exclusive (because the loop body doesn't execute when i is 12, the loop ends instead). In this case the result is 6 (0, 2, 4, 6, 8, 10).
|
canterbury_cs
|
canterbury_cs_0000606
|
How many times does the following Java code print "hello world"?
<pre lang="text/x-java">for (int i=10; i>=4; i++) {
System.out.print("hello world");
}
</pre>
|
[
"1",
"none of the above",
"5",
"6"
] |
B
|
Trick question! This is an infinite loop, because the update condition is i++, but we are starting at 10 and continue while i>=4!
|
canterbury_cs
|
canterbury_cs_0000607
|
What does the following Java code print?
<pre lang="text/x-java">for (int i=6; i>=2; i--) {
System.out.print(i+" ");
}
</pre>
|
[
"6 5 4 3 2 1 0",
"6 5 4 3 2 1",
"6 5 4 3 2",
"6 5 4 3"
] |
C
|
This is a basic for loop that counts down from 6 to 2.
|
canterbury_cs
|
canterbury_cs_0000608
|
You are storing a complete binary tree in an array, with the root at index 0. At what index is node i's left child?
|
[
"2i",
"2i + 1",
"i + i + 2",
"i / 2 + 1"
] |
B
|
(i` - 1) / 2
|
canterbury_cs
|
canterbury_cs_0000609
|
Consider the following Python code:
<pre lang="text/x-python">in_str = input(’Enter a string: ’)
a = ’’
d = 0
r = 1
for c in ’abcdefghijklmnopqrstuvwxyz’:
if c in in_str:
a = c + a
else:
d = d + 1
r += 2
print(a) # Line 1
print(d) # Line 2
print(r) # Line 3
</pre>
|
[
"2",
"3",
"21",
"27"
] |
B
|
The statement r+=2 is not inside the loop, so we just assign 1 to r, then add 2 to it to get 3.
|
canterbury_cs
|
canterbury_cs_0000610
|
Consider the following Python code:
<pre lang="text/x-python">s = input(’Enter a string: ’)
w = ’’
for c in s:
if c in "0123456789":
#REPLACE
else:
w = w + c
print w
</pre>
What replacement for the comment #REPLACE will cause the program to print the input string with all of the digits removed? In other words, for the 'aaa3b3c1', we would print 'aaabc'.
|
[
"break",
"continue",
"return w",
"any of the above"
] |
B
|
A is wrong because break will end the loop.
C is wrong because the problem asks us to print, not to return.
The continue statement works because it goes back to the top of the loop.
|
canterbury_cs
|
canterbury_cs_0000611
|
Consider the following Python code:
<pre lang="text/x-python">s = input(’Enter a string: ’)
w = ’’
for c in s:
if c in "0123456789":
#REPLACE
else:
w = w + c
print(w)
</pre>
What replacement statement for the comment <code>#REPLACE <span style="font-family: arial,helvetica,sans-serif;">will cause the code to print out all of the characters up to the first digit? In other words, if the input is 'aaa3bb3c1', we should output 'aaa'.</span></code>
|
[
"break",
"continue",
"return w",
"any of the above will work"
] |
A
|
B is wrong because <code>continue</code> will skip the digits but continue with the rest of the String.
C is wrong because the exercise askes us to <strong>PRINT</strong>, not return the String.
|
canterbury_cs
|
canterbury_cs_0000612
|
Searching a heap is
|
[
"O(1)",
"O(log N)",
"O(N)",
"O(N log N)"
] |
C
|
In a heap, we only know that a node's key is greater than both its chidrens' keys. We may need to search both subheaps for an element. In the worst case, we'll visit every element.
|
canterbury_cs
|
canterbury_cs_0000613
|
What is the heap condition?
|
[
"All but the leaf nodes have two children",
"The tree is a binary tree",
"Each node's key is greater than its childrens' keys",
"Only the last level of the tree may not be full"
] |
C
|
All are true, but only C provides the definition of the heap condition.
|
canterbury_cs
|
canterbury_cs_0000614
|
Why can a heap be efficiently implemented using an array instead of a linked structure?
|
[
"It is complete",
"The array never needs to change size",
"We only traverse the heap in a breadth-first fashion",
"The heap condition makes it easier to calculate indices"
] |
A
|
Because the heap is complete, the elements can be stored contiguously and parent and child nodes (which we're guaranteed to have) fall in locations we can compute with simple arithmetic.
|
canterbury_cs
|
canterbury_cs_0000615
|
You have a class Custom:
<pre lang="text/x-java">class Custom {
private int i;
...
public String toString() {
return "" + i;
}
}
</pre>
<span style="font-family: arial, helvetica, sans-serif;">Consider this code, which prints a Custom instance:</span>
<code>Custom a = ...;</code> <span style="font-family: arial, helvetica, sans-serif;"><code>System.out.println(a);</code><br></span>
<span style="font-family: arial, helvetica, sans-serif;"><span style="font-family: arial, helvetica, sans-serif;">What overloaded version of PrintStream.println is called?</span></span>
|
[
"println()",
"println(int i)",
"println(Object o)",
"println(Custom c)"
] |
C
|
The version of println that we call must have a type that is a supertype of Custom, leaving only Custom and Object as our two choices. Since PrintStream was written years before our Custom class ever existed, it doesn't know anything about our class. However, it does know about Objects and that all Objects have a toString method.
|
canterbury_cs
|
canterbury_cs_0000616
|
What does the following Java code print?
<pre lang="text/x-java">int sum=0;
for (int i=1; i<=5; i++) {
sum += i;
}
System.out.println(sum);
</pre>
|
[
"0",
"5",
"10",
"15"
] |
D
|
This is basically 1 + 2 + 3 + 4 + 5
|
canterbury_cs
|
canterbury_cs_0000617
|
What does the following Java code print?
<pre lang="text/x-java">int sum=0;
for (int i=0; i<10; i+=4) {
sum += i;
}
System.out.println(sum);
</pre>
|
[
"4",
"8",
"12",
"16"
] |
C
|
0 + 4 + 8
|
canterbury_cs
|
canterbury_cs_0000618
|
What does the following Java code print?
<pre lang="text/x-java">int sum=0;
for (int i=0; i>100; i++) {
sum += i;
}
System.out.println(sum);
</pre>
|
[
"0",
"100",
"0 + 1 + 2 + ... + 99 + 100",
"0 + 1 + 2 + ... + 98 + 99"
] |
A
|
This is a trick question! The stop condition of the for loop is i>100, and since i starts at 0, i is never > 100, so the loop immediately ends.
|
canterbury_cs
|
canterbury_cs_0000619
|
Suppose the following Java code prints "statement 2":
<pre lang="text/x-java">if (num < 6) {
System.out.println("statement 1");
} else {
System.out.println("statement 2");
}
</pre>
What must be true about num?
|
[
"greater than 6",
"greater than or equal to 6",
"less than 6",
"less than or equal to 6"
] |
B
|
The opposite of less than is greater than or equal to. Not just greater than.
|
canterbury_cs
|
canterbury_cs_0000620
|
Suppose <code>a</code> is <code>true<span style="font-family: arial, helvetica, sans-serif;">,</span></code><span style="font-family: arial, helvetica, sans-serif;"> <code>b</code> is <code>false<span style="font-family: arial, helvetica, sans-serif;">, and <code>c</code> is <code>false</code></span></code>. Which of the following expressions is <code>true</code>?</span>
|
[
"<code>b && c && !a</code>",
"<code>!a || (b || c)</code>",
"c && !a",
"<code>!c && !b</code>"
] |
D
|
Substitute:
<ol>
<li>!c && !b</li>
<li>!false && !false</li>
<li>true && true</li>
<li>true</li>
</ol>
|
canterbury_cs
|
canterbury_cs_0000621
|
Only one of the following will not compile and run under Quick C. Which one?
|
[
"main(){}",
" main(void){}",
"int main(){return 65535;}",
"int main(void){return 1}"
] |
C
|
The semi-colon end of line delimiter occurs within a curly bracket pairing, which is not a logical combination from the compiler's parsing perspective
|
canterbury_cs
|
canterbury_cs_0000622
|
What is output by the code shown in the question below. Think about it carefully - it may be a bit tricky!
void main (void)
{
#define LIMIT 8
int i = 0;
while ( i++ < LIMIT )
{
if ( i )
{
printf( "%d", LIMIT - i );
}
}
}
|
[
"Nothing",
"876543210",
"876543210-1",
"76543210"
] |
D
|
The while loop increments the index before each iteration of the loop and the resulting increased index value is subtracted from the Limit of 8. Thus the values printed range from 7 to 0.
|
canterbury_cs
|
canterbury_cs_0000623
|
You are preparing test data for this function that accepts a day of the month (as a number) from the user:
int iGetDay(int iMonth);
You are currently working with a month value of 5 (May). What should your boundary test values be for iGetDay?
|
[
"0, 1, 30, 31",
"0, 1, 29, 30",
"-1, 0, 31, 32",
"0, 1, 31, 32"
] |
D
|
The month of May has 31 days and starts on the 1st of May. So the day before and the day after these boundary days consitute the boundary test conditions
|
canterbury_cs
|
canterbury_cs_0000624
|
How many times will the printf statement be executed in this code?
In each case assume the definition
int i = 0;
WARNING There are some very nasty traps in some of the code here. LOOK AT IT ALL VERY CAREFULLY!
do
{
printf("Count me!");
i++;
} while(++i < 10);
|
[
"0",
"5",
" 6",
"10"
] |
B
|
The line i++; in the body of the loop after the printf function, in combination with the ++i preceding each iteration in the while loop, causes the index to increment twice in each iteration, so the printf function is only executed 5 times
|
canterbury_cs
|
canterbury_cs_0000625
|
The worst-case analysis (Big-Oh) of the following Java code is:
<pre lang="text/x-java">for (j=0; j<n; j++) {
for (k=0; k<n; k++) {
base = base * 2;
}
</pre>
<code>}</code>
|
[
"O(1)",
"O(n)",
"O(n<sup>2</sup>)",
"O(log<sub>2</sub>n)"
] |
C
|
The body of the inner loop will be executed n<sup>2</sup> times. Each execution will take a constant amount of time. Thus, the total amount of time will be a function of n<sup>2.</sup>
|
canterbury_cs
|
canterbury_cs_0000626
|
The worst-case analysis (Big-Oh) of the following Java code is:
<br><code>for (j=0; j<n; j++) {</code>
<pre lang="text/x-java"> base = base * 2;
}
</pre>
|
[
"O(1)",
"O(n)",
"O(n<sup>2</sup>)",
"O(log<sub>2</sub>n)"
] |
B
|
The loop will be executed n times, and each time involves a certain constant amount of work.
|
canterbury_cs
|
canterbury_cs_0000627
|
Consider the following Java code:
<pre lang="text/x-java">public interface StackADT<T> { // 1
public void push (T element);
public T pop();
public T peek();
public boolean isEmpty();
}
</pre>
<br><span style="font-family: arial, helvetica, sans-serif;">The capital letter “T” on line 1 stands for:</span>
|
[
"a temporary value",
"the type of the items in the Stack",
"a class named T defined somewhere else in the program",
"the top of the stack"
] |
B
|
This interface is defined using Java's generics. We can tell what the T means by looking at how it is used. The "T" in angle brackets on line 1 corresponds to the type of the parameter to <code>push</code> and the type of value returned by <code>pop</code> and <code>peek</code>. In other words, it is the type of value stored in the stack.
|
canterbury_cs
|
canterbury_cs_0000628
|
The <code>StackADT</code>'s <code>push</code> operation:
|
[
"adds a new item at the bottom of the Stack",
"returns without removing the top item on the Stack",
"removes and returns the top item on the Stack",
"adds a new item at the top of the Stack"
] |
D
|
"push" is the traditional term for adding a new item to a stack. Stacks work like a pile of paper. The bottom piece of paper is the first one that was put in the pile. The top piece of paper was added most recently. When another piece of paper is added to the stack, it goes on top of the rest of the pile.
|
canterbury_cs
|
canterbury_cs_0000629
|
Suppose you are trying to choose between an array and a linked list to store the data in your Java program. Which data structure can change size as needed while the program is running?
|
[
"an array",
"a linked list",
"both",
"neither"
] |
B
|
When initializing a Java array, you must allocate a fixed amount of memory for storing data. Linked lists vary in size depending on how much data they contain.
|
canterbury_cs
|
canterbury_cs_0000630
|
Suppose <code>StackADT</code> is implemented in Java using a linked list. The big-Oh time complexity of the following <code>pop</code> method is:
<pre lang="text/x-java">public T pop() {
T tmp;
if (this.top == null)
tmp = null;
else {
tmp = this.top.getElement();
this.top = this.top.getNext();
}
return tmp;
}
</pre>
|
[
"O(1)",
"O(log n)",
"O(n)",
"O(n<sup>2</sup>)"
] |
A
|
These operations can be done in constant time, independent of the size of the stack.
|
canterbury_cs
|
canterbury_cs_0000631
|
Consider the following Java method:
<pre lang="text/x-java">public T pop() { \\ 1
T tmp; \\ 2
if (this.top == null) \\ 3
tmp = null; \\ 4
else { \\ 5
tmp = this.top.getElement(); \\ 6
this.top = this.top.getNext(); \\ 7
} \\ 8
return tmp; \\ 9
} \\ 10
</pre>
<span style="font-family: arial, helvetica, sans-serif;">What would happen if line 7 were before line 6?</span>
|
[
"The code would not compile.",
"The code would compile, but it wouldn't run.",
"The code would compile and run, but it would return the wrong value.",
"The code would compile and run, and it would work just the same. "
] |
C
|
If the two lines are re-ordered, the code will change the value of top to the second node, and then the second value in the stack will be the one that is returned.
|
canterbury_cs
|
canterbury_cs_0000632
|
Consider the following Java method:
<pre lang="text/x-java">public T pop() { // 1
T tmp; // 2
if (this.top == null) // 3
tmp = null; // 4
else { // 5
tmp = this.top.getElement(); // 6
this.top = this.top.getNext(); // 7
} // 8
return tmp; // 9
} // 10
</pre>
<span style="font-family: arial, helvetica, sans-serif;">What would happen if line 7 were before line 6?</span>
|
[
"The code would not compile.",
"The code would compile, but it wouldn't run.",
"The code would compile and run, but it would return the wrong value.",
"The code would compile and run, and it would work just the same. "
] |
C
|
If the two lines are re-ordered, the code will change the value of top to the second node, and then the second value in the stack will be the one that is returned.
|
canterbury_cs
|
canterbury_cs_0000633
|
What is output by the code shown below. Think about it carefully - it may be a bit tricky!
#define DEF_1 (2 + 2)
#define DEF_2 DEF_1 - DEF_1
int main(void)
{
printf("%d", DEF_2);
}
|
[
"0",
"2",
"4",
"6"
] |
A
|
The declaration of DEF_2 [DEF_1 after subtracting DEF_1 = 0 ] is the value printed.
|
canterbury_cs
|
canterbury_cs_0000634
|
What is output by the code shown below. Think about it carefully -it may be a bit tricky!
void main( void )
{
static char szCode[] = "111";
szCode[2] = '0';
puts( szCode );
}
|
[
"Nothing",
"011",
"101",
"110"
] |
D
|
The value of the character in the array at index [2] (where index starts from 0) is set to zero and the other initialized array values remain unchanged
|
canterbury_cs
|
canterbury_cs_0000635
|
What would be the performance of removeMin and insert methods in a priority queue if it is implemented by an unsorted list?
|
[
"O(1) , O(1)",
" O(1) , O(n)",
" O(n) , O(1)",
"O(n) , O(n)"
] |
C
|
O(n) , O(1)
|
canterbury_cs
|
canterbury_cs_0000636
|
Consider this code. It is part of a program that receives MMP list votes from constituencies and stores them in a database. The field PartyName stores the names of each party involved, and the field VoteTotal keeps a running total of list votes for a party. Tally is a table-type Recordset.
Do
' Get a name and vote pair from the newly arrived text
' Returns 0 when there are no more pairs
Start% = ExtractNextPair((txtData.Caption), Start%,_<br> PartyName$,_Votes&)
If Start% > 0 Then
' See if the party name is already present
KeyField$ = UCase$(Trim$(PartyName$))
Tally.Seek "=", KeyField$
If Tally.NoMatch Then
' Add a new record
Tally.AddNew
Tally("PartyName") = KeyField$
Tally("VoteTotal") = Votes
Else
' Update the existing record - add latest votes
Tally.Edit
Tally("VoteTotal") = Tally("VoteTotal") + Votes
End If
End If
Loop While Start% > 0
Tally.Update
Why does this code not successfully store all the incoming data?
|
[
"The loop logic is wrong - it should be Start% < 0",
"There was no call to Tally.Refresh",
"There was no call to Tally.UpdateRecords",
"The line Tally.Update must come inside the loop"
] |
D
|
Because the line Tally.Update is outside the loop it is only executed once at the end of the program
|
canterbury_cs
|
canterbury_cs_0000637
|
Consider these lines of code:
txtName.SetFocus
txtName.SelStart = 0
txtName.SelLength = Len(txtName.Text)
txtName is an edit text box which is currently visible.
The code may cause a run time error. This will happen if:
|
[
"txtName is a multiline text box.",
"there is no text in txtName.",
"txtName is currently disabled.",
"some of the text in txtName is currently highlighted."
] |
C
|
A disabled edit text box may not be accessed by the setfocus method as this combination is not a logical action and therefore clashes with the underlying VB event model which controls the permissible sequences of events.
|
canterbury_cs
|
canterbury_cs_0000638
|
What would be the minimum number of required queue, to implement a stack?
|
[
"1",
"2",
"3",
"4"
] |
B
|
3
|
canterbury_cs
|
canterbury_cs_0000639
|
Which data structures would be proper in terms of performance to implement flight waiting lists?
|
[
"Priority Queue",
"Heap",
"Array",
"Linked list"
] |
B
|
Array
|
canterbury_cs
|
canterbury_cs_0000640
|
To implement student's database, which data structure is more appropriate?
|
[
"Heap",
"Binary Tree",
"Map",
"Priority Queue"
] |
C
|
Map
|
canterbury_cs
|
canterbury_cs_0000641
|
Using double hashing and following hash function and data, in which array slot number 31 will be inserted*?
N = 13
h(k) = k mod 13
d(k) = 7 - k mod 7
(h(k) + jd(k)) mod 13
18, 41, 22, 44, 59, 32, 31, 73
*Credit goes to Goodrich et.al. (Data Structures & Algorithms in Java)
|
[
"0",
"5",
"9",
"10"
] |
A
|
9
|
canterbury_cs
|
canterbury_cs_0000642
|
Given the declaration
<strong>Public StudentRecords as Collection</strong>
What line of code is required before the first student record is added to the collection?
|
[
"StudentRecords = New Collection",
"Set StudentRecords = Collection",
"Set StudentRecords = New Collection",
"Set Collection = StudentRecords"
] |
C
|
Set assigns an object reference to the new Collection object named StudentRecords
|
canterbury_cs
|
canterbury_cs_0000643
|
The following processes are arranged in alphabetical order:
1 Application event procedure is called.
2 Event procedure code is executed.
3 User clicks on a button with the mouse.
4 Windows detects an event.
5 Windows passes a message to the application containing the button.
In what order will these processes normally occur in a Visual Basic application?
|
[
"3, 4, 5, 1, 2",
"2, 3, 5, 4, 1",
"4, 3, 2, 5, 1",
"3, 5, 4, 1, 2"
] |
A
|
The button click causes windows to detect an event and pass a message to the application containing the button.
The application event procedure is called and the event procedure code is executed.
|
canterbury_cs
|
canterbury_cs_0000644
|
This Perl script does not work correctly. It is supposed to work out the average assessment mark for a student who has 6 marked exercises.
Whatever data the student enters, the script always displays an average mark of zero.
Note that line numbers have been added for reference only – they are not part of the script
1 my $total = 0;
2
3 for(my $mk = 1; $mk <= 6; $mk++)
4 {
5 print "Enter mark $mk: ";
6 my $mark = <STDIN>;
7 my $total += $mark;
8 }
9
10 my $average = $total / 6;
11 print "Average mark = $average.\n";
What is the problem?
|
[
"<= 6 on line 3 should be <",
"The my on line 6 should not be there.",
"The my on line 7 should not be there.",
" Line 10 should show $mark / 6."
] |
C
|
A my declares the listed variables to be local (lexically) to the enclosing block, file, or eval.
source http://perldoc.perl.org/functions/my.html
Therefore the accumulated value of $total is not available to the following block where the average is calculated
|
canterbury_cs
|
canterbury_cs_0000645
|
This Perl subroutine does not work correctly. It is supposed to work out whether or not a person qualifies for a discount and return the correct fare, discounted or not as the case may be. Children under 13, and elderly people 65 and over qualify for a discount.
The subroutine never gives a discount
Note that line numbers have been added for reference only – they are not part of the script
1 sub getDiscount
2 {
3 my $age = $_[0]; # First parameter is age
4 my $fare = $_[1]; # Second parameter is fare
5
6 if(($age < 13) && ($age >= 65))
7 {
8 $fare *= 0.9; # 10% discount
9 }
11
12 return $fare;
13 }
What is the problem?
|
[
"$_ on lines 3 and 4 should be @_.",
"The and (&&) on line 6 should be an or (||)..",
"On line 6, the < should be replaced by <=.",
"The return statement on line 12 should return $_[0]."
] |
B
|
No age can be less than 13 AND 65 and over. It should be OR.
|
canterbury_cs
|
canterbury_cs_0000646
|
This Bash command was supposed to run a Perl script, exE1.pl from a University student's bin directory on the Cislinux server, passing it a command line argument which was a text file in the copy area. It was supposed to put the results into a text file in the student's bin directory.
~/bin/exE1.pl /copy/ex/exe-1.txt > /bin/exe1out.txt
Why will this give an error?
|
[
"~ cannot be used at the start of a line",
".txt is not a valid extension in linux",
"> does not redirect output",
"Students cannot write to /bin"
] |
D
|
The /bin directory has restricted access for security reasons to prevent students running unauthorised code on the server or modifyng or overwriting other programs in the directory
|
canterbury_cs
|
canterbury_cs_0000647
|
The following is a skeleton for a method called "maxPos":
<pre lang="text/x-java">public static int maxPos(int[] y, int first, int last) {
/* This method returns the position of the maximum element in the
* subsection of the array "y", starting at position
* "first" and ending at position "last".
*/
int bestSoFar = first;
xxx missing for loop goes here
return bestSoFar;
} // method maxPos
</pre>
In this question, the missing "for" loop is to run "forwards". That is, the code should search the array from the low subscripts to the high subscripts. Given that, the correct code for the missing "for" loop is:
|
[
"<pre lang=\"text/x-java\">for (int i=last; i>first; i--) {\n if ( y[i] < y[bestSoFar] ) {\n bestSoFar = i;\n } // if\n} // for\n</pre>",
"<pre lang=\"text/x-java\">for (int i=first+1; i<=last; i++) {\n if ( y[i] > y[bestSoFar] ) {\n bestSoFar = i;\n } // if\n} // for\n</pre>",
"<pre lang=\"text/x-java\">for (int i=last; i>first; i--) {\n if ( y[i] > y[bestSoFar] ) {\n bestSoFar = i;\n } // if\n} // for\n</pre>",
"<pre lang=\"text/x-java\">for (int i=last; i>first; i--) {\n if ( y[i] < bestSoFar ) {\n bestSoFar = i\n } // if\n} // for\n</pre>"
] |
B
|
Explanation<br>a)
INCORRECT:
if (y[i] < y[bestSoFar]) ... This is setting bestSoFar to the index of the SMALLEST number so far, but this is MAXPOS, it needs to find the highest!
b)
CORRECT:
The code finds the maximum position in the array, searching forwards as intended.
c)
CORRECT:
The loop is running backwards.
d)
INCORRECT:
The if statement compares y[i] with the integer bestSoFar, not what is in the array at the position bestSoFar.
c)
INCORRECT:
Same as c) and d)
|
canterbury_cs
|
canterbury_cs_0000648
|
<p class="MsoPlainText"><span style="font-family: 'Times New Roman','serif';"><span style="font-family: arial,helvetica,sans-serif;">The following is a skeleton for a method called "maxPos":<br></span></span>
<pre lang="text/x-java">public static int maxPos(int[] y, int first, int last) {
/* This method returns the position of the maximum element in the
* subsection of the array "y", starting at position
* "first" and ending at position "last".
*/
int bestSoFar = first;
xxx missing for loop goes here
</pre>
<code> </code> <code>return bestSoFar;</code>
<code>} // method maxPos</code>
<span style="font-family: arial,helvetica,sans-serif;">In this question<span>, the missing "for" loop is to run "backwards". That is, the code should search the array f<span>rom the high subscripts to the low subscripts. Given that, the correct code for the missing "for" loop is:</span></span></span>
|
[
"<pre lang=\"text/x-java\">for (int i=last; i>first; i--) { \n if ( y[i] < y[bestSoFar] ) { \n bestSoFar = i; \n } // if \n} // for\n</pre>",
"<pre lang=\"text/x-java\">for (int i=first+1; i<=last; i++) {\n if ( y[i] > y[bestSoFar] ) {\n bestSoFar = i;\n } // if\n} // for\n</pre>",
"<pre lang=\"text/x-java\">for (int i=last; i>first; i--) { \n\n if ( y[i] > y[bestSoFar] ) { \n\n bestSoFar = i; \n\n } // if\n\n} // for \n</pre>",
"<pre lang=\"text/x-java\">for (int i=last; i>first; i--) {\n if ( y[i] < bestSoFar ) {\n bestSoFar = i\n } // if\n} // for\n</pre>"
] |
C
|
a)
INCORRECT:
if (y[i] < y[bestSoFar]) ... This is setting bestSoFar to the index of the SMALLEST number so far, but this is MAXPOS, it needs to find the highest!
b)
INCORRECT:
The loop starts at [first+1] ... This loop is not running backwards.
c)
CORRECT:
The code finds the maximum position in the array, searching backwards as intended.
d)
INCORRECT:
The if statement compares y[i] with the integer bestSoFar, not what is in the array at the position bestSoFar.
c)
INCORRECT:
Same as b) and d)
|
canterbury_cs
|
canterbury_cs_0000649
|
The following code for a method "minVal" contains a logic error on a single line in the method body, on one of the four lines indicated by comments:<br><br><code>public static int minVal(int[] y, int first, int last) {</code>
<pre lang="text/x-java">/* This method returns the value of the minimum element in the
* subsection of the array "y", starting at position
* "first" and ending at position "last".
*/
int bestSoFar = first; // line 1
for (int i=first+1; i<=last; i++)
{
if ( y[i] < bestSoFar ) // line 2
</pre>
<code> bestSoFar = y[i]; // line 3</code><br><br><code> } // for</code><br><br><code> return bestSoFar; // line 4</code><br><br><code>} // method minVal</code><br><br>Which one of the four lines indicated by the comments contains the logic error?
|
[
"line 1",
"line 2",
"line 3",
"line 4"
] |
A
|
line 1 should be <code>int bestSoFar = y[first];</code>
This correct code assigns the value at <code>y[first] <span style="font-family: arial,helvetica,sans-serif;">into</span></code> bestSoFar. <span style="font-family: arial,helvetica,sans-serif;">This is because the other lines are using bestSoFar to remember the best VALUE seen thus far. <br></span>
|
canterbury_cs
|
canterbury_cs_0000650
|
The following code for a method "minVal" contains a logic error on a single line in the method body, on one of the four lines indicated by comments:<br><br><code>public static int minVal(int[] y, int first, int last) {</code>
<pre lang="text/x-java">/* This method returns the value of the minimum element in the
* subsection of the array "y", starting at position
* "first" and ending at position "last".
*/
int bestSoFar = first; // line 1
for (int i=first+1; i<=last; i++)
{
if ( y[i] > bestSoFar ) // line 2
</pre>
<code> bestSoFar = y[i]; // line 3</code><br><br><code> } // for</code><br><br><code> return bestSoFar; // line 4</code><br><br><code>} // method minVal</code><br><br>Which one of the four lines indicated by the comments contains the logic error?
|
[
"line 1",
"line 2",
"line 3",
"line4"
] |
B
|
line 2 should be <code>if ( y[i] < bestSoFar )</code>
<code>The > sign in this buggy line is looking for the MAXIMUM value in the array.</code>
|
canterbury_cs
|
canterbury_cs_0000651
|
The following code for a method "minVal" contains a logic error on a single line in the method body, on one of the four lines indicated by comments:<br><br><code>public static int minVal(int[] y, int first, int last) {</code>
<pre lang="text/x-java">/* This method returns the value of the minimum element in the
* subsection of the array "y", starting at position
* "first" and ending at position "last".
*/
int bestSoFar = y[first]; // line 1
for (int i=first+1; i<=last; i++)
{
</pre>
<code> if ( bestSoFar </code><code><code>< </code></code><code><code>y[i] </code>) // line 2</code>
<code> bestSoFar = y[i]; // line 3</code><br><br><code> } // for</code><br><br><code> return bestSoFar; // line 4</code><br><br><code>} // method minVal</code><br><br>Which one of the four lines indicated by the comments contains the logic error?
|
[
"line 1",
"line 2",
"line 3",
"line 4"
] |
B
|
line 2 should be if ( bestSoFar > y[i] )
The < sign in this buggy line is looking for the MAXIMUM value in the array.
|
canterbury_cs
|
canterbury_cs_0000652
|
What does this print?
<pre lang="text/x-java">double d = 8 / 10;
System.out.println(d);
</pre>
|
[
"d",
"0",
"0.8",
"2"
] |
B
|
Integer division! 10 goes into 8 zero times, with a remainder of 8. But since this is integer division, we only care about the quotient, which is zero!
|
canterbury_cs
|
canterbury_cs_0000653
|
After the assignments <code>x = 27</code> and <code>y = 12</code>, what is returned by <code>x%y</code>?
|
[
"2",
"2.25",
"3",
"3.0"
] |
C
|
The % operation performs "mod" (modular division), which means "do the division but return the remainder rather than the quotient".
|
canterbury_cs
|
canterbury_cs_0000654
|
After the assignments <code>x = 27</code> and <code>y = 12</code>, what is returned by <code>not x <= y < x + y</code> ?
|
[
"True",
"False",
"1",
"0"
] |
B
|
Python has an incredibly flexible syntax which allows expressions like x <= y < x + y (this statement would not compile in many other languages).
If we break this expression down:
x+y is 39, so the expression is asking:
not 12 <= 27 < 39
12 <= 27 < 39 is True, as 27 is in fact between 12 and 39. And if we apply not to True, we get False.
|
canterbury_cs
|
canterbury_cs_0000655
|
Which of the following assertions about the effect of <code>int(my_var)</code> is correct?
|
[
"It modifies the value of <code>my_var</code>, truncating it to make it an integer.",
"It returns <code>True </code>if <code>my_var</code> is an integer; and <code>False</code>, otherwise.",
"It produces an error if <code>my_var </code>is not an integer.",
"It returns the integer representation of the value associated with <code>my_var</code>, or an error"
] |
D
|
A is wrong because the int() function does not modify (mutate) its parameter
B is wrong because the int() function does not return a Boolean, it returns an int()
C is wrong because the int() function works with double/float inputs, which are not ints (although it will generate an error if invoked with a String, for example)
D is correct! int() converts ints and doubles to an int, truncating any extra information (i.e. the decimal portion of a double), and will generate an error if called with a non-numeric value
|
canterbury_cs
|
canterbury_cs_0000656
|
The following methods are in Java's String class:
<ul>
<li>int indexOf(int ch) - Returns the index within this string of the first occurrence of the specified character.</li>
<li>int indexOf(int ch, int fromIndex) - Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.</li>
<li>String substring(int beginIndex, int endIndex) - Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.</li>
</ul>
Given these specifications, what does the following code print?
<pre lang="text/x-java">String s = "ichi,ni,san,go";
int i1 = s.indexOf(',');
int i2 = s.indexOf(',', i1);
System.out.println(s.substring(i1, i2));
</pre>
|
[
"<code>\"ni\"</code>",
"The empty string",
"<code>\",\"</code>",
"<code>\",ni\"</code>"
] |
A
|
i1 is 4, and so is i2, since the second indexOf call begins searching at the position of the same comma that was found by the first indexOf call. Calling substring with identical parameters yields a 0-length String.
|
canterbury_cs
|
canterbury_cs_0000657
|
The following is a skeleton for a method called "minPos":
<pre lang="text/x-java">public static int minPos(int[] y, int first, int last) {
/* This method returns the position of the minimum element in the
* subsection of the array "y", starting at position
* "first" and ending at position "last".
*/
int bestSoFar = first;
xxx missing for loop goes here
return bestSoFar;
} // method minPos
</pre>
In this question, the missing "for" loop is to run "backwards". That is, the code should search the array from the high subscripts to the low subscripts. Given that, the correct code for the missing "for" loop is:
|
[
"<pre lang=\"text/x-java\">for (int i=last; i>first; i--) {\n if ( y[bestSoFar] < y[i] ) {\n bestSoFar = i;\n } // if\n} // for\n</pre>",
"<pre lang=\"text/x-java\">for (int i=first+1; i<=last; i++) {\n if ( y[bestSoFar] < y[i] ) {\n bestSoFar = i;\n } // if\n} // for\n</pre>",
"<pre lang=\"text/x-java\">for (int i=last; i>first; i--) {\n if ( y[bestSoFar] < y[i] ) {\n bestSoFar = i;\n } // if\n} // for\n</pre>",
"<pre lang=\"text/x-java\">for (int i=last; i>first; i--) {\n if ( y[i] < y[bestSoFar] ) {\n bestSoFar = i\n } // if\n} // for\n</pre>"
] |
D
|
a)<br>INCORRECT:<br>if (y[bestSoFar] < y[i]) ... This is setting bestSoFar to the index of the LARGEST number so far.<br><br>b)<br>INCORRECT:<br>The loop starts at first+1 ...not running backwards.
This loop is if (y[bestSoFar] < y[i]) ... This is setting bestSoFar to the index of the LARGEST number so far.
<br>c)<br>INCORRECT:<br>if (y[bestSoFar] < y[i]) ... This is setting bestSoFar to the index of the LARGEST number so far.<br><br>d) CORRECT!<br><br>e)<br>INCORRECT:<br>The if statement compares y[i] with the integer bestSoFar, not what is in the array at the position bestSoFar.<br>The loop starts at first+1 ... This loop is not running backwards.
|
canterbury_cs
|
canterbury_cs_0000658
|
<pre lang="text/x-java">The following is a skeleton for a method called "minPos":
public static int minPos(int[] y, int first, int last) {
/* This method returns the position of the minimum element in the
* subsection of the array "y", starting at position
* "first" and ending at position "last".
*/
int bestSoFar = first;
xxx missing for loop goes here
return bestSoFar;
} // method minPos
In this question, the missing "for" loop is to run "forwards". That is, the code should search the array from the high subscripts to the low subscripts. Given that, the correct code for the missing "for" loop is:
</pre>
|
[
"<pre lang=\"text/x-java\">for (int i=last; i>first; i--) {\n if ( y[bestSoFar] < y[i] ) {\n bestSoFar = i;\n } // if\n} // for\n</pre>",
"<pre lang=\"text/x-java\">for (int i=first+1; i<=last; i++) {\n if ( y[bestSoFar] > y[i] ) {\n bestSoFar = i;\n } // if\n} // for\n</pre>",
"<pre lang=\"text/x-java\">for (int i=last; i>first; i--) {\n if ( y[bestSoFar] > y[i] ) {\n bestSoFar = i;\n } // if\n} // for\n</pre>",
"<pre lang=\"text/x-java\">for (int i=last; i>first; i--) {\n if ( bestSoFar < y[i] ) {\n bestSoFar = i\n } // if\n} // for\n</pre>"
] |
B
|
a)<br>INCORRECT:<br>The loop starts at last ... This loop is not running forwards.<br>if (y[bestSoFar] < y[i]) ... This is setting bestSoFar to the index of the LARGEST number so far.<br><br>b)<br>CORRECT!<br><br>c)<br>INCORRECT:<br>The loop starts at last ... This loop is not running forwards.<br><br>d)<br>INCORRECT:<br>The loop starts at last ... This loop is not running forwards.<br>The if statement compares y[i] with the integer bestSoFar, not what is in the array at the position bestSoFar.<br><br>e)<br>INCORRECT:<br>The if statement compares y[i] with the integer bestSoFar, not what is in the array at the position bestSoFar.<br>The loop starts at first+1 ... This loop is not running forwards.
|
canterbury_cs
|
canterbury_cs_0000659
|
Assume this Perl subroutine is called like this:
&compare(6, 4);
sub compare
{
my $ret = 0;
if($_[0] > $_[1])
{
$ret = 1;
}
elsif($_[0] < $_[1])
{
$ret = -1;
}
return $ret;
}
What will the subroutine return?
|
[
"-1",
"0",
"1",
"undef"
] |
C
|
6 (the first parameter) is greater than 4 (the second parameter) so it returns 1
|
canterbury_cs
|
canterbury_cs_0000660
|
In this question, you are given a Perl regular expression that you are required to evaluate.
There are no leading or trailing spaces in any of the text, nor are there any spaces in the regex.
Identify the answer which best matches the regex below:
/write/
|
[
"Write your name at the top.",
"Your writing is hard to read.",
"Did you write your ID on the paper?",
"Who sends handwritten letters anymore?"
] |
C
|
The expression generates a match for a sentence which contains the full string (case sensitive) given between the / / delimiters
|
canterbury_cs
|
canterbury_cs_0000661
|
In this question, you are given a Perl regular expression that you are required to evaluate.
There are no leading or trailing spaces in any of the text, nor are there any spaces in the regex.
Identify the answer which best matches the regex below:
/^[A-Z]\d/
|
[
"ABC123",
"X3g5.",
"3Y3",
"a5D2"
] |
B
|
Must begin with an upper case letter followed by a digit.
|
canterbury_cs
|
canterbury_cs_0000662
|
An example of something that could be built using a ListADT is a structure that models:
|
[
"The undo operation in a word processor",
"The back button in a web browser",
"the customers waiting to pay at the university dining hall",
"an ordered to-do list "
] |
D
|
In ListADT, items can be added to or removed from the beginning, middle, or end of the structure. This is not well suited for choices A or B, where you need to remove only the most recently added item.
For choice C, you need to ensure that the item removed is always the one that was stored first, so ListADT won't work there either.
Choice E would be best modeled by a nonlinear structure, and ListADT (like StackADT and QueueADT) is linear.
That leaves Choice D. A list of things to do can be linear -- one thing after another -- and it may be desirable to add new items at the beginning, middle, or end, depending on how important they are.
|
canterbury_cs
|
canterbury_cs_0000663
|
Identify the bug in the following code (if any):
<br><code>public boolean search(T item,MyList<T> list){ // 1</code>
<code> this.search(item, list.getRest()); // 2</code> <code>} // 3</code>
|
[
"There is no base case",
"The problem is not self-similar",
"There is a base case, but the problem does not get smaller",
"There are no bugs"
] |
A
|
For a recursive solution to a problem, you need three things:
(1) a base case (where the problem can be solved without recursion)
(2) a self-similar problem (one that contains similar problem(s) to itself)
(3) a way of making the problem smaller so you get closer to the base case
Here (2) is satisfied -- lists contain smaller lists. (3) is satisfied by line 2 of the method. But there is no base case.
|
canterbury_cs
|
canterbury_cs_0000664
|
Identify the bug in the following code (if any):
<br><code>public boolean search(T item, ListADT list){ // 1</code>
<pre lang="text/x-java"> if (list == null) // 2
return false; // 3
else if (list.first()==item) // 4
return true; // 5
else // 6
return this.search(item, list); // 7
</pre>
<code>} // 8</code>
|
[
"There is no base case",
"The problem is not self-similar",
"The problem does not get smaller",
"There are no bugs"
] |
C
|
For a recursive solution to a problem, you need three things:<br>(1) a base case (where the problem can be solved without recursion)<br>(2) a self-similar problem (one that contains similar problem(s) to itself)<br>(3) a way of making the problem smaller so you get closer to the base case
Here (2) is satisfied -- lists contain smaller lists. (1) is satisfied by lines 2-5 of the method. But the recursive call in line 7 has the same parameter as the method itself (line 1), so the problem never gets smaller.
|
canterbury_cs
|
canterbury_cs_0000665
|
The worst-case time complexity of the following Java method is:
<pre lang="text/x-java">public int fibonacci (int n) {
if (n == 1)
return 1;
else if (n == 2)
return 1;
else if (n > 2)
</pre>
<code> return</code><code> fibonacci(n-1)+fibonacci(n-2);</code>
<pre lang="text/x-java"> else
return -1; // invalid input
</pre>
<code>}</code>
|
[
"O(log n)",
"O(n)",
"O(n<sup>2</sup>)",
"none of the above"
] |
D
|
This code is exponential, because it's doubly recursive.
|
canterbury_cs
|
canterbury_cs_0000666
|
The worst-case time complexity of quicksort is:
|
[
"O(1)",
"O(n)",
"O(n log n)",
"O(n<sup>2</sup>)"
] |
C
|
In the worst case, every time we partition the list, we divide it into two parts, one of size 0 and one of size n-1 (plus the pivot element). This would happen, for example, if all the elements of the list are equal, or if the list is already sorted and you choose the left-most element as your pivot.
We'd have to partition the list n times, because each time the pivot element is the only one that gets put in place. The first time we compare the pivot element with all n-1 other elements. The second time, we compare the new pivot with n-2 other elements, and so forth down to n - (n-1). So we do work proportional to 1+2+3+...+(n-1), or n(n-1)/2.<span style="font-size: 12px;"><br></span>
|
canterbury_cs
|
canterbury_cs_0000667
|
The following is a skeleton for a method called "minVal":<br><br><code>public static int minVal(int[] y, int first, int last) {</code>
<pre lang="text/x-java">/* This method returns the value of the minimum element in the
* subsection of the array "y", starting at position
* "first" and ending at position "last".
*/
int bestSoFar = y[first];
</pre>
<code> xxx missing for loop goes here</code><br><br><code> return bestSoFar;</code><br><br><code>} // method minVal</code> <br><br>In this question, the missing "for" loop is to run "forwards". That is, the code should search the array from the low subscripts to the high subscripts. Given that, the correct code for the missing "for" loop is:
|
[
"<pre lang=\"text/x-java\">for (int i=first+1; i<=last; i++) {\n if ( y[i] > y[bestSoFar] ) {\n bestSoFar = y[i];\n } // if\n} // for\n</pre>",
"<pre lang=\"text/x-java\">for (int i=last; i>first; i--) {\n if ( y[i] > y[bestSoFar] ) {\n bestSoFar = i;\n } // if\n} // for\n</pre>",
"<pre lang=\"text/x-java\">for (int i=last; i>first; i--) {\n if ( bestSoFar < y[i] ) {\n bestSoFar = i\n } // if\n} // for\n</pre>",
"<pre lang=\"text/x-java\">for (int i=first+1; i<=last; i++) {\n if ( y[i] < bestSoFar ) {\n bestSoFar = y[i];\n } // if\n} // for\n</pre>"
] |
D
|
a)<br>INCORRECT<br>The loop starts at last ... This loop is not running backwards.<br><br>b)<br>INCORRECT:<br>if ( y[i] > y[bestSoFar] ) ... bestSoFar is storing a value, not a position. In any event, this if condition is searching for the maximum value.<br><br>c)<br>INCORRECT:<br>The loop starts at last ... This loop is not running backwards.<br>if ( y[i] > y[bestSoFar] ) ... bestSoFar is storing a value, not a position. In any event, this if condition is searching for the maximum value.<br>bestSoFar = i; ... bestsoFar is being set to the position, not the value.<br><br>d)<br>INCORRECT:<br>The loop starts at last ... This loop is not running backwards.<br>if ( bestSoFar < y[i] ) ... this if condition is searching for the maximum value.<br>bestSoFar = i; ... bestsoFar is being set to the position, not the value.<br><br>e)<br>CORRECT!
|
canterbury_cs
|
canterbury_cs_0000668
|
The following is a skeleton for a method called "minVal":
<pre lang="text/x-java">public static int minVal(int[] y, int first, int last) {
/* This method returns the value of the minimum element in the
* subsection of the array "y", starting at position
* "first" and ending at position "last".
*/
int bestSoFar = y[first];
xxx missing for loop goes here
return bestSoFar;
} // method minVal
</pre>
In this question, the missing "for" loop is to run "backwards". That is, the code should search the array from the high subscripts to the low subscripts. Given that, the correct code for the missing "for" loop is:
|
[
"<pre lang=\"text/x-java\">for (int i=last; i>first; i--) {\n if ( y[i] < bestSoFar ) {\n bestSoFar = y[i];\n } // if\n} // for\n</pre>",
"<pre lang=\"text/x-java\">for (int i=first+1; i<=last; i++) {\n if ( y[i] > y[bestSoFar] ) {\n bestSoFar = y[i];\n } // if\n} // for\n</pre>",
"<pre lang=\"text/x-java\">for (int i=last; i>first; i--) {\n if ( y[i] > y[bestSoFar] ) {\n bestSoFar = i;\n } // if\n} // for\n</pre>",
"<pre lang=\"text/x-java\">for (int i=last; i>first; i--) {\n if ( bestSoFar < y[i] ) {\n bestSoFar = i\n } // if\n} // for\n</pre>"
] |
A
|
a)
CORRECT!
b)
INCORRECT:
The loop starts at first+1 ... This loop is not running backwards.
if ( y[i] > y[bestSoFar] ) ... bestSoFar is storing a value, not a position. In any event, this if condition is searching for the maximum value.
c)
INCORRECT:
if ( y[i] > y[bestSoFar] ) ... bestSoFar is storing a value, not a position. In any event, this if condition is searching for the maximum value.
bestSoFar = i; ... bestsoFar is being set to the position, not the value.
d)
INCORRECT:
<span style="font-family: arial,helvetica,sans-serif;">if ( bestSoFar < y[i] ) ... this if condition is searching for the maximum value.</span>
bestSoFar = i; ... bestsoFar is being set to the position, not the value.
e)
INCORRECT:
The loop starts at first+1 ... This loop is not running backwards.
if ( y[i] > y[bestSoFar] ) ... this if condition is searching for the maximum value.
bestSoFar = i; ... bestsoFar is being set to the position, not the value.
|
canterbury_cs
|
canterbury_cs_0000669
|
The following is a skeleton for a method called "maxVal":
<pre lang="text/x-java">public static int maxVal(int[] y, int first, int last) {
/* This method returns the value of the maximum element in the
* subsection of the array "y", starting at position
* "first" and ending at position "last".
*/
int bestSoFar = y[first];
xxx missing for loop goes here
return bestSoFar;
} // method maxVal
In this question, the missing "for" loop is to run "forwards". That is, the code should search the array from the low subscripts to the high subscripts. Given that, the correct code for the missing "for" loop is:
</pre>
|
[
"<pre lang=\"text/x-java\">for (int i=last; i>first; i--) {\n if ( y[i] < bestSoFar ) {\n bestSoFar = y[i];\n } // if\n} // for\n</pre>",
"<pre lang=\"text/x-java\">for (int i=first+1; i<=last; i++) {\n if ( y[i] > bestSoFar ) {\n bestSoFar = y[i];\n } // if\n} // for\n</pre>",
"<pre lang=\"text/x-java\">for (int i=last; i>first; i--) {\n if ( y[i] > y[bestSoFar] ) {\n bestSoFar = i;\n } // if\n} // for\n</pre>",
"<pre lang=\"text/x-java\">for (int i=last; i>first; i--) {\n if ( y[i] < bestSoFar ) {\n bestSoFar = i;\n } // if\n} // for\n</pre>"
] |
B
|
a)
INCORRECT:
<span style="font-family: arial,helvetica,sans-serif;">The loop starts at <code><span style="font-family: arial,helvetica,sans-serif;">last ... it is NOT running forward.</span></code><br></span>
if (y[i] < y[bestSoFar]) ... This is setting bestSoFar to the value of the SMALLEST number so far.
b)
CORRECT!
c)
CORRECT:
<span style="font-family: arial,helvetica,sans-serif;">The loop starts at <code><span style="font-family: arial,helvetica,sans-serif;">last ... it is NOT running forward.</span></code></span>
<span style="font-family: arial,helvetica,sans-serif;">if ( y[i] > y[bestSoFar] ) ... bestSoFar is storing the minimum value, NOT the position of the minimum value.</span>
<pre lang="text/x-java"><span style="font-family: arial,helvetica,sans-serif;">bestSoFar = i ... bestSoFar is being set to a postion, not the value at that poisition.
</span></pre>
d)
INCORRECT:
<span style="font-family: arial,helvetica,sans-serif;">The loop starts at <code><span style="font-family: arial,helvetica,sans-serif;">last ... it is NOT running forward.</span></code></span>
if (y[i] < bestSoFar) ... This is lokking for theSMALLEST value.
<code><span style="font-family: arial,helvetica,sans-serif;">bestSoFar = i ... bestSoFar is being set to a postion, not the value at that position.</span></code>
e)
INCORRECT:
b<code><span style="font-family: arial,helvetica,sans-serif;">estSoFar = i ... bestSoFar is being set to a postion, not the value at that position.</span></code>
|
canterbury_cs
|
canterbury_cs_0000670
|
Suppose you have the following partial code to sum up an array of <code>int</code>s:
<pre lang="text/x-java">int sum(int[] nums) {
int sum = 0;
...
return sum;
}
</pre>
Which of the following <em>does not</em> correctly complete this method?
|
[
"<pre lang=\"text/x-java\">for (int i = 0; i < nums.length; ++i)\n sum += nums[i];\n</pre>",
"<pre lang=\"text/x-java\">for (int i : nums)\n sum += i;\n</pre>",
"<pre lang=\"text/x-java\">for (int i = nums.length - 1; i >= 0; ++i)\n sum += nums[i];\n</pre>",
"<pre lang=\"text/x-java\">int i = 0;\nwhile (i < nums.length) {\n sum += nums[i];\n ++i;\n}\n</pre>"
] |
C
|
It appears in this solution that we are iterating through the list in reverse. However, we incorrectly increment the iterator, leading to an IndexOutOfBoundsException.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.