Canterbury QuestionBank

Field Value
ID 633463 [created: 2013-06-21 17:35:09, author: jspacco (xjaime), avg difficulty: 0.0000]
Question

What does the following Java code print?

int sum=0;
for (int i=0; i>100; i++) {
   sum += i;
}
System.out.println(sum);
*A*

0

B

100

C

0 + 1 + 2 + ... + 99 + 100

D

0 + 1 + 2 + ... + 98 + 99

E

none of the above

Explanation

This is a trick question!  The stop condition of the for loop is i>100, and since i starts at 0, i is never > 100, so the loop immediately ends.

Tags ATT-Transition-ApplyCode, Contributor_Jaime_Spacco, Skill-Trace_IncludesExpressions, ATT-Type-How, Difficulty-1-Low, Block-Horizontal-2-Struct_Control, ExternalDomainReferences-1-Low, Block-Vertical-2-Block, Language-Java, Bloom-3-Analysis, TopicSimon-LoopsSubsumesOperators, CS1, LinguisticComplexity-1-Low, CodeLength-lines-06-to-24_Medium, Nested-Block-Depth-1
Field Value
ID 633607 [created: 2013-06-22 04:34:25, author: jspacco (xjaime), avg difficulty: 0.0000]
Question

Suppose the following Java code prints "statement 2":

if (num < 6) {
   System.out.println("statement 1");
} else {
   System.out.println("statement 2");
}

 

What must be true about num?

A

greater than 6

*B*

greater than or equal to 6

C

less than 6

D

less than or equal to 6

E

this program cannot print "statement 2"

Explanation

The opposite of less than is greater than or equal to.  Not just greater than.

Tags ATT-Transition-ApplyCode, Contributor_Jaime_Spacco, Skill-Trace_IncludesExpressions, ATT-Type-How, Difficulty-1-Low, Block-Horizontal-2-Struct_Control, ExternalDomainReferences-1-Low, Block-Vertical-2-Block, TopicSimon-DataTypesAndVariables, Language-Java, Bloom-3-Analysis, CS1, LinguisticComplexity-1-Low, CodeLength-lines-00-to-06_Low, ConceptualComplexity-1-Low, Nested-Block-Depth-1
Field Value
ID 632809 [created: 2013-05-21 08:30:16, author: crjjrc (xchris), avg difficulty: 0.0000]
Question

Suppose a is true, b is false, and c is false. Which of the following expressions is true?

A b && c && !a
B !a || (b || c)
C

c && !a

*D* !c && !b
E !!b
Explanation

Substitute:

  1. !c && !b
  2. !false && !false
  3. true && true
  4. true
Tags Contributor_Chris_Johnson, ATT-Transition-CSspeak_to_Code, Skill-WriteCode_MeansChooseOption, ATT-Type-How, Difficulty-1-Low, Block-Horizontal-1-Struct_Text, ExternalDomainReferences-1-Low, Block-Vertical-1-Atom, Language-Java, Bloom-2-Comprehension, TopicSimon-LogicalOperators, CS1, LinguisticComplexity-1-Low, CodeLength-lines-00-to-06_Low, ConceptualComplexity-1-Low, Nested-Block-Depth-0-no_ifs_loops
Field Value
ID 632100 [created: 2013-06-14 23:20:51, author: tclear (xtony), avg difficulty: 0.0000]
Question

Only one of the following will not compile and run under Quick C. Which one?

A

main(){}

B

 main(void){}

*C*

int main(){return 65535;}

D

int main(void){return 1}

E

void main(void){}

Explanation

The semi-colon end of line delimiter occurs within a curly bracket pairing, which is not a logical combination from the compiler's parsing perspective

Tags Skill-DebugCode, Contributor_Tony_Clear, Difficulty-2-Medium, Block-Horizontal-1-Struct_Text, Block-Vertical-1-Atom, Language-C, Bloom-2-Comprehension, CS1, TopicSimon-MethodsFuncsProcs, CodeLength-lines-00-to-06_Low
Field Value
ID 632147 [created: 2013-06-17 23:01:10, author: tclear (xtony), avg difficulty: 0.0000]
Question

What is output by the code shown in the question below. Think about it carefully - it may be a bit tricky!

void main (void)

{

     #define LIMIT 8

     int i = 0;

     while ( i++ < LIMIT )

    {

          if ( i )

         {

               printf( "%d", LIMIT - i );

         }

     }

}

A

Nothing

B

876543210

C

876543210-1

*D*

76543210

E

76543210-1

Explanation

The while loop increments the index before each iteration of the loop and the resulting increased index value is subtracted from the Limit of 8.  Thus the values printed range from 7 to 0.

Tags Contributor_Tony_Clear, Skill-Trace_IncludesExpressions, Difficulty-2-Medium, Block-Horizontal-2-Struct_Control, Block-Vertical-2-Block, Language-C, Bloom-2-Comprehension, TopicSimon-IO, TopicSimon-LoopsSubsumesOperators, CS1, TopicSimon-Params-SubsumesMethods, CodeLength-lines-06-to-24_Medium
Field Value
ID 632170 [created: 2013-06-17 23:42:32, author: tclear (xtony), avg difficulty: 0.0000]
Question

You are preparing test data for this function that accepts a day of the month (as a number) from the user:

int iGetDay(int iMonth);

You are currently working with a month value of 5 (May). What should your boundary test values be for iGetDay?

A

-1, 0, 30, 31

B

-1, 0, 31, 32

C

0, 1, 29, 30

D

0, 1, 30, 31

*E*

0, 1, 31, 32

Explanation

The month of May has 31 days and starts on the 1st of May.  So the day before and the day after these boundary days consitute the boundary test conditions

Tags Skill-TestProgram, Contributor_Tony_Clear, Difficulty-2-Medium, Language-C, Bloom-3-Analysis, CS1, TopicSimon-Params-SubsumesMethods, CodeLength-lines-00-to-06_Low, TopicSimon-Testing
Field Value
ID 632177 [created: 2013-06-18 00:15:35, author: tclear (xtony), avg difficulty: 0.0000]
Question

How many times will the printf statement be executed in this code?

In each case assume the definition

int i = 0;

WARNING There are some very nasty traps in some of the code here. LOOK AT IT ALL VERY CAREFULLY!

 do

{

     printf("Count me!");

     i++;

} while(++i < 10);

A

0

*B*

5

C

 6

D

10

E

11

Explanation

The line i++; in the body of the loop after the printf function, in combination with the ++i preceding each iteration in the while loop, causes the index to increment twice in each iteration, so the printf function is only executed 5 times

Tags Contributor_Tony_Clear, Skill-Trace_IncludesExpressions, Difficulty-2-Medium, Block-Horizontal-2-Struct_Control, Block-Vertical-2-Block, Language-C, TopicSimon-LoopsSubsumesOperators, CS1, CodeLength-lines-00-to-06_Low
Field Value
ID 632194 [created: 2013-06-18 02:18:14, author: kate (xkate), avg difficulty: 0.0000]
Question

The worst-case analysis (Big-Oh) of the following Java code is:

for (j=0; j<n; j++) {
   for (k=0; k<n; k++) {
      base = base * 2;
   }

}

A

O(1)

B

O(n)

*C*

O(n2)

D

O(log2n)

E

O(2n)

Explanation

The body of the inner loop will be executed n2 times. Each execution will take a constant amount of time. Thus, the total amount of time will be a function of n2.

Tags Nested-Block-Depth-2-two-nested, Contributor_Kate_Sanders, ATT-Transition-Code_to_CSspeak, ATT-Type-How, SkillWG-AnalyzeCode, Difficulty-2-Medium, Block-Horizontal-1-Struct_Text, TopicSimon-AlgorithmComplex-BigO, ExternalDomainReferences-1-Low, Block-Vertical-2-Block, Language-Java, Bloom-3-Analysis, LinguisticComplexity-1-Low, CS2, CodeLength-lines-00-to-06_Low, ConceptualComplexity-2-Medium
Field Value
ID 632195 [created: 2013-06-18 01:58:23, author: kate (xkate), avg difficulty: 0.0000]
Question

The worst-case analysis (Big-Oh) of the following Java code is:


for (j=0; j<n; j++) {

   base = base * 2;
}

A

O(1)

*B*

O(n)

C

O(n2)

D

O(log2n)

E

O(2n)

Explanation

The loop will be executed n times, and each time involves a certain constant amount of work. 

Tags Contributor_Kate_Sanders, ATT-Transition-Code_to_CSspeak, ATT-Type-How, SkillWG-AnalyzeCode, Difficulty-2-Medium, Block-Horizontal-1-Struct_Text, TopicSimon-AlgorithmComplex-BigO, ExternalDomainReferences-1-Low, Block-Vertical-2-Block, Language-Java, Bloom-3-Analysis, LinguisticComplexity-1-Low, CS2, CodeLength-lines-00-to-06_Low, ConceptualComplexity-2-Medium, Nested-Block-Depth-1
Field Value
ID 632221 [created: 2013-06-18 07:09:00, author: kate (xkate), avg difficulty: 0.0000]
Question

Consider the following Java code:

public interface StackADT<T> { // 1
   public void push (T element);
   public T pop();
   public T peek();
   public boolean isEmpty(); 
}


The capital letter “T” on line 1 stands for:

A

a temporary value

*B*

the type of the items in the Stack

C

a class named T defined somewhere else in the program

D

the top of the stack

E

none of the above

Explanation

This interface is defined using Java's generics. We can tell what the T means by looking at how it is used.  The "T" in angle brackets on line 1 corresponds to the type of the parameter to push and the type of value returned by pop and peek. In other words, it is the type of value stored in the stack. 

Tags Contributor_Kate_Sanders, ATT-Transition-Code_to_CSspeak, Skill-ExplainCode, Difficulty-2-Medium, TopicWG-ADT-Stack-DefInterfaceUse, Block-Horizontal-1-Struct_Text, Block-Vertical-2-Block, TopicSimon-DataTypesAndVariables, TopicWG-JavaInterface, Bloom-1-Knowledge, Language-Java, LinguisticComplexity-1-Low, CS2, CodeLength-lines-00-to-06_Low, ConceptualComplexity-1-Low, Nested-Block-Depth-0-no_ifs_loops
Field Value
ID 632224 [created: 2013-06-18 07:18:14, author: kate (xkate), avg difficulty: 0.0000]
Question

The StackADT's push operation:

A

adds a new item at the bottom of the Stack

B

returns without removing the top item on the Stack

C

removes and returns the top item on the Stack

*D*

adds a new item at the top of the Stack

E

returns true if the Stack is empty and otherwise false

Explanation

"push" is the traditional term for adding a new item to a stack. Stacks work like a pile of paper. The bottom piece of paper is the first one that was put in the pile. The top piece of paper was added most recently. When another piece of paper is added to the stack, it goes on top of the rest of the pile.

Tags ATT-Transition-ApplyCSspeak, Contributor_Kate_Sanders, Skill-DesignProgramWithoutCoding, ATT-Type-How, Difficulty-2-Medium, TopicWG-ADT-Stack-Implementations, ExternalDomainReferences-1-Low, Block-Horizontal-3-Funct_ProgGoal, Block-Vertical-2-Block, Language-Java, Bloom-2-Comprehension, LinguisticComplexity-2-Medium, TopicSimon-MethodsFuncsProcs, CS2, CodeLength-lines-00-to-06_Low, ConceptualComplexity-2-Medium, Nested-Block-Depth-0-no_ifs_loops
Field Value
ID 632254 [created: 2013-06-18 09:20:32, author: kate (xkate), avg difficulty: 0.0000]
Question

Suppose you are trying to choose between an array and a linked list to store the data in your Java program. Which data structure can change size as needed while the program is running?

A

an array

*B*

a linked list

C

both

D

neither

Explanation

When initializing a Java array, you must allocate a fixed amount of memory for storing data. Linked lists vary in size depending on how much data they contain. 

Tags ATT-Transition-ApplyCSspeak, Contributor_Kate_Sanders, ATT-Type-How, Difficulty-1-Low, Block-Horizontal-2-Struct_Control, ExternalDomainReferences-1-Low, TopicSimon-Arrays, TopicWG-ChoosingAppropriateDS, Block-Vertical-1-Atom, TopicWG-LinkedLists, Bloom-1-Knowledge, Language-none-none-none, LinguisticComplexity-1-Low, CS2, CodeLength-NotApplicable, ConceptualComplexity-2-Medium, Nested-Block-Depth-0-no_ifs_loops
Field Value
ID 632285 [created: 2013-06-18 11:14:35, author: kate (xkate), avg difficulty: 0.0000]
Question

Suppose StackADT is implemented in Java using a linked list. The big-Oh time complexity of the following pop method is:

public T pop() {
   T tmp;
   if (this.top == null) 
      tmp = null;
   else {
      tmp = this.top.getElement();
      this.top = this.top.getNext();
   }
   return tmp;
}
*A*

O(1)

B

O(log n)

C

O(n)

D

O(n2)

E

O(2n)

Explanation

These operations can be done in constant time, independent of the size of the stack. 

Tags ATT-Transition-ApplyCSspeak, Contributor_Kate_Sanders, ATT-Type-How, SkillWG-AnalyzeCode, Difficulty-2-Medium, TopicWG-ADT-Stack-Implementations, Block-Horizontal-1-Struct_Text, TopicSimon-AlgorithmComplex-BigO, ExternalDomainReferences-1-Low, Block-Vertical-1-Atom, TopicWG-LinkedLists, Language-Java, Bloom-3-Analysis, LinguisticComplexity-1-Low, CS2, CodeLength-NotApplicable, ConceptualComplexity-2-Medium, Nested-Block-Depth-1
Field Value
ID 632288 [created: 2013-06-18 11:24:55, author: kate (xkate), avg difficulty: 0.0000]
Question

Consider the following Java method:

public T pop() { \\ 1
   T tmp; \\ 2
   if (this.top == null) \\ 3
      tmp = null; \\ 4
   else { \\ 5
      tmp = this.top.getElement(); \\ 6
      this.top = this.top.getNext(); \\ 7
   } \\ 8
   return tmp; \\ 9
} \\ 10

 

What would happen if line 7 were before line 6?

A

The code would not compile.

B

The code would compile, but it wouldn't run.

*C*

The code would compile and run, but it would return the wrong value.

D

The code would compile and run, and it would work just the same. 

Explanation

If the two lines are re-ordered, the code will change the value of top to the second node, and then the second value in the stack will be the one that is returned. 

Tags Contributor_Kate_Sanders, ATT-Transition-Code_to_CSspeak, Skill-Trace_IncludesExpressions, ATT-Type-How, Difficulty-2-Medium, TopicWG-ADT-Stack-Implementations, Block-Horizontal-2-Struct_Control, ExternalDomainReferences-1-Low, TopicSimon-Assignment, Block-Vertical-2-Block, TopicWG-LinkedLists, Language-Java, Bloom-3-Analysis, LinguisticComplexity-1-Low, CS2, CodeLength-lines-06-to-24_Medium, ConceptualComplexity-2-Medium, Nested-Block-Depth-1
Field Value
ID 632293 [created: 2013-06-18 11:34:15, author: kate (xkate), avg difficulty: 0.0000]
Question

Consider the following Java method:

public T pop() { // 1
   T tmp; // 2
   if (this.top == null) // 3
      tmp = null; // 4
   else { // 5
      tmp = this.top.getElement(); // 6
      this.top = this.top.getNext(); // 7
   } // 8
   return tmp; // 9
} // 10

 

What would happen if line 7 were before line 6?

A

The code would not compile.

B

The code would compile, but it wouldn't run.

*C*

The code would compile and run, but it would return the wrong value.

D

The code would compile and run, and it would work just the same. 

Explanation

If the two lines are re-ordered, the code will change the value of top to the second node, and then the second value in the stack will be the one that is returned. 

Tags Contributor_Kate_Sanders, ATT-Transition-Code_to_CSspeak, Skill-Trace_IncludesExpressions, ATT-Type-How, Difficulty-2-Medium, TopicWG-ADT-Stack-Implementations, Block-Horizontal-2-Struct_Control, ExternalDomainReferences-1-Low, TopicSimon-Assignment, Block-Vertical-2-Block, TopicWG-LinkedLists, Language-Java, Bloom-3-Analysis, LinguisticComplexity-1-Low, CS2, CodeLength-lines-06-to-24_Medium, ConceptualComplexity-2-Medium, Nested-Block-Depth-1
Field Value
ID 632137 [created: 2013-06-17 22:14:35, author: tclear (xtony), avg difficulty: 0.0000]
Question

What is output by the code shown below. Think about it carefully - it may be a bit tricky!

#define DEF_1 (2 + 2)

#define DEF_2 DEF_1 - DEF_1

int main(void)

{

printf("%d", DEF_2);

}

*A*

0

B

2

C

4

D

6

E

8

Explanation

The declaration of DEF_2 [DEF_1 after subtracting DEF_1 = 0 ] is the value printed.

Tags Contributor_Tony_Clear, Skill-Trace_IncludesExpressions, Difficulty-2-Medium, Block-Horizontal-1-Struct_Text, TopicSimon-ArithmeticOperators, Block-Vertical-2-Block, Language-C, Bloom-2-Comprehension, TopicSimon-IO, CS1, TopicSimon-Params-SubsumesMethods, CodeLength-lines-00-to-06_Low
Field Value
ID 632131 [created: 2013-06-17 21:44:27, author: tclear (xtony), avg difficulty: 0.0000]
Question

What is output by the code shown below. Think about it carefully -it may be a bit tricky!

void main( void )

{

static char szCode[] = "111";

szCode[2] = '0';

puts( szCode );

}

A

Nothing

B

011

C

101

*D*

110

E

111

Explanation

The value of the character in the array at index [2] (where index starts from 0)  is set to zero and the other initialized array values remain unchanged

Tags Contributor_Tony_Clear, Skill-Trace_IncludesExpressions, Difficulty-2-Medium, Block-Horizontal-2-Struct_Control, TopicSimon-Arrays, Block-Vertical-2-Block, Language-C, Bloom-2-Comprehension, CS1, CodeLength-lines-00-to-06_Low
Field Value
ID 632106 [created: 2013-06-17 20:45:00, author: marzieh (xmarzieh), avg difficulty: 0.0000]
Question

What would be the performance of removeMin and insert methods in a priority queue if it is implemented by an unsorted list?

A

O(1) , O(1)

B

 O(1) , O(n)

*C*

 O(n) , O(1)

D

O(n) , O(n)

Field Value
ID 632107 [created: 2013-06-14 22:35:47, author: tclear (xtony), avg difficulty: 0.0000]
Question

Consider this code. It is part of a program that receives MMP list votes from constituencies and stores them in a database. The field PartyName stores the names of each party involved, and the field VoteTotal keeps a running total of list votes for a party. Tally is a table-type Recordset.

Do

   ' Get a name and vote pair from the newly arrived text

   ' Returns 0 when there are no more pairs

   Start% = ExtractNextPair((txtData.Caption), Start%,_
                                                PartyName$,_Votes&)

   If Start% > 0 Then

     ' See if the party name is already present

     KeyField$ = UCase$(Trim$(PartyName$))

    Tally.Seek "=", KeyField$

     If Tally.NoMatch Then

       ' Add a new record

       Tally.AddNew

       Tally("PartyName") = KeyField$

       Tally("VoteTotal") = Votes

    Else

        ' Update the existing record - add latest votes

        Tally.Edit

        Tally("VoteTotal") = Tally("VoteTotal") + Votes

    End If

  End If

Loop While Start% > 0

Tally.Update

Why does this code not successfully store all the incoming data?

A

The loop logic is wrong - it should be Start% < 0

B

There was no call to Tally.Refresh

C

There was no call to Tally.UpdateRecords

*D*

The line Tally.Update must come inside the loop

E

The AddNew and Edit lines should be swapped

Explanation

Because the line Tally.Update is outside the loop it is only executed once at the end of the program

Tags Skill-DebugCode, Contributor_Tony_Clear, Difficulty-3-High, Block-Horizontal-3-Funct_ProgGoal, Block-Vertical-3-Relations, TopicSimon-FileIO, Bloom-3-Analysis, Language-VB, TopicSimon-LoopsSubsumesOperators, CS1, TopicSimon-Params-SubsumesMethods, CodeLength-lines-06-to-24_Medium
Field Value
ID 632112 [created: 2013-06-17 20:54:15, author: marzieh (xmarzieh), avg difficulty: 0.0000]
Question

What node will be visited prior to E in an inorder traversal of the following tree?

no description

A

A

B

B

*C*

C

D

D

Field Value
ID 632115 [created: 2013-06-14 00:24:46, author: tclear (xtony), avg difficulty: 0.0000]
Question

Consider these lines of code:

txtName.SetFocus

txtName.SelStart = 0

txtName.SelLength = Len(txtName.Text)

txtName is an edit text box which is currently visible.

The code may cause a run time error. This will happen if:

A

txtName is a multiline text box.

B

there is no text in txtName.

*C*

txtName is currently disabled.

D

some of the text in txtName is currently highlighted.

E

none of the text in txtName is currently highlighted.

Explanation

A disabled edit text box may not be accessed by the setfocus method as this combination is not a logical action and therefore clashes with the underlying VB event model which controls the permissible sequences of events.

Tags Skill-DebugCode, Contributor_Tony_Clear, Difficulty-2-Medium, Block-Horizontal-1-Struct_Text, Block-Vertical-1-Atom, Bloom-3-Analysis, TopicSimon-GUI-Design-Implementat, Language-VB, CS1, CodeLength-lines-00-to-06_Low
Field Value
ID 632116 [created: 2013-06-17 21:01:57, author: marzieh (xmarzieh), avg difficulty: 0.0000]
Question

What would be the minimum number of required queue, to implement a stack?

A

1

*B*

2

C

3

D

4

Field Value
ID 632117 [created: 2013-06-17 21:03:19, author: marzieh (xmarzieh), avg difficulty: 0.0000]
Question

Which data structures would be proper in terms of performance to implement flight waiting lists?

A

Priority Queue

*B*

Heap

C

Array

D

Linked list

Field Value
ID 632121 [created: 2013-06-17 21:04:42, author: marzieh (xmarzieh), avg difficulty: 0.0000]
Question

To implement student's database, which data structure is more appropriate?

A

Heap

B

Binary Tree

*C*

Map

D

Priority Queue

Field Value
ID 632124 [created: 2013-06-17 21:08:25, author: marzieh (xmarzieh), avg difficulty: 0.0000]
Question

Using double hashing and following hash function and data, in which array slot number 31 will be inserted*?

N = 13

h(k) = k mod 13

d(k) = 7 - k mod 7

(h(k) + jd(k)) mod 13

18, 41, 22, 44, 59, 32, 31, 73

*Credit goes to Goodrich et.al. (Data Structures & Algorithms in Java)

*A*

0

B

5

C

9

D

10

Field Value
ID 632125 [created: 2013-06-13 23:54:59, author: tclear (xtony), avg difficulty: 0.0000]
Question

Given the declaration

Public StudentRecords as Collection

What line of code is required before the first student record is added to the collection?

A

StudentRecords = New Collection

B

Set StudentRecords = Collection

*C*

Set StudentRecords = New Collection

D

Set Collection = StudentRecords

E

Set StudentRecords.New

Explanation

Set assigns an object reference to the new Collection object named StudentRecords

Tags Skill-PureKnowledgeRecall, Contributor_Tony_Clear, Difficulty-1-Low, Block-Horizontal-1-Struct_Text, Block-Vertical-1-Atom, TopicSimon-CollectionsExceptArray, Bloom-1-Knowledge, TopicSimon-FileIO, Language-VB, CS1, CodeLength-lines-00-to-06_Low
Field Value
ID 632126 [created: 2013-06-17 21:11:06, author: marzieh (xmarzieh), avg difficulty: 0.0000]
Question

Using Dijkstra’s Algorithm what would be the path between A and B?

(Picture taken from Data Structures & algorithms in Java by Goodrich at. al.)

no description

A

AB

B

ACB

*C*

ACEB

D

ADCEB

Field Value
ID 632127 [created: 2013-06-13 23:31:12, author: tclear (xtony), avg difficulty: 0.0000]
Question

The following processes are arranged in alphabetical order:

1 Application event procedure is called.

2 Event procedure code is executed.

3 User clicks on a button with the mouse.

4 Windows detects an event.

5 Windows passes a message to the application containing the button.

In what order will these processes normally occur in a Visual Basic application?

*A*

3, 4, 5, 1, 2

B

2, 3, 5, 4, 1

C

4, 3, 2, 5, 1

D

3, 5, 4, 1, 2

E

 1, 2, 3, 5, 4

Explanation

The button click causes windows to detect an event and pass a message to the application containing the button. 

The application event procedure is called and the event procedure code is executed.

Tags Skill-ExplainCode, Contributor_Tony_Clear, Difficulty-2-Medium, Block-Horizontal-2-Struct_Control, Block-Vertical-2-Block, Bloom-2-Comprehension, TopicSimon-GUI-Design-Implementat, Language-VB, CS1, TopicSimon-MethodsFuncsProcs, CodeLength-lines-00-to-06_Low, TopicSimon-ProgramDesign
Field Value
ID 632327 [created: 2013-06-18 12:09:21, author: tclear (xtony), avg difficulty: 0.0000]
Question

This Perl script does not work correctly. It is supposed to work out the average assessment mark for a student who has 6 marked exercises.

Whatever data the student enters, the script always displays an average mark of zero.

Note that line numbers have been added for reference only – they are not part of the script

1 my $total = 0;

2

3 for(my $mk = 1; $mk <= 6; $mk++)

4 {

5     print "Enter mark $mk: ";

6     my $mark = <STDIN>;

7     my $total += $mark;

8 }

9

10 my $average = $total / 6;

11 print "Average mark = $average.\n";

What is the problem?

A

<= 6 on line 3 should be <

B

The my on line 6 should not be there.

*C*

The my on line 7 should not be there.

D

 Line 10 should show $mark / 6.

E

A <= 6 on line 3 should be <

B The my on line 6 should not be there.

C The my on line 7 should not be there.

D Line 10 should show $mark / 6.

Explanation

A my declares the listed variables to be local (lexically) to the enclosing block, file, or eval.

source http://perldoc.perl.org/functions/my.html

Therefore the accumulated value of $total is not available to the following block where the average is calculated

Tags Skill-DebugCode, Contributor_Tony_Clear, Difficulty-2-Medium, Block-Horizontal-1-Struct_Text, Block-Vertical-2-Block, Language-Perl, Bloom-3-Analysis, TopicSimon-LoopsSubsumesOperators, CSother, CodeLength-lines-06-to-24_Medium, TopicSimon-Scope-Visibility
Field Value
ID 632332 [created: 2013-06-18 12:29:51, author: tclear (xtony), avg difficulty: 0.0000]
Question

This Perl subroutine does not work correctly. It is supposed to work out whether or not a person qualifies for a discount and return the correct fare, discounted or not as the case may be. Children under 13, and elderly people 65 and over qualify for a discount.

The subroutine never gives a discount

Note that line numbers have been added for reference only – they are not part of the script

1 sub getDiscount

2 {

3   my $age = $_[0]; # First parameter is age

4   my $fare = $_[1]; # Second parameter is fare

5

6   if(($age < 13) && ($age >= 65))

7   {

8   $fare *= 0.9; # 10% discount

9   }

11

12   return $fare;

13 }

What is the problem?

A

$_ on lines 3 and 4 should be @_.

*B*

The and (&&) on line 6 should be an or (||)..

C

On line 6, the < should be replaced by <=.

D

The return statement on line 12 should return $_[0].

Explanation

No age can be less than 13 AND 65 and over. It should be OR.

Tags Skill-DebugCode, Contributor_Tony_Clear, Difficulty-2-Medium, Block-Horizontal-2-Struct_Control, Block-Vertical-2-Block, Language-Perl, Bloom-3-Analysis, CSother, CodeLength-lines-06-to-24_Medium, TopicSimon-SelectionSubsumesOps
Field Value
ID 632333 [created: 2013-06-18 12:20:58, author: tclear (xtony), avg difficulty: 0.0000]
Question

This Bash command was supposed to run a Perl script, exE1.pl from a University student's bin directory on the Cislinux server, passing it a command line argument which was a text file in the copy area. It was supposed to put the results into a text file in the student's bin directory.

~/bin/exE1.pl /copy/ex/exe-1.txt > /bin/exe1out.txt

Why will this give an error?

A

~ cannot be used at the start of a line

B

.txt is not a valid extension in linux

C

> does not redirect output

*D*

Students cannot write to /bin

Explanation

The /bin directory has restricted access for security reasons to prevent students running unauthorised code on the server or modifyng or overwriting other programs in the directory

Tags Skill-DebugCode, Contributor_Tony_Clear, Difficulty-2-Medium, Block-Horizontal-2-Struct_Control, Block-Vertical-2-Block, Bloom-2-Comprehension, Language-Perl, TopicSimon-FileIO, CSother, CodeLength-lines-00-to-06_Low
Field Value
ID 632758 [created: 2013-06-20 00:26:53, author: ray (ray), avg difficulty: 0.0000]
Question

The following is a skeleton for a method called "maxPos":

public static int maxPos(int[] y, int first, int last) {
/* This method returns the position of the maximum element in the
* subsection of the array "y", starting at position
* "first" and ending at position "last".
*/

int bestSoFar = first;

xxx missing for loop goes here

return bestSoFar;

} // method maxPos

 

In this question, the missing "for" loop is to run "forwards". That is, the code should search the array from the low subscripts to the high subscripts. Given that, the correct code for the missing "for" loop is:

A
for (int i=last; i>first; i--) {
    if ( y[i] < y[bestSoFar] ) {
       bestSoFar = i;
    } // if
} // for
*B*
for (int i=first+1; i<=last; i++) {
    if ( y[i] > y[bestSoFar] ) {
       bestSoFar = i;
    } // if
} // for
C
for (int i=last; i>first; i--) {
    if ( y[i] > y[bestSoFar] ) {
       bestSoFar = i;
    } // if
} // for
D
for (int i=last; i>first; i--) {
     if ( y[i] < bestSoFar ) {
        bestSoFar = i
     } // if
} // for
E
for (int i=first+1; i<=last; i++) {
    if ( y[i] > bestSoFar ) {
       bestSoFar = i;
    } // if
} // for
Explanation

Explanation
a)

INCORRECT:

if (y[i] < y[bestSoFar]) ... This is setting bestSoFar to the index of the SMALLEST number so far, but this is MAXPOS, it needs to find the highest!

b)

CORRECT:

The code finds the maximum position in the array, searching forwards as intended.

c)

CORRECT:

The loop is running backwards.

d)

INCORRECT:

The if statement compares y[i] with the integer bestSoFar, not what is in the array at the position bestSoFar.

c)

INCORRECT:

Same as c) and d)

Tags Nested-Block-Depth-2-two-nested, Contributor_Raymond_Lister, ATT-Transition-English_to_Code, Skill-WriteCode_MeansChooseOption, ATT-Type-How, Difficulty-2-Medium, Block-Horizontal-2-Struct_Control, ExternalDomainReferences-1-Low, TopicSimon-Arrays, Block-Vertical-2-Block, Language-Java, TopicSimon-LoopsSubsumesOperators, CS1, CodeLength-lines-06-to-24_Medium, Neo-Piaget-2-Preoperational, ConceptualComplexity-1-Low, ConceptualComplexity-2-Medium
Field Value
ID 632760 [created: 2013-06-19 21:28:34, author: ray (ray), avg difficulty: 0.0000]
Question

The following is a skeleton for a method called "maxPos":

public static int maxPos(int[] y, int first, int last) {
/* This method returns the position of the maximum element in the
 * subsection of the array "y", starting at position
 * "first" and ending at position "last".

 */

  int bestSoFar = first;

  xxx missing for loop goes here

 

   return bestSoFar;

} // method maxPos

 

In this question, the missing "for" loop is to run "backwards".  That is, the code should search the array from the high subscripts to the low subscripts.  Given that, the correct code for the missing "for" loop is:

A
for (int i=last; i>first; i--) { 
    if ( y[i] < y[bestSoFar] ) { 
       bestSoFar = i; 
   } // if 
} // for
B
for (int i=first+1; i<=last; i++) {
    if ( y[i] > y[bestSoFar] ) {
       bestSoFar = i;
    } // if
} // for
*C*
for (int i=last; i>first; i--) { 

    if ( y[i] > y[bestSoFar] ) { 

       bestSoFar = i; 

    } // if

} // for 
D
for (int i=last; i>first; i--) {
    if ( y[i] < bestSoFar ) {
       bestSoFar = i
    } // if
} // for
E
for (int i=first+1; i<=last; i++) {
    if ( y[i] > bestSoFar ) {
       bestSoFar = i;
    } // if
} // for
Explanation

a)

INCORRECT:

if (y[i] < y[bestSoFar])  ... This is setting bestSoFar to the index of the SMALLEST number so far, but this is MAXPOS, it needs to find the highest!

b)

INCORRECT:

The loop starts at [first+1] ... This loop is not running backwards.

c)

CORRECT:

The code finds the maximum position in the array, searching backwards as intended.

d)

INCORRECT:

The if statement compares y[i] with the integer bestSoFar, not what is in the array at the position bestSoFar.

c)

INCORRECT:

Same as b) and d)

Tags Nested-Block-Depth-2-two-nested, Contributor_Raymond_Lister, ATT-Transition-English_to_Code, Skill-WriteCode_MeansChooseOption, ATT-Type-How, Difficulty-1-Low, Block-Horizontal-2-Struct_Control, ExternalDomainReferences-1-Low, TopicSimon-Arrays, Block-Vertical-2-Block, Language-Java, Bloom-3-Analysis, TopicSimon-LoopsSubsumesOperators, CS1, LinguisticComplexity-1-Low, CodeLength-lines-06-to-24_Medium, Neo-Piaget-2-Preoperational, ConceptualComplexity-1-Low, ConceptualComplexity-2-Medium
Field Value
ID 632765 [created: 2013-06-20 04:00:09, author: ray (ray), avg difficulty: 0.0000]
Question

The following code for a method "minVal" contains a logic error on a single line in the method body, on one of the four lines indicated by comments:

public static int minVal(int[] y, int first, int last) {

/* This method returns the value of the minimum element in the
 * subsection of the array "y", starting at position
 * "first" and ending at position "last".
 */
 
  int bestSoFar = first;             // line 1
 
  for (int i=first+1; i<=last; i++) 
  {
    if ( y[i] < bestSoFar )          // line 2

       bestSoFar = y[i];             // line 3

  } // for

  return bestSoFar;                  // line 4

} // method minVal

Which one of the four lines indicated by the comments contains the logic error?

*A*

line 1

B

line 2

C

line 3

D

line 4

Explanation

line 1 should be int bestSoFar = y[first];

This correct code assigns the value at y[first] into bestSoFar. This is because the other lines are using bestSoFar to remember the best VALUE seen thus far.

Tags Nested-Block-Depth-2-two-nested, Skill-DebugCode, ATT-Transition-DefineCSspeak, Contributor_Raymond_Lister, ATT-Type-How, Difficulty-2-Medium, Block-Horizontal-2-Struct_Control, ExternalDomainReferences-1-Low, TopicSimon-Arrays, Block-Vertical-2-Block, Language-Java, Bloom-3-Analysis, TopicSimon-LoopsSubsumesOperators, CS1, CodeLength-lines-00-to-06_Low, Neo-Piaget-3-Concrete_Operational, ConceptualComplexity-2-Medium
Field Value
ID 632767 [created: 2013-06-20 04:09:56, author: ray (ray), avg difficulty: 0.0000]
Question

The following code for a method "minVal" contains a logic error on a single line in the method body, on one of the four lines indicated by comments:

public static int minVal(int[] y, int first, int last) {

/* This method returns the value of the minimum element in the
 * subsection of the array "y", starting at position
 * "first" and ending at position "last".
 */
 
  int bestSoFar = first;             // line 1
 
  for (int i=first+1; i<=last; i++)
  {
    if ( y[i] > bestSoFar )          // line 2

       bestSoFar = y[i];             // line 3

  } // for

  return bestSoFar;                  // line 4

} // method minVal

Which one of the four lines indicated by the comments contains the logic error?

A

line 1

*B*

line 2

C

line 3

D

line4

Explanation

line 2 should be if ( y[i] < bestSoFar )

The > sign in this buggy line is looking for the MAXIMUM value in the array.
Tags Nested-Block-Depth-2-two-nested, Skill-DebugCode, ATT-Transition-DefineCSspeak, Contributor_Raymond_Lister, ATT-Type-How, Difficulty-2-Medium, Block-Horizontal-2-Struct_Control, ExternalDomainReferences-1-Low, TopicSimon-Arrays, Block-Vertical-2-Block, Language-Java, Bloom-3-Analysis, TopicSimon-LoopsSubsumesOperators, CS1, LinguisticComplexity-1-Low, CodeLength-lines-00-to-06_Low, Neo-Piaget-3-Concrete_Operational, ConceptualComplexity-2-Medium
Field Value
ID 632768 [created: 2013-06-20 04:47:57, author: ray (ray), avg difficulty: 0.0000]
Question

The following code for a method "minVal" contains a logic error on a single line in the method body, on one of the four lines indicated by comments:

public static int minVal(int[] y, int first, int last) {

/* This method returns the value of the minimum element in the
 * subsection of the array "y", starting at position
 * "first" and ending at position "last".
 */
 
  int bestSoFar = y[first];          // line 1
 
  for (int i=first+1; i<=last; i++)
  {

    if ( bestSoFar < y[i] )          // line 2

       bestSoFar = y[i];             // line 3

  } // for

  return bestSoFar;                  // line 4

} // method minVal

Which one of the four lines indicated by the comments contains the logic error?

A

line 1

*B*

line 2

C

line 3

D

line 4

Explanation

line 2 should be if ( bestSoFar >  y[i] )

The < sign in this buggy line is looking for the MAXIMUM value in the array.

Tags Nested-Block-Depth-2-two-nested, Skill-DebugCode, ATT-Transition-DefineCSspeak, Contributor_Raymond_Lister, ATT-Type-How, Difficulty-2-Medium, Block-Horizontal-2-Struct_Control, ExternalDomainReferences-1-Low, TopicSimon-Arrays, Block-Vertical-2-Block, Language-Java, Bloom-3-Analysis, TopicSimon-LoopsSubsumesOperators, CS1, LinguisticComplexity-1-Low, CodeLength-lines-00-to-06_Low, Neo-Piaget-3-Concrete_Operational, ConceptualComplexity-2-Medium
Field Value
ID 632792 [created: 2013-06-20 08:22:54, author: jspacco (xjaime), avg difficulty: 0.0000]
Question

Does this compile?

int x=7.0
A

yes

*B*

no

C

It depends on the version of the compiler

Explanation

This doesn't compile.  Although 7.0 could be converted to an integer without loss of precision, Java will not perform any conversion of a double to an int without an explicit cast by the programmer.

Tags ATT-Transition-ApplyCode, Contributor_Jaime_Spacco, Skill-Trace_IncludesExpressions, ATT-Type-How, Difficulty-1-Low, Block-Horizontal-1-Struct_Text, ExternalDomainReferences-1-Low, Block-Vertical-1-Atom, TopicSimon-DataTypesAndVariables, Language-Java, Bloom-2-Comprehension, CS1, LinguisticComplexity-1-Low, CodeLength-lines-00-to-06_Low, Nested-Block-Depth-0-no_ifs_loops
Field Value
ID 632793 [created: 2013-06-20 08:28:09, author: jspacco (xjaime), avg difficulty: 0.0000]
Question

What does this print?

double d = 8 / 10;
System.out.println(d);
A

d

*B*

0

C

0.8

D

2

E

None of the above

Explanation

Integer division!  10 goes into 8 zero times, with a remainder of 8.  But since this is integer division, we only care about the quotient, which is zero!

Tags ATT-Transition-ApplyCode, Contributor_Jaime_Spacco, Skill-Trace_IncludesExpressions, ATT-Type-How, Difficulty-1-Low, Block-Horizontal-1-Struct_Text, ExternalDomainReferences-1-Low, TopicSimon-ArithmeticOperators, Block-Vertical-1-Atom, TopicSimon-DataTypesAndVariables, Language-Java, Bloom-2-Comprehension, CS1, LinguisticComplexity-1-Low, CodeLength-lines-00-to-06_Low, Nested-Block-Depth-0-no_ifs_loops
Field Value
ID 632796 [created: 2013-06-20 08:36:08, author: jspacco (xjaime), avg difficulty: 0.0000]
Question

After the assignments x = 27 and y = 12, what is returned by x%y?

A

2

B

2.25

*C*

3

D

3.0

E

None of the above

Explanation

The % operation performs "mod" (modular division), which means "do the division but return the remainder rather than the quotient".

Tags ATT-Transition-ApplyCode, Contributor_Jaime_Spacco, Skill-Trace_IncludesExpressions, ATT-Type-How, Difficulty-1-Low, Block-Horizontal-1-Struct_Text, ExternalDomainReferences-1-Low, TopicSimon-ArithmeticOperators, Block-Vertical-1-Atom, TopicSimon-DataTypesAndVariables, Bloom-2-Comprehension, Language-Python, CS1, LinguisticComplexity-1-Low, CodeLength-lines-00-to-06_Low, Nested-Block-Depth-0-no_ifs_loops
Field Value
ID 632800 [created: 2013-06-20 08:47:08, author: jspacco (xjaime), avg difficulty: 0.0000]
Question

After the assignments x = 27 and y = 12, what is returned by not x <= y < x + y ?

A

True

*B*

False

C

1

D

0

E

An error

Explanation

Python has an incredibly flexible syntax which allows expressions like x <= y < x + y (this statement would not compile in many other languages). 

If we break this expression down:

x+y is 39, so the expression is asking:

not 12 <= 27 < 39

12 <= 27 < 39 is True, as 27 is in fact between 12 and 39.  And if we apply not to True, we get False.

Tags ATT-Transition-ApplyCode, Contributor_Jaime_Spacco, Skill-Trace_IncludesExpressions, ATT-Type-How, Difficulty-1-Low, Block-Horizontal-1-Struct_Text, ExternalDomainReferences-1-Low, TopicSimon-ArithmeticOperators, Block-Vertical-1-Atom, Bloom-2-Comprehension, Language-Python, TopicSimon-LogicalOperators, CS1, LinguisticComplexity-1-Low, CodeLength-lines-00-to-06_Low, Nested-Block-Depth-0-no_ifs_loops
Field Value
ID 632804 [created: 2013-06-20 08:53:48, author: jspacco (xjaime), avg difficulty: 0.0000]
Question

Which of the following assertions about the effect of int(my_var) is correct?

A

It modifies the value of my_var, truncating it to make it an integer.

B

It returns True if my_var is an integer; and False, otherwise.

C

It produces an error if my_var is not an integer.

*D*

It returns the integer representation of the value associated with my_var, or an error

E

None of the above

Explanation

A is wrong because the int() function does not modify (mutate) its parameter

B is wrong because the int() function does not return a Boolean, it returns an int()

C is wrong because the int() function works with double/float inputs, which are not ints (although it will generate an error if invoked with a String, for example)

D is correct!  int() converts ints and doubles to an int, truncating any extra information (i.e. the decimal portion of a double), and will generate an error if called with a non-numeric value

Tags ATT-Transition-ApplyCode, Contributor_Jaime_Spacco, Skill-Trace_IncludesExpressions, ATT-Type-How, Difficulty-1-Low, Block-Horizontal-1-Struct_Text, ExternalDomainReferences-1-Low, Block-Vertical-1-Atom, TopicSimon-DataTypesAndVariables, TopicWG-Numeric-Float-Precision, Bloom-2-Comprehension, Language-Python, TopicWG-Numeric-Float-Rounding, LinguisticComplexity-1-Low, CodeLength-lines-00-to-06_Low, Nested-Block-Depth-0-no_ifs_loops
Field Value
ID 632806 [created: 2013-05-24 09:01:48, author: crjjrc (xchris), avg difficulty: 0.0000]
Question

The following methods are in Java's String class:

  • int indexOf(int ch) - Returns the index within this string of the first occurrence of the specified character.
  • int indexOf(int ch, int fromIndex) - Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
  • String substring(int beginIndex, int endIndex) - Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Given these specifications, what does the following code print?

String s = "ichi,ni,san,go";
int i1 = s.indexOf(',');
int i2 = s.indexOf(',', i1);
System.out.println(s.substring(i1, i2));
*A* "ni"
B

The empty string

C ","
D ",ni"
E ",ni,"
Explanation

i1 is 4, and so is i2, since the second indexOf call begins searching at the position of the same comma that was found by the first indexOf call. Calling substring with identical parameters yields a 0-length String.

Tags Contributor_Chris_Johnson, ATT-Transition-ApplyCode, Skill-Trace_IncludesExpressions, ATT-Type-How, Difficulty-1-Low, Block-Horizontal-1-Struct_Text, ExternalDomainReferences-1-Low, Block-Vertical-2-Block, Language-Java, Bloom-2-Comprehension, CS1, LinguisticComplexity-1-Low, TopicSimon-Params-SubsumesMethods, CodeLength-lines-00-to-06_Low, ConceptualComplexity-1-Low, TopicSimon-Strings, Nested-Block-Depth-0-no_ifs_loops
Field Value
ID 632757 [created: 2013-06-20 01:03:35, author: ray (ray), avg difficulty: 0.0000]
Question

The following is a skeleton for a method called "minPos":

public static int minPos(int[] y, int first, int last) {
/* This method returns the position of the minimum element in the
* subsection of the array "y", starting at position
* "first" and ending at position "last".
*/

  int bestSoFar = first;

  xxx missing for loop goes here

  return bestSoFar;

} // method minPos

 

In this question, the missing "for" loop is to run "backwards". That is, the code should search the array from the high subscripts to the low subscripts. Given that, the correct code for the missing "for" loop is:

A
for (int i=last; i>first; i--) {
    if ( y[bestSoFar] < y[i] ) {
       bestSoFar = i;
    } // if
} // for
B
for (int i=first+1; i<=last; i++) {
    if ( y[bestSoFar] < y[i] ) {
       bestSoFar = i;
    } // if
} // for
C
for (int i=last; i>first; i--) {
    if ( y[bestSoFar] < y[i] ) {
       bestSoFar = i;
    } // if
} // for
*D*
for (int i=last; i>first; i--) {
    if ( y[i] < y[bestSoFar] ) {
       bestSoFar = i
    } // if
} // for
E
for (int i=first+1; i<=last; i++) {
    if ( bestSoFar < y[i] ) {
       bestSoFar = i;
    } // if
} // for
Explanation

a)
INCORRECT:
if (y[bestSoFar] < y[i])  ... This is setting bestSoFar to the index of the LARGEST number so far.

b)
INCORRECT:
The loop starts at first+1 ...not running backwards.

This loop is if (y[bestSoFar] < y[i])  ... This is setting bestSoFar to the index of the LARGEST number so far.


c)
INCORRECT:
if (y[bestSoFar] < y[i])  ... This is setting bestSoFar to the index of the LARGEST number so far.

d) CORRECT!

e)
INCORRECT:
The if statement compares y[i] with the integer bestSoFar, not what is in the array at the position bestSoFar.
The loop starts at first+1 ... This loop is not running backwards.

Tags Nested-Block-Depth-2-two-nested, Contributor_Raymond_Lister, ATT-Transition-English_to_Code, Skill-WriteCode_MeansChooseOption, ATT-Type-How, Difficulty-2-Medium, Block-Horizontal-2-Struct_Control, ExternalDomainReferences-1-Low, TopicSimon-Arrays, Block-Vertical-2-Block, Language-Java, Bloom-3-Analysis, TopicSimon-LoopsSubsumesOperators, CS1, CodeLength-lines-06-to-24_Medium, Neo-Piaget-2-Preoperational, ConceptualComplexity-2-Medium
Field Value
ID 632756 [created: 2013-06-20 01:30:36, author: ray (ray), avg difficulty: 0.0000]
Question
The following is a skeleton for a method called "minPos":

public static int minPos(int[] y, int first, int last) {
/* This method returns the position of the minimum element in the
 * subsection of the array "y", starting at position
 * "first" and ending at position "last".
 */

  int bestSoFar = first;

  xxx missing for loop goes here

  return bestSoFar;

} // method minPos

In this question, the missing "for" loop is to run "forwards". That is, the code should search the array from the high subscripts to the low subscripts. Given that, the correct code for the missing "for" loop is:
A
for (int i=last; i>first; i--) {
    if ( y[bestSoFar] < y[i] ) {
       bestSoFar = i;
    } // if
} // for
*B*
for (int i=first+1; i<=last; i++) {
    if ( y[bestSoFar] > y[i] ) {
       bestSoFar = i;
    } // if
} // for
C
for (int i=last; i>first; i--) {
    if ( y[bestSoFar] > y[i] ) {
       bestSoFar = i;
    } // if
} // for
D
for (int i=last; i>first; i--) {
    if ( bestSoFar < y[i] ) {
       bestSoFar = i
    } // if
} // for
E
for (int i=first+1; i<=last; i++) {
    if ( bestSoFar > y[i] ) {
       bestSoFar = i;
    } // if
} // for
Explanation

a)
INCORRECT:
The loop starts at last ... This loop is not running forwards.
if (y[bestSoFar] < y[i])  ... This is setting bestSoFar to the index of the LARGEST number so far.

b)
CORRECT!

c)
INCORRECT:
The loop starts at last ... This loop is not running forwards.

d)
INCORRECT:
The loop starts at last ... This loop is not running forwards.
The if statement compares y[i] with the integer bestSoFar, not what is in the array at the position bestSoFar.

e)
INCORRECT:
The if statement compares y[i] with the integer bestSoFar, not what is in the array at the position bestSoFar.
The loop starts at first+1 ... This loop is not running forwards.

Tags Nested-Block-Depth-2-two-nested, Contributor_Raymond_Lister, ATT-Transition-English_to_Code, ATT-Type-How, Difficulty-2-Medium, Block-Horizontal-2-Struct_Control, ExternalDomainReferences-1-Low, TopicSimon-Arrays, Block-Vertical-2-Block, Language-Java, Bloom-3-Analysis, TopicSimon-LoopsSubsumesOperators, CS1, LinguisticComplexity-1-Low, CodeLength-lines-06-to-24_Medium, Neo-Piaget-2-Preoperational, ConceptualComplexity-2-Medium
Field Value
ID 632334 [created: 2013-06-18 12:34:06, author: tclear (xtony), avg difficulty: 0.0000]
Question

Assume this Perl subroutine is called like this:

&compare(6, 4);

sub compare

{

  my $ret = 0;

  if($_[0] > $_[1])

  {

       $ret = 1;

   }

  elsif($_[0] < $_[1])

  {

       $ret = -1;

  }

  return $ret;

}

What will the subroutine return?

A

-1

B

0

*C*

1

D

undef

Explanation

6 (the first parameter) is greater than 4 (the second parameter) so it returns 1

Tags Contributor_Tony_Clear, Skill-Trace_IncludesExpressions, Difficulty-1-Low, Block-Horizontal-2-Struct_Control, Block-Vertical-2-Block, Bloom-2-Comprehension, Language-Perl, CSother, CodeLength-lines-06-to-24_Medium, TopicSimon-SelectionSubsumesOps
Field Value
ID 632343 [created: 2013-06-18 15:39:56, author: tclear (xtony), avg difficulty: 0.0000]
Question

In this question, you are given a Perl regular expression that you are required to evaluate.  

There are no leading or trailing spaces in any of the text, nor are there any spaces in the regex.

Identify the answer which best matches the regex below:

/write/

A

Write your name at the top.

B

Your writing is hard to read.

*C*

Did you write your ID on the paper?

D

Who sends handwritten letters anymore?

Explanation

The expression generates a match for a sentence which contains the full string (case sensitive) given between the / / delimiters

Tags Skill-PureKnowledgeRecall, Contributor_Tony_Clear, Difficulty-1-Low, Block-Vertical-1-Atom, Bloom-1-Knowledge, Language-Perl, CSother, CodeLength-lines-00-to-06_Low, TopicSimon-Strings
Field Value
ID 632355 [created: 2013-06-18 15:56:07, author: tclear (xtony), avg difficulty: 0.0000]
Question

In this question, you are given a Perl regular expression that you are required to evaluate.

There are no leading or trailing spaces in any of the text, nor are there any spaces in the regex.

Identify the answer which best matches the regex below:

/^[A-Z]\d/

A

ABC123

*B*

X3g5.

C

3Y3

D

a5D2

Explanation

Must begin with an upper case letter followed by a digit.

Tags Contributor_Tony_Clear, Skill-Trace_IncludesExpressions, Difficulty-1-Low, Block-Horizontal-1-Struct_Text, Block-Vertical-1-Atom, Bloom-1-Knowledge, Language-Perl, CSother, CodeLength-lines-00-to-06_Low, TopicSimon-Strings
Field Value
ID 632468 [created: 2013-06-19 04:07:05, author: kate (xkate), avg difficulty: 0.0000]
Question

An example of something that could be built using a ListADT is a structure that models:

A

The undo operation in a word processor

B

The back button in a web browser

C

the customers waiting to pay at the university dining hall

*D*

an ordered to-do list 

E

the computers at the university and the network that connects them

Explanation

In ListADT, items can be added to or removed from the beginning, middle, or end of the structure. This is not well suited for choices A or B, where you need to remove only the most recently added item.

For choice C, you need to ensure that the item removed is always the one that was stored first, so ListADT won't work there either. 

Choice E would be best modeled by a nonlinear structure, and ListADT (like StackADT and QueueADT) is linear.

That leaves Choice D. A list of things to do can be linear -- one thing after another -- and it may be desirable to add new items at the beginning, middle, or end, depending on how important they are. 

Tags Contributor_Kate_Sanders, TopicWG-ADT-List-DefInterfaceUse, ATT-Transition-CSspeak_to_English, Skill-DesignProgramWithoutCoding, ATT-Type-How, Difficulty-2-Medium, Block-Horizontal-1-Struct_Text, ExternalDomainReferences-2-Medium, Block-Vertical-4-Macro-Structure, Bloom-4-Application, Language-none-none-none, LinguisticComplexity-1-Low, CS2, CodeLength-NotApplicable, TopicSimon-ProgramDesign, ConceptualComplexity-2-Medium, Nested-Block-Depth-0-no_ifs_loops
Field Value
ID 632469 [created: 2013-06-19 04:20:10, author: kate (xkate), avg difficulty: 0.0000]
Question

Identify the bug in the following code (if any):


public boolean search(T item,MyList<T> list){ // 1

this.search(item, list.getRest()); // 2

} // 3

*A*

There is no base case

B

The problem is not self-similar

C

There is a base case, but the problem does not get smaller

D

There are no bugs

E

None of the above

Explanation

For a recursive solution to a problem, you need three things:

(1) a base case (where the problem can be solved without recursion)

(2) a self-similar problem (one that contains similar problem(s) to itself)

(3) a way of making the problem smaller so you get closer to the base case

Here (2) is satisfied -- lists contain smaller lists. (3) is satisfied by line 2 of the method. But there is no base case. 

Tags Contributor_Kate_Sanders, Skill-DebugCode, ATT-Transition-Code_to_CSspeak, ATT-Type-How, Difficulty-2-Medium, Block-Horizontal-1-Struct_Text, ExternalDomainReferences-1-Low, Block-Vertical-2-Block, Language-Java, Bloom-3-Analysis, LinguisticComplexity-1-Low, CS2, CodeLength-lines-00-to-06_Low, TopicSimon-Recursion, ConceptualComplexity-2-Medium, Nested-Block-Depth-1
Field Value
ID 632475 [created: 2013-06-19 04:33:18, author: kate (xkate), avg difficulty: 0.0000]
Question

Identify the bug in the following code (if any): 


public boolean search(T item, ListADT list){ // 1

   if (list == null) // 2
      return false; // 3
   else if (list.first()==item) // 4
      return true; // 5
   else // 6
      return this.search(item, list); // 7

} // 8

A

There is no base case

B

The problem is not self-similar

*C*

The problem does not get smaller

D

There are no bugs

E

None of the above

Explanation

For a recursive solution to a problem, you need three things:
(1) a base case (where the problem can be solved without recursion)
(2) a self-similar problem (one that contains similar problem(s) to itself)
(3) a way of making the problem smaller so you get closer to the base case

Here (2) is satisfied -- lists contain smaller lists. (1) is satisfied by lines 2-5 of the method. But the recursive call in line 7 has the same parameter as the method itself (line 1), so the problem never gets smaller. 

Tags Nested-Block-Depth-2-two-nested, Contributor_Kate_Sanders, Skill-DebugCode, ATT-Transition-Code_to_CSspeak, ATT-Type-How, Difficulty-2-Medium, Block-Horizontal-1-Struct_Text, ExternalDomainReferences-1-Low, Block-Vertical-2-Block, Language-Java, Bloom-3-Analysis, LinguisticComplexity-1-Low, CS2, CodeLength-lines-06-to-24_Medium, TopicSimon-Recursion, ConceptualComplexity-2-Medium
Field Value
ID 632476 [created: 2013-06-19 05:09:55, author: kate (xkate), avg difficulty: 0.0000]
Question

The worst-case time complexity of the following Java method is:

public int fibonacci (int n) {
   if (n == 1) 
      return 1;
   else if (n == 2) 
      return 1;
   else if (n > 2) 

      return fibonacci(n-1)+fibonacci(n-2);

   else 
      return -1; // invalid input

}

A

O(1)

B

O(log n)

C

O(n)

D

O(n2)

*E*

none of the above

Explanation

This code is exponential, because it's doubly recursive. 

Tags Nested-Block-Depth-2-two-nested, Contributor_Kate_Sanders, ATT-Transition-Code_to_CSspeak, ATT-Type-How, SkillWG-AnalyzeCode, Difficulty-2-Medium, Block-Horizontal-1-Struct_Text, TopicSimon-AlgorithmComplex-BigO, ExternalDomainReferences-2-Medium, Block-Vertical-2-Block, Language-Java, Bloom-3-Analysis, LinguisticComplexity-1-Low, CS2, CodeLength-lines-06-to-24_Medium, TopicSimon-Recursion, ConceptualComplexity-2-Medium
Field Value
ID 632565 [created: 2013-06-19 13:46:30, author: kate (xkate), avg difficulty: 0.0000]
Question

The worst-case time complexity of quicksort is:

A

O(1)

B

O(n)

*C*

O(n log n)

D

O(n2)

E

none of the above

Explanation

In the worst case, every time we partition the list, we divide it into two parts, one of size 0 and one of size n-1 (plus the pivot element). This would happen, for example, if all the elements of the list are equal, or if the list is already sorted and you choose the left-most element as your pivot. 

We'd have to partition the list n times, because each time the pivot element is the only one that gets put in place.  The first time we compare the pivot element with all n-1 other elements. The second time, we compare the new pivot with n-2 other elements, and so forth down to n - (n-1). So we do work proportional to 1+2+3+...+(n-1), or n(n-1)/2.

Tags ATT-Transition-ApplyCSspeak, Contributor_Kate_Sanders, Skill-PureKnowledgeRecall, ATT-Type-How, Difficulty-2-Medium, Block-Horizontal-1-Struct_Text, TopicSimon-AlgorithmComplex-BigO, ExternalDomainReferences-1-Low, Block-Vertical-2-Block, Bloom-1-Knowledge, Language-none-none-none, LinguisticComplexity-1-Low, CS2, CodeLength-NotApplicable, TopicWG-Sorting-Quadratic, Nested-Block-Depth-0-no_ifs_loops
Field Value
ID 632743 [created: 2013-06-20 03:25:02, author: ray (ray), avg difficulty: 0.0000]
Question

The following is a skeleton for a method called "minVal":

public static int minVal(int[] y, int first, int last) {

/* This method returns the value of the minimum element in the
* subsection of the array "y", starting at position
* "first" and ending at position "last".
*/
 
  int bestSoFar = y[first];
 

  xxx missing for loop goes here

  return bestSoFar;

} // method minVal

In this question, the missing "for" loop is to run "forwards". That is, the code should search the array from the low subscripts to the high subscripts. Given that, the correct code for the missing "for" loop is:

A
for (int i=last; i>first; i--) {
    if ( y[i] < bestSoFar ) {
       bestSoFar = y[i];
    } // if
} // for
B
for (int i=first+1; i<=last; i++) {
    if ( y[i] > y[bestSoFar] ) {
       bestSoFar = y[i];
    } // if
} // for
C
for (int i=last; i>first; i--) {
    if ( y[i] > y[bestSoFar] ) {
       bestSoFar = i;
    } // if
} // for
D
for (int i=last; i>first; i--) {
    if ( bestSoFar < y[i] ) {
       bestSoFar = i
    } // if
} // for
*E*
for (int i=first+1; i<=last; i++) {
    if ( y[i] < bestSoFar ) {
       bestSoFar = y[i];
    } // if
} // for
Explanation

a)
INCORRECT
The loop starts at last ... This loop is not running backwards.

b)
INCORRECT:
if  ( y[i] > y[bestSoFar] ) ... bestSoFar is storing a value, not a position.  In any event, this if condition is searching for the maximum value.

c)
INCORRECT:
The loop starts at last ... This loop is not running backwards.
if  ( y[i] > y[bestSoFar] ) ... bestSoFar is storing a value, not a position.  In any event, this if condition is searching for the maximum value.
bestSoFar = i; ... bestsoFar is being set to the position, not the value.

d)
INCORRECT:
The loop starts at last ... This loop is not running backwards.
if ( bestSoFar < y[i] ) ... this if condition is searching for the maximum value.
bestSoFar = i; ... bestsoFar is being set to the position, not the value.

e)
CORRECT!

Tags Nested-Block-Depth-2-two-nested, Contributor_Raymond_Lister, ATT-Transition-English_to_Code, Skill-WriteCode_MeansChooseOption, ATT-Type-How, Difficulty-2-Medium, Block-Horizontal-2-Struct_Control, ExternalDomainReferences-1-Low, TopicSimon-Arrays, Block-Vertical-2-Block, Language-Java, Bloom-3-Analysis, TopicSimon-LoopsSubsumesOperators, CS1, LinguisticComplexity-1-Low, CodeLength-lines-06-to-24_Medium, Neo-Piaget-2-Preoperational
Field Value
ID 632753 [created: 2013-06-20 03:09:31, author: ray (ray), avg difficulty: 0.0000]
Question

The following is a skeleton for a method called "minVal":

public static int minVal(int[] y, int first, int last) {
/* This method returns the value of the minimum element in the
* subsection of the array "y", starting at position
* "first" and ending at position "last".
*/

  int bestSoFar = y[first];

  xxx missing for loop goes here

  return bestSoFar;

} // method minVal

 

In this question, the missing "for" loop is to run "backwards". That is, the code should search the array from the high subscripts to the low subscripts. Given that, the correct code for the missing "for" loop is:

*A*
for (int i=last; i>first; i--) {
    if ( y[i] < bestSoFar ) {
       bestSoFar = y[i];
    } // if
} // for
B
for (int i=first+1; i<=last; i++) {
    if ( y[i] > y[bestSoFar] ) {
       bestSoFar = y[i];
    } // if
} // for
C
for (int i=last; i>first; i--) {
    if ( y[i] > y[bestSoFar] ) {
       bestSoFar = i;
    } // if
} // for
D
for (int i=last; i>first; i--) {
    if ( bestSoFar < y[i] ) {
       bestSoFar = i
    } // if
} // for
E
for (int i=first+1; i<=last; i++) {
    if ( y[i] > bestSoFar ) {
       bestSoFar = i;
    } // if
} // for
Explanation

a)

CORRECT!

b)

INCORRECT:

The loop starts at first+1 ... This loop is not running backwards.

if  ( y[i] > y[bestSoFar] ) ... bestSoFar is storing a value, not a position.  In any event, this if condition is searching for the maximum value.

c)

INCORRECT:

if  ( y[i] > y[bestSoFar] ) ... bestSoFar is storing a value, not a position.  In any event, this if condition is searching for the maximum value.

bestSoFar = i; ... bestsoFar is being set to the position, not the value.

d)

INCORRECT:

if ( bestSoFar < y[i] ) ... this if condition is searching for the maximum value.

bestSoFar = i; ... bestsoFar is being set to the position, not the value.

e)

INCORRECT:

The loop starts at first+1 ... This loop is not running backwards.

if  ( y[i] > y[bestSoFar] ) ...  this if condition is searching for the maximum value.

bestSoFar = i; ... bestsoFar is being set to the position, not the value.

Tags Nested-Block-Depth-2-two-nested, Contributor_Raymond_Lister, ATT-Transition-English_to_Code, Skill-WriteCode_MeansChooseOption, ATT-Type-How, Difficulty-2-Medium, Block-Horizontal-2-Struct_Control, ExternalDomainReferences-1-Low, TopicSimon-Arrays, Block-Vertical-2-Block, Language-Java, Bloom-3-Analysis, TopicSimon-LoopsSubsumesOperators, CS1, LinguisticComplexity-1-Low, CodeLength-lines-06-to-24_Medium, Neo-Piaget-2-Preoperational, ConceptualComplexity-2-Medium
Field Value
ID 632754 [created: 2013-06-20 02:37:58, author: ray (ray), avg difficulty: 0.0000]
Question

The following is a skeleton for a method called "maxVal":

public static int maxVal(int[] y, int first, int last) {
/* This method returns the value of the maximum element in the
* subsection of the array "y", starting at position
* "first" and ending at position "last".
*/

int bestSoFar = y[first];

xxx missing for loop goes here

return bestSoFar;

} // method maxVal

In this question, the missing "for" loop is to run "forwards".  That is, the code should search the array from the low subscripts to the high subscripts.  Given that, the correct code for the missing "for" loop is:
A
for (int i=last; i>first; i--) {
    if ( y[i] < bestSoFar ) {
       bestSoFar = y[i];
   } // if
} // for
*B*
for (int i=first+1; i<=last; i++) {
    if ( y[i] > bestSoFar ) {
       bestSoFar = y[i];
    } // if
} // for
C
for (int i=last; i>first; i--) {
    if ( y[i] > y[bestSoFar] ) {
       bestSoFar = i;
    } // if
} // for
D
for (int i=last; i>first; i--) {
    if ( y[i] < bestSoFar ) {
       bestSoFar = i;
    } // if
} // for
E
for (int i=first+1; i<=last; i++) {
    if ( y[i] > bestSoFar ) {
       bestSoFar = i;
    } // if
} // for
Explanation

a)

INCORRECT:

The loop starts at  last ... it is NOT running forward.

if (y[i] < y[bestSoFar]) ... This is setting bestSoFar to the value of the SMALLEST number so far.

b)

CORRECT!

c)

CORRECT:

The loop starts at  last ... it is NOT running forward.

if ( y[i] > y[bestSoFar] ) ... bestSoFar is storing the minimum value, NOT the position of the minimum value.

bestSoFar = i  ... bestSoFar is being set to a postion, not the value at that poisition.

 

d)

INCORRECT:

The loop starts at  last ... it is NOT running forward.

if (y[i] < bestSoFar) ... This is lokking for theSMALLEST value.

bestSoFar = i ... bestSoFar is being set to a postion, not the value at that position.

 

e)

INCORRECT:

bestSoFar = i  ... bestSoFar is being set to a postion, not the value at that position.

Tags Nested-Block-Depth-2-two-nested, Contributor_Raymond_Lister, ATT-Transition-English_to_Code, Difficulty-2-Medium, Block-Horizontal-2-Struct_Control, ExternalDomainReferences-1-Low, TopicSimon-Arrays, Block-Vertical-2-Block, Language-Java, Bloom-3-Analysis, TopicSimon-LoopsSubsumesOperators, CS1, LinguisticComplexity-1-Low, CodeLength-lines-06-to-24_Medium, Neo-Piaget-2-Preoperational, ConceptualComplexity-2-Medium
Field Value
ID 632808 [created: 2013-05-24 00:08:46, author: crjjrc (xchris), avg difficulty: 0.0000]
Question

Suppose you have the following partial code to sum up an array of ints:

int sum(int[] nums) {
  int sum = 0;
  ...
  return sum;
}

 

Which of the following does not correctly complete this method?

A
for (int i = 0; i < nums.length; ++i)
  sum += nums[i];
B
for (int i : nums)
  sum += i;
*C*
for (int i = nums.length - 1; i >= 0; ++i)
  sum += nums[i];
D
int i = 0;
while (i < nums.length) {
  sum += nums[i];
  ++i;
}
Explanation

It appears in this solution that we are iterating through the list in reverse. However, we incorrectly increment the iterator, leading to an IndexOutOfBoundsException.

Tags Contributor_Chris_Johnson, ATT-Transition-ApplyCode, Skill-WriteCode_MeansChooseOption, ATT-Type-How, Difficulty-1-Low, Block-Horizontal-1-Struct_Text, ExternalDomainReferences-1-Low, TopicSimon-Arrays, Block-Vertical-2-Block, Language-Java, Bloom-2-Comprehension, TopicSimon-LoopsSubsumesOperators, CS1, LinguisticComplexity-1-Low, CodeLength-lines-06-to-24_Medium, ConceptualComplexity-1-Low, Nested-Block-Depth-1