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_0000500
What is the correct way to declare pFile as a file pointer?
[ " FILE *pFile;", "FILE pFile*;", "*FILE pFile;", "*pFile FILE;" ]
A
FILE * For C File I/O you need to use a FILE pointer, which will let the program keep track of the file being accessed. (You can think of it as the memory address of the file or the location of the file). Source http://www.cprogramming.com/tutorial/cfileio.html For other examples also refer http://stackoverflow.com/questions/589389/what-is-the-correct-way-to-declare-and-use-a-file-pointer-in-c-c
canterbury_cs
canterbury_cs_0000501
What will be displayed by static char szName[] = "Peter"; printf("%d", strcmp("Peter", szName));
[ "A negative number such as -1", "0", " A positive number such as 1", " 5" ]
B
DescriptionThe C library function int strcmp(const char *str1, const char *str2) compares the string pointed to by str1 to the string pointed to by str2. DeclarationFollowing is the declaration for strcmp() function. int strcmp(constchar*str1,constchar*str2)Parametersstr1 -- This is the first string to be compared. str2 -- This is the second string to be compared. Return ValueThis function returned values are as follows: if Return value if < 0 then it indicates str1 is less than str2 if Return value if > 0 then it indicates str2 is less than str1 if Return value if = 0 then it indicates str1 is equal to str2 As the strings "Peter" are equal in this instance the value returned is 0 source: http://www.tutorialspoint.com/c_standard_library/c_function_strcmp.htm
canterbury_cs
canterbury_cs_0000502
Consider the following short program, which does not meet all institutional coding standards: void vCodeString(char szText[ ]); /* First line */ #include <stdio.h> #include <string.h> #define MAX_LEN 12 int main(void) {      char szData[MAX_LEN];      printf("Enter some text to code: ");      scanf("%s", szData);      vCodeString(szData); /* Line 8 */      printf("Coded string is %s\n", szData); } void vCodeString(char szText[ ]) {      int i = -1;      while(szText[++i])      {           szText[i] += (char)2;      } } Knowing that scanf does not read beyond a space in the input, I try the routine by typing in Hello there when it asks me for some text. What is output after "Coded string is “?
[ "Jgnnq vjgtg", "Jgnnq", "Hello there", "Hello" ]
B
The vCodeString function converts the input by incrementing the next two alphabetical letters for each character passed to it from the szData array containing the the "hello" substring after which the scanf function had haled on meeting the delimiting space character
canterbury_cs
canterbury_cs_0000503
Consider the following short program, which does not meet all institutional coding standards: void vCodeString(char szText[ ]); /* First line */ #include <stdio.h> #include <string.h> #define MAX_LEN 12 int main(void) {      char szData[MAX_LEN];      printf("Enter some text to code: ");     scanf("%s", szData);     vCodeString(szData); /* Line 8 */      printf("Coded string is %s\n", szData); } void vCodeString(char szText[ ]) {      int i = -1;      while(szText[++i])      {             szText[i] += (char)2;      } } Why is there no address operator (&) before szData in the scanf line?:
[ "Parameters are passed to scanf by value, not by reference.", "scanf takes only inward parameters so does not need the &", "szData is the address of the array.", "szData is an array so it does not have an address." ]
C
On declaration of a variable a space in memory is reserved for its storage.  Here szData is an array of type char which has already been defined in the program and a memory location has been allocated to it
canterbury_cs
canterbury_cs_0000504
Consider the following short program, which does not meet all institutional coding standards: void vCodeString(char szText[ ]); /* First line */ #include <stdio.h> #include <string.h> #define MAX_LEN 12 int main(void) {      char szData[MAX_LEN];      printf("Enter some text to code: ");      scanf("%s", szData);      vCodeString(szData); /* Line 8 */      printf("Coded string is %s\n", szData); } void vCodeString(char szText[ ]) {      int i = -1;      while(szText[++i])      {               szText[i] += (char)2;      } } The first line is:
[ "a macro definition", "a function definition", "a function call", " a function prototype" ]
D
A function prototype or function interface in C, Perl, PHP or C++ is a declaration of a function that omits the function body but does specify the function's return type, name, arity and argument types. While a function definition specifies what a function does, a function prototype can be thought of as specifying its interface. [Source "Function prototype" Wikipedia]. Note the function is called from Main at line 8 and defined at line 11.
canterbury_cs
canterbury_cs_0000505
<p class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; line-height: 115%;">What will be printed by this code?</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'; color: black;"><span style="mso-tab-count: 1;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: #7f0055;">public <span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: #7f0055;">static <span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: #7f0055;">void</span></span></span></strong><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: black;"> main(String [] args){</span></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'; color: black;"><span style="mso-tab-count: 2;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: #7f0055;">int</span></strong><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: black;"> number = 6;</span></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'; color: black;"><span style="mso-tab-count: 2;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: #7f0055;">int</span></strong><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: black;"> secondNumber = <em>changeNumber</em> (number);</span></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'; color: black;"><span style="mso-tab-count: 2;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.<em><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: #0000c0;">out</span></em><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: black;">.print(number + <span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: #2a00ff;">" "<span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: black;"> + secondNumber);</span></span></span></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'; 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'; color: black;"><span style="mso-tab-count: 1;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: #7f0055;">public <span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: #7f0055;">static <span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: #7f0055; background: lightgrey; mso-highlight: lightgrey;">int</span></span></span></strong><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: black;"> changeNumber(<strong><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: #7f0055;">int</span></strong><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: black;"> number){</span></span></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'; color: black;"><span style="mso-tab-count: 2;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; number = 12;</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'; color: black;"><span style="mso-tab-count: 2;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: #7f0055; background: lightgrey; mso-highlight: lightgrey;">return</span></strong><span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; color: black; background: lightgrey; mso-highlight: lightgrey;"> number;</span></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'; color: black;"><span style="mso-tab-count: 1;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></span>
[ "6&nbsp; 6", "6&nbsp; 12", "12&nbsp; 6", "12&nbsp; 12" ]
B
12&nbsp; 6
canterbury_cs
canterbury_cs_0000506
What will be written in the gaps from top to bottom and left to right respectively to create a correct program. public static void main(String [] args){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; double n1 = 1.5; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; double n2 = 2; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ........ returnValue = compute ( n1 , n2); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print(returnValue); } public static ....... compute (...... a , ...... b){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (int) (a+b); }
[ "int int double double", "double int double double", "int int double int", "double int double int" ]
A
int int double int
canterbury_cs
canterbury_cs_0000507
"The value when iVal is divided by 10" would be coded as:
[ "iVal % 10", "&nbsp;iVal &amp; 10", "&nbsp;iVal == 10", "&nbsp;iVal / 10" ]
D
Expresses the value of a variable using the division operator and the divisor (here 10)
canterbury_cs
canterbury_cs_0000508
Which of the following assertions about methods is correct?
[ "The body of a method must contain at least one return statement.", "A method must return a value.", "A method invocation must contain at least one argument.", "A method with no return statement must not be invoked on the right side&nbsp;of an assignment statement." ]
D
(a) and (b) are true of functions, but not of Java methods in general. (c) is incorrect. Methods in general and functions specifically can be written with or without parameters. (d) is correct: methods without a return statement can't be on the right-hand side of an assignment statement, because they don't have a value. And if (d) is correct, (e) must be wrong.&nbsp;
canterbury_cs
canterbury_cs_0000509
Given the input "Click &amp; Clack", what is the output of line 16 of the following Java code? <pre lang="text/x-java">System.out.println("Enter a string: "); String input = kbd.nextLine(); String a = ""; String letter = ""; int d = 0; int r = 1; String englishAlphabet = "abcdefghijklmnopqrstuvwxyz"; for (int i = 0; i &lt; englishAlphabet.length(); i++) { letter = englishAlphabet.substring(i,i+1); if (input.contains(letter)){ a = letter + a; } else { d++; } } System.out.println("String a is: " + a); //line 16 System.out.println("int d is: " + d); // line 17 </pre> <code> </code>
[ "ClickClack", "kcalkcil", "acikl", "lkica" ]
D
This code checks all the letters in the lower-case alphabet to see which ones appear in the input, and concatenates them into a string where each letter appears once, in reverse alphabetical order. The resulting string is output in line 16.
canterbury_cs
canterbury_cs_0000510
<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;">What should be done to initialize <em>counter</em><span style="font-size: 10.0pt; line-height: 115%;"> to one in <em>main</em> method?</span></span> <p class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;">&nbsp; <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<span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; mso-fareast-font-family: Calibri; color: black;"> C {</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; private int counter;</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<span style="font-size: 10.0pt; line-height: 115%; font-family: 'Courier New'; mso-fareast-font-family: Calibri; color: black;"> class F {</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 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-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>
[ "c.counter = 1;", "C c = new C(); \n c.counter = 1;", "C. add the following to C: \n public void setCounter( int c){ \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; counter = c; \n } \n Add the following to F: \n C c = new C(); \n c.setCounter(1);", "Change the definition of counter as follow: \n private int counter = 1;" ]
C
Change the definition of counter as follow: private int counter = 1;
canterbury_cs
canterbury_cs_0000511
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 x will 3 be among the values printed?
[ "x &gt;= 0", "x &lt; 20", "None of the above.", "All values of x" ]
C
The if-condition is true for all values of x &gt;= 0; the if-else condition is true for all values of x &lt; 0. So that doesn't leave any possible values for the else-clause.
canterbury_cs
canterbury_cs_0000512
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 x will <code>4</code> be among the values printed?
[ "x &lt; 0", "x &gt;= 0", "x &gt;= 20", "All values of x" ]
D
The final <code>println</code> statement is outside the conditional, so it is printed whatever the value of <code>x</code> is.&nbsp;
canterbury_cs
canterbury_cs_0000513
After the assignments <code>a = true</code> and <code>b = true</code>, what is returned by<br><code>(! a || b) &amp;&amp; (a || ! b)</code> ?
[ "true", "false", "&nbsp;1", "0" ]
A
Substituting the assigned values for a and b (both true) &nbsp;into the original expression &nbsp; &nbsp;(! a || b) &amp;&amp; (a || ! b) we get &nbsp; &nbsp;(! true || true) &amp;&amp; (true || ! true) In Java, evaluating what's inside the parentheses has higher precedence than any of the other operators here. Inside the parentheses, logical not (!) takes precedence over logical or (||). Evaluating the not's first, we get &nbsp; &nbsp;(false || true) &nbsp;&amp;&amp; (true || false)&nbsp; Next, evaluate the or's, and we get: &nbsp; &nbsp;true &amp;&amp; true which gives us &nbsp; &nbsp;true
canterbury_cs
canterbury_cs_0000514
What is the value of the expression <code>"J R R Tolkien".compareTo("J K Rowling") &lt; 0</code>
[ "True", "False", "\"J R R Tolkien\"", "An error." ]
B
The <code>compareTo</code>&nbsp;method when called on a <code>String</code> object with a <code>String</code> parameter, returns <code>true</code> if the first <code>String</code> is before the second in alphabetical order. Otherwise it returns <code>false</code>.&nbsp;
canterbury_cs
canterbury_cs_0000515
What is the value of the expression <code> "J".compareTo("J K Rowling") &lt; 0</code>
[ "true", "false", "\"J\"", "an error" ]
A
The <code>compareTo</code> method when called on a <code>String</code> object with a <code>String</code> parameter, returns<code> true</code> if the first <code>String</code> is before the second in alphabetical order. Otherwise it returns<code> false</code>.
canterbury_cs
canterbury_cs_0000516
After the assignment statement<br><code>&nbsp; &nbsp;String word = "entropy";</code><br>what is returned by<br><code>&nbsp; &nbsp;word.substring(2);</code> <span style="font-family: arial, helvetica, sans-serif;">?</span>
[ "\"en\"", "\"tropy\"", "\"entropy\"", "An error" ]
B
<code>word.substring(n)</code> returns the substring of <code>word</code> that starts at index n and ends at the end of the <code>String</code>.
canterbury_cs
canterbury_cs_0000517
After the assignment statement<br><code>&nbsp; &nbsp;String word = "entropy";</code><br>what is returned by<br><code>&nbsp; &nbsp;word.substring(-1);</code>
[ "\"e\"", "\"entropy\"", "the empty String", "an error" ]
D
When this code is executed, it throws a <code>StringIndexOutOfBounds</code> exception.&nbsp;
canterbury_cs
canterbury_cs_0000518
Given the input "Click &amp; Clack", what is the output of line 17 of the following Java code? <pre lang="text/x-java">System.out.println("Enter a string: "); String input = kbd.nextLine(); String a = ""; String letter = ""; int d = 0; int r = 1; String englishAlphabet = "abcdefghijklmnopqrstuvwxyz"; for (int i = 0; i &lt; englishAlphabet.length(); i++) { letter = englishAlphabet.substring(i,i+1); if (input.contains(letter)){ a = letter + a; } else { d++; } } System.out.println("String a is: " + a); //line 16 System.out.println("int d is: " + d); // line 17 </pre>
[ "16", "18", "21", "22" ]
C
d contains a count of the number of lower-case letters that do *not* appear in the input string. Since there are 5 letters that do appear, the answer is 26-5 or 21.&nbsp;
canterbury_cs
canterbury_cs_0000519
Fill in the gap to create a correct program. public static void main(String [] args){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; final int firstDim = 2; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; final int secondDim = 3; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; read(firstDim, secondDim); } public static __________ read (int dim1, int dim2){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int [][] array = new int[dim1][dim2]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Scanner sc = new Scanner(System.in); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; dim1; i++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int j = 0; j &lt; dim2; j++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; array[i][j] = sc.nextInt(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return array; }
[ "void", "int", "int []", "int[][]" ]
D
int []
canterbury_cs
canterbury_cs_0000520
Suppose you have a Java array of <code>int</code>s. Which of the following operations can be performed in constant (O(1)) time? (Circle all correct answers.)
[ "&nbsp;Insert a number at a given position.", "Retrieve a number from a given position", "Print out the numbers in the array", "Compute the sum of all the numbers in the array" ]
B
<strong>Note:&nbsp;</strong>Only one answer can be marked as correct here, but both A and B are correct. <strong>Note 2:&nbsp;</strong>We need a new tag &nbsp;SkillAnalyze-Code for this kind of thing. C is incorrect because printing out all the numbers takes O(n) time.<br>D is incorrect because computing the sum of the numbers also takes O(n) time.<br>E is incorrect because while hashtables come close on average, in the worst case, sorting is not O(1).<br>A is correct (assuming you don't have to move any of the numbers that are already in the array).<br>B is correct.
canterbury_cs
canterbury_cs_0000521
Which response best explains in plain English what this segment of code does? int a = 10; int b = 8; int c = 0; c = (a + b)/2
[ "Calculates half the sum of two numbers", "Calculates the average of two numbers", "Swaps the values of a and b", "demonstrates the use of the assignment statement" ]
B
Converts an integer value to double
canterbury_cs
canterbury_cs_0000522
What is wrong with this code? interface A{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; abstract double aMethod(); } interface B{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; abstract int aMethod(); } class testInterface implements A, B{ }
[ "A class cannot implement more than one interface.", "Both the interfaces have a method with the same name and different return type.", "Methods defined in an interface should not be abstract.", "aMethod should be overridden in testInterface class." ]
B
Methods defined in an interface should not be abstract.
canterbury_cs
canterbury_cs_0000523
Which one of the options cannot be a choice to override the aMethod in class testInterface? interface A{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; abstract Object aMethod(); } interface B{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; abstract Object aMethod(); } class testInterface implements A, B{ }
[ "public String aMethod(){ return \"\";}", "public Object aMethod(){ return null;}", "public Double aMethod(){ return 0.0;}", "public int aMethod(){ return 0;}" ]
D
public Double aMethod(){ return 0.0;}
canterbury_cs
canterbury_cs_0000524
Which statement produces a compilation error?
[ "class A extends M implements I {// code was removed} \n class B extends N implements I {// code was removed}", "class A extends M implements I, L, J {// code was removed}", "class A extends M, N implements I {// code was removed}", "class A extends M implements I {// code was removed}" ]
C
class A extends M, N implements I {// code was removed}
canterbury_cs
canterbury_cs_0000525
Which of the following choices cannot be another constructor for academic class? class personnel{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String name, ID; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char qualificationCode; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public personnel(String n, String i, char q){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name = n; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; qualificationCode = q; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public personnel (){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name = null; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; qualificationCode = ' '; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } } class academic extends personnel{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int teachingHours; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public academic(String n, String i, char q, int t){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; super(n,i,q); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; teachingHours = t; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public academic(int t){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; super(null, null, ' '); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; teachingHours = t; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } }
[ "public academic(){ \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; super(null, null, ' '); \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this (0); \n }", "public academic(){ \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this (null, null, ' ', 0); \n }", "public academic(){ \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name = null; \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;\n \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; qualificationCode = ' '; \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; teachingHours = 0; \n }", "public academic(){ \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; super(null, null, ' '); \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; teachingHours = 0; \n }" ]
A
public academic(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name = null; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; qualificationCode = ' '; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; teachingHours = 0; }
canterbury_cs
canterbury_cs_0000526
Where in this code a compiler error is reported and why? 1&nbsp; class pen{ 2 &nbsp; &nbsp; &nbsp; &nbsp; char colorCode; 3&nbsp; } 4&nbsp; public class penCounter { 5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static void main(String[] arg){ 6 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; int numberOfPen; 7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pen myPen = new pen(); 8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(myPen.colorCode + numberOfPen); 9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } 10}
[ "line 8, numberOfPen has not been initialized.", "line 8, colorCode has not been initialized.", "line 6, numberOfPen has not been initialized.", "line 2, colorCode has not been initialized." ]
A
line 6, numberOfPen has not been initialized.
canterbury_cs
canterbury_cs_0000527
What will be the outputted? class A{ &nbsp;&nbsp;&nbsp;&nbsp; int firstMethod(int input){ &nbsp;&nbsp;&nbsp;&nbsp; return input*2; &nbsp;&nbsp;&nbsp;&nbsp; } } class B extends A{ &nbsp;&nbsp;&nbsp;&nbsp; int firstMethod(int input){ &nbsp;&nbsp;&nbsp;&nbsp; super.firstMethod(input); &nbsp;&nbsp;&nbsp;&nbsp; return input*2; &nbsp;&nbsp;&nbsp; } } class C extends B{ &nbsp;&nbsp;&nbsp; int firstMethod(int input){ &nbsp;&nbsp;&nbsp; return super.firstMethod(input)* 2; &nbsp;&nbsp;&nbsp; } } public class test { &nbsp;&nbsp;&nbsp; public static void main(String[] arg){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; C myObject = new C(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(myObject.firstMethod(2)); &nbsp;&nbsp; } }
[ "4", "8", "16", "32" ]
B
16
canterbury_cs
canterbury_cs_0000528
What will be outputted? class A{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int firstMethod(int input){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return input+2; &nbsp;&nbsp;&nbsp;&nbsp; } } class B extends A{ } class C extends B{ &nbsp;&nbsp;&nbsp;&nbsp; int firstMethod(int input){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return input-2; &nbsp;&nbsp;&nbsp;&nbsp; } } public class test { &nbsp;&nbsp;&nbsp;&nbsp; public static void main(String[] arg){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; B myObject = new B(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(myObject.firstMethod(2)); &nbsp;&nbsp;&nbsp;&nbsp; } }
[ "0", "2", "4", "Compiler Error" ]
C
4
canterbury_cs
canterbury_cs_0000529
Which sentence is NOT correct?
[ "If a class has no constructor, it cannot be extended.", "If a class has only private constructors, it cannot be extended.", "If a class is final, it cannot be extended.", "If a class is public, it is extendable anywhere." ]
A
If a class is final, it cannot be extended.
canterbury_cs
canterbury_cs_0000530
Which part of the following code will produce a compiler error if we know class cat extends a class called animal and both of the classes have a method called makeNoise and class cat has a method called showFood. 1&nbsp;&nbsp;&nbsp;&nbsp; animal mydog = new animal(); 2&nbsp;&nbsp;&nbsp;&nbsp; mydog.makeNoise(); 3&nbsp;&nbsp;&nbsp;&nbsp; animal mycat = new cat(); 4 &nbsp; &nbsp; mycat.makeNoise(); 5&nbsp;&nbsp;&nbsp;&nbsp; mycat.showFood();
[ "line 3, new cat should be changed to new animal.", "line 3, animal should be changed to cat.", "line 4, makeNoise has not been recognized by mycat", "line 5, showFood has not been recognized by mycat" ]
D
line 4, makeNoise has not been recognized by mycat
canterbury_cs
canterbury_cs_0000531
Which on these four following definitions is not allowed? abstract class first{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void firstMethod(){} } abstract class second{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; abstract void secondMethod(); } class third { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; abstract void thirdMethod(); } class fourth{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void fourthMethod(){} }
[ "first", "second", "third", "fourth" ]
C
third
canterbury_cs
canterbury_cs_0000532
Which option is NOT an alternative solution for the bug that exists in this code? class shape{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; float area; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public shape( float a){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; area = a; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } } class square extends shape{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; float side; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public square (float s){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; side = s; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } }
[ "<em>square</em> constructor should call a super constructor explicitly.", "Class <em>shape</em> must have a null constructor.", "class <em>square</em> should have a null constructor.", "shape constructor should be removed." ]
C
class <em>square</em> should have a null constructor.
canterbury_cs
canterbury_cs_0000533
Considering the following code, which of the choices are wrong when access to ID is desired? class N{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private int ID; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void setID(int id){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int getID(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return ID; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } }
[ "if we had the following in class N \n N n = new N(); \n System.out.print(n.ID);", "If we had the following in another class but the same package as N \n N n = new N(); \n System.out.print(n.ID);", "If we had the following in class N \n System.out.print(ID);", "If we had the following in another class but the same package as N \n N n = new N(); \n System.out.print(n.getID());" ]
B
If we had the following in class N System.out.print(ID);
canterbury_cs
canterbury_cs_0000534
What will be printed? class A{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected void A_Method(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println ("This is the first A_Method"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } } class B extends A{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected void A_Method(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print ("This is the second A_Method"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } } class C extends B{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected void A_Method(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print ("This is the third A_Method"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } } public class test { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static void main(String[] args){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; A [] objects = new A[3]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; objects[0]= new A(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; objects[1]= new B(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; objects[2]= new C(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; objects[1].A_Method(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } }
[ "This is the first A_Method", "This is the second A_Method", "This is the third A_Method", "Nothing, this is an error." ]
B
Nothing, this is an error.
canterbury_cs
canterbury_cs_0000535
<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; mso-themecolor: text1;">What is wrong with this code?</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'; color: black; mso-themecolor: text1;">final class A{</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'; color: black; mso-themecolor: text1;">}</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'; color: black; mso-themecolor: text1;">class B extends A{</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'; color: black; mso-themecolor: text1;">}</span>
[ "Class B is not public.", "Class A is not public.", "A final class cannot be extended.", "There are no instance variables and methods defined for these classes." ]
C
A final class cannot be extended.
canterbury_cs
canterbury_cs_0000536
How many object references will be created after initializing the following array? String [][] names = new String [3][2];
[ "3", "2", "6", "7" ]
D
6
canterbury_cs
canterbury_cs_0000537
What would be outputted? String s_1 = "Hello"; String s_2 = "World"; System.out.format("%-7S %7s", s_1,s_2);
[ "Hello &nbsp; &nbsp; &nbsp; &nbsp; World", "HELLO&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; World", "&nbsp; &nbsp;&nbsp; Hello World", "&nbsp;&nbsp;&nbsp;&nbsp; HELLO World" ]
B
&nbsp; &nbsp;&nbsp; Hello World
canterbury_cs
canterbury_cs_0000538
How many times the capacity of the vector in following code changes? Vector&lt;Integer&gt; intVect = new Vector&lt;Integer&gt;(10,2); for (int i = 0; i &lt;40; i++) &nbsp;&nbsp;&nbsp;&nbsp; intVect.add(i);
[ "2", "5", "15", "30" ]
C
15
canterbury_cs
canterbury_cs_0000539
In a program 5 objects are created initially and inserted into a vector. These objects increase to 64 during the execution of the program. Each time 8 objects is added to the vector except the last time in which 3 objects is added. Which of the following definition results in better performance in terms of execution time and allocated space at the end?
[ "Vector&lt;Object&gt; objectVect = new Vector&lt;Object&gt;(5,8);", "Vector&lt;Object&gt; objectVect = new Vector&lt;Object&gt;(8);", "Vector&lt;Object&gt; objectVect = new Vector&lt;Object&gt;(8,5);", "Vector&lt;Object&gt; objectVect = new Vector&lt;Object&gt;(5);" ]
B
Vector&lt;Object&gt; objectVect = new Vector&lt;Object&gt;(8,5);
canterbury_cs
canterbury_cs_0000540
Which of the following variables are object references? public class firstClass { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; double[] doubleArray = {2.3, 3.4}; &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; int width = 250; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Color col = new Color(88,34,200); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } }
[ "doubleArray, col", "width, doubleArray", "col, width", "col, width, doubleArray" ]
A
col, width
canterbury_cs
canterbury_cs_0000541
What change should be made to correct the code? String returnValue = ""; try { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BufferedReader d = new BufferedReader(new InputStreamReader(System.in)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String userInput = new String(d.readLine()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; returnValue = userInput; }
[ "Need to insert <em>finally</em>.", "Need to insert <em>catch</em>.", "Need to insert <em>finally</em> or <em>catch.</em>", "Need to remove <em>try</em>." ]
C
Need to insert <em>finally</em> or <em>catch.</em>
canterbury_cs
canterbury_cs_0000542
What would you put in the blank areas to let the following code read some integer data from console? Scanner sc = new Scanner(_______________); int intValue ; while ((intValue = _______________)!= -1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(intValue);
[ "System.in , sc.nextInt()", "nothing, leave it blank , sc.nextInt()", "nothing, leave it blank , sc.hasNextInt()", "System.in , sc.hasNextInt()" ]
A
nothing, leave it blank , sc.hasNextInt()
canterbury_cs
canterbury_cs_0000543
What will be printed by the following code if in.txt includes two sentences which have been inserted in two separate lines? try { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inputStream = new FileReader("in.txt"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int c; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ((c = inputStream.read()) != -1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println((char)c); } finally { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (inputStream != null) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inputStream.close(); }
[ "A sequence of integers that represents the unicode of characters that form the text.", "A sequence of characters that form the text.", "The first line in in.txt.", "The whole text in in.txt." ]
B
The first line in in.txt.
canterbury_cs
canterbury_cs_0000544
What will be outputted? String input = "Home&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; is &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where the heart is"; Scanner sc_input = new Scanner(input).useDelimiter("\\s*is\\s*"); while (sc_input.hasNext()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(sc_input.next());
[ "Home \n is \n where \n the \n heart \n is", "Home \n is \n Where the heart \n is", "Home \n where \n the \n heart", "Home \n where the heart" ]
D
Home where the heart
canterbury_cs
canterbury_cs_0000545
What will be the output? class Pen{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; float hight; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Pen(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hight = 0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Pen ( float h){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hight = h; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } } class Pencil extends Pen{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String type; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Pencil(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type = null; &nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp; public Pencil (String t){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type = t; &nbsp;&nbsp;&nbsp;&nbsp; } } public class test { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static void main(String[] arg){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Pen mypen = new Pen(10); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print(mypen.hight+ " "); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Pencil mypencil = new Pencil("HB"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(mypencil.hight + " "+ mypencil.type); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } }
[ "10.0 0.0 HB", "10.0 10.0 HB", "A compiler error", "A runtime error" ]
A
A compiler error
canterbury_cs
canterbury_cs_0000546
What will be the output? class Pen{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; float hight; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Pen ( float h){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hight = h; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } } class Pencil extends Pen{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String type; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Pencil(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type = null; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Pencil (String t){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type = t; &nbsp;&nbsp;&nbsp;&nbsp; } } public class test { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static void main(String[] arg){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Pen mypen = new Pen(10); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print(mypen.hight+ " "); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Pencil mypencil = new Pencil("HB"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(mypencil.hight + " "+ mypencil.type); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } }
[ "10.0 0.0 HB", "10.0 10.0 HB", "A compiler error", "A runtime error" ]
C
A compiler error
canterbury_cs
canterbury_cs_0000547
Which option is NOT a correct solution to handle an exception if we know that NumberException is a user defined exception?
[ "public static int testNumber(int x) throws NumberException{ \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (x &gt;= 12) \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new NumberException(\"This is my created exception message\"); \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return x; \n }", "public static int testNumber(int x) throws NumberException{ \n &nbsp;&nbsp;&nbsp; try{ \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (x &gt;= 12) \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new NumberException(\"This is my created exception message\"); \n &nbsp;&nbsp; } \n &nbsp;&nbsp; finally{} \n &nbsp;&nbsp; return x; \n }", "public static int testNumber(int x) throws NumberException{ \n &nbsp;&nbsp;&nbsp; try{ \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (x &gt;= 12) new NumberException(); \n &nbsp;&nbsp;&nbsp; } \n &nbsp;&nbsp;&nbsp; catch (NumberException e){ \n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); \n &nbsp;&nbsp;&nbsp; } \n &nbsp;&nbsp;&nbsp; finally{} \n &nbsp;&nbsp;&nbsp; return x; \n }", "public static int testNumber(int x) { \n &nbsp;&nbsp;&nbsp;&nbsp; try{ \n &nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; if (x &gt;= 12) throw new NumberException(); \n &nbsp;&nbsp;&nbsp; } \n &nbsp;&nbsp;&nbsp; catch (NumberException e){ \n &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; e.printStackTrace(); \n &nbsp;&nbsp; } \n &nbsp;&nbsp; finally{} \n &nbsp;&nbsp; return x; \n }" ]
C
public static int testNumber(int x) throws NumberException{ &nbsp;&nbsp;&nbsp; try{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (x &gt;= 12) new NumberException(); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; catch (NumberException e){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace(); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; finally{} &nbsp;&nbsp;&nbsp; return x; }
canterbury_cs
canterbury_cs_0000548
2. Consider the following class definition: <pre lang="text/x-java">public class SillyTestClass { public SillyTestClass(int x, int y) { System.out.println(y); } public SillyTestClass(String string1, String string2) { System.out.println(string2); } public static void main (String [ ] args) { SillyTestClass app = new SillyTestClass(20, &ldquo;Try this!&rdquo;); } } </pre> Which of the following is the most accurate statement about this code?
[ "The class definition won't compile, because it has two constructors.", "The class definition won't compile, because two constructors have the same number of parameters.", "The class definition won't compile, because the actual and formal parameter types don't match.", "It will compile, and the output when the <code>main</code> method is executed will be: 20" ]
C
Answer A is wrong because Java programs can have more than one constructor. Answer B is wrong because a variation between the type of the parameters is also sufficient. Answers D and E are wrong because the program won't compile or execute. Answer C identifies the problem.
canterbury_cs
canterbury_cs_0000549
The code fragment given above was intended to read values until a negative&nbsp;value was read and then to print the product of the positive values read. Unfortunately, it does not work. <pre lang="text/x-java">1. Scanner kbd = new Scanner(System.in); 2. int x, product; 3. product = 0; 4. x = kbd.nextInt(); 5. while (x &gt;= 0) { 6. if (x &gt; 0) { 7. product *= x; 8. } 9. x = kbd.nextInt(); 10. } 11. System.out.println(product); </pre> &nbsp; Which of the following best describes the error that prevents the code from computing the correct answer?
[ "&nbsp;Variable <code>x</code> is not initialized correctly.", "Variable <code>product</code> is not initialized correctly.", "The loop is executed one too many times.", "The loop is executed one two few times." ]
B
Because the variable <code>product</code> is initialized to 0 instead of 1, the answer will always be 0.&nbsp;
canterbury_cs
canterbury_cs_0000550
Consider the following code: <pre lang="text/x-java"> public int examMethod(int n) { if (n == 1) return 1; else if (n &gt; 1) return (n + this.examMethod(n-1)); } </pre> &nbsp; What is the purpose of <code>examMethod</code>?
[ "to compute fibonacci(n)", "to compute factorial(n)", "to compute the sum of the positive integers from 1 to n", "none of the above" ]
C
The method returns 1 if n is 1, 2+1 if n is 2, 3+2+1 if n is 3, etc. In other words, it computes the sum of the integers from 1 to n (answer C).
canterbury_cs
canterbury_cs_0000551
Consider the following 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; Which of the following inputs will cause a non-terminating recursion?
[ "0", "1", "20", "30,000" ]
A
The base case for this recursion is n == 1. If n is 1, the recursion is done. If n is 20, then the value of n will be reduced by 1 with each recursive call (examMethod(19), examMethod(18), etc.), the value of n will finally reach 1, and the recursion will end. Similarly if n is 30,000. But if n is 0 to begin with, then the next recursive call will be to examMethod(-1), then examMethod(-2), etc. The value of n will never reach the base case, and the method will (in theory) never terminate.&nbsp;
canterbury_cs
canterbury_cs_0000552
Consider the following Java interface definition: public interface Mover {<br>&nbsp; &nbsp;public int getX();<br>&nbsp; &nbsp;public int getY();<br>&nbsp; &nbsp;public void setLocation(int x, int y);<br>} Which of the following is a correct implementation of the Mover interface?
[ "<pre lang=\"text/x-java\">public class CartoonCharacter implements Mover{\n private int x, y;\n public int getX;\n public int getY;\n public void setLocation;\n</pre>\n <code>}</code>", "<pre lang=\"text/x-java\">public class CartoonCharacter implements Mover{\n private int x, y;\n public int getX();\n public int getY();\n public void setLocation(int x, int y);\n</pre>\n <code>}</code>", "<pre lang=\"text/x-java\">public class CartoonCharacter implements Mover{\n private int x, y;\n public int getX() {\n // code for method body\n }\n public int getY() {\n // code for method body\n }\n</pre>\n <code>}</code>", "<pre lang=\"text/x-java\">public class CartoonCharacter implements Mover{\n private int x, y;\n public int getX() { \n // code for method body\n }\n public int getY() {\n // code for method body\n }\n public void setLocation(int x, int y){\n // code for method body\n }\n</pre>\n <code>}</code>" ]
D
Choice A is wrong because it doesn't include parameter lists or implementations of any of the methods required by the interface. Choice B is wrong because it doesn't include implementations of the methods on the list.&nbsp; Choice C is wrong because it implements some but not all of the interface methods.&nbsp; Choice E is wrong because the method signatures do not match those in the interface. Choice D is the correct answer, because it's the only one where the required methods are all implemented, and their signatures match&nbsp; <strong>NOTE:</strong>&nbsp;There is no appropriate topic for this question in the list. Suggestion: TopicSimon-interfaces-Java
canterbury_cs
canterbury_cs_0000553
Consider the following Java interface: <pre lang="text/x-java">public interface Mover { public int getX(); public int getY(); public void setLocation(int x, int y); } </pre> &nbsp; Choose the best description of the following implementation of the Mover interface: <pre lang="text/x-java">public class CartoonCharacter implements Mover{ public int getX; public int getY; public void setLocation; } </pre>
[ "The implementation is correct, because it includes all the required methods and their return types are correct.", "The implementation is incorrect, because it doesn't include the method parameter types.", "The implementation is incorrect, because it doesn't include implementations of the methods.", "Both B and C." ]
D
To implement a Java interface, a class must define all the methods required by the interface (or declare itself abstract). <strong>NOTE:&nbsp;</strong>There is no appropriate topic for this question. Suggestion: TopicSimon-Interface-Java.&nbsp;
canterbury_cs
canterbury_cs_0000554
1. What is the value of the following Java arithmetic expression? &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<code> 4 * 3 + 6 / 4</code>
[ "4", "4.5", "13", "9" ]
C
This question addresses two points: first, operator precedence (multiplication and division are both done before addition) and second, integer division. Answer A is wrong about operator precedence: the answer you get if you apply the operators in order from left to right. Answer B makes the same mistake and is also wrong about integer division. Answer D is the answer you get if you assume that addition has a higher precedence than the other two operations. Answer E gets the operator precedence right, but is wrong about integer division. Answer C is the only one that has both right.
canterbury_cs
canterbury_cs_0000555
What is the <em>maximum</em> result of computing X % 7, where all we know about X is that it is a positive integer?
[ "0", "1", "6", "7" ]
C
There is not enough information in the question description to answer.
canterbury_cs
canterbury_cs_0000556
Which sentence is not correct regarding exception handling in java?
[ "A method can throw more than one exception.", "You can have several catch statement for one try.", "Statements inside finally run if no exception happens.", "Statements inside catch are never run unless an exception happens." ]
C
Statements inside finally run if no exception happens.
canterbury_cs
canterbury_cs_0000557
What will be outputted? int c = 1; int result = 10; result += ++c; System.out.print(result+ " "+ c);
[ "12&nbsp; 2", "11&nbsp; 2", "12&nbsp; 1", "11&nbsp; 1" ]
A
12&nbsp; 1
canterbury_cs
canterbury_cs_0000558
Which of these following codes result the same? 1 if (mark =='A'){ &nbsp;&nbsp;&nbsp; if (GPA &gt; 3.5) &nbsp;&nbsp;&nbsp; x = 1; } else &nbsp;&nbsp;&nbsp; x = 2; &nbsp; 2 if (mark =='A') &nbsp;&nbsp;&nbsp; if (GPA &gt; 3.5) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x = 1; &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x = 2; &nbsp; 3 if (mark =='A'){ &nbsp;&nbsp;&nbsp; if (GPA &gt; 3.5) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x = 1; &nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x = 2; }
[ "1,2", "2,3", "1,3", "1,2,3" ]
B
1,3
canterbury_cs
canterbury_cs_0000559
What will be outputted? int num = 3; int counter = 1; boolean condition = true; while(condition){ &nbsp;&nbsp;&nbsp; num+= counter++; &nbsp;&nbsp;&nbsp; if(num&gt;10){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; condition=false; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; num+= ++counter; &nbsp;&nbsp;&nbsp; } }
[ "counter = 5 num = 16", "counter = 5 num = 17", "counter = 6 num = 18", "counter = 6 num = 19" ]
D
counter = 6 num = 18
canterbury_cs
canterbury_cs_0000560
What will be outputted? int income = 30; boolean condition1 = true, condition2 = true; if(income &lt; 100) &nbsp;&nbsp;&nbsp; if(income &gt; 10) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(condition1){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print("A"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(income &lt; 20) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print("B"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print("C"); &nbsp;&nbsp;&nbsp; if(!condition2){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(income &gt; 50) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print("D"); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print("E");
[ "AE", "AC", "ABC", "ACE" ]
A
ABC
canterbury_cs
canterbury_cs_0000561
Fill the gap in such a way that the odd number less than 10 and greater than zero is printed. for (_________________________) &nbsp;&nbsp;&nbsp; System.out.println(i+1);
[ "int i = 0; i &lt;= 10; i= i+2", "int i = 0; i &lt; 10; i= i+2", "int i = 1; i &lt; 10; i= i+2", "int i = 1; i &lt;= 10; i= i+2" ]
B
int i = 1; i &lt; 10; i= i+2
canterbury_cs
canterbury_cs_0000562
What would be outputted? char initial = 'a'; switch (initial){ case 'a': &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print("A"); 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"); }
[ "ABC", "AB", "BC", "A" ]
A
BC
canterbury_cs
canterbury_cs_0000563
What will be outputted? char initial = 'a'; switch (initial){ &nbsp; &nbsp; case 'a': &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print("A"); &nbsp;&nbsp;&nbsp; default: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print("C"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; &nbsp;&nbsp;&nbsp;&nbsp; case 'b': &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print("B"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; }
[ "compiler error", "A", "AC", "ACB" ]
C
AC
canterbury_cs
canterbury_cs_0000564
What would be the value of sum at the end of executing this code? int sum; for (int i = 0; i &lt; 2; i++) &nbsp;&nbsp;&nbsp; for (int j = 0; j &lt; 5; j++ ){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sum = i + j; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (sum &gt; 5) break; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else continue; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print(sum); }
[ "Compiler error due to having unreachable code.", "Compiler error due to not initializing local variable.", "6", "5" ]
A
6
canterbury_cs
canterbury_cs_0000565
To compute the following series, the following code will do the job. What will be the initial value of stat, fact and sum respectively? -X + X3/3! - X5/5! + ... for ( int i = 2; i &lt;= n; i++){ &nbsp;&nbsp;&nbsp; for (int j = 2*i -1; j &gt; 2*i -3 ; j--){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stat *=x; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fact*= j; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; p_f *= -1; &nbsp;&nbsp;&nbsp; sum += p_f* stat/fact;
[ "1, x, 0", "x, 1, 0", "&nbsp;-x, 1, x", "x,1,-x" ]
D
&nbsp;-x, 1, x
canterbury_cs
canterbury_cs_0000566
Consider the following static method in Java: <pre lang="text/x-java">public static int[] mystery(int[] arr, int x) { int a=0; for (int i=0; i&lt;arr.length; i++) { if (arr[i] &gt; x) { a++; } } int[] ar2=new int[a]; int z=0; for (int i=0; i&lt;arr.length; i++) { if (arr[i] &gt; x) { ar2[z]=arr[i]; z++; } } return ar2; } </pre> &nbsp; <span style="font-family: arial,helvetica,sans-serif;">What will this function (static method) return when invoke with the array {1, 9, 3, 4, 9, 4, 5, 2, 7} and the integer 5?</span>
[ "{} (an empty array)", "{9, 5, 7}", "{9, 9, 5, 7}", "{9, 9, 7}" ]
D
It will throw ArrayIndesOutOfBoundsException
canterbury_cs
canterbury_cs_0000567
Consider the following interface definition: <pre lang="text/x-java">public interface Mover { public int getX(); public int getY(); public void setLocation(int x, int y); } </pre> &nbsp; <span style="font-family: arial, helvetica, sans-serif;">Choose the answer that best describes the following implementation of the <code>Mover</code> interface:<br></span> <code><code><code>public class CartoonCharacter implements Mover {</code></code></code> <code><code><code>&nbsp; &nbsp;public int getX();</code></code></code> <code><code><code>&nbsp; &nbsp;public int getY();</code></code></code> <code><code><code>&nbsp; &nbsp;public void setLocation(int x, int y);</code></code></code> <code><code><code>}</code></code></code>
[ "The implementation is correct, because it includes all the methods.", "The implementation is correct, because the method names, return types, and parameter lists match the interface.", "The implementation is incorrect, because the method bodies are not included.", "Both A and B.&nbsp;" ]
C
A class that implements a Java interface must define all the methods required by the interface (or declare itself abstract). <strong>NOTE:&nbsp;</strong>There is no appropriate topic for this question. Suggestion: TopicSimon-Interface-Java.
canterbury_cs
canterbury_cs_0000568
Consider the following code: <pre lang="text/x-python">if x &gt;= 0: print (1) elif x &lt; 20: print(2) else: print(3) print(4) </pre> &nbsp; For what values of x will 2 be among the values printed?
[ "<code>x &lt; 0</code>", "<code>x &gt;= 0</code>", "<code>x &lt; 20</code>", "All values of <code>x</code>" ]
A
Although the condition of the elif is x&lt;20, all values between 0 and 20 are subsumed (i.e. covered by) the first condition, x&gt;=0.&nbsp; So only values of x strictly less than 0 will result in 2 being printed.
canterbury_cs
canterbury_cs_0000569
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() || getFalse(); } } </pre>
[ "T", "FF", "F", "FT" ]
B
|| evaluates the left operand first. It's not true, so it checks the second.
canterbury_cs
canterbury_cs_0000570
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) { getTrue() &amp;&amp; getTrue(); } } </pre>
[ "TT", "F", "T", "FT" ]
A
The first operand is true. &amp;&amp; requires both be true, so we check the second, which is also true.
canterbury_cs
canterbury_cs_0000571
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) { getTrue() &amp;&amp; getFalse(); } } </pre>
[ "T", "TT", "TF", "F" ]
C
The first operand is true. &amp;&amp; requires both operands be true, so we evaluate the second, which is false.
canterbury_cs
canterbury_cs_0000572
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; getTrue(); } } </pre>
[ "FT", "T", "F", "FF" ]
C
The first operand is false. &amp;&amp; requires both be true, so we don't bother to evaluate the second.
canterbury_cs
canterbury_cs_0000573
Consider the code int i = 3; int *p = &amp;i; Which of the following will print out &ldquo;The value is 3.&rdquo;?
[ "printf(\"The value is %p\", p);", "printf(\"The value is %d\", *p);", "printf(\"The value is %d\", &amp;i);", "printf(\"The value is %d\", &amp;p);" ]
B
The value of p will be the memory address of i -- to dereference p, we need to use *p.
canterbury_cs
canterbury_cs_0000574
Which of the following lines of code will correctly read in the two floats x and y?
[ "scanf(\"%d%d\", &amp;x, &amp;y);", "scanf(\"&amp;d&amp;d\", %x, %y);", "scanf(\"%f%f\", &amp;x, &amp;y);", "scanf(\"&amp;f&amp;f\", %x, %y);" ]
C
Scanf requires pointers to x and y -- hence the ampersands. As x and y are floats, %f is used for both.
canterbury_cs
canterbury_cs_0000575
Kejun has the executable add that adds three command-line args together and prints the<br>result. He runs it like so:<br><code>./add 25 10 5</code> <code>the sum is 40</code> <br>In his code, what is the value of argc?
[ "2", "4", "1", "3" ]
B
./add is considered a command-line argument
canterbury_cs
canterbury_cs_0000576
After the assignment <code>signal = &rsquo;abracadabra&rsquo;</code>, what is returned by <code>signal[:5]</code>?
[ "&rsquo;abraca&rsquo;", "&rsquo;abrac&rsquo;", "&rsquo;c&rsquo;", "An error" ]
B
This is a slice operation.&nbsp; Slices in Python work with both lists and with Strings, and go up to but not including the limit.&nbsp; Since the limit is 5, we get the first 5 characters (indexed 0 through 4).&nbsp; Hence abrac
canterbury_cs
canterbury_cs_0000577
After the assignment <code>signal = &rsquo;abracadabra&rsquo;</code>, what is returned by <code>signal[-1:]</code>?
[ "'a'", "'abracadabra'", "''", "an error" ]
A
Python actually allows negative indexes, which start counting from the back of a list or String.&nbsp; However, the slice operation by default still only uses increasing indexes.&nbsp; So, slicing [-1:] means to slice from the last character through the rest of the String (slice operations that leave out the upper boundary implicitly mean "slice to the end").
canterbury_cs
canterbury_cs_0000578
What is returned by <code>&rsquo;A A Milne&rsquo; &lt; &rsquo;A Linkletter&rsquo;</code>?
[ "True", "False", "'A A Milne'", "an error" ]
A
This is a lexicographic (i.e. alphabetic) comparison.&nbsp; The first 2 characters of both strings are the same (i.e. 'A '), but for the third character, 'A' comes before 'L' so we return True.
canterbury_cs
canterbury_cs_0000579
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() || getTrue(); } } </pre>
[ "TF", "T", "F", "FT" ]
D
|| guarantees left-to-right evaluation, stopping at the first operand that is true.
canterbury_cs
canterbury_cs_0000580
Many English words have separate singular and plural forms, e.g., "dog" and "dogs." We are trying to write a method that properly pluralizes a word (though naively, by only adding an "s" at the end) if the count we have of that word is not 1. If the count is 1, we leave the word as is. Which of the following proposed solutions does not meet this specification?
[ "<pre lang=\"text/x-java\">String getCounted(String singular, int count) {\n if (count != 1) {\n return singular + \"s\";\n } else {\n return singular;\n }\n}\n</pre>", "<pre lang=\"text/x-java\">String getCounted(String singular, int count) {\n String quantified = singular;\n if (count != 1) {\n quantified = quantified + \"s\";\n }\n return quantified;\n}\n</pre>", "<pre lang=\"text/x-java\">String getCounted(String singular, int count) {\n if (count == 1) {\n return singular;\n } else {\n String plural = singular + \"s\";\n return plural;\n }\n}\n</pre>", "<pre lang=\"text/x-java\">String getCounted(String singular, int count) {\n String suffix = null;\n if (count != 1) {\n suffix = \"s\";\n }\n return singular + suffix;\n}\n</pre>" ]
D
Suffix is initialized to null. The concatenation of singular and suffix does not produce the desired behavior.
canterbury_cs
canterbury_cs_0000581
<span style="font-family: arial,helvetica,sans-serif;">Consider the following Python code:</span> <pre lang="text/x-python">if x &gt;= 0: print (1) elif x &lt; 20: print(2) else: print(3) print(4) </pre> &nbsp; For what integer values of <code>x</code> will 1 be among the values printed?
[ "<code>x &lt; 0</code>", "<code>x &gt;= 0</code>", "<code>x &gt;= 20</code>", "All values of <code>x</code>" ]
B
Clearly x must be greather than or equal to 0 to for 1 to be printed.
canterbury_cs
canterbury_cs_0000582
Consider the following Python code: <pre lang="text/x-python">if x &gt;= 0: print (1) elif x &lt; 20: print(2) else: print(3) print(4) </pre> &nbsp; For what integer values of x will 3 be amont the values printed?
[ "All values of x", "None of the above", "x &lt; 20", "x &gt;= 0" ]
B
It's not possible to get 3 to print.&nbsp; This is because all possible values of x are covered by if x&gt;=0 and x&lt;20.&nbsp; This creates a range that includes all possible values of x, since any number is either &gt;=0 or &lt; 20.
canterbury_cs
canterbury_cs_0000583
Consider the following Python code: if x &gt;= 0: &nbsp;&nbsp;&nbsp; print 1 elif x &lt; 20: &nbsp;&nbsp;&nbsp; print 2 else: &nbsp;&nbsp;&nbsp; print 3 print 4 For what values of x will 4 be among the values printed?
[ "x &lt; 0", "x &gt;= 0", "x &gt;= 20", "All values of x" ]
D
The statement that prints 4 is NOT part of the if expression; thus it will be preinted regardless of what happens with the loop
canterbury_cs
canterbury_cs_0000584
Converting a value from one type to another sometimes requires an explicit cast and sometimes does not. Which of the following conversions and lines of reasoning explains how to convert a double d to an int i?
[ "i = d. No explicit cast is necessary because if the conversion isn't valid, an exception is thrown.", "i = (int) d. An explicit cast is needed to round d to the nearest integer.", "i = d. No explicit cast is necessary because any int can be stored in a double.", "i = (int) d. An explicit cast is needed because information may be lost in the conversion." ]
D
Not all doubles can be stored as ints. You must sign off on the potential information loss with an explicit cast.
canterbury_cs
canterbury_cs_0000585
After the assignment <code>s = &rsquo;slam&rsquo;</code>, which of the following code fragments prints <code>scam</code>?
[ "s[1] = &rsquo;c&rsquo; \n print(s)", "s.replace(&rsquo;l&rsquo;, &rsquo;c&rsquo;) \n print(s)", "s = s[:s.find(&rsquo;l&rsquo;)] + &rsquo;c&rsquo; + s[s.find(&rsquo;l&rsquo;)+1:] \n print(s)", "All of the above" ]
C
A doesn't work because Strings cannot be changed.&nbsp; The [] operation can be used to read values inside a String, but not to change them. B doesn't work because the replace() function does not change the String, it returns a new String.&nbsp; This would work if it were s=s.replace('l', 'c'). C works, it's basically concatenating everything before the 'l' with a 'c' with everything after the 'l'.
canterbury_cs
canterbury_cs_0000586
In this question, you are given a Perl regular expression that you are required to evaluate. There are no leading or trailing spaces in any of the text, nor are there any spaces in the regex. Identify the answer which best matches the regex below: /20[0-3]+/
[ "205", "2003", "0230", "2300" ]
B
20 followed by one or more digits in the range 0 to 3.
canterbury_cs
canterbury_cs_0000587
In this question, you are given a Perl regular expression that you are required to evaluate. There are no leading or trailing spaces in any of the text, nor are there any spaces in the regex. Identify the answer which matches the regex below: /[A-Z][a-z]{2,4}day/
[ "Saturday", "tuesday", "Yesterday", "Today" ]
A
Must start with an upper case letter, be followed by from 2 to 4 lower case letters, and be followed by day.
canterbury_cs
canterbury_cs_0000588
In this question, you are given a Perl regular expression that you are required to evaluate. There are no leading or trailing spaces in any of the text, nor are there any spaces in the regex. Identify the answer which matches the regex below: /\d\d\d/
[ "1d34", "A123", "12", "12A12D" ]
B
Must contain 3 consecutive digits. It does not matter what comes before or after.
canterbury_cs
canterbury_cs_0000589
What are the merits of insertion sort compared to bubble sort and selection sort?
[ "It doesn't require as much extra storage.", "It copies elements only to their final locations.", "It requires less code.", "It is faster for already sorted data." ]
D
Neither selection nor bubble sort require extra storage. Selection sort doesn't make unnecessary copies. Bubble sort can be expressed in very little code. Any of them could be expressed recursively. If the array is already sorted, then insertion sort will only make N comparisons and no copies, giving it better performance than the other sorts.
canterbury_cs
canterbury_cs_0000590
What is the output of the following program? <pre lang="text/x-java">public class Main { public static void swap(int a, int b) { int tmp = a; a = b; b = tmp; } public static void main(String[] args) { int a = 5, b = 7; swap(a, b); System.out.println(a + " " + b); } } </pre>
[ "7 5", "5 7", "5 5", "7 7" ]
B
Main.swap only receives copies of main's a and b. Its assignments do not alter main's variables. Thus, a is still 5 and b is still 7 when the print statement is executed.
canterbury_cs
canterbury_cs_0000591
In Java, what does it mean if something is marked <code>static</code>?
[ "It never changes value.", "It exists outside of any particular instance of the class.", "It cannot be overridden.", "Its value is undetermined." ]
B
Something that is static is defined at the class level and is accessed through the class, rather than through an instance.
canterbury_cs
canterbury_cs_0000592
Suppose you've got a generic class: <pre lang="text/x-java">class Rosters&lt;T&gt; { ... } </pre> &nbsp; You create a Rosters instance: <code>Rosters&lt;ArrayList&lt;String&gt;&gt; rosters;</code> &nbsp; What is the erasure type of Rosters?
[ "Object", "ArrayList&lt;Object&gt;", "ArrayList&lt;String&gt;", "Rosters" ]
A
You need only examine the supertype of generic parameters on the Rosters class to determine the erasure type. There is no explicit supertype, so the supertype is Object.
canterbury_cs
canterbury_cs_0000593
Consider the following Python code: <pre lang="text/x-python">in_str = input(&rsquo;Enter a string: &rsquo;) a = &rsquo;&rsquo; d = 0 r = 1 for c in &rsquo;abcdefghijklmnopqrstuvwxyz&rsquo;: if c in in_str: a = c + a else: d = d + 1 r += 2 print(a) # Line 1 print(d) # Line 2 print(r) # Line 3 </pre> &nbsp; Given the input <code>&rsquo;Frick &amp; Frack&rsquo; </code>what output is produced by Line 1?
[ "FrickFrack", "kcarkcir", "acikr", "rkica" ]
D
Essentially this is filtering out all the upper case and non-alphabetic characters.&nbsp; The for loop goes through each lower-case letter, and if that letter is in the input string in_str, we concatenate the letter to the variable a.
canterbury_cs
canterbury_cs_0000594
Consider the following Python code: <pre lang="text/x-python">in_str = input(&rsquo;Enter a string: &rsquo;) a = &rsquo;&rsquo; d = 0 r = 1 for c in &rsquo;abcdefghijklmnopqrstuvwxyz&rsquo;: if c in in_str: a = c + a else: d = d + 1 r += 2 print(a) # Line 1 print(d) # Line 2 print(r) # Line 3 </pre> &nbsp; Given the input <code>&rsquo;Frick &amp; Frack&rsquo;</code> what output is produced by Line 2?
[ "5", "21", "10", "86" ]
B
This basically counts the number of lower case letters.
canterbury_cs
canterbury_cs_0000595
What does the following Java code produce? <pre lang="text/x-java">int result=1; for (int i=1; i&lt;=N; i++) { result *= 2; } System.out.println(result); </pre>
[ "0", "2*N", "2<sup>N</sup>", "2<sup>N+1</sup>" ]
C
This produces 2<sup>N</sup>.&nbsp;
canterbury_cs
canterbury_cs_0000596
Which of the following expressions, when applied to each element of the array {0, 1, 2, 3, 4, 5, 6}, produces the array {0, 0, 1, 4, 5, 5, 9}?
[ "x / 10 + x * 10 - x * x % 20", "x + x / 2 - x % 3", "2 + x - x * x", "(10 - x) * -x" ]
B
x + x / 2 - x % 3
canterbury_cs
canterbury_cs_0000597
Your data has 4-bit keys. You also have magical, perfect hash function that will prevent any collisions from happening, provided there is room enough for all possible elements in the array. What's the minimum number of elements the array must be able to hold?
[ "8", "16", "32", "64" ]
B
4-bit keys uniquely identify 2<sup>4</sup>&nbsp;= 16 elements. The array must be able to hold these 16 items.
canterbury_cs
canterbury_cs_0000598
You've got this Word class for a project you are working on: <pre lang="text/x-java">class Word { private String letters; ... public int hashCode() { return letters.charAt(1); } } </pre> &nbsp; <span style="font-family: arial, helvetica, sans-serif;">For which of the word lists below is this implementation of hashCode a poor choice?</span>
[ "we, us, oh, by", "aa, bb, cc, dd", "mom, mill, mull, mat", "cat, bad, fall, late" ]
D
The hashCode examines the second letter of each word to find the location in the hashtable. In list D, all words share the second letter 'a,' resulting in many collisions.
canterbury_cs
canterbury_cs_0000599
You know exactly how much data you need to store, but there's not much of it. You do not need to be able to search the collection quickly, but insertion should be as fast as possible. What is the simplest data structure that best suits for your needs?
[ "Unordered array", "Ordered array", "Linked list", "Hashtable" ]
A
If you know the memory needs, you can allocate a large enough array. Inserting elements in the array can be done in constant time, and requires less work than inserting in a linked list.