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_0000200
What will the following code output? <br><code>int arr[5];</code> <pre lang="text/x-c">int *p = arr; printf("p is %p -- ", p); ++p; printf("p is %p \n", p); </pre>
[ "p is 0x7fffb04846d0 &ndash; p is 0x7fffb04846d1", "p is 0x7fffb04846d0 &ndash; p is 0x7fffb04846d2", "p is 0x7fffb04846d0 &ndash; p is 0x7fffb04846d4", "Segmentation fault" ]
C
p will increase by the sizeof(int) == 4.
canterbury_cs
canterbury_cs_0000201
Consider the following short program, which does not meet all institutional coding standards: void vCodeString(char szText[ ]); /* First line */ #include &lt;stdio.h&gt; #include &lt;string.h&gt; #define MAX_LEN 12 int main(void) { &nbsp;&nbsp;&nbsp;&nbsp; char szData[MAX_LEN]; &nbsp;&nbsp;&nbsp;&nbsp; printf("Enter some text to code: "); &nbsp;&nbsp;&nbsp;&nbsp; scanf("%s", szData); &nbsp;&nbsp;&nbsp;&nbsp; vCodeString(szData); /* Line 8 */ &nbsp;&nbsp;&nbsp;&nbsp; printf("Coded string is %s\n", szData); } void vCodeString(char szText[ ]) { &nbsp;&nbsp;&nbsp;&nbsp; int i = -1; &nbsp;&nbsp;&nbsp;&nbsp; while(szText[++i]) &nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; szText[i] += (char)2; &nbsp;&nbsp;&nbsp;&nbsp; } } With the array size defined as MAX_LEN (or 12) bytes, what happens if I enter a word with more than 12 letters, such as hippopotamuses?
[ "You will get a run time error", "You will get a syntax error from the compiler", "Other data may be overwritten", "The array will be enlarged" ]
C
Now, C provides open power to the programmer to write any index value in [] of an array. This is where we say that no array bound check is there in C. SO, misusing this power, we can access arr[-1] and also arr[6] or any other illegal location. Since these bytes are on stack, so by doing this we end up messing with other variables on stack. Consider the following example : #include&lt;stdio.h&gt; unsigned int count = 1; int main(void) { int b = 10; int a[3]; a[0] = 1; a[1] = 2; a[2] = 3; printf("\n b = %d \n",b); a[3] = 12; printf("\n b = %d \n",b); return 0; } In the above example, we have declared an array of 3 integers but try to access the location arr[3] (which is illegal but doable in C) and change the value kept there. But, we end up messing with the value of variable &lsquo;b&rsquo;. Cant believe it?, check the following output . We see that value of b changes from 10 to 12. $ ./stk b = 10 b = 12 Source http://www.thegeekstuff.com/2011/12/c-arrays/
canterbury_cs
canterbury_cs_0000202
Suppose all of the computer's memory is available to you, and no other storage is available. Every time your array is filled to its capacity, you enlarge it by creating an array twice the size of the original, if sufficient memory is available, and copying over all elements. How large can your growable array get?
[ "33% of memory", "66% of memory", "25% of memory", "100% of memory" ]
B
When the array consumes 33% of memory and needs to expand, it can do so. The new array will consume 66% of memory. After this, there is not enough room for a larger array.
canterbury_cs
canterbury_cs_0000203
Using linear probing and following hash function and data, in which array slot number 31 will be inserted*? h(x) = x mod 13 18, 41, 22, 44, 59, 32, 31, 73 &nbsp; &nbsp; *credit goes to Goodrich et. al. (Data Structures &amp; Algorithms in Java)
[ "5", "6", "8", "10" ]
D
8
canterbury_cs
canterbury_cs_0000204
Which of the following is NOT a fundamental data type in C?
[ "int", "float", "string", "&nbsp;short" ]
C
A String is not a primitive data type.&nbsp; It can be thought of as an array of characters.
canterbury_cs
canterbury_cs_0000205
After the assignment statement<br><code>&nbsp; &nbsp;String word = "entropy";</code><br>what is returned by<br><code>&nbsp; &nbsp;word.substring(word.length());</code>
[ "\"entropy\"", "\"y\"", "the empty <code>String</code>", "an error" ]
C
<code>word.substring(n)</code> returns the substring of <code>word</code> that starts at index <code>n</code> and ends at the end of the <code>String</code>. If index <code>n</code> is greater than the index of the last character in the <code>String</code>, as it is here, <code>substring</code> simply returns the empty <code>String</code>.&nbsp;
canterbury_cs
canterbury_cs_0000206
The worst-case time complexity of quicksort is:
[ "O(1)", "O(n)", "O(n log n)", "O(n<sup>2</sup>)" ]
D
In the worst case, every time quicksort partitions the list, it is divided 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 in the list are equal, or if the list is already sorted and you always choose the leftmost element as a pivot.&nbsp; Quicksort would have to partition the list n times, because each time the pivot element is the only one that gets put in place. &nbsp;The first time quicksort compares the pivot element with all n-1 other elements. The second time, quicksort compares the new pivot with n-2 other elements, and so forth down to n - (n-1). So it does work proportional to 1+2+3+...+(n-1), or n(n-1)/2.<span style="font-size: 12px;"><br></span>
canterbury_cs
canterbury_cs_0000207
The time complexity of linear search is:
[ "O(1)", "O(log n)", "O(n)", "O(2<sup>n</sup>)" ]
C
The time required for linear search in the worst case is directly proportional to the amount of data.&nbsp;
canterbury_cs
canterbury_cs_0000208
An NP-Complete problem is:
[ "solvable, and the best known solution runs in polynomial time (i.e. feasible)", "solvable, and the best known solution is not feasible (i.e. runs in exponential time)", "currently unsolvable, but researchers are hoping to find a solution.", "provably unsolvable: it has been shown that this problem has no algorithmic solution." ]
B
NP-Complete problems typically have rather simplistic algorithmic solutions. &nbsp;The problem is that these solutions require exponential time to run. &nbsp;
canterbury_cs
canterbury_cs_0000209
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 </pre> &nbsp; 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&gt;first; i--) {\n if ( y[i] &lt; bestSoFar ) {\n bestSoFar = y[i];\n } // if\n} // for\n</pre>", "<pre lang=\"text/x-java\">for (int i=first+1; i&lt;=last; i++) {\n if ( y[i] &gt; y[bestSoFar] ) {\n bestSoFar = y[i];\n } // if\n} // for\n</pre>", "<pre lang=\"text/x-java\">for (int i=last; i&gt;first; i--) {\n if ( y[i] &gt; y[bestSoFar] ) {\n bestSoFar = i;\n } // if\n} // for\n</pre>", "<code>for (int i=last; i&gt;first; i--) {</code> <code>&nbsp;&nbsp;&nbsp; if ( bestSoFar &lt; </code><code><code>y[i] </code>) {</code> \n<pre lang=\"text/x-java\"> bestSoFar = y[i] \n } // if\n} // for\n</pre>" ]
D
a) INCORRECT: if (y[i] &lt; y[bestSoFar]) ... This is setting bestSoFar to the value of the SMALLEST number so far. b) INCORRECT: <span style="font-family: arial,helvetica,sans-serif;">The loop starts at first+1 ... This loop is not running backwards.</span> <pre lang="text/x-java"><span style="font-family: arial,helvetica,sans-serif;">if ( y[i] &gt; y[bestSoFar] ) ... bestSoFar is storing a value, not a position. </span></pre> &nbsp; c) INCORRECT <pre lang="text/x-java"><span style="font-family: arial,helvetica,sans-serif;">bestSoFar = i; ... bestsoFar is being set to the position, not the value. </span></pre> &nbsp; d) CORRECT! e) INCORRECT: <span style="font-family: arial,helvetica,sans-serif;">The loop starts at first+1 ... This loop is not running backwards.</span> <pre lang="text/x-java"><span style="font-family: arial,helvetica,sans-serif;">bestSoFar = i; ... bestsoFar is being set to the position, not the value. </span></pre>
canterbury_cs
canterbury_cs_0000210
What would the following line of code do with a form named frmMain? frmMain.Caption = txtName.Text
[ "Make the text &ldquo;txtName&rdquo; appear as the caption of the form.", "B Execute the method Caption, passing txtName as a parameter.", "Change the contents of the text box txtName to the caption of the form.", "Change the caption of the form to the contents of the text box txtName." ]
D
The code assigns the contents of the text box to the caption for the form
canterbury_cs
canterbury_cs_0000211
Which one of the following methods should be made <code>static</code>? <pre lang="text/x-java">public class Clazz { private final int num = 10; double a() { System.out.println(num); return c(); } void b() { System.out.println(this); } double c() { double r = Math.random(); System.out.println(r); return r; } void d() { a(); a(); } int e() { return num; } } </pre>
[ "a", "b", "c", "d" ]
C
Method c() does not depend on the invoking instance or its instance variables.
canterbury_cs
canterbury_cs_0000212
What output will the following code fragment produce? <pre lang="text/x-java">public void fn() { int grade = 91; int level = -1; if (grade &gt;= 90) if (level &lt;= -2) System.out.println("A-level"); else System.out.println("B-status"); } </pre>
[ "B-status", "no output is produced", "\"A-level\"", "\"B-status\"" ]
B
Despite the indentation, no braces appear around the body of either if statement's branch(es). As a result, the else is associated with the second (inner) if. Since the outer if statement's condition is true, but the inner if statement is false (and it has no else branch), no output is produced.
canterbury_cs
canterbury_cs_0000213
What output will the following code fragment produce? <pre lang="text/x-java">public void fn() { int grade = 81; int level = -3; if (grade &gt;= 90) if (level &lt;= -2) System.out.println("A-level"); else System.out.println("B-status"); } </pre>
[ "\"A-level\"", "no output is produced", "B-status", "\"B-status\"" ]
B
Despite the indentation, no braces appear around the body of either if statement's branch(es). As a result, the else is associated with the second (inner) if. Since the outer if statement's condition is false, no output is produced.
canterbury_cs
canterbury_cs_0000214
What output will the following code fragment produce? <pre lang="text/x-java">public void fn() { int grade = 81; int level = -1; if (grade &gt;= 90) if (level &lt;= -2) System.out.println("A-level"); else System.out.println("B-status"); } </pre>
[ "B-status", "\"B-status\"", "\"A-level\"", "no output is produced" ]
D
Despite the indentation, no braces appear around the body of either if statement's branch(es). As a result, the else is associated with the second (inner) if. Since the outer if statement's condition is false, no output is produced.
canterbury_cs
canterbury_cs_0000215
Consider the struct: <br><code>struct book{</code> <pre lang="text/x-c"> char title[50]; int isbn; }; </pre> <br>Assuming no address padding, what will <code>sizeof(struct book)</code> return?
[ "12", "54", "50", "204" ]
B
sizeof(the array) + sizeof(the int) = 50 + 4.
canterbury_cs
canterbury_cs_0000216
What will this code output on 64-bit Linux? <br><code>char vals[10];</code> <code>printf("%d\n", sizeof(vals + 0));</code>
[ "1", "4", "8", "10" ]
C
Size of the pointer.
canterbury_cs
canterbury_cs_0000217
A compiler error existed in this code. Why is that happening? public class test { &nbsp;&nbsp;&nbsp;&nbsp; int testCount; &nbsp;&nbsp;&nbsp;&nbsp; public static int getCount(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return testCount; &nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp; public test(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; testCount ++; &nbsp;&nbsp;&nbsp;&nbsp; } }
[ "testCount has not been initialized.", "testCount has never been used.", "testCount as a non-static variable cannot be referenced in a static method such as getCount() .", "testCount&rsquo;s access modifier is not public." ]
C
testCount as a non-static variable cannot be referenced in a static method such as getCount() .
canterbury_cs
canterbury_cs_0000218
Which of the following choices would best be modeled by a class, followed by an instance of that class?
[ "Country, Sweden", "Sweden, Country", "Country, ScandinavianCountry", "Sweden, Norway" ]
A
Choice B is wrong because the items listed are object, class, not class, object. Choice C is wrong, because the items listed are class,subclass, not class,object. Choices D and E are wrong because in each case, the items listed are both concrete objects.&nbsp;
canterbury_cs
canterbury_cs_0000219
The following method, called <code>maxRow()</code>, is intended to take one parameter: a <code>List</code> where the elements are <code>List</code>s of <code>Integer</code> objects. You can think of this parameter as a matrix--a list of rows, where each row is a list of "cells" (plain integers). The method sums up the integers in each row (each inner list), and returns the index (row number) of the row with the largest row sum. Choose the best choice to fill in the blank on <strong>Line 8</strong>&nbsp;so that this method works as intended: <pre lang="text/x-java">public static int maxRow(List&lt;List&lt;Integer&gt;&gt; matrix) { int maxVec = -1; // Line 1 int maxSum = Integer.MIN_VALUE; // Line 2 for (int row = 0; row &lt; __________; row++) // Line 3 { int sum = 0; // Line 4 for (int col = 0; col &lt; __________; col++) // Line 5 { sum = sum + __________; // Line 6 } if (___________) // Line 7 { maxSum = __________; // Line 8 maxVec = __________; // Line 9 } } return maxVec; // Line 10 } </pre>
[ "<code>maxSum</code>", "<code>maxVec</code>", "<code>sum</code>", "<code>row</code>" ]
C
The local variable <code>maxSum</code> is used to keep track of the maximum row sum seen so far, as the outer loop progresses across all rows in the matrix. &nbsp;The inner loop computes the sum of all cells in the current row, which is stored in the local variable <code>sum</code>. &nbsp;If the current row's sum is larger than the maximum seen so far, the variable <code>maxSum</code> should be updated to be <code>sum</code>.
canterbury_cs
canterbury_cs_0000220
Given the following Java class declaration: <pre lang="text/x-java">public class T2int { private int i; public T2int() { i = 0; } public T2int(int i) { this.i = i; } public int get() { return i; } } </pre> &nbsp; The following method, called <code>rangeSum()</code>, is intended to take three parameters: a <code>List</code> of <code>T2int</code> objects, plus the <code>low</code> and <code>high</code> end of a range within the list. The method computes the sum of the values in the <code>List</code> that are <strong>within</strong> the "range" (but <strong>not including</strong> the range end values). Choose the best choice to fill in the blank on <strong>Line 8</strong>&nbsp;so that the method will work as intended: <pre lang="text/x-java">public int rangeSum(List&lt;T2int&gt; list, int low, int high) { int num = 0; // Line 1 int sum = 0; // Line 2 for (int idx = 0; idx &lt; list.size(); idx++) // Line 3 { int ival = list.get(idx).get(); // Line 4 if (__________) // Line 5 { num++; // Line 6 sum = __________; // Line 7 } } return __________; // Line 8 } </pre>
[ "<code>sum</code>", "<code>num</code>", "<code>ival</code>", "<code>idx</code>" ]
A
The method should return the sum of all of the list elements that are within the specified range. &nbsp;From examining the body of the loop, it is clear that this value is accumulated in the local variable <code>sum</code>.
canterbury_cs
canterbury_cs_0000221
Consider the following class definition: <pre lang="text/x-java">import java.util.Scanner; // 1 public class SillyClass2 { // 2 private int num, totalRed, totalBlack; // 3 public SillyClass2 () { // 4 num = 0; // 5 totalRed = 0; // 6 totalBlack = 0; // 7 this.spinWheel(); // 8 System.out.print("Black: " + totalBlack); // 9 System.out.println(" and red: " + totalRed); // 10 } // 11 </pre> <br><code>&nbsp; &nbsp;public void spinWheel () { // 12</code> <pre lang="text/x-java"> Scanner kbd = new Scanner(System.in); // 13 System.out.println("Enter 1 or 0, -1 to quit."); // 14 num = kbd.nextInt(); // 15 while (num &gt;= 0) { // 16 if (num == 0) // 17 totalRed++; // 18 else if (num == 1) // 19 totalBlack++; // 20 else System.out.println("Try again"); // 21 System.out.println("Enter 1 or 0, -1 to quit)."); // 22 num = kbd.nextInt(); // 23 } // 24 System.out.println("Thanks for playing."); // 25 } // 26 } // 27 </pre> &nbsp; Which sequence of inputs will cause line 21 to be executed?
[ "-1", "0 &nbsp; 1 &nbsp; -1", "0 &nbsp; 1 &nbsp; 1 &nbsp; 0 &nbsp; -1", "0 &nbsp; 1 &nbsp; 2 &nbsp; 1 &nbsp; -1" ]
D
Answers A, B, C, and E are incorrect because they contain no inputs greater than 1. Answer D does contain an input greater than 1, and that input is after a 0 (which causes the loop to be entered) and before the -1 (which causes the loop to be exited).&nbsp;
canterbury_cs
canterbury_cs_0000222
Which method call is an efficient and correct way of calling methods compute_1 and compute_2 inside main method? public class test { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static void compute_1(){} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void compute_2(){} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static void main(String [] Args){} }
[ "test t = new test(); \n t.compute_1(); \n t.compute_2();", "compute_1(); \n compute_2();", "test.compute_1(); \n test.compute_2();", "test.compute_1(); \n test t = new test(); \n t.compute_2();" ]
D
test.compute_1(); test.compute_2();
canterbury_cs
canterbury_cs_0000223
What output will the following code fragment produce? <pre lang="text/x-java">public void fn() { int grade = 91; int level = -3; if (grade &gt;= 90) if (level &lt;= -2) System.out.println("A-level"); else System.out.println("B-status"); } </pre>
[ "A-level", "B-status", "\"A-level\"", "\"B-status\"" ]
A
Despite the indentation, no braces appear around the body of either if statement's branch(es). &nbsp;As a result, the else is associated with the second (inner) if. &nbsp;Still, both if statement conditions are true, so the output is <code>A-level</code>.
canterbury_cs
canterbury_cs_0000224
Suppose you&rsquo;re on a project that is writing a large program. One of the programmers is implementing a <code>Clock</code> class; the other programmers are writing classes that will use the <code>Clock</code> class. Which of the following aspects of the <code>public</code> methods of the <code>Clock</code> class do <strong>not</strong> need to be known by both the author of the <code>Clock</code> class and the programmers who are using the <code>Clock</code> class?
[ "The methods&rsquo; names", "The methods&rsquo; return types", "The methods' parameter types", "The method bodies" ]
D
This question addresses the principle of encapsulation. All these items must be known to the author of the <code>Clock</code> class. In order to use the class, the other programmers must know what the methods do, and must know their names, parameter types, and return values. The method bodies, on the other hand, are hidden from the users.
canterbury_cs
canterbury_cs_0000225
Consider these two Java methods: <pre lang="text/x-java">public void fuzzy(int x) { x = 73; System.out.print(x + " "); } public void bunny() { int x = 29; fuzzy(x); System.out.println(x); } </pre> &nbsp; What is printed when <code>bunny()</code> is called?
[ "29 29", "73 73", "29 73", "73 29" ]
D
Remember that in Java, parameters are passed by value. &nbsp;Although a method may assign to a parameter, this change only affects the method's local copy of the parameter and is not visible outside the method (i.e., does not affect the actual value passed in by the caller).
canterbury_cs
canterbury_cs_0000226
Given the following Java class declaration: <pre lang="text/x-java">public class T2int { private int i; public T2int() { i = 0; } public T2int(int i) { this.i = i; } public int get() { return i; } } </pre> &nbsp; The following method, called <code>rangeSum()</code>, is intended to take three parameters: a <code>List</code> of <code>T2int</code> objects, plus the <code>low</code> and <code>high</code> end of a range within the list. The method computes the sum of the values in the <code>List</code> that are <strong>within</strong> the "range" (but <strong>not including</strong> the range end values). Choose the best choice to fill in the blank on <strong>Line 5</strong> so that the method will work as intended: <pre lang="text/x-java">public int rangeSum(List&lt;T2int&gt; list, int low, int high) { int num = 0; // Line 1 int sum = 0; // Line 2 for (int idx = 0; idx &lt; list.size(); idx++) // Line 3 { int ival = list.get(idx).get(); // Line 4 if (__________) // Line 5 { num++; // Line 6 sum = __________; // Line 7 } } return __________; // Line 8 } </pre>
[ "<code>(idx &gt; low) &amp;&amp; (idx &lt; high)</code>", "<code>(ival &gt; low) &amp;&amp; (ival &lt; high)</code>", "<code>(list.get(idx) &gt; low) &amp;&amp; (list.get(idx) &lt; high)</code>", "<code>(idx &gt;= low) &amp;&amp; (idx &lt;= high)</code>" ]
B
The local variable <code>ival</code> contains the integer value stored in the current position in the list. &nbsp;To check this value to ensure it is within the desired range, use the condition <code>(ival &gt; low) &amp;&amp; (ival &lt; high)</code>.
canterbury_cs
canterbury_cs_0000227
What terminates a failed linear probe in a non-full hashtable?
[ "The end of the array", "A deleted node", "A null entry", "A node with a non-matching key" ]
C
A null entry marks the end of the probing sequence. Seeing the end of the array isn't correct, since we need to examine all elements, including those that appear before our original hash index. A node with a non-matching key is what started our probe in the first place. Revisiting the original hash index would mean we looked at every entry, but we could have stopped earlier at the null entry. The purpose of leaving a deleted node in the table is so that probing may proceed past it.
canterbury_cs
canterbury_cs_0000228
If a hashtable's array is resized to reduce collisions, what must be done to the elements that have already been inserted?
[ "All items must be copied over to the same indices in the new array.", "Nodes must be rehashed and reinserted in the new array.", "The existing items can be placed anywhere in the new array.", "The hashCode method must be updated." ]
B
Since calculating a node's position in the hashtable is a function of the node's key's hashcode and the array size, all items must be reinserted.
canterbury_cs
canterbury_cs_0000229
The following method, called <code>maxRow()</code>, is intended to take one parameter: a <code>List</code> where the elements are <code>List</code>s of <code>Integer</code> objects. You can think of this parameter as a matrix--a list of rows, where each row is a list of "cells" (plain integers). The method sums up the integers in each row (each inner list), and returns the index (row number) of the row with the largest row sum. Choose the best choice to fill in the blank on <strong>Line 7</strong>&nbsp;so that this method works as intended: <pre lang="text/x-java">public static int maxRow(List&lt;List&lt;Integer&gt;&gt; matrix) { int maxVec = -1; // Line 1 int maxSum = Integer.MIN_VALUE; // Line 2 for (int row = 0; row &lt; __________; row++) // Line 3 { int sum = 0; // Line 4 for (int col = 0; col &lt; __________; col++) // Line 5 { sum = sum + __________; // Line 6 } if (___________) // Line 7 { maxSum = __________; // Line 8 maxVec = __________; // Line 9 } } return maxVec; // Line 10 } </pre>
[ "<code>sum &gt; matrix.size()</code>", "<code>sum &lt; maxSum</code>", "<code>sum &lt;= maxSum</code>", "<code>sum &gt; maxSum</code>" ]
D
The local variable <code>sum</code> represents the sum of all cell values in the current row, which is computed by the inner loop in the code. &nbsp;The if test on Line 6 checks whether to update the local variables <code>maxSum</code> and <code>maxVec</code>, which represent information about the largest row sum found so far. &nbsp;This should happen when <code>sum &gt; maxSum</code>.
canterbury_cs
canterbury_cs_0000230
Which sentence is NOT correct?
[ "In a class, you can have a method with the same name as the constructor.", "In a class, you can have two methods with the same name and return type, but different number and type of input arguments.", "In a class, you can have two methods with the same number and type of input arguments and different return type.", "In a class you can have two constructors with the same name." ]
C
In a class, you can have two methods with the same number and type of input arguments and different return type.
canterbury_cs
canterbury_cs_0000231
You see the expression <code>n = -15</code> in some code that successfully compiles. What type can <code>n</code> not be?
[ "int", "float", "char", "short" ]
C
Chars can only hold integers in [0, 65535]. Assigning an int variable to a char requires an explicit cast. Assigning an int literal in this interval does not require a cast. Assign an int literal outside of this interval is compile error.
canterbury_cs
canterbury_cs_0000232
After the assignment <code>signal = &rsquo;abracadabra&rsquo;</code>, what is returned by <code>signal[len(signal)]</code>?
[ "'a'", "'abracadabra'", "11", "an error" ]
D
This is the classic way to go one character over the edge of a String.
canterbury_cs
canterbury_cs_0000233
Which of the following sorting algorithms has a best-case time performance that is the same as its and worst-case time performance (in big O notation)?
[ "Insertion sort", "Selection sort", "Bubble sort", "None of the above" ]
B
Selection sort has both O(n^2) worst and best case.
canterbury_cs
canterbury_cs_0000234
What advantage does using dummy nodes in a linked list implementation offer?
[ "Reduced storage needs.", "Simplified insertion.", "Easier detection of the list's head and tail.", "Simplified iteration." ]
B
Dummy nodes consume a little more space, and they don't simplify iteration or bounds detection any. They do reduce the special casing that would otherwise need to be done when updating forward and backward links on an insertion.
canterbury_cs
canterbury_cs_0000235
How many asterisks will be printed as a result of executing this code? int counter = 0, N = 10; while (counter++ &lt; N){ &nbsp;&nbsp;&nbsp; if (counter%2 == 0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; continue; &nbsp;&nbsp;&nbsp; System.out.print("*"); }
[ "none, infinite loop.", "10", "5", "1" ]
C
5
canterbury_cs
canterbury_cs_0000236
Given the following Java class declaration: <pre lang="text/x-java">public class T2int { private int i; public T2int() { i = 0; } public T2int(int i) { this.i = i; } public int get() { return i; } } </pre> &nbsp; The following method, called <code>rangeSum()</code>, is intended to take three parameters: a <code>List</code> of <code>T2int</code> objects, plus the <code>low</code> and <code>high</code> end of a range within the list. The method computes the sum of the values in the <code>List</code> that are <strong>within</strong> the "range" (but <strong>not including</strong> the range end values). Choose the best choice to fill in the blank on <strong>Line 7</strong> so that the method will work as intended: <pre lang="text/x-java">public int rangeSum(List&lt;T2int&gt; list, int low, int high) { int num = 0; // Line 1 int sum = 0; // Line 2 for (int idx = 0; idx &lt; list.size(); idx++) // Line 3 { int ival = list.get(idx).get(); // Line 4 if (__________) // Line 5 { num++; // Line 6 sum = __________; // Line 7 } } return __________; // Line 8 } </pre>
[ "<code>sum + ival</code>", "<code>sum + num</code>", "<code>sum + idx</code>", "<code>sum + list.size()</code>" ]
A
Since the method computes the sum of all values found, and the local variable <code>sum</code> is used to accumulate this total, sum should be updated to <code>sum + ival</code>.
canterbury_cs
canterbury_cs_0000237
Consider the following Python code: <pre lang="text/x-python">number = int(input("Enter a positive number: ")) while number &gt; 1: if (number % 2 == 1): number = number * 3 + 1 else: number = number/2 print number if number == 1: break else: print "The end" </pre> &nbsp; Given the input &rsquo;8&rsquo;, what output is produced by the program?
[ "an error", "'The end'", "4 \n 2 \n 1", "4 \n 2 \n 1 \n The end" ]
C
Basically just trace the code.
canterbury_cs
canterbury_cs_0000238
Chris implements a standard sorting algorithm that sorts the numbers 64, 25, 12, 22, 11 like so: 64 25 12 22 11 11 25 12 22 64 11 12 25 22 64 11 12 22 25 64 11 12 22 25 64 <br>Which sorting algorithm is this?
[ "Inserton sort", "Selection sort", "Bubble sort", "Merge sort" ]
B
In the first line, we see the min is pulled from the end of the array so we know it's selection sort.
canterbury_cs
canterbury_cs_0000239
If the following hierarchy of exception is defined by a user, which option is the correct order of catching these exceptions? class firstLevelException extends Exception{} class secondLevelException_1 extends firstLevelException{} class secondLevelException_2 extends firstLevelException{} class thirdLevelException extends secondLevelException_1{}
[ "A. \n try{ \n &nbsp;&nbsp;&nbsp;&nbsp; //code was removed \n } \n catch (firstLevelException e){ \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); \n } \n catch (secondLevelException_1 e){ \n &nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); \n } \n catch (secondLevelException_2 e){ \n &nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); \n } \n catch (thirdLevelException e){ \n &nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); \n }", "try{ \n &nbsp;&nbsp;&nbsp;&nbsp; //code was removed \n } \n catch (thirdLevelException e){ \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); \n } \n catch (secondLevelException_1 e){ \n &nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); \n } \n catch (secondLevelException_2 e){ \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); \n } \n catch (firstLevelException e){ \n &nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); \n }", "try{ \n &nbsp;&nbsp;&nbsp;&nbsp; //code was removed \n } \n catch (firstLevelException e){ \n &nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); \n } \n catch (secondLevelException_2 e){ \n &nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); \n } \n catch (secondLevelException_1 e){ \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); \n } \n catch (thirdLevelException e){ \n &nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); \n }", "try{ \n &nbsp;&nbsp;&nbsp;&nbsp; //code was removed \n } \n catch (thirdLevelException e){ \n &nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); \n } \n catch (firstLevelException e){ \n &nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); \n } \n catch (secondLevelException_2 e){ \n &nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); \n } \n catch (secondLevelException_1 e){ \n &nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); \n }" ]
B
try{ &nbsp;&nbsp;&nbsp;&nbsp; //code was removed } catch (firstLevelException e){ &nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); } catch (secondLevelException_2 e){ &nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); } catch (secondLevelException_1 e){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); } catch (thirdLevelException e){ &nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); }
canterbury_cs
canterbury_cs_0000240
You see the expression&nbsp;<code>n = 47<span style="font-family: arial, helvetica, sans-serif;"> in some code that successfully compiles. What type can&nbsp;<code>n<span style="font-family: arial, helvetica, sans-serif;"> not be</span></code></span></code>?<code><br></code>
[ "double", "byte", "float", "String" ]
D
Ints cannot be stored in Strings directly.
canterbury_cs
canterbury_cs_0000241
Which sentence is NOT correct?
[ "If you define a variable as a final, you will never be able to change its.", "If you define a method as a final, you will never be able to override it.", "If you define a class as a final, you will never be able to extend it.", "If you define a class as a final, you will have to mark all its method as a final too." ]
D
If you define a class as a final, you will never be able to extend it.
canterbury_cs
canterbury_cs_0000242
What would be the output? public class test { &nbsp;&nbsp;&nbsp; static int testCount = 0; &nbsp;&nbsp;&nbsp; public test(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; testCount ++; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; public static void main(String [] Args){ &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; test t1 = new test(); &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; System.out.println(t1.testCount); &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; test t2 = new test(); &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; System.out.println(t1.testCount + " "+ t2.testCount); &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; test t3 = new test(); &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; System.out.println(t1.testCount+ " "+ t2.testCount+ " "+ t3.testCount); &nbsp;&nbsp; } }
[ "0 \n 0 0 \n 0 0 0", "1 \n 1 1 \n 1 1 1", "1 \n 2 2 \n 3 3 3", "1 \n 2 3 \n 4 5 6" ]
C
1 2 2 3 3 3
canterbury_cs
canterbury_cs_0000243
Consider the code below; what value will i have at the end? <br><code>int *p, i;</code> <pre lang="text/x-c"> i = 2; p = &amp;i; *p = 5; i++; </pre>
[ "2", "5", "6", "3" ]
C
p updates i
canterbury_cs
canterbury_cs_0000244
Lennox has a method:<br><code>void dele_root(struct node **root)</code> <pre lang="text/x-c">{ free(*root); *root = NULL; } </pre> <br>What is most likely to happen?
[ "Deletes a copy of root (will not have an effect outside the function)", "Segfault", "Deletes the argument given to the function (will have an effect outside the function)", "A lot of crazy stuff will be printed" ]
C
This will free what root is pointing to; it is a pesumably valid memory location and hence won't segfault or core dump.
canterbury_cs
canterbury_cs_0000245
Consider the code below; what value will i have at the end of the code? <br><code>int *p, i;</code> <pre lang="text/x-c"> i = 3; p = &amp;i; (*p)++; i++; </pre>
[ "3", "4", "5", "6" ]
C
i is incremented twice, once through p
canterbury_cs
canterbury_cs_0000246
A compiler error existed in this code. Why is that happening? public class test { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static int testCount; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int getCount(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return testCount; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public test(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; testCount ++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static void main(String [] Args){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print(getCount()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } }
[ "testCount has not been initialized", "getCount() cannot be called inside main method.", "testCount as a static variable cannot be referenced in a non-static method such as getCount() or a constructor such as test().", "testCount&rsquo;s access modifier is not public." ]
B
testCount as a static variable cannot be referenced in a non-static method such as getCount() or a constructor such as test().
canterbury_cs
canterbury_cs_0000247
The following method, called <code>maxRow()</code>, is intended to take one parameter: a <code>List</code> where the elements are <code>List</code>s of <code>Integer</code> objects. You can think of this parameter as a matrix--a list of rows, where each row is a list of "cells" (plain integers). The method sums up the integers in each row (each inner list), and returns the index (row number) of the row with the largest row sum. Choose the best choice to fill in the blank on <strong>Line 9</strong>&nbsp;so that this method works as intended: <pre lang="text/x-java">public static int maxRow(List&lt;List&lt;Integer&gt;&gt; matrix) { int maxVec = -1; // Line 1 int maxSum = Integer.MIN_VALUE; // Line 2 for (int row = 0; row &lt; __________; row++) // Line 3 { int sum = 0; // Line 4 for (int col = 0; col &lt; __________; col++) // Line 5 { sum = sum + __________; // Line 6 } if (___________) // Line 7 { maxSum = __________; // Line 8 maxVec = __________; // Line 9 } } return maxVec; // Line 10 } </pre>
[ "<code>maxSum</code>", "<code>row</code>", "<code>sum</code>", "<code>col</code>" ]
B
The local variable <code>maxVec</code> is used to keep track of the row number of the maximum row sum seen so far, as the outer loop progresses across all rows in the matrix. &nbsp;The inner loop computes the sum of all cells in the current row, which is stored in the local variable <code>sum</code>. &nbsp;If the current row's sum is larger than the maximum seen so far, the variable <code>maxVec</code> should be updated to be <code>row</code>.
canterbury_cs
canterbury_cs_0000248
Consider the following classes: <pre lang="text/x-java">public class A { private int myNum; public A(int x) { myNum = x; } public int getNumber() { return myNum; } public String getLetters() { return "A"; } public String getMessage() { return getLetters() + "-" + getNumber(); } } public class AB extends A { public AB(int x) { super(x + 1); } public int getNumber() { return super.getNumber() + 1; } public String getLetters() { return "AB"; } } </pre> &nbsp; What is the output of the following code segment? <br><code>A test = new AB(0);</code> <code>System.out.print(test.getMessage());</code>
[ "AB-0", "AB-2", "AB-1", "A-2" ]
B
The object created is an instance of class <code>AB</code>, despite the static type of the variable <code>test</code>. Because of the constructor defined in <code>AB</code>, the object's <code>myNum</code> field will be initialized with the value 1. &nbsp;Because of polymorphism, when <code>getMessage()</code> calls <code>getLetters()</code>, the definition of the method in <code>AB</code> will be used, returning "AB". &nbsp;Similarly, when <code>getMessage()</code> calls <code>getNumber()</code>, the definition of the method in <code>AB</code> will be used, returning 2.
canterbury_cs
canterbury_cs_0000249
When deleting a node with both left and right children from a binary search tree, it may be replaced by which of the following?
[ "its left child", "its right child", "its preorder successor", "its inorder successor" ]
D
A common choice for the replacement node is deleted node's right child's leftmost descendent. This descendent is the deleted node's inorder successor.
canterbury_cs
canterbury_cs_0000250
Suppose you are writing a program for a robot that will go around a building and clean the floor. Your program will contain, among other things, a Robot class and a Building class (with information about the layout of the building). The Building class is also used in a different program for scheduling maintenance of various parts of the building.&nbsp; The relationship between your Robot class and your Building class is best modeled as:
[ "a class-subclass relationship", "a subclass-class relationship", "a peer relationship", "a whole-component relationship" ]
C
A and B are wrong, because a Building object doesn't share all the properties and behaviors of a Robot object (or vice versa). D is wrong because the Building object is not part of the Robot object (unlike the Robot's wheels, for example). The two classes are sometimes part of different programs and sometimes work together -- a peer relationship.&nbsp;
canterbury_cs
canterbury_cs_0000251
What is the worst-case time performance of heapsort?
[ "O(nlgn)", "O(n)", "O(lgn)", "O(n2)" ]
A
O(n2)
canterbury_cs
canterbury_cs_0000252
What is output from the following section of code? int i = 4; int j = i - 1; printf("%d bows = %d wows", i, j+1);
[ "3 bows = 4 wows", "4 bows = 4 wows", "3 bows = 5 wows", "&nbsp;4 bows = 5 wows" ]
B
Through the printf function the decimal integer values defined by the %d field specifiers are replaced by the values for i and j resulting from the expression. The value of variable j is both decremented and incremented to remain equivalent to that of variable i when printed.
canterbury_cs
canterbury_cs_0000253
Read the following method skeleton and choose the best expression to fill in the blank on <strong>line 4</strong>&nbsp;so that the method will behave correctly: <pre lang="text/x-java">/** * Takes a string reference and counts the number of times * the character 'A' or 'a' appears in the string object. * @param aString String reference to object containing chars. * @precondition aString is not null (you may assume this is true). * @return The number of times 'A' or 'a' appears in the string. */ public static int countAs(String aString) // line 1 { int counter = __________; // line 2 int totalA = 0; // line 3 while (counter &lt; __________) // line 4 { if ( __________.equals("A") ) // line 5 { totalA = totalA + __________; // line 6 } counter++; // line 7 } return __________; // line 8 } </pre>
[ "<code>aString.size()</code>", "<code>aString.size() - 1</code>", "<code>aString.length</code>", "<code>aString.length()</code>" ]
D
The variable <code>counter</code> is being used as an index into the string that is being examined, and from line 7 it is clear that this index is increasing on each loop iteration, moving from left to right. &nbsp;Because the loop test uses the <code>&lt;</code> operator, the correct upper limit is <code>aString.length()</code>. &nbsp;Remember that strings provide a <code>length()</code> method for obtaining their length (<code>size()</code> is used for containers like lists and maps, and <code>length</code>&nbsp;written as a field reference instead of a method call is used for arrays).
canterbury_cs
canterbury_cs_0000254
Consider the code<br>int i, *q, *p;<br>p = &amp;i;<br>q = p;<br>*p = 5; <br>Which of the following will print out &ldquo;The value is 5.&rdquo;?
[ "printf(\"The value is %d\", &amp;i);", "printf(\"The value is %d\", p);", "printf(\"The value is %d\", *q);", "printf(\"The value is %d\", *i);" ]
C
p and q are currently "sharing" --- they both point to the same variable (i).
canterbury_cs
canterbury_cs_0000255
What order must elements 1, 2, 3, 4, 5, 6, and 7 be inserted in a binary search tree such that the tree is perfectly balanced afterward?
[ "4 1 2 3 5 6 7", "4 2 6 1 3 5 7", "2 1 3 4 6 5 7", "1 2 3 4 5 6 7" ]
B
The middle element 4 will need to be the root, so that must be inserted first. The middle elements of the remaining halves must be 4's children, so 2 and 6 must be inserted next. (Their relative order does not matter.) The last four elements can be inserted in any order.
canterbury_cs
canterbury_cs_0000256
Which algorithm does the following code implement? def mystery(target, listOfValues):<br>&nbsp; &nbsp; &nbsp;beginning = 0<br>&nbsp; &nbsp; &nbsp;end = len(listOfValues)<br>&nbsp; &nbsp; &nbsp;found = False<br>&nbsp; &nbsp; &nbsp;while (! found and (beginning &lt;= end)):<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; middle = (beginning + end) / 2<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (listOfValues[middle] == target):<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;found = True<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print "Found it at location: ", middle<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (target &lt; listOfValues[middle]):<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end = middle -1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; beginning = middle + 1 &nbsp; &nbsp; &nbsp;if (! found):<br>&nbsp; &nbsp; &nbsp;print "Item not found"
[ "Short Sequential Search", "Sequential Search", "Binary Search", "Bubble Search" ]
C
This a classic implementation of binary search.
canterbury_cs
canterbury_cs_0000257
What abstract data type is best suited to help us implement a breadth-first search?
[ "priority queue", "queue", "stack", "hashtable" ]
B
In a breadth-first search, we visit the starting element, its neighbors, its neighbors' neighbors, and so on. We want to make sure we visit the neighbors of the elements we saw earliest before we visit the neighbors of elements we saw later. This suggests a first-in, first out approach.
canterbury_cs
canterbury_cs_0000258
This sorting algorithm repeatedly examines neighboring elements, swapping those pairs that are out of order.
[ "selection sort", "insertion sort", "bubble sort", "quick sort" ]
C
Bubble sort operates by reordering neighbors.
canterbury_cs
canterbury_cs_0000259
The enqueue operation:
[ "adds a new item at the front of the queue", "returns without removing the item at the front of&nbsp;the queue", "removes and returns the item at the front of the queue", "adds a new item at the end of the queue" ]
D
enqueue is the name for the operation that adds an element to a queue. Specifically, it adds each item to the end of the queue (just like standing in line).&nbsp;
canterbury_cs
canterbury_cs_0000260
If Matthew is entering the values 1, 2, 3, 4, 5 into a complete tree, where insertion happens in level-order, in what order should he enter the values so he produces a binary search tree?
[ "1, 2, 3, 4, 5", "4, 2, 5, 1, 3", "3, 1, 5, 2, 4", "1, 3, 2, 5, 4" ]
B
Only B will produce a complete BST.
canterbury_cs
canterbury_cs_0000261
Consider the following code: <pre lang="text/x-java">if (!somethingIsTrue()) { return true; } else { return false; } </pre> &nbsp; <span style="font-family: arial, helvetica, sans-serif;">Which replacement for this code will produce the same result?</span>
[ "<code>return true;</code>", "<code>return false;</code>", "<code>return somethingIsTrue();</code>", "<code>return !somethingIsTrue();</code>" ]
D
The method <code>somethingIsTrue()</code> must return a boolean value, because of the way it is used in the if statement's condition. &nbsp;If it returns the value <code>false</code>, the if statement will cause the value <code>true</code> to be returned; otherwise, the if statement will cause the value <code>false</code> to be returned. &nbsp;Therefore, the entire if statement is equivalent to <code>return !somethingIsTrue();</code>.
canterbury_cs
canterbury_cs_0000262
You are storing a complete binary tree in an array, with the root at index 0. At what index is node i's right child?
[ "2i", "2i + 1", "i + i + 2", "i / 2 + 1" ]
C
(i - 1) / 2
canterbury_cs
canterbury_cs_0000263
You've got a <code>byte</code> variable holding the value 127. You add 1 with <code>byte += 1</code>. What happens?
[ "It becomes a short with value 128.", "An OverflowException is raised.", "It remains a byte and stays at the value 127.", "It remains a byte and takes on the value -128." ]
D
When you exceed the maximum value of an integral type, you wrap around to other extreme. Bytes range from -128 to 127.
canterbury_cs
canterbury_cs_0000264
What is the size of the largest min-heap which is also a BST?
[ "2", "1", "4", "3" ]
B
You cannot add a left child to the root because it would break the BST/min-heap property.
canterbury_cs
canterbury_cs_0000265
Which of the following is true about both interfaces and abstract classes?
[ "None of the above", "They can have both abstract and non-abstract methods", "They can be used to model shared capabilities of unrelated classes", "All of their methods are abstract" ]
A
Abstract methods have constructors, interfaces do not An anstract class can include both abstract and non-abstract methods, while all methods in interfaces are abstract An abstract class can model shared capabilities of classes, but only those who share the abstract class as an ancestor (so related), whereas there is no such restriction on interfaces.
canterbury_cs
canterbury_cs_0000266
Jacob has written some code using our standard linked list node definition (it has an <code>int val</code> and a <code>struct node *next</code>, in that order ): <br><code>struct node *my_node = malloc(sizeof(struct node));</code> <pre lang="text/x-c">my_node-&gt;val = 4; my_node-&gt;next = NULL; struct node **ptr_node = &amp;my_node; *(*ptr_node)++; </pre> <br>If the value of <code>my_node</code> is initially 0x50085008, what will the value of <code>my_node</code> most likely be after this code?
[ "0x50085008", "0x50085010", "0x5008500C", "0x50085018" ]
D
You would it expect it to be 0x50085014 (0x50085008 + size of int + size of pointer), but C actually does address padding, pushing it up to&nbsp;0x50085018, which is wha you would see if you run the code.
canterbury_cs
canterbury_cs_0000267
A coworker suggests that when you delete a node from a hashtable that you just set it to null. What is your response?
[ "Yes, that way the garbage collector can reclaim the memory.", "Yes, that will speed up probing.", "No, the user may have alias references to the node.", "No, doing so may make other nodes irretrievable." ]
D
When using probing to resolve collisions, the probing algorithm walks along the probing sequence until it finds a null element. If nulls appear in the wrong places, probing may terminate earlier than it should.
canterbury_cs
canterbury_cs_0000268
Suppose you are trying to choose between an array and a singly linked list to store the data in your program. Which data structure must be traversed one element at a time to reach a particular point?
[ "an array", "a linked list", "both", "neither" ]
B
In a basic linked list, each element is connected to the one after it. To reach a given element, you must start with the first node, if it's not the one you want, follow the link to the next one, and so forth until you reach the end of the list. (These are called "singly linked lists"; it is also possible to construct a doubly linked list, where each node is connected to the previous *and* the next element.)&nbsp;
canterbury_cs
canterbury_cs_0000269
Consider the following recursive method: <pre lang="text/x-java">public int examMethod(int n) { if (n == 1) return 1; else return (n + this.examMethod(n-1)); } </pre> &nbsp; What value is returned by the call <code>examMethod(16)</code>?
[ "1", "16", "136", "None of the above." ]
C
This method returns the sum of the numbers from 1 to the parameter n (as long as n is greater than or equal to 1).&nbsp;
canterbury_cs
canterbury_cs_0000270
Suppose you place m items in a hash table with an&nbsp;array size of s. What is the correct formula for the load&nbsp;factor?
[ "m − s", "m/s", "s/m", "s − m" ]
B
The load factor is the number of elements in the array, divided by the size of the array. It gives you an idea of how full the hashtable is.&nbsp;
canterbury_cs
canterbury_cs_0000271
The <strong>Fibonacci sequence</strong> is 0, 1, 1, 2, 3, 5, 8, 13 ... Any term (value) of the sequence that follows the first two terms (0 and 1) is equal to the sum of the preceding two terms. Consider the following incomplete method to compute any term of the Fibonacci sequence: <pre lang="text/x-java">public static int fibonacci(int term) { int fib1 = 0; // Line 1 int fib2 = 1; // Line 2 int fibn = 0; // Line 3 if (term == 1) // Line 4 { return fib1; // Line 5 } if (term == 2) // Line 6 { return fib2; // Line 7 } for (__________) // Line 8: loop to the nth term { fibn = __________; // Line 9: compute the next term fib1 = __________; // Line 10: reset the second preceding term fib2 = __________; // Line 11: reset the immediate preceding term } return fibn; // Line 12: return the computed term } <span style="font-family: arial, helvetica, sans-serif;">Choose the best answer to fill in the blank on line 8. </span></pre>
[ "<code>int n = 0; n &lt; term; n++</code>", "<code>int n = 1; n &lt; term; n++</code>", "<code>int n = 2; n &lt; term; n++</code>", "<code>int n = 3; n &lt; term; n++</code>" ]
B
From the question, it is clear that the terms in the sequence are numbered starting at 1. &nbsp;The two base cases cover terms 1 and 2, and the loop must then repeat (term - 2) times in total. &nbsp;This will be achieved if the initial value on the loop counter is 1.
canterbury_cs
canterbury_cs_0000272
Which of these relationships best exemplify the "IS-A" relationships in object-oriented programming (OOP)?
[ "Empire State Building IS-A Building", "Cat IS-A Mammal", "Angry Cat IS-A Cat \n (Note that \"Angry Cat\" is a specific cat that has become an online internet meme)", "All of the above" ]
B
A and C are wrong because the Empire State Building would be best be described as an instance of the category Cuilding, while Angry Cat is an instance of the category Cat. B is correct because Cat is a sub-category of Mammal, which best exemplifies the IS-A relations.&nbsp; In OOP, the IS-A relationship is used to denote relationships between classes, which are sort of like categories.
canterbury_cs
canterbury_cs_0000273
What does this print when x is assigned 1? <pre lang="text/x-java">if (x==1) { System.out.println("x is 1"); } if (x==2) { System.out.println("x is 2"); } else { System.out.println("not 1 or 2"); } </pre>
[ "x is 1", "x is 2", "x is 1 \n x is 2", "x is 1 \n not 1 or 2" ]
D
This code snipped does not have an else-if!&nbsp; It may as well be written like this: <pre lang="text/x-java">if (x==1) { System.out.println("x is 1"); } if (x==2) { System.out.println("x is 2"); } else { System.out.println("not 1 or 2"); } </pre> &nbsp; So when x==1, clearly we get "x is 1" printed, bu since 1 does not equal 2, we also get "not 1 or 2" with the else. Be aware of when you are using an if, and when are are using an else-if, because they are quite different!
canterbury_cs
canterbury_cs_0000274
In C, which of the following will return 1, if s1 = "hi" and s2 = "hi"? Circle all that apply.
[ "s1 == s2", "strcmp(s1, s2)", "strlen(s1)", "s1 == \"hi\"" ]
B
C is not Python! Only strcmp will do what Python and similar languages would do. (My CS2 students come into C having learnt Python.)
canterbury_cs
canterbury_cs_0000275
Inserting a node into a heap is
[ "O(1)", "O(log N)", "O(N)", "O(N log N)" ]
B
Inserting a node involves putting the new node into the bottom level of the heap and trickling up, possibly to the root level. Since heaps are balanced and the number of levels is log N, the performance is O(log N).
canterbury_cs
canterbury_cs_0000276
Consider the following Java method: <pre lang="text/x-java">public void printMenu(){ System.out.println("Choose one of the following options:"); System.out.println("1) Display account balance"); System.out.println("2) Make deposit"); System.out.println("3) Make withdrawal"); System.out.println("4) Quit"); } </pre> &nbsp; <span style="font-family: arial, helvetica, sans-serif;">Select the best reason for making this a separate method with a name, instead of including the code in some other method:</span>
[ "Otherwise, you would have to write the same code more than once.&nbsp;", "By breaking your program up into logical parts, you make it more readable.&nbsp;", "By giving this block of code a name, you make your program more readable.&nbsp;", "It's necessary to keep the calling method under 20 lines long.&nbsp;" ]
A
There are multiple correct answers to this question. A, B, and C are all reasonable answers. D is less good, because while it's a good idea to keep your methods relatively short, it won't always make sense to stick to an arbitrary limit like 20 lines.&nbsp;
canterbury_cs
canterbury_cs_0000277
A given O(n<sup>2</sup>) algorithm takes five seconds to execute on a data set size of 100. &nbsp;Using the same computer and the same algorithm, how many seconds should this algorithm run for when executed on a data set of size 500?
[ "25 seconds", "100 seconds", "42 seconds", "125 seconds" ]
D
Quadratic algorithms represent an n<sup>2</sup> increase in run time. &nbsp;Hence if the data set size increases by a factor of &nbsp;five, for a quadratic algorithm, the increase in run time becomes a factor of 25. &nbsp;Hence 25 times 5 (base run time) is 125.
canterbury_cs
canterbury_cs_0000278
If the StackADT operation <code>push</code> is implemented using a linked list, the big-Oh time complexity of the method is:
[ "O(1)", "O(log<sub>2</sub>n)", "O(n)", "O(n<sup>2</sup>)" ]
A
The push method adds a new element to a stack. Regardless of the size of the list, the operations needed to add a new element include creating a node to store it in and setting the values of a few pointers, all of which can be done in constant time.&nbsp;
canterbury_cs
canterbury_cs_0000279
Which line of the following code has a compilation error? import java.util.*; public class bicycles { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static void main(String[] Main) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Vector&lt;bike&gt; q = new Vector(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bike b = new bike(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; q.add(b); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } } class bike{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private int bikePrice; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private bike(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bikePrice = 0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private bike(int p){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bikePrice = p; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } }
[ "public static void main(String[] Main)", "Vector&lt;bike&gt; q = new Vector();", "bike b = new bike();", "private int bikePrice;" ]
C
bike b = new bike();
canterbury_cs
canterbury_cs_0000280
After the assignments <code>a = True</code> and <code>b = True</code>, what is returned by <code>(not a or b) and (a or not b)</code>?
[ "True", "False", "1", "0" ]
A
the not operator has a higher precedence than or, so this expression should be read: ((not a) or b) and (a or (not b)) Which is True when a=True and b=False
canterbury_cs
canterbury_cs_0000281
Consider the following code: <pre lang="text/x-python">number = int(input("Enter a positive number: ")) while number &gt; 1: if (number % 2 == 1): number = number * 3 + 1 else: number = number/2 print number if number == 1: break else: print "The end" </pre> &nbsp; What output is produced when the input is '-1'?
[ "an error", "The end", "no output is produced", "-1" ]
B
if number is not greater than 1, we immediately get to "The end".&nbsp; In Python, while loops are allowed to have an else statement.
canterbury_cs
canterbury_cs_0000282
3. Consider the following class definition. <pre lang="text/x-java">import java.util.Scanner; public class SillyClass2 { private int num, totalRed, totalBlack; public SillyClass2 () { num = 0; totalRed = 0; totalBlack = 0; this.spinWheel(); System.out.print("Black: " + totalBlack); System.out.println(" and red: " + totalRed); } public void spinWheel () { Scanner kbd = new Scanner(System.in); System.out.println("Enter 1 or 0, -1 to quit."); num = kbd.nextInt(); while (num &gt;= 0) { if (num == 0) totalRed++; else if (num == 1) totalBlack++; else System.out.println("Try again"); System.out.println("Enter 1 or 0, -1 to quit)."); num = kbd.nextInt(); } System.out.println("Thanks for playing."); } } </pre> &nbsp; Which of the following sequences of inputs causes every line of code to be executed at least once?
[ "0 &nbsp; &nbsp;0 &nbsp; &nbsp;10 &nbsp; &nbsp;-1", "1 &nbsp; &nbsp;0 &nbsp; &nbsp;1 &nbsp; &nbsp;-1", "1 &nbsp; &nbsp;1 &nbsp; &nbsp;10 &nbsp; &nbsp;-1", "1 &nbsp; &nbsp;0 &nbsp; &nbsp;10 &nbsp; &nbsp;-1" ]
D
This question tests understanding of a conditional nested inside a loop. Choice A is wrong, because the initial value must be &gt;= 0 for the loop to be executed. Choice E is wrong, because the last value must be -1, or the code never exits the loop and the last line of code is not executed. Choices B and C are wrong, because inside the loop, we need one value that's 0, one value that's 1, and one value that's greater than 1, so that each branch of the conditional will be executed.&nbsp;
canterbury_cs
canterbury_cs_0000283
Suppose <code>StackADT</code> is implemented in Java using a linked list. The big-Oh time complexity of the following <code>peek</code> method is:&nbsp; <pre lang="text/x-java">public T peek() { T tmp; if (this.top == null) { tmp = null; } else { tmp = this.top.getElement(); } return tmp; } </pre>
[ "O(1)", "O(log n)", "O(n)", "O(n<sup>2</sup>)" ]
A
The method body is executed in a fixed amount of time, independent of the size of the stack.&nbsp;
canterbury_cs
canterbury_cs_0000284
You need to store a large amount of data, but you don't know the exact number of elements. The elements must&nbsp;be searched quickly by some key. You want to waste no storage space. The elements to be added are in sorted order. What is the simplest data structure that meets your needs?&nbsp;
[ "Binary search tree", "Self-balancing tree", "Linked list", "Hashtable" ]
B
Hashtables provide fast searching, but they may waste storage space. A tree makes better use of memory. Since the keys are in a sorted order, it's likely a binary tree will end up looking like a linked list instead of a well-balanced tree. With a self-balancing tree, we can make sure searching goes faster.
canterbury_cs
canterbury_cs_0000285
Which one of the following is a limitation of Java's arrays?
[ "They cannot change size.", "They can only hold reference types.", "Once an element is assigned, that element cannot be modified.", "Their length must be stored separately." ]
A
Arrays hold primitives and references, have a builtin length property, and allow modification of individual elements. They cannot be resized.
canterbury_cs
canterbury_cs_0000286
What will be printed? String name = "John"; String surname = "Smith"; name.concat(surname); System.out.print(name + " "); System.out.println(surname);
[ "John Smith", "John Smith Smith", "JohnSmith Smith", "Smith John" ]
A
JohnSmith Smith
canterbury_cs
canterbury_cs_0000287
What should be done to change the following code to a correct one? public class exam { &nbsp;&nbsp;&nbsp;&nbsp; float mark; &nbsp;&nbsp;&nbsp;&nbsp; public static void main(String[]arg){ &nbsp;&nbsp;&nbsp;&nbsp; float aCopyofMark; &nbsp;&nbsp;&nbsp;&nbsp; exam e = new exam(); &nbsp;&nbsp;&nbsp;&nbsp; System.out.println( e.mark + aCopyofMark); &nbsp;&nbsp;&nbsp;&nbsp; } }
[ "Change float mark; to static float mark;", "Delete exam e = new exam();", "Initialize aCopyofMark", "This is a correct code." ]
C
Initialize aCopyofMark
canterbury_cs
canterbury_cs_0000288
Consider the following two simple Java classes: <pre lang="text/x-java">public class Base { protected int x; } public class Derived extends Base { protected int y; } </pre> &nbsp; Which of the following is/are legal?
[ "Base b=new Base(); \n Derived d=b;", "Derived d=new Derived(); \n Base b=d;", "Base b=new Derived();", "Derived d=new Base();" ]
B
B and C are OK.&nbsp; Remember your Liskov substitution principle:&nbsp; You can swap in a derived class anywhere that you expect a base class, because a derived class has at least as much information as a base class. The reverse, however, is not true!&nbsp; A derived class may add many more instance variables that the base class knows nothing about!
canterbury_cs
canterbury_cs_0000289
This question refers to a method "swap", for which part of the code is shown below: <p class="MsoNormal"><code>public static void swap(int[] x, int i, int j) {</code> <p class="MsoNormal">&nbsp;<span style="font-family: Courier; layout-grid-mode: line;">// swaps elements "i" and "j" of array "x".</span> <p class="MsoNormal"><span style="font-family: Courier; layout-grid-mode: line;"><code>&nbsp;&nbsp;&nbsp; int temp;</code><span style="mso-tab-count: 1;">&nbsp;&nbsp;&nbsp;&nbsp; // temporary storage for swapping</span></span> &nbsp;<span style="font-family: Courier; layout-grid-mode: line;">&nbsp; </span> <p class="MsoNormal"><span style="font-family: Courier; layout-grid-mode: line;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; xxx missing code goes here xxx</span></span> <p class="MsoNormal"><code>} // method swap</code> <p class="MsoNormal">&nbsp; <p class="MsoNormal"><span style="font-family: Courier; layout-grid-mode: line;">The missing code from "swap" is:</span>
[ "<pre lang=\"text/x-java\">temp = x[i];\nx[i] = x[j];\nx[j] = temp;\n</pre>", "<pre lang=\"text/x-java\">temp = x[i];\nx[j] = x[i];\nx[j] = temp;\n</pre>", "<pre lang=\"text/x-java\">temp = x[j];\nx[j] = x[i];\nx[j] = temp;\n</pre>", "<pre lang=\"text/x-java\">temp = x[j];\nx[i] = x[j];\nx[j] = temp;\n</pre>" ]
A
Suppose&nbsp; initially x[i]=A, x[j]=B.&nbsp; After swap, we want x[i]=B, and x[j]=A, in both cases it doesn&rsquo;t matter what temp equals, so long as x[i] and x[j] values have been swapped after completion. a) temp = x[i] = A x[i] = x[j] = B x[j] = temp = A Before: x[i] = A, x[j] = B After: x[i] = B, x[j] = A Both have been swapped, .&rsquo;. CORRECT b) temp = x[i] = A x[j] = x[i] = A x[j] = temp = A Before: x[i] = A, x[j] = B After: x[i] = A, x[j] = A Only x[j] has been swapped, .&rsquo;. INCORRECT c) temp = x[j] = B x[j] = x[i] = A x[j] = temp = B Before: x[i] = A, x[j] = B After: x[i] = A, x[j] = B Neither have been swapped, .&rsquo;. INCORRECT d) temp = x[j] = B x[i] = x[j] = B x[j] = temp = B Before x[i] = A, x[j] = B After: x[i] = B, x[j] = B Onlu x[i] has been swapped, .&rsquo;. INCORRECT e) temp = x[i] = A x[j] = x[i] = A x[i] = temp = A Before: x[i] = A, x[j] = B After: x[i] = A, x[j] = A &nbsp; <p class="MsoNormal"><span>Only x[j] has been swapped, .&rsquo;. INCORRECT</span>
canterbury_cs
canterbury_cs_0000290
Class B extends class A in which a method called firstMethod existed. The signature of firstMethod is as follow. In class B we are going to override firstMethod. Which declaration of this method in class B is correct? protected void firstMethod(int firstVar)
[ "private void firstMethod(int firstVar)", "default void firstMethod(int firstVar)", "public void firstMethod(int firstVar)", "void firstMethod(int firstVar)" ]
C
public void firstMethod(int firstVar)
canterbury_cs
canterbury_cs_0000291
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 &lt; other.a) { return -1; } else if (a &gt; other.a) { return 1; } else { if (b == other.b) { return 0; } else if (b &gt; other.b) { return -1; } else { return 1; } } } } </pre> &nbsp; <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;">[5 7], [2 9], [3 2]</span> You sort them using IntPair.compareTo. What is their sorted order?
[ "[2 9], [3 2], [5 7]", "[5 7], [3 2], [2 9]", "[3 2], [5 7], [2 9]", "[2 9], [5 7], [3 2]" ]
A
compareTo orders on IntPair.a first, in ascending fashion. Since all elements have unique a values, we simple sort according to the first element.
canterbury_cs
canterbury_cs_0000292
What does the following Java code print? <pre lang="text/x-java">int inner=0; int outer=0; for (int i=0; i&lt;6; i++) { outer++; for (int j=0; j&lt;=3; j++) { inner++; } } System.out.println("outer "+outer+", inner"+inner); </pre>
[ "outer 7, inner 24", "outer 7, inner 4", "outer 6, inner 24", "outer 6, inner 18" ]
C
For nested loops, the inner loop runs to completion one time for each iteration of the outer loop.&nbsp; So if the outer loop executes 6 times, that means the inner loop, which normally executes its loop body 4 times, will actually run the loop body 24 times (4 executes times 6 outer executions).&nbsp; I probably could explain that better.
canterbury_cs
canterbury_cs_0000293
After the assignments <code>x = 27</code> and <code>y = 12</code>, what is returned by<code> x/y</code>?&nbsp; (Assume Python &lt;= 2.7, not Python3).
[ "2", "2.25", "3", "3.0" ]
A
This is an integer division question.&nbsp; Python will return 2, since 27/12 is 2, with a remainder.&nbsp; But integer division only cares about the quotient, not the remainder. Note that you may get a different answer using Python3 rather than with Python2.x
canterbury_cs
canterbury_cs_0000294
Which of the following is not an ADT?
[ "List", "Queue", "Hashtable", "Stack" ]
C
Hashtable is a data structure, not an abstract datatype. Note: this question is based on a question by Andrew Luxton-Reilly.
canterbury_cs
canterbury_cs_0000295
What kind of variable is <em>label</em>? public class Labeller extends JFrame { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Labeller () { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JLabel label = new JLabel("Field or Variable"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static void main (String[] args) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new Labeller(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } }
[ "local variable, primitive", "Instance variable, primitive", "local variable, Object Reference", "Instance variable, Object Reference" ]
C
local variable, Object Reference
canterbury_cs
canterbury_cs_0000296
A method called myMethod has been defined in a class with the following signature. Which of these four choices cannot be an overloaded alternative for myMethod? public void myMethod (int i, double d)
[ "public int myMethod (int i, double d)", "public void myMethod (double d)", "public void myMethod (String i , double d)", "public int myMethod (int i)" ]
A
public void myMethod (String i , double d)
canterbury_cs
canterbury_cs_0000297
In quicksort, what does partitioning refer to?
[ "How the list/array always has three partitions: the area before low, the area between<br>low and high, and the area after high.", "The process in which the memory allocated for the list/array that you are sorting is<br>partitioned into different parts of memory, to improve memory efficiency.", "The process in which the list/array is partitioned into two equal halves; the sort proceeds<br>by recursively partitioning until we have partitions of size 2, in which case elements are<br>merged in correct order.", "The step where all elements less than a given <em>pivot</em> are placed on the left of the pivot,<br>and all elements larger than the pivot are moved to the right." ]
D
D is how it works.
canterbury_cs
canterbury_cs_0000298
Many languages allow negative indexing for arrays: items[-1] for the last item, items[-2] for the second to last, and so on. Java doesn't support this indexing directly, but we can create a class that wraps around an array and converts a negative index into the appropriate positive one: <pre lang="text/x-java">class WrappedStringArray { private String[] items; ... public String get(int i) { if (i &gt;= 0) { return items[i]; } else { return TODO; } } } </pre> &nbsp; <span style="font-family: arial, helvetica, sans-serif;">What expression must we replace TODO with to support negative indexing?</span>
[ "(int) Math.abs(i)", "items.length + i", "(int) Math.abs(i + 1)", "items.length - 1 - i" ]
B
We want items[-1] to map to items[items.length - 1], and only the correct answer handles this case.
canterbury_cs
canterbury_cs_0000299
What happens if you forget to define the <code>equals</code> method for your key class used to insert elements into a hashtable?
[ "The elements can be inserted but remain inaccessible.", "Inserting an element will raise an exception.", "Elements can be inserted and retrieved only by using the same key instances used to insert the elements.", "All elements will map to the same location in the table." ]
C
When a key lookup is performed, we will find the hashtable location and then compare the keys using Object.equals. Object.equals returns true only when the two keys refer to the same object.