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_0000300
You've got two classes, Key and Value, both of which descend from Object. You intend to use these classes to populate a hashtable. Which class needs the hashCode method and which needs the equals method?
[ "Key.hashCode, Value.equals", "Key.hashCode, Key.equals", "Value.hashCode, Object.equals", "Key.hashCode, Object.equals" ]
B
Only the Key needs these methods. On a search, only the Key will be provided, so that alone must provide the necessary information for finding the corresponding Value. Object's implementation of the equals method is too restrictive, comparing Keys only for identity.
canterbury_cs
canterbury_cs_0000301
If you are about to evaluate the following code, which following comments would you choose? FileReader inputStream = null; try {        inputStream = new FileReader("in.txt");        int c;        while ((c = inputStream.read()) != -1)                  // ... code has been deleted }catch (IOException e){         System.out.println(e.getLocalizedMessage()); }
[ "Add <em>finally</em> to the above structure.", "Make sure you close in.txt.", "Buffering input streams is a must.", "All of the above choices must be applied." ]
B
Buffering input streams is a must.
canterbury_cs
canterbury_cs_0000302
Double hashing
[ "is less vulnerable to clustering than linear or quadratic probing", "prevents collisions by keeping two tables", "produces a second index in [0, table.length)", "avoids collisions by computing a hash code that's a <code>double</code> instead of an <code>int</code>" ]
A
With double hashing, a second hash function is used to determine the step size for the probing sequence. With linear or quadratic probing, elements that collide also tend to have the same step size, which leads to clustering. A secondary hash breaks up this uniformity.
canterbury_cs
canterbury_cs_0000303
The time complexity of selection sort is:
[ "O(1)", "O(n)", "O(n log n)", "O(n<sup>2</sup>)" ]
D
Selection sort requires n(n-1)/2 comparisons.&nbsp;
canterbury_cs
canterbury_cs_0000304
Martina has created an array, arr[50], and she has written the following line of code:<br>*(arr + 20) = 6;<br>Rewrite this line to use the [ ] notation for arrays.
[ "arr[21] = 6", "*arr[20] = 6", "arr[20] = 6", "*arr[21] = 6" ]
C
arr[20] == *(arr + 20)
canterbury_cs
canterbury_cs_0000305
Which statement or statements store 0.8 into the variable <code>d</code> of type <code>double</code>?
[ "double d=8/10;", "double d=8.0/10.0;", "double d=8/10.0;", "double d=8.0/10;" ]
B
A is wrong because 8/10 is zero.&nbsp; But the B,C,D (8.0/10.0, 8/10.0, 8.0/10) all produce 0.8 because Java will promote any ints to a double in an arithmetic expression so long as there is at least one double in the expression.
canterbury_cs
canterbury_cs_0000306
Suppose you have a Java array of ints. The worst-case time complexity of printing out the elements in the list is:
[ "O(1)", "O(log n)", "O(n)", "O(n log n)" ]
C
If we assume that printing a single value can be done in constant time for some constant k, then printing n values can be done in kn time, which is O(n).
canterbury_cs
canterbury_cs_0000307
Suppose you have a binary search tree with no left children. Duplicate keys are not allowed. Which of the following explains how this tree may have ended up this way?
[ "It was filled in ascending order.", "The root value was the maximum.", "All keys were identical.", "The tree is a preorder tree." ]
A
If the least node was inserted first, with each successive node having a greater key than its predecessor, we'd end up with all right children. Adding nodes with identical keys is prohibited by the problem statement, though it would yield a similar tree.
canterbury_cs
canterbury_cs_0000308
After the Java assignment statement &nbsp; &nbsp;<code>String word = "blank";</code> which of the following Java code fragments prints &nbsp; &nbsp;<code>blink</code> ?
[ "<pre lang=\"text/x-java\">word[2] = \"i\";\nSystem.out.println(word);\n</pre>", "<code>word.replaceAll(\"a\",\"i\");</code> <code>System.out.println(word);</code>", "<code>word = word.substring(0,1) + \"i\" + word.substring(3,5);</code> <code>System.out.println(word);</code>", "All of (a)&ndash;(c)." ]
B
(a) is wrong because the Java syntax word[n] requires the variable word to be an array. (c) is almost right; it just needs to have a 2 instead of the 1 in the first call to substring. Since (a) and (c) are wrong, (d) must be wrong. (b) is correct, so (e) must be wrong.&nbsp;
canterbury_cs
canterbury_cs_0000309
Consider the following possible Java class names: &nbsp; &nbsp;<code>General, colonel, Private, Major-General, Strategy&amp;Tactics</code> When determining which names are valid, which of the following factors is important:
[ "Class names must start with a letter or underscore, followed by zero or more letters, digits, and/or underscores.&nbsp;", "Class names are generally nouns, corresponding to a person, place, thing, or idea.&nbsp;", "Class names must not be a Java reserved word.&nbsp;", "All of the above.&nbsp;" ]
D
Choices (b) and (d) are Java syntactic rules; A and C are generally accepted conventions.
canterbury_cs
canterbury_cs_0000310
In Java, what value will the variable <code>d</code> have after this declaration: <code>double d = 18 / 4;</code>
[ "<code>4.0</code>", "<code>4.5</code>", "<code>18.4</code>", "None of these" ]
A
While the variable <code>d</code> is being declared as a <code>double</code>, the initial value provided on the righthand side is actually an <code>int</code> expression, consisting of one integer value divided by another. &nbsp;When the <code>int</code> expression is evaluated, its result is also an <code>int</code> and any fractional part is truncated. &nbsp;The value of <code>18 / 4</code> is <code>4</code> (an <code>int</code>). &nbsp;After this <code>int</code> value is produced, it is then promoted to a <code>double</code> value when initializing the variable <code>d</code>, giving <code>d</code> the initial value <code>4.0</code>.
canterbury_cs
canterbury_cs_0000311
This code fails to compile: <pre lang="text/x-java">char current = 'a'; current = current + 1; </pre> &nbsp; Why?
[ "We can't add ints and chars.", "We're trying to squeeze an int into a char.", "The character after 'a' is platform-dependent.", "We're trying to squeeze a String into a char." ]
B
The char is promoted to an int when an int is added to it. Thus, our right-hand side is an int while our left-hand side is a char. Assigning an int into a char may result in information less, which requires us to sign off on this risky operation with an explicit cast.
canterbury_cs
canterbury_cs_0000312
Which problem is not P (assuming P!=NP)?
[ "Integer knapsack", "Single-source shortest path", "Minimum spanning tree", "Sorting" ]
A
Integer knapsack is considered NP-Complete.
canterbury_cs
canterbury_cs_0000313
What will this code output on a 64-bit machine? <br><code>int vals[10];</code> <code>printf("%d\n", sizeof(vals + 0));</code>
[ "4", "8", "40", "80" ]
B
sizeof(vals + 0) will get the size of the memory address of the first element in vals; a pointer is 8 bytes on the 64-bit machine
canterbury_cs
canterbury_cs_0000314
Which algorithm is most arguably a greedy algorithm? (Circle the best answer.)
[ "merge sort", "insertion sort", "selection sort", "bubble sort" ]
C
Selection sort greedily takes the min/max of the array. This is a clicker question to discuss what greedy means; none of the algorithms are really greedy!
canterbury_cs
canterbury_cs_0000315
Anastasiya has written code that loops over an int array named <code>a</code> which is of length N. Fill in the missing parts of the for loop&rsquo;s declaration. <br><code>int sum = 0;</code> <pre lang="text/x-c">int *p; for(p = a; ; ) // complete this line { sum += *p; } </pre>
[ "p &lt; N; p++", "p; p++", "p &lt; a + N; p++", "p; p = p-&gt;next" ]
C
p will not work as the ending condition as you don't know what's at the end of the array; N is not an appropriate ending condition as it needs to be relative from where you started. D and E are wrong as those would be for linked lists.
canterbury_cs
canterbury_cs_0000316
You've got an algorithm that is O(log&nbsp;N). On the first run, you feed it a collection of size M. On the second run, you feed it a collection of size M / 2. Assuming each run has worst-case performance, how many fewer operations does the second run take?
[ "0", "1", "2", "3" ]
B
The first run takes log M operations. The second run takes log (M/2) = log M - log 2 = log M - 1 operations. The second is just one operation less than the first.
canterbury_cs
canterbury_cs_0000317
In Java, what value will the variable <code>i</code> have after this declaration: <code>int i = 2 + 8 % 4;</code>
[ "0", "2", "4", "4.0" ]
B
When evaluating the initialization expression, precedence requires the modulo operator % to be applied first. 8 % 4 produces the value 0, since 8 divided by 4 has a remainder of zero. &nbsp;When zero is then added to 2, the result is 2, which becomes the initial value of the newly declared variable.
canterbury_cs
canterbury_cs_0000318
Assume M and N are positive integers. What does the following code print? <pre lang="text/x-java">int sum=0; for (int j=0; j&lt;M; j++) { for (int i=0; i&lt;N; i++) { if (N%2==0) { sum += 1; } } } System.out.println(sum); </pre>
[ "M", "M<sup>N</sup>", "M*N", "M*N/2" ]
D
The outer loop executes M times, while the inner loop executes N/2 times (i.e. if N is 6, the loop executes 3 times because there are 3 even numbers (0,2,4) between 0 and 5).
canterbury_cs
canterbury_cs_0000319
Which definition is not right?
[ "ArrayList&lt;Character&gt; charArray = new ArrayList&lt;Character&gt;();", "ArrayList&lt;Object&gt; objectArray = new ArrayList&lt;Object&gt;();", "ArrayList&lt;Integer&gt; intArray = new ArrayList&lt;Integer&gt;(10);", "ArrayList&lt;double&gt; doubleArray = new ArrayList&lt;double&gt;(10);" ]
D
ArrayList&lt;Integer&gt; intArray = new ArrayList&lt;Integer&gt;(10);
canterbury_cs
canterbury_cs_0000320
Which of the following choices would best be modeled by a class, followed by a subclass of that class?
[ "CountryInAfrica, Botswana", "Botswana, CountryInAfrica", "CountryInAfrica, Country", "Country, CountryInAfrica" ]
D
Choices A and B are wrong, because one of the items listed is a concrete item that would be better modeled by an object. Choice E is wrong because both items listed are concrete. Choices C and D both list a class and a subclass, but only in Choice D does the class come first, followed by the subclass.&nbsp;
canterbury_cs
canterbury_cs_0000321
In Java, the actual type of a parameter or variable&rsquo;s value can be any concrete class that is
[ "a. the same as the declared type, or any subclass of the declared type (if the declared type is a class)", "b. any subclass of a class that <code>implements</code> the declared type (if the declared type is an interface)", "c. any class that <code>implements</code> the declared type (if the declared type is an interface)", "d. All of the above" ]
D
e. A and C above, but not B
canterbury_cs
canterbury_cs_0000322
Given the code: <pre lang="text/x-java">if (x &gt;= 0) System.out.println("1"); else if (x &lt; 20) System.out.println("2"); else System.out.println("3"); System.out.println("4"); </pre> &nbsp; for what integer values of <code>x</code> will <code>2</code> be among the values printed?&nbsp;
[ "x &lt; 0", "x &gt;= 0", "x &lt; 20", "All values of x" ]
A
The if-else clause will be executed only when the if-clause is false. So for all int values less than 0, 2 will be printed.
canterbury_cs
canterbury_cs_0000323
What will the display look like after these lines of code, with [ ] representing a space? float fData = 3.6239; printf("%3.1f \n", fData);
[ "3.6", "3.62", "3.624", "[ ] 3.62" ]
A
The printf function follows the pattern printf("format" [, list of_ fields]).&nbsp; Within the "format" element the field specifiers have the following format [% [-] [+] [width [.precision]] data_type], where the square brackets indicate that the element is optional. &nbsp; In this case %f represents a floating point element, and 3.1&nbsp; the required precision of 1 decimal place.
canterbury_cs
canterbury_cs_0000324
What will be output by this loop? for(n = 1, m = 5; n &lt;= 5; n++, m--) printf("%d %d\n", n, m);
[ "1 5 \n 2 4 \n 3 3 \n 4 2 \n 5 1", "1 4 \n 2 3 \n 3 2 \n 4 1 \n 5 0", "&nbsp;5 1 \n 4 2 \n 3 3 \n 2 4 \n 1 5", "1 5 \n 2 4 \n 3 3 \n 4 2" ]
A
The loop increments the value of n and decrements the value of m on each iteration printing the value of each variable, until n reaches the terminal value of 5
canterbury_cs
canterbury_cs_0000325
What would be the performance of a method called addAfter(p), in which p is a position of a node or an index of an array, if the data structure is implemented by an array or linked list structure respectively?
[ "&nbsp;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_0000326
<pre lang="text/x-java">public class RecursiveMath ... public int fib (int a) { if (a == 1) return 1; else return fib(a-1) + fib(a-2); } ... } </pre> &nbsp; Given the above definition, what is the result of executing the following? <pre lang="text/x-java">RecursiveMath bill = new RecursiveMath(); int x = bill.fib(-1); </pre>
[ "x is set to -1", "x is set to undefined", "The code does not terminate", "The code cannot be executed, because it won't compile" ]
C
The problem:&nbsp; When we invoke fib(-1),&nbsp; a gets bound to -1; since that is not equal to 1, we call fib(-2) and fib(-3), and so on.&nbsp; We keep making recursive calls, and the parameter will never be equal to 1 since we are getting further away from 1 with each call.&nbsp; So it will not terminate.
canterbury_cs
canterbury_cs_0000327
You don't know exactly how much data you need to store, but there's not much of it. You'd like to not allocate any memory that won't be used. You do not need to be able to search the collection quickly. What is the simplest data structure that best suits for your needs?
[ "Unordered array", "Ordered array", "Linked list", "Hashtable" ]
C
Since arrays must be allocated before they are used, we tend to overallocate to make sure we have sufficient capacity. This wastes space. If we're not exactly sure of how much storage we need and without a need for fast searching, a linked list is a good choice.
canterbury_cs
canterbury_cs_0000328
Which of the choices will produce the same result as the following statement? if ( mark == 'A' &amp;&amp; GPA &gt; 3.5) &nbsp;&nbsp;&nbsp; System.out.println("First Class"); else &nbsp;&nbsp;&nbsp; System.out.println("Not First Class");
[ "if ( mark != 'A' &amp;&amp; GPA &lt;= 3.5) \n &nbsp;&nbsp;&nbsp; System.out.println(\"First Class\"); \n else \n &nbsp;&nbsp;&nbsp; System.out.println(\"Not First Class\");", "if ( mark != 'A' || GPA &lt;= 3.5) \n &nbsp;&nbsp;&nbsp; System.out.println(\"First Class\"); \n else \n &nbsp;&nbsp;&nbsp; System.out.println(\"Not First Class\");", "if ( mark == 'A' || GPA &gt; 3.5) \n &nbsp;&nbsp;&nbsp; System.out.println(\"First Class\"); \n else \n &nbsp;&nbsp;&nbsp; System.out.println(\"Not First Class\");", "if ( mark != 'A' || GPA &lt;= 3.5) \n &nbsp;&nbsp;&nbsp; System.out.println(\"Not First Class\"); \n else \n &nbsp;&nbsp;&nbsp; System.out.println(\"First Class\");" ]
D
if ( mark == 'A' || GPA &gt; 3.5) &nbsp;&nbsp;&nbsp; System.out.println("First Class"); else &nbsp;&nbsp;&nbsp; System.out.println("Not First Class");
canterbury_cs
canterbury_cs_0000329
Which one of the following assignments will be resulted in 1.0?
[ "double x = 6.0/4.0;", "double y = (int)6.0/4.0;", "double z = (int)(6.0/4.0);", "double t = 6.0/(int)4.0;" ]
C
double z = (int)(6.0/4.0);
canterbury_cs
canterbury_cs_0000330
If class test is going to listen to an event, which statement, if added to the following code, cannot be a solution for event handling? public class test implements ActionListener{ &nbsp;&nbsp;&nbsp;&nbsp; JButton okButton; &nbsp;&nbsp;&nbsp;&nbsp; public static void main(String [] args){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SimpleGui_V2 firstGui = new SimpleGui_V2(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; firstGui.draw(); &nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp; public void draw(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JFrame rootFrame = new JFrame(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; okButton = new JButton("OK"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // missing code &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rootFrame.add(okButton); &nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp; public void actionPerformed (ActionEvent event){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (okButton.getText().compareToIgnoreCase("ok")== 0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; okButton.setText("clicked"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; okButton.setText("OK"); &nbsp;&nbsp;&nbsp;&nbsp; } }
[ "okButton.addActionListener(new test());", "okButton.addActionListener(this);", "okButton.addActionListener(new ActionListener());", "okButton.addActionListener(new ActionListener(){ \n &nbsp;&nbsp;&nbsp; public void actionPerformed (ActionEvent event){ \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (okButton.getText().compareToIgnoreCase(\"ok\")== 0) \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; okButton.setText(\"clicked\"); \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; okButton.setText(\"OK\"); \n }});" ]
C
okButton.addActionListener(new ActionListener());
canterbury_cs
canterbury_cs_0000331
A compiler error existed in this code. Why is that happening? public class test { &nbsp;&nbsp;&nbsp; final int testCount1; &nbsp;&nbsp;&nbsp; final static int testCount2; &nbsp;&nbsp;&nbsp; static int testCount3; &nbsp;&nbsp;&nbsp; int testCount4; }
[ "testCount1 and testCount2 have not been initialized.", "testCount2 and testCount3 have not been initialized.", "testCount3 and testCount4 have not been initialized.", "None of the instance variables have been initialized." ]
A
testCount3 and testCount4 have not been initialized.
canterbury_cs
canterbury_cs_0000332
A compiler error existed in this code. Why is that happening? public class test { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; final int testCount; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int classCount; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int getCount(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return testCount+classCount; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } }
[ "testCount and classCount have not been initialized.", "testCount has not been initialized.", "classCount has not been initialized.", "testCount and classCount cannot be added in return statement." ]
B
classCount has not been initialized.
canterbury_cs
canterbury_cs_0000333
consider the section of code below: int a = 3, b = 4, c = 5; x = a * b &lt;= c What will x be after executing this code?
[ "0", "1", "3", "12" ]
A
Due to the precedence of operators here, first a and b are multiplied and then the total [12] is compared with c [5].&nbsp; If the logical comparison evaluates to true x assumes the value 1,&nbsp; otherwise (as in this case) , it evaluates to false and x assumes the value 0.
canterbury_cs
canterbury_cs_0000334
What would be the output? Vector&lt;Object&gt; vect_1 = new Vector&lt;Object&gt;(); Vector&lt;Integer&gt; vect_2 = new Vector&lt;Integer&gt;(); vect_1.addElement(1); vect_1.addElement(2); vect_2.addElement(3); vect_2.addElement(4); vect_1.addElement(vect_2); System.out.print(vect_1.toString()); System.out.print(vect_1);
[ "This is a compiler Error. A vector of type Integer cannot be added to a vector of type Object.", "[1, 2, [3, 4]][]", "[1, 2, 3, 4][]", "[1, 2, [3, 4]][1, 2, [3, 4]]" ]
D
[1, 2, 3, 4][]
canterbury_cs
canterbury_cs_0000335
How many times will the printf statement be executed in this piece of 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! while (i &lt; 20) { &nbsp;&nbsp;&nbsp;&nbsp; if ( i = 2 ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Count me!"); &nbsp;&nbsp;&nbsp;&nbsp; i++; }
[ "&nbsp;20", "19", "1", "an infinite number" ]
D
The program fails the test of equality on the first pass through as i=0, so falls through the if statement without incrementing the index.&nbsp; This triggers the while loop again (which&nbsp;again fails the equality test&nbsp; as the index remains at 0) and the program loops in an endless cycle.
canterbury_cs
canterbury_cs_0000336
How many times will the printf statement be executed in this piece of 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! for(i = 0; i &lt;= 3; i++) { &nbsp;&nbsp;&nbsp;&nbsp; switch ( i ) &nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case 0: puts("Case 0"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case 1: puts("Case 1"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case 2: puts("Case 2"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; default: printf("Count me!"); &nbsp;&nbsp;&nbsp;&nbsp; } }
[ "0", "&nbsp;1", "&nbsp;2", "&nbsp;3" ]
D
The code iterates until the index reaches 3 (a total of four times) but on the first iteration the case statement bresk before executing the printf function.&nbsp; On subsequent iterations the printf fucntion is executed, twice by matching the index values and once by default.
canterbury_cs
canterbury_cs_0000337
This sorting algorithm starts by finding the smallest value in the entire array. Then it finds the second smallest value, which is the smallest value among the remaining elements. Then it finds the third smallest value, the fourth, and so on.
[ "selection sort", "insertion sort", "bubble sort", "quick sort" ]
A
This procedure describes the steps of the selection sort, which repeatedly selects outs the ith smallest element.
canterbury_cs
canterbury_cs_0000338
You see the expression <code>n = 128</code> in some code that successfully compiles. What type can <code>n</code> not be?
[ "int", "char", "byte", "float" ]
C
Bytes only hold values in [-128, 127].
canterbury_cs
canterbury_cs_0000339
Which one of the codes bellow will NOT compute the summation of 10 consecutive even numbers starting from zero?
[ "sum = 0; \n for (int i = 0; i &lt;20 ; i = i+2){ \n &nbsp;&nbsp;&nbsp; sum+=i; \n }", "sum = 0; \n for (int i = 0; i &lt;10 ; i++){ \n &nbsp;&nbsp;&nbsp; sum+=i*2; \n }", "sum = 0; \n for (int i = 0; i &lt;10 ; i= i+2){ \n &nbsp;&nbsp;&nbsp; sum+=i; \n }", "sum = 0; \n for (int i = 0; i &lt;20 ; i++){ \n &nbsp;&nbsp;&nbsp; sum+=i/2; \n }" ]
C
sum = 0; for (int i = 0; i &lt;10 ; i= i+2){ &nbsp;&nbsp;&nbsp; sum+=i; }
canterbury_cs
canterbury_cs_0000340
What will be printed by this code? public static void main(String [] args){ &nbsp; &nbsp; &nbsp; &nbsp; int [] number = {1, 2, 3}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; changeNumber (number); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i=0; i&lt;number.length; i++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print(number[i] + " "); } public static void changeNumber(int [] number){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i=0; i&lt;number.length; i++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; number[i] *=2 ; }
[ "1 2 3", "2 4 6", "2 2 2", "0 0 0" ]
B
2 2 2
canterbury_cs
canterbury_cs_0000341
The following method <code>isSorted</code> should return<code> true</code> if the array <code>x</code> is sorted in ascending order. Otherwise, the method should return <code>false</code>: <code>public boolean isSorted (int[] x)</code><code>{</code> <pre lang="text/x-java"> //missing code goes here } <span style="font-family: arial, helvetica, sans-serif;">Which of the following is the missing code from the method isSorted? </span></pre>
[ "<pre lang=\"text/x-java\">boolean b = true;\nfor (int i=0; i&lt;x.length-1; i++) {\n if (x[i] &gt; x[i+1])\n b = false;\n else\n b = true;\n}\nreturn b;\n</pre>", "<pre lang=\"text/x-java\">for (int i=0; i&lt;x.length-1; i++) {\n if (x[i] &gt; x[i+1])\n return false;\n}\nreturn true;\n</pre>", "<pre lang=\"text/x-java\">boolean b = false;\nfor (int i=0; i&lt;x.length-1; i++) {\n if (x[i] &gt; x[i+1])\n b = false;\n}\nreturn b;\n</pre>", "<code>boolean b = false;</code> <code>for (int i=0; i&lt;x.length-1; i++)</code><code>{</code> \n<pre lang=\"text/x-java\"> if (x[i] &gt; x[i+1])\n b = true;\n}\n</pre>\n <code>return b;</code>" ]
B
This code should check each pair of consecutive values in the array and return false (that is, the array is not sorted) if it finds a single pair where the first value is greater than the second. Choice A is wrong because its answer depends only on the relationship of the last two values it checks. Choice C is wrong because it always returns <code>false</code>. Choices D and E are wrong &nbsp;because they return <code>false</code> when the array *is* sorted and<code> true</code> when it isn't.&nbsp;
canterbury_cs
canterbury_cs_0000342
For the purposes of this question, two code fragments are considered to be&nbsp;equivalent if, when they are run using the same input, they always produce the same output. Which line could be removed from the following code fragment so that the resulting code fragment is equivalent to the original one? <pre lang="text/x-java">1. Scanner kbd = new Scanner(System.in); 2. int x, product; 3. x = -1; 4. product = 1; 5. x = kbd.nextInt(); 6. while (x &gt;= 0) { 7. if (x &gt; 0) { 8. product *= x; 9. } 10. x = kbd.nextInt(); 11. } 12. System.out.println(product); </pre>
[ "Line 3", "Line 4", "Line 5", "Line 8" ]
A
Line 3 can be removed because x is initialized again in line 5, before it is ever used.&nbsp;
canterbury_cs
canterbury_cs_0000343
What will be outputted? char initial = 'a'; switch (initial){ case 'a': &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print("a"); case 'A': &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print("A"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; case 'b': &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print("B"); default: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print("C"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; }
[ "a", "aA", "aAB", "aABC" ]
B
aAB
canterbury_cs
canterbury_cs_0000344
Depth-first traversal is kind of like selection sort, in the same way that breadth-first traversal is like what sorting algorithm?
[ "Insertion sort", "Heapsort", "Quicksort", "Bubblesort" ]
A
Insertion sort is breadth-first-like as you keep increasing the "breadth" of the array that is sorted.
canterbury_cs
canterbury_cs_0000345
Thomas has the following program that he is running on a 64-bit machine: <br><code>#include&lt;stdlib.h&gt;</code> <pre lang="text/x-c">int main() { int x = rand(); int y = rand(); } </pre> &nbsp; What is <strong>not</strong> true about y?
[ "y is less than RAND_MAX", "y is four bytes in size", "y is stored in the same stack frame as x", "y&rsquo;s value cannot be deduced from x&rsquo;s" ]
A
y &lt;= RAND_MAX, not y &lt; RAND_MAX. As rand has not been seeded, and is pseudorandomly generated, y can theoretically be deduced from x.
canterbury_cs
canterbury_cs_0000346
Consider the following output consisting of a block of stars, with no spaces between each star, and no blank lines in between the lines of stars: &nbsp; &nbsp;***** &nbsp; &nbsp;***** &nbsp; &nbsp;***** &nbsp; &nbsp;***** Considering a plan for a program to print out this shape, which of the following would be your&nbsp;<strong>main</strong>&nbsp;control structure?
[ "sequence", "selection", "repetition", "or something else" ]
C
or something else
canterbury_cs
canterbury_cs_0000347
Which of the following recommendations for testing software is good advice?
[ "Limit your test cases to one assertion, since each test should check only one expected outcome.", "Save your testing until after the solution is completely written, so you can concentrate solely on testing without distractions.", "Test a program with all possible values of input data.", "Test each piece of your solution as you build it, so you will find errors as quickly as possible." ]
D
Incrementally testing your code as you write it helps you find errors more quickly, and ensures that new work builds on previous work you have already confirmed works. &nbsp;The other four choices here represent less effective or impractical advice.
canterbury_cs
canterbury_cs_0000348
Which of the following is not an abstract datatype?
[ "Queue", "Array", "Hashtable", "Both C and D" ]
D
Hashtables and arrays are data structures, not abstract datatypes.&nbsp; Note: This question was written by A. Luxton-Reilly. K. Sanders entered and tagged the question and added choice E, after A. Luxton-Reilly had to withdraw from the WG due to illness.
canterbury_cs
canterbury_cs_0000349
What algorithm does <em>mystery&nbsp;</em>implement, when a list of values is passed in as its argument? def mystery (listOfVals):<br>&nbsp; &nbsp; current = 1<br>&nbsp; &nbsp; while (current &lt; len(listOfVals)):<br>&nbsp; &nbsp; &nbsp; &nbsp; pivot = listOfVals[current]<br>&nbsp; &nbsp; &nbsp; &nbsp; underExam = current - 1<br>&nbsp; &nbsp; &nbsp; &nbsp; while ((underExam &gt;= 0) and (listOfVals[underExam] &gt; pivot)):<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listOfVals[underExam+1] = listOfVals[underExam]<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; underExam = underExam - 1<br>&nbsp; &nbsp; &nbsp; &nbsp; listOfVals[underExam+1] = pivot<br>&nbsp; &nbsp; &nbsp; &nbsp; current = current + 1<br>&nbsp; &nbsp; return (listOfVals)
[ "Insertion Sort", "Quicksort", "Selection Sort", "List Reverser" ]
A
This is a classic implementation of Insertion Sort. &nbsp;Each item in the list is repeatedly <em>inserted</em> into its correct spot in the portion of the list already sorted.
canterbury_cs
canterbury_cs_0000350
What operation contributes the log N to heap sort's O(N log N) complexity?
[ "Trickling up only", "Trickling down only", "Both trickling up and trickling down", "Searching the array" ]
C
The heapsort involves inserting all elements and removing all elements. Inserting requires a trickle up and removing requires a trickle down.
canterbury_cs
canterbury_cs_0000351
We say indexing is fast if it's done in O(1) time, searching is fast if done in O(lg N) time, and inserting and deleting are fast if done in O(1) time.&nbsp;Compared to other data structures, sorted arrays offer:
[ "slow indexing, slow search, slow insertions and deletions.", "fast indexing, fast search, slow insertions and deletions.", "fast indexing, slow search, slow insertions and deletions.", "slow indexing, slow search, fast insertions and deletions." ]
B
Sorted arrays can be indexed in constant time (which is fast), searched in O(lg N) time (which is fast), and restructured in O(N) time (which is not as good as O(1) time).
canterbury_cs
canterbury_cs_0000352
Jack has the code below. What will it print out? <br><code>int array[] = { 45, 67, 89, 22, 13 };</code> <pre lang="text/x-c">int *array_ptr = &amp;array[1]; printf("%d, ", array_ptr[1]); printf("%d, ", *(array_ptr++)); printf("%d, ", *array_ptr++); printf("%d\n", *array_ptr); </pre>
[ "67, 67, 67, 89", "67, 45, 67, 89", "89, 67, 89, 22", "89, 89, 89, 67" ]
C
The array pointer starts at the second element; the incrementing is done post-evaluation.
canterbury_cs
canterbury_cs_0000353
Which of the following is <strong>not</strong> a reason why wall clock time is a poor measure of an algorithm's performance?
[ "It's dependent on processor speed.", "Other programs may interrupt the one being measured.", "The input cases may not capture the worst case running time.", "Different platforms measure wall clock time differently." ]
D
Wall clock time is a measure of real elapsed time between two events, which is no different from one computer to the next.
canterbury_cs
canterbury_cs_0000354
Choose the answer that best describes how to compute the value of the following Java arithmetic expression:<br>&nbsp; &nbsp; &nbsp;4 * 3 + 6 / 4
[ "B and C.&nbsp;", "Evaluate * and / first (from left to right), and then evaluate +.", "A and C.", "Round the answer to 6/4 towards 0.&nbsp;" ]
A
In Java, multiplication and division have higher precedence than addition, and division is closed under ints.
canterbury_cs
canterbury_cs_0000355
How many times will F be called if n = 5, ignoring F(5) itself? public static long F(int n) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (n == 0)return 0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (n == 1) return 1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return F(n-1) + F(n-2); }
[ "15", "12", "7", "5" ]
A
7
canterbury_cs
canterbury_cs_0000356
Compared to a sorted linked list, a balanced binary search tree offers
[ "faster searching and slower insertion", "slower searching and slower insertion", "the same performance for searching and insertion", "faster searching and faster insertion" ]
D
Insertion is O(n) for a sorted linked list, as we must traverse the list to find the correct insertion point. Searching is O(n) since we only have sequential access; the binary search does not work for linked lists. Balanced BSTs can be traversed in O(lg N) time, making it faster for both operations.
canterbury_cs
canterbury_cs_0000357
You need to sort an array but have no extra memory to spare. Which algorithm do you avoid?
[ "bubble sort", "merge sort", "quick sort", "heap sort" ]
B
Merge sort requires a secondary array to merge into.
canterbury_cs
canterbury_cs_0000358
Which of the following is true of a Java interface?
[ "Instance variables are allowed, but they must be public.", "Private methods may not be declared.", "Static and final constants may not defined.", "Methods may have implementations provided they are marked <code>final</code>." ]
B
All methods in an interface are public, even if you leave off the access modifier.
canterbury_cs
canterbury_cs_0000359
After calling the following function, what will the values of n and m be? Assume that before<br>calling the function, n = 2 and m = 5. <br><code>void example(int *n, int *m)<br>{<br>&nbsp;&nbsp; *n++;<br>&nbsp;&nbsp; *m += 5;<br>}</code> <pre lang="text/x-c"> void main() { int n = 2; int m = 5; example(&amp;n, &amp;m); printf("n = %d and m = %d", n, m); } </pre>
[ "n = 2 and m = 5", "n = 3 and m = 5", "n = 2 and m = 10", "n = 3 and m = 10" ]
C
Due to order of operations, the ++ will not change n.
canterbury_cs
canterbury_cs_0000360
Finding the maximum value in a complete and&nbsp;balanced binary search tree is
[ "O(1)", "O(log N)", "O(N)", "O(N<sup>2</sup>)" ]
B
The maximum is the root's right-most descendent, which can be reached in log N steps.
canterbury_cs
canterbury_cs_0000361
You need to store a large amount of data that can be searched quickly by some key, but you don't know exactly how many elements you'll need to store. You want to waste no storage space. The elements to be added are in random order. What is the simplest data structure that meets your needs?
[ "Ordered array", "Linked list", "Hashtable", "Binary search tree" ]
D
Hashtables provide fast searching, but they may waste storage space. A tree makes better use of memory. Since the keys are in a random order, it's likely a binary tree will end up balanced just through the insertion price.
canterbury_cs
canterbury_cs_0000362
What is printed when the following program runs? <pre lang="text/x-java">public class Main { public static boolean getTrue() { System.out.print("T"); return true; } public static boolean getFalse() { System.out.print("F"); return false; } public static void main(String[] args) { getFalse() &amp;&amp; getFalse(); } } </pre>
[ "FF", "T", "TF", "F" ]
D
The first operand is false. &amp;&amp; requires both operands to be true, so we don't bother evaluating the second.
canterbury_cs
canterbury_cs_0000363
<span style="font-size: 10.0pt; line-height: 115%; font-family: 'Calibri','sans-serif'; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: Arial; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;">It is desired to have an output as shown bellow as a consequence of running the following code. What would be the type of <em>st1?</em> </span> __________ st1 = new __________("Measure twice, cut once"); System.out.println(st1); System.out.println(st1.toUpperCase()); System.out.println(st1); &nbsp; Measure twice, cut once MEASURE TWICE, CUT ONCE Measure twice, cut once
[ "String", "StringBuffer", "StringBuilder", "All of the above is possible" ]
A
StringBuilder
canterbury_cs
canterbury_cs_0000364
What package should be imported for this code to work correctly? ArrayList &lt;Integer&gt; intValues = new ArrayList&lt;Integer&gt; (); public void add (){ &nbsp;&nbsp;&nbsp;&nbsp; for (int i=0; i &lt;50; i++ ) &nbsp;&nbsp;&nbsp;&nbsp; intValues.add(i); }
[ "java.util", "java.lang", "java.io", "java.math" ]
A
java.io
canterbury_cs
canterbury_cs_0000365
If we know the length of the string used in our program is almost 200, is there any difference between these following definitions in terms of performance? StringBuilder string1 = new StringBuilder(); StringBuilder string2 = new StringBuilder(50); StringBuilder string3 = new StringBuilder(100);
[ "No, there is no performance difference.", "Yes, the definition of string1 is better.", "Yes, the definition of string2 is better.", "Yes, the definition of String3 is better." ]
D
Yes, the definition of string2 is better.
canterbury_cs
canterbury_cs_0000366
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. What is the code expected to do?
[ "Clear the text in txtName.", "Copy the text in txtName to the clipboard.", "Fill txtName with text pasted from the clipboard.", "Highlight all the text in txtName." ]
D
The code sets focus to the text box and by selecting the focus area from start of the text box to its end thereby highlights the contents
canterbury_cs
canterbury_cs_0000367
Depending on which delimiter is used in the following code, which output will be resulted respectively? String input = "Keep your friends close and your enemies closer"; Scanner sc_input = new Scanner(input).useDelimiter("_______________"); while (sc_input.hasNext()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(sc_input.next()); sc_input.close(); I.&nbsp;&nbsp;&nbsp; \\s*close\\s II.&nbsp;&nbsp;&nbsp; \\s*close\\s* III.&nbsp;&nbsp; \\s*close
[ "I. \n Keep your friends \n and your enemies closer \n II. \n Keep your friends \n and your enemies closer \n III. \n Keep your friends \n and your enemies \n r", "I. \n Keep your friends \n and your enemies closer \n II. \n Keep your friends \n and your enemies \n r \n III. \n Keep your friends \n and your enemies \n r", "I. \n Keep your friends \n and your enemies closer \n II. \n Keep your friends \n and your enemies \n r \n III. \n Keep your friends \n and your enemies \n r", "I. \n Keep your friends \n and your enemies closer \n II. \n Keep your friends \n and your enemies closer \n III. \n Keep your friends \n and your enemies \n r" ]
C
I. Keep your friends and your enemies closer II. Keep your friends and your enemies r III. Keep your friends and your enemies r
canterbury_cs
canterbury_cs_0000368
Before testing an executable version of your program with data from a test file using DOS redirection, you should remove all calls to fflush from your program. Why is this necessary?
[ "fflush is not part of the ANSI C standard.", "fflush is for debugging only. It causes a run time error in an exe file.", "fflush would read and ignore all remaining data in the test file.", "fflush is not in any QuickC library." ]
C
fflush would read and ignore all remaining data in the test file, as it has already flushed the input buffer
canterbury_cs
canterbury_cs_0000369
To let the <em>Scanner sc = new Scanner(System.in);</em> statement works fine, which package should be imported?
[ "java.util", "java.io", "java.lang", "java.Scanner" ]
A
java.lang
canterbury_cs
canterbury_cs_0000370
Suppose we have an arbitrary number of entries that each includes a key and a value. We never need to search the entries while the rule for insertion and removal is first-in-last-out. Which data structure is proper?
[ "one dimensional array", "two dimensional array", "single linked list", "double linked list" ]
C
single linked list
canterbury_cs
canterbury_cs_0000371
I have a number of different forms in my program, and have help topics explaining how each should be used. When the users presses F1, I want the appropriate help topic to appear depending on which form is currently open. What is the easiest way to achieve this?
[ "Set the HelpContextID property of the Application object to the appropriate ID value.", "Set the HelpContextID property of each form to the appropriate ID value.", "Set the HelpContext property of a Common Dialog control to the appropriate ID value.", "Set the HelpCommand property of a Common Dialog control to the appropriate ID value." ]
B
The HelpContextID property is used to link a user interface element (such as a control, form, or menu) to a related topic in a Help file. The HelpContextID property must be a Long that matches the Context ID of a topic in a WinHelp (.hlp) or HTML Help (.chm) file. source http://msdn.microsoft.com/en-us/library/aa261329%28v=vs.60%29.aspx
canterbury_cs
canterbury_cs_0000372
<p class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: black;">To make the following code work without any error, one of the given solutions is <strong>not</strong> right. Which one is it?</span> <p class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; mso-fareast-font-family: Calibri; color: black;">class D {</span> <p class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; mso-fareast-font-family: Calibri; color: black;"><span style="mso-tab-count: 1;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int Diameter;</span></span> <p class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; mso-fareast-font-family: Calibri; color: black;"><span style="mso-tab-count: 1;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public D(int d){</span></span> <p class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; mso-fareast-font-family: Calibri; color: black;"><span style="mso-tab-count: 2;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Diameter = d;</span></span> <p class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; mso-fareast-font-family: Calibri; color: black;"><span style="mso-tab-count: 1;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></span> <p class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; mso-fareast-font-family: Calibri; color: black;">}</span> <p class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; mso-fareast-font-family: Calibri; color: black;">public class E {</span> <p class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; mso-fareast-font-family: Calibri; color: black;"><span style="mso-tab-count: 1;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static void main(String arg[]){</span></span> <p class="MsoNormal"><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; mso-fareast-font-family: Calibri; color: black;"><span style="mso-tab-count: 2;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; D diam = new D();</span></span> <p class="MsoNormal"><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; mso-fareast-font-family: Calibri; color: black;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></span> <span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; mso-fareast-font-family: Calibri; color: black; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;">}</span>
[ "To omit the input argument of the constructor.", "To define diam by passing an int value to constructor.", "To define a null constructor.", "To define the constructor private." ]
D
To define a null constructor.
canterbury_cs
canterbury_cs_0000373
Suppose you are writing a method in a new class. You are also writing unit test cases to demonstrate that this method works correctly. You know you have written enough test cases to demonstrate the method works as desired when?
[ "You have written at least one test case that uses the method.", "You have written separate test cases for each identifiable &ldquo;group&rdquo; of input values and/or output values where the behavior is expected to be similar.", "You have written at least one test case for every input value that can be given to the method.", "You have written at least one test case for every output value that can be produced by the method." ]
B
For most interesting methods, there are too many possible input values and/or output values for you to check them all. &nbsp;However, checking just one input our output is insufficient for methods that do anything sophisticated. &nbsp;As a result, it is often helpful to think of the different cases or "equivalence classes" of input/output values where the methods behaves similarly, and ensure you test each such group separately. &nbsp;Often, writing multiple tests for each group--so that you can double-check the method's behavior "at the boundary" of the group--is especially helpful.
canterbury_cs
canterbury_cs_0000374
Is there any error in this code? String [] firstNames = {"Neda", "Dorsa"}; int [] ages = {10, 14}; if (ages[0]&gt;ages[1]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (firstNames[0]&gt; firstNames [1]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print(firstNames[0] + "has the priority"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else System.out.print("They both have the same priority"); else System.out.print(firstNames[1] + "has the priority");
[ "Yes, wrong operator has been used for string type.", "Yes, wrong operator has been used for int type.", "Yes, array is out of bound.", "This code has no Error." ]
A
Yes, array is out of bound.
canterbury_cs
canterbury_cs_0000375
Let's call the index at which an element is placed in an otherwise empty hashtable index h. With open addressing, a key-value pair may collide with another and be placed in a different location according to some defined scheme. Where may a key-value pair be placed?
[ "anywhere in the array", "at a list stored at index h", "at index h or after", "at index h or before" ]
A
With open addressing, some sort of probing algorithm is used to resolve collisions. Probing involves stepping along from the collision index to a subsequent element whose distance is determined by some step size. This probing visits all elements of the array, if need be.
canterbury_cs
canterbury_cs_0000376
We have defined two JButton in a class and are going to handle the events by implementing ActionListener. As we know, this interface has a method called actionPerformed that we have to override it in our class. What would be the best way (i.e. object oriented approach) to listen to the events regarding both the buttons?
[ "Override actionPerformed method twice for both the buttons.", "Override actionPerformed method once, get the source of event in this method and then handle the events.", "Create two separate classes, each should implements ActionListener interface. Then register each button with each classes.", "use inner classes, each implements ActionListener interface. Each button should be registered with one of these inner classes." ]
D
Create two separate classes, each should implements ActionListener interface. Then register each button with each classes.
canterbury_cs
canterbury_cs_0000377
Grace has written a method to pop from a stack; fill in the pre-condition. <br><code>struct node* pop(struct node **head)</code> <pre lang="text/x-c">{ // pops the stack pointed to by head; does nothing to alter that node // PRE: // POST: head now points to what was the second element of the list struct node* to_pop = *head; *head = (*head)-&gt;next; return to_pop; } </pre>
[ "head != NULL", "*head != NULL", "**head != NULL", "to_pop != NULL" ]
B
*head != NULL is the best answer of A-C as it has the closest level of detail to what is used in the code
canterbury_cs
canterbury_cs_0000378
Which of the following statements about binary search algorithms is FALSE?
[ "The data must be sorted.", "There must be a mechanism to access elements in the middle of the data structure.&nbsp;", "Binary search is inefficient when performed on a linked list.", "Binary search can be implemented to take&nbsp;<img src=\"img/19.png\" alt=\"\" title=\"latex\">(n) time in the worst case." ]
D
Binary search takes&nbsp;<img src="img/20.png" alt="" title="latex">(log n) time, worst case.
canterbury_cs
canterbury_cs_0000379
Suppose you are using a library implementation of a LinkedList.&nbsp; You have only the compiled binary version of the code, but not the source code.&nbsp; You want to know if the runtime complexity of the size() method is O(1).&nbsp; (i.e. you want to know if the library stores the size, or if it counts each node every time size() is invoked). Which approach(es) would tell you if the size() method is O(1)?
[ "Insert one element and time how long the size method takes; compare this to how long the size method takes if you had inserted 100 elements. O(1) means they should be about equal.", "Insert a million elements and time how long the size method takes; compare this to how long the size method takes if you had inserted 10 million elements. O(1) means they should be about equal.", "Insert 10 billion elements and time how long the size method takes; compare this to how long the size method takes if you had inserted 100 billion elements. O(1) means they should be about equal.", "B and C both work" ]
B
A won't work because most of what you are timing is the JVM starting up and shutting down.&nbsp; In fact, just checking the time elapsed may require as many machine instructions as checking the size of a linked list of size 1. C is also unlikely to work.&nbsp; Suppose each element is a single int, which is 4 bytes (this is a lower-bound on things you can put in a collection). A billion elements is 4 billion bytes. A gigabyte is a little more than a billion bytes, so this likely fills most of your RAM (about 4GB for many laptops and desktops in 2013). 10 billion elements is about 37 GB, which will fill the memory of all but the biggest machines available. You will be timing the garbage collector and memory allocator algorithms, not the size() method.
canterbury_cs
canterbury_cs_0000380
What is returned by <code>&rsquo;A&rsquo; &lt; &rsquo;A Linkletter&rsquo;</code>?
[ "True", "False", "'A'", "an error" ]
A
This is a lexicographic (i.e. alphabetic) comparison, but the first String 'A' is a substring of the second String.&nbsp; We fall back on the "nothing before something" principle, and return True, because when sorting Strings, nothing comes before something.&nbsp; This is why the word 'a' comes before 'aardvark' in the dictionary, as well as why 'ant' comes before 'anteater' and 'the' comes before 'these' or 'then'.
canterbury_cs
canterbury_cs_0000381
Growing the array used to implement a heap so that it may hold more elements is
[ "O(1)", "O(log N)", "O(N)", "O(N log N)" ]
C
The order of elements in the array doesn't change any. They only need to be copied over in their original sequence, making this an O(N) operation.
canterbury_cs
canterbury_cs_0000382
Suppose you are writing software for a helpdesk. Each request is entered into the system as it arrives. Which of the following abstract datatypes would be the best choice to store these requests? Be prepared to justify your answer.&nbsp;
[ "A Stack", "A Queue", "An OrderedList", "A PriorityQueue" ]
D
B and D are the two best choices. B allows you to handle the requests in the order received; D allows you the flexibility to override that order when needed, by giving certain requests a higher priority. E is less good because although you could let a priority be the index into the Dictionary, Dictionaries aren't guaranteed to return items of the same priority in the order they were added to the Dictionary. A is wrong, because it would guarantee that the most recent requests are always handled first.&nbsp; An argument could be made for C &nbsp;-- elements could be added to the list in order of priority, following any other elements with the same priority. The needed operations will likely be less efficient than they would be with a Queue or PriorityQueue, however.&nbsp;
canterbury_cs
canterbury_cs_0000383
After the assignment statement &nbsp; &nbsp;<code>String word = "entropy";</code> what is returned by &nbsp; &nbsp;<code>word.substring(2, 4);</code>
[ "\"ntro\"", "\"nt\"", "\"trop\"", "\"tr\"" ]
D
The Java <code>String</code> method <code>word.substring (2, 4)</code> returns the substring of <code>word</code> starting at the third character (array index 2) and ending with the fourth character (array index 4-1).&nbsp;
canterbury_cs
canterbury_cs_0000384
You must store a large amount of data, and both searching and inserting new elements must be as fast as possible. What's the simplest data structure that meets your needs?
[ "Ordered array", "Linked list", "Hashtable", "Binary search tree" ]
C
Hashtables provide O(1) insertion and searching, provided collisions aren't too numerous.
canterbury_cs
canterbury_cs_0000385
Which data structure does java use when evaluates a mathematical expression in a program?
[ "A tree", "A binary tree", "A linked list", "An array" ]
B
A linked list
canterbury_cs
canterbury_cs_0000386
Suppose we have a Queue implemented with a circular array. The <code>capacity</code> is 10 and the <code>size</code> is 5. Which are <em>not</em> legal values for <code>front</code> and <code>rear</code>?
[ "front: 0 \n rear:&nbsp; 5", "front: 5 \n rear:&nbsp; 9", "front: 7 \n rear:&nbsp; 2", "front: 9 \n rear:&nbsp; 4" ]
B
There would only be 4 items in this queue, not 5.&nbsp; This is easier to illustrate with a picture.&nbsp; But you can sort of see the pattern by looking at the absolute value of the difference betwen all of the pairings.&nbsp; B is the only one that differs by 4; the others all differ by 5.
canterbury_cs
canterbury_cs_0000387
You know exactly how much data you need to store, but there's not much of it. You do need to be able to search the collection quickly. What is the simplest data structure that best suits for your needs?
[ "Unordered array", "Ordered array", "Linked list", "Hashtable" ]
B
The memory needs are known, making an array a fine choice. Sorted arrays can be searched quickly with binary search.
canterbury_cs
canterbury_cs_0000388
What would be the output? public class test { &nbsp;&nbsp;&nbsp; static int testCount; &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;&nbsp;&nbsp; test t1 = new test(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(t1.testCount); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test t2 = new test(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(t1.testCount + " "+ t2.testCount); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test t3 = new test(); &nbsp;&nbsp;&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", "This is an error. testCount has never been initialized." ]
C
1 2 2 3 3 3
canterbury_cs
canterbury_cs_0000389
I have a data control, dtData, and several text boxes which are bound to it. The program user edits some of the text in one of the boxes, but makes a mistake and presses the Cancel button to undo the changes. What code should the Cancel button invoke?
[ "dtData.Recordset.Update", "dtData.Recordset.Delete", "dtData.UpdateControls", "dtData.UpdateRecord" ]
C
This method applies the cancel event procedure to restore the display content of the text boxes associated with the Data control without affecting the underlying recordset.
canterbury_cs
canterbury_cs_0000390
How many active object references and reachable objects will be produced by this code? String first = new String("Dorsa"); String second = new String("Mahsa"); String third = second; second = null;
[ "2 , 2", "3, 2", "3, 3", "2, 3" ]
A
3, 3
canterbury_cs
canterbury_cs_0000391
Programs implementing the "Examine All" programming pattern typically run in time:
[ "<em>O</em>(n<sup>2</sup>)", "<em>O</em>(n<sup>3</sup>)", "<em>O</em>(n)", "<em>O</em>(n!)" ]
C
Examine all algorithm need, in the worst case, to individually "process" each element or value in the input once. &nbsp;Hence examine all algorithms exhibit a linear run time. &nbsp;Examples of examine all algorithms include <em>Find Largest, Count Odd, </em>or<em>, Is Sorted.</em>
canterbury_cs
canterbury_cs_0000392
Which of the following are <em>not</em> legal Java identifiers?
[ "fiveGuys", "5Guys", "five_guys", "numGuys5" ]
D
E is also illegal.
canterbury_cs
canterbury_cs_0000393
Which of the following choices will NOT produce the same result as the following condition? if ( mark == 'A' &amp;&amp; GPA &gt; 3.5) &nbsp;&nbsp;&nbsp; System.out.println("First Class"); else if ( mark == 'A' &amp;&amp; GPA &lt;= 3.5) &nbsp;&nbsp;&nbsp; System.out.println("Second Class"); else if ( mark != 'A' &amp;&amp; GPA &gt; 3.5) &nbsp;&nbsp;&nbsp; System.out.println("Third Class"); else if ( mark != 'A' &amp;&amp; GPA &lt;= 3.5) &nbsp;&nbsp;&nbsp; System.out.println("Fourth Class");
[ "if ( mark != 'A' || GPA &lt; 3.5) \n &nbsp;&nbsp;&nbsp; System.out.println(\"First Class\"); \n else if ( mark != 'A' || GPA &gt;= 3.5) \n &nbsp;&nbsp;&nbsp; System.out.println(\"Second Class\"); \n else if ( mark == 'A' || GPA &lt; 3.5) \n &nbsp;&nbsp;&nbsp; System.out.println(\"Third Class\"); \n else if ( mark == 'A' || GPA &gt;= 3.5) \n &nbsp;&nbsp;&nbsp; System.out.println(\"Fourth Class\");", "if ( mark != 'A') \n &nbsp;&nbsp;&nbsp; if (GPA &gt; 3.5) \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(\"Third Class\"); \n &nbsp;&nbsp;&nbsp; else \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(\"Fourth Class\"); \n &nbsp;&nbsp;&nbsp; else if (GPA &gt; 3.5) \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(\"First Class\"); \n &nbsp;&nbsp;&nbsp; else \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(\"Second Class\");", "if ( GPA &gt; 3.5) \n &nbsp;&nbsp;&nbsp;&nbsp; if (mark == 'A') \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(\"First Class\"); \n &nbsp;&nbsp;&nbsp; else \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(\"Third Class\"); \n &nbsp;&nbsp;&nbsp; else if (mark == 'A') \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(\"Second Class\"); \n &nbsp;&nbsp;&nbsp; else \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(\"Fourth Class\");", "if ( mark == 'A') \n &nbsp;&nbsp;&nbsp;&nbsp; if (GPA &gt; 3.5) \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(\"First Class\"); \n &nbsp;&nbsp;&nbsp; else \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(\"Second Class\"); \n &nbsp;&nbsp;&nbsp; else if (GPA &gt; 3.5) \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(\"Third Class\"); \n &nbsp;&nbsp;&nbsp; else \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(\"Fourth Class\");" ]
A
if ( GPA &gt; 3.5) &nbsp;&nbsp;&nbsp;&nbsp; if (mark == 'A') &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("First Class"); &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Third Class"); &nbsp;&nbsp;&nbsp; else if (mark == 'A') &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Second Class"); &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Fourth Class");
canterbury_cs
canterbury_cs_0000394
Chengcheng has an array that he would like to sort, and implements the following sorting<br>method in C. Which sorting algorithm is this? (code is adapted from http://en.wikibooks.org/wiki/Algorithm_implementation) <br><code>void sort_array(int a[], int length)</code> <pre lang="text/x-c">{ // sorts the array a[], of length "length" int i, j, value; for(i = 1; i &lt; length; i++) { value = a[i]; for (j = i - 1; j &gt;= 0 &amp;&amp; a[j] &gt; value; j--) { a[j + 1] = a[j]; } a[j + 1] = value; } } </pre>
[ "Insertion Sort", "Selection Sort", "Bubble sort", "Mergesort" ]
A
The code is working from one end of the array to the other, sorting as it goes.
canterbury_cs
canterbury_cs_0000395
Which definition is not allowed? firstLevel first = new firstLevel(); secondLevel second = new secondLevel(); firstLevel third = new secondLevel(); secondLevel fourth = new firstLevel();
[ "first", "second", "third", "fourth" ]
D
third
canterbury_cs
canterbury_cs_0000396
The following method, called maxRow(), is intended to take one parameter: a List where the elements are Lists of Integer 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 5</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>matrix.size()</code>", "<code>matrix[row].size()</code>", "<code>matrix.get(row).size()</code>", "<code>matrix.get(col).size()</code>" ]
C
The blank on line 5 controls the upper limit of the <code>col</code> counter. &nbsp;It should represent the total number of columns in the current row, which can be retrieved with the expression <code>matrix.get(row).size()</code>.
canterbury_cs
canterbury_cs_0000397
The <em>scope</em> of a variable in Java is:
[ "The thing that allows it to see other variables;", "The other variables that it can interact with", "The part of the program in which it can be used", "One of its instance variables" ]
C
The scope of a variable is the part of the program in which that variable can be referred to by its name.&nbsp; A private instance variable in a class definition, for example, can only be referred to within that class definition.
canterbury_cs
canterbury_cs_0000398
After the following syntactically correct code is executed, where will Karel be standing and which direction will the robot be facing? How many beepers will karel be carrying? There are beepers at (2,2), (2,3), (2,4), (2,5), (2,6). (Note: a beeper at (2,5) means there is a beeper at the intersection of 2<sup>nd</sup> street and 5<sup>th</sup> avenue.) <br>def task ():<br>&nbsp; karel = Robot (2,2,East,0);<br>&nbsp; while (karel.nextToABeeper()):<br>&nbsp; &nbsp; karel.turnLeft()<br>&nbsp; &nbsp; karel.move()<br>&nbsp; &nbsp; for i in range (2):<br>&nbsp; &nbsp; &nbsp; karel.turnLeft() &nbsp; &nbsp; karel.move()<br>&nbsp; &nbsp; karel.turnLeft()<br>&nbsp; &nbsp; if (karel.nextToABeeper()):<br>&nbsp; &nbsp; &nbsp; karel.pickBeeper()<br>&nbsp; &nbsp; &nbsp; karel.move() &nbsp; karel.move()<br>&nbsp; karel.turnOff()
[ "&nbsp;Robot is facing East with five beepers and is located at (2,8)", "Robot is facing East with five beepers and is located at (2,7)", "Robot is facing North with five beepers and is located at (8,2)", "Robot is facing East with one beeper and is located at (2,2)" ]
A
There is a final "move" after the loop terminates, moving the robot over one block east (from (2,7) to (2,8)).
canterbury_cs
canterbury_cs_0000399
<pre lang="text/x-java"> public BallPanel extends javax.swing.JPanel { private Ball[] _balls; public BallPanel(){ _balls = new Ball[20]; for (int i=0;i&lt;10;i++) _balls[i] = new Ball(); } ... } </pre> &nbsp; After I have instantiated a <code>BallPanel</code> using the above code, which of the following Java statements would work (if executed in some method of <code>BallPanel</code>)?&nbsp; Assume <code>Ball</code> has a public <code>getColor()</code> method.
[ "<code>_balls[20] = new Ball();</code>", "<pre lang=\"text/x-java\">_balls[0] = _balls[15];\n\n</pre>", "<code>_balls[10].getColor();</code>", "Both A and C work" ]
B
A doesn't work, since the possible range of a 20 element array index is 0 to 19 inclusive. B works, even though _balls[15] was never assigned a value -- its value is set to <code>null<span style="font-family: arial,helvetica,sans-serif;"> when the array is instantiated, and B would set <code>_balls[0</code>] to <code>null</code></span></code> <span style="font-family: arial,helvetica,sans-serif;">C doesn't work: since only <code>_balls[0]</code> to <code>_balls[9]</code> were assigned values, <code>_balls[10]</code>'s value is <code>null</code>, and we cannot call methods on a null instance. </span> <span style="font-family: arial,helvetica,sans-serif;">So the answer is B.</span>