- > matrix)
{
int maxVec = -1; // Line 1
int maxSum = Integer.MIN_VALUE; // Line 2
for (int row = 0; row < __________; row++) // Line 3
{
int sum = 0; // Line 4
for (int col = 0; col < __________; col++) // Line 5
{
sum = sum + __________; // Line 6
}
if (___________) // Line 7
{
maxSum = __________; // Line 8
maxVec = __________; // Line 9
}
}
return maxVec; // Line 10
}
a. maxSum
b. maxVec
*c. sum
d. row
e. col
f. "
g. "
h. "
i. "
j. "
General Feedback:
The local variable maxSum is used to keep track of the maximum row sum seen so far, as the outer loop progresses across all rows in the matrix. The inner loop computes the sum of all cells in the current row, which is stored in the local variable sum. If the current row's sum is larger than the maximum seen so far, the variable maxSum should be updated to be sum.
635047
Given the following Java class declaration:
public class T2int
{
private int i;
public T2int()
{
i = 0;
}
public T2int(int i)
{
this.i = i;
}
public int get()
{
return i;
}
}
The following method, called rangeSum(), is intended to take three parameters: a List of T2int objects, plus the low and high end of a range within the list. The method computes the sum of the values in the List that are within the "range" (but not including the range end values). Choose the best choice to fill in the blank on Line 8 so that the method will work as intended:
public int rangeSum(List

- > matrix)
{
int maxVec = -1; // Line 1
int maxSum = Integer.MIN_VALUE; // Line 2
for (int row = 0; row < __________; row++) // Line 3
{
int sum = 0; // Line 4
for (int col = 0; col < __________; col++) // Line 5
{
sum = sum + __________; // Line 6
}
if (___________) // Line 7
{
maxSum = __________; // Line 8
maxVec = __________; // Line 9
}
}
return maxVec; // Line 10
}
a. sum > matrix.size()
b. sum < maxSum
c. sum <= maxSum
*d. sum > maxSum
e. sum > matrix.get(row).size()
f. "
g. "
h. "
i. "
j. "
General Feedback:
The local variable sum represents the sum of all cell values in the current row, which is computed by the inner loop in the code. The if test on Line 6 checks whether to update the local variables maxSum and maxVec, which represent information about the largest row sum found so far. This should happen when sum > maxSum.
618589
Which sentence is NOT correct?
a. In a class, you can have a method with the same name as the constructor.
b. In a class, you can have two methods with the same name and return type, but different number and type of input arguments.
*c. In a class, you can have two methods with the same number and type of input arguments and different return type.
d. In a class you can have two constructors with the same name.
e.
f. "
g. "
h. "
i. "
General Feedback:
In a class, you can have two methods with the same number and type of input arguments and different return type.
633401
You see the expression n = -15 in some code that successfully compiles. What type can n not be?
a. int
b. float
*c. char
d. short
e. long
f. "
g. "
h. "
i. "
j. "
General Feedback:
Chars can only hold integers in [0, 65535]. Assigning an int variable to a char requires an explicit cast. Assigning an int literal in this interval does not require a cast. Assign an int literal outside of this interval is compile error.
633273
After the assignment signal = ’abracadabra’, what is returned by signal[len(signal)]?
a. 'a'
b. 'abracadabra'
c. 11
*d. an error
e. none of the above
f. "
g. "
h. "
i. "
j. "
General Feedback:
This is the classic way to go one character over the edge of a String.
635002
Which of the following sorting algorithms has a best-case time performance that is the same as its and worst-case time performance (in big O notation)?
a. Insertion sort
*b. Selection sort
c. Bubble sort
d. None of the above
e.
f. "
g. "
h. "
i. "
General Feedback:
Selection sort has both O(n^2) worst and best case.
633262
What advantage does using dummy nodes in a linked list implementation offer?
a. Reduced storage needs.
*b. Simplified insertion.
c. Easier detection of the list's head and tail.
d. Simplified iteration.
e.
f. "
g. "
h. "
i. "
General Feedback:
Dummy nodes consume a little more space, and they don't simplify iteration or bounds detection any. They do reduce the special casing that would otherwise need to be done when updating forward and backward links on an insertion.
618985
How many asterisks will be printed as a result of executing this code?
int counter = 0, N = 10;
while (counter++ < N){
if (counter%2 == 0)
continue;
System.out.print("*");
}
a. none, infinite loop.
b. 10
*c. 5
d. 1
e.
f. "
g. "
h. "
i. "
General Feedback:
5
635045
Given the following Java class declaration:
public class T2int
{
private int i;
public T2int()
{
i = 0;
}
public T2int(int i)
{
this.i = i;
}
public int get()
{
return i;
}
}
The following method, called rangeSum(), is intended to take three parameters: a List of T2int objects, plus the low and high end of a range within the list. The method computes the sum of the values in the List that are within the "range" (but not including the range end values). Choose the best choice to fill in the blank on Line 7 so that the method will work as intended:
public int rangeSum(List

- > matrix)
{
int maxVec = -1; // Line 1
int maxSum = Integer.MIN_VALUE; // Line 2
for (int row = 0; row < __________; row++) // Line 3
{
int sum = 0; // Line 4
for (int col = 0; col < __________; col++) // Line 5
{
sum = sum + __________; // Line 6
}
if (___________) // Line 7
{
maxSum = __________; // Line 8
maxVec = __________; // Line 9
}
}
return maxVec; // Line 10
}
a. maxSum
*b. row
c. sum
d. col
e. maxVec
f. "
g. "
h. "
i. "
j. "
General Feedback:
The local variable maxVec is used to keep track of the row number of the maximum row sum seen so far, as the outer loop progresses across all rows in the matrix. The inner loop computes the sum of all cells in the current row, which is stored in the local variable sum. If the current row's sum is larger than the maximum seen so far, the variable maxVec should be updated to be row.
635074
Consider the following classes:
public class A
{
private int myNum;
public A(int x) { myNum = x; }
public int getNumber() { return myNum; }
public String getLetters() { return "A"; }
public String getMessage()
{
return getLetters() + "-" + getNumber();
}
}
public class AB extends A
{
public AB(int x) { super(x + 1); }
public int getNumber() { return super.getNumber() + 1; }
public String getLetters() { return "AB"; }
}
What is the output of the following code segment?
A test = new AB(0);
System.out.print(test.getMessage());
a. A-0
b. A-2
c. AB-0
d. AB-1
*e. AB-2
f. "
g. "
h. "
i. "
j. "
General Feedback:
The object created is an instance of class AB, despite the static type of the variable test. Because of the constructor defined in AB, the object's myNum field will be initialized with the value 1. Because of polymorphism, when getMessage() calls getLetters(), the definition of the method in AB will be used, returning "AB". Similarly, when getMessage() calls getNumber(), the definition of the method in AB will be used, returning 2.
632274
Which data structure uses less space per object?
*a. an array
b. a linked list
c. they are both the same
d.
e.
f. "
g. "
h. "
General Feedback:
In a linked list, you create a Node for each element that contains not only the element but a link to the next Node. In addition, you create a List object that contains additional fields such as a link to the first Node in the list and (often) the size of the list. In an array, you store only the elements, not a Node or a link to the next Node.
633890
Which code snippet is tail recursive?
a.
int sum(int x)
{
if(x == 1)
{
return x;
}
return x + sum(x - 1);
}
*b.
int sum(int x, int running_total)
{
if(x == 0)
{
return running_total;
}
return sum(x - 1, running_total + x);
}
c.
d.
e.
f. "
g. "
General Feedback:
A) requires the call to sum() to be completed before adding x to it.
633237
When deleting a node with both left and right children from a binary search tree, it may be replaced by which of the following?
a. its left child
b. its right child
c. its preorder successor
*d. its inorder successor
e. its postorder successor
f. "
g. "
h. "
i. "
j. "
General Feedback:
A common choice for the replacement node is deleted node's right child's leftmost descendent. This descendent is the deleted node's inorder successor.
630967
Suppose you are writing a program for a robot that will go around a building and clean the floor. Your program will contain, among other things, a Robot class and a Building class (with information about the layout of the building). The Building class is also used in a different program for scheduling maintenance of various parts of the building.
The relationship between your Robot class and your Building class is best modeled as:
a. a class-subclass relationship
b. a subclass-class relationship
*c. a peer relationship
d. a whole-component relationship
e.
f. "
g. "
h. "
i. "
General Feedback:
A and B are wrong, because a Building object doesn't share all the properties and behaviors of a Robot object (or vice versa). D is wrong because the Building object is not part of the Robot object (unlike the Robot's wheels, for example). The two classes are sometimes part of different programs and sometimes work together -- a peer relationship.
634187
What is the worst-case time performance of heapsort?
*a. O(nlgn)
b. O(n)
c. O(lgn)
d. O(n2)
e.
f. "
g. "
h. "
i. "
General Feedback:
O(n2)
632097
What is output from the following section of code?
int i = 4;
int j = i - 1;
printf("%d bows = %d wows", i, j+1);
a. 3 bows = 4 wows
*b. 4 bows = 4 wows
c. 3 bows = 5 wows
d. 4 bows = 5 wows
e. E 4 bows = 3 wows
f. "
g. "
h. "
i. "
j. "
General Feedback:
Through the printf function the decimal integer values defined by the %d field specifiers are replaced by the values for i and j resulting from the expression. The value of variable j is both decremented and incremented to remain equivalent to that of variable i when printed.
634251
Read the following method skeleton and choose the best expression to fill in the blank on line 4 so that the method will behave correctly:
/**
* Takes a string reference and counts the number of times
* the character 'A' or 'a' appears in the string object.
* @param aString String reference to object containing chars.
* @precondition aString is not null (you may assume this is true).
* @return The number of times 'A' or 'a' appears in the string.
*/
public static int countAs(String aString) // line 1
{
int counter = __________; // line 2
int totalA = 0; // line 3
while (counter < __________) // line 4
{
if ( __________.equals("A") ) // line 5
{
totalA = totalA + __________; // line 6
}
counter++; // line 7
}
return __________; // line 8
}
a. aString.size()
b. aString.size() - 1
c. aString.length
*d. aString.length()
e. aString.length() - 1
f. "
g. "
h. "
i. "
j. "
General Feedback:
The variable counter is being used as an index into the string that is being examined, and from line 7 it is clear that this index is increasing on each loop iteration, moving from left to right. Because the loop test uses the < operator, the correct upper limit is aString.length(). Remember that strings provide a length() method for obtaining their length (size() is used for containers like lists and maps, and length written as a field reference instead of a method call is used for arrays).
633249
Consider the code
int i, *q, *p;
p = &i;
q = p;
*p = 5;
Which of the following will print out “The value is 5.”?
a. printf("The value is %d", &i);
b. printf("The value is %d", p);
*c. printf("The value is %d", *q);
d. printf("The value is %d", *i);
e.
f. "
g. "
h. "
i. "
General Feedback:
p and q are currently "sharing" --- they both point to the same variable (i).
633372
What order must elements 1, 2, 3, 4, 5, 6, and 7 be inserted in a binary search tree such that the tree is perfectly balanced afterward?
a. 4 1 2 3 5 6 7
*b. 4 2 6 1 3 5 7
c. 2 1 3 4 6 5 7
d. 1 2 3 4 5 6 7
e. 2 1 3 6 5 7 4
f. "
g. "
h. "
i. "
j. "
General Feedback:
The middle element 4 will need to be the root, so that must be inserted first. The middle elements of the remaining halves must be 4's children, so 2 and 6 must be inserted next. (Their relative order does not matter.) The last four elements can be inserted in any order.
634925
Which algorithm does the following code implement?
def mystery(target, listOfValues):
beginning = 0
end = len(listOfValues)
found = False
while (! found and (beginning <= end)):
middle = (beginning + end) / 2
if (listOfValues[middle] == target):
found = True
print "Found it at location: ", middle
else:
if (target < listOfValues[middle]):
end = middle -1
else:
beginning = middle + 1
if (! found):
print "Item not found"
a. Short Sequential Search
b. Sequential Search
*c. Binary Search
d. Bubble Search
e. None of the above.
f. "
g. "
h. "
i. "
j. "
General Feedback:
This a classic implementation of binary search.
633310
What abstract data type is best suited to help us implement a breadth-first search?
a. priority queue
*b. queue
c. stack
d. hashtable
e. array-based list
f. "
g. "
h. "
i. "
j. "
General Feedback:
In a breadth-first search, we visit the starting element, its neighbors, its neighbors' neighbors, and so on. We want to make sure we visit the neighbors of the elements we saw earliest before we visit the neighbors of elements we saw later. This suggests a first-in, first out approach.
633220
This sorting algorithm repeatedly examines neighboring elements, swapping those pairs that are out of order.
a. selection sort
b. insertion sort
*c. bubble sort
d. quick sort
e. merge sort
f. "
g. "
h. "
i. "
j. "
General Feedback:
Bubble sort operates by reordering neighbors.
632313
The enqueue operation:
a. adds a new item at the front of the queue
b. returns without removing the item at the front of the queue
c. removes and returns the item at the front of the queue
*d. adds a new item at the end of the queue
e. returns true if the queue is empty and otherwise false
f. "
g. "
h. "
i. "
j. "
General Feedback:
enqueue is the name for the operation that adds an element to a queue. Specifically, it adds each item to the end of the queue (just like standing in line).
633897
If Matthew is entering the values 1, 2, 3, 4, 5 into a complete tree, where insertion happens in level-order, in what order should he enter the values so he produces a binary search tree?
a. 1, 2, 3, 4, 5
*b. 4, 2, 5, 1, 3
c. 3, 1, 5, 2, 4
d. 1, 3, 2, 5, 4
e.
f. "
g. "
h. "
i. "
General Feedback:
Only B will produce a complete BST.
630974
Consider the following code:
if (!somethingIsTrue())
{
return true;
}
else
{
return false;
}
Which replacement for this code will produce the same result?
a. return true;
b. return false;
c. return somethingIsTrue();
*d. return !somethingIsTrue();
e. none of these
f. "
g. "
h. "
i. "
j. "
General Feedback:
The method somethingIsTrue() must return a boolean value, because of the way it is used in the if statement's condition. If it returns the value false, the if statement will cause the value true to be returned; otherwise, the if statement will cause the value false to be returned. Therefore, the entire if statement is equivalent to return !somethingIsTrue();.
633296
You are storing a complete binary tree in an array, with the root at index 0. At what index is node i's right child?
a. 2i
b. 2i + 1
*c. i + i + 2
d. i / 2 + 1
e. (i - 1) / 2
f. "
g. "
h. "
i. "
j. "
General Feedback:
(i - 1) / 2
633572
You've got a byte variable holding the value 127. You add 1 with byte += 1. What happens?
a. It becomes a short with value 128.
b. An OverflowException is raised.
c. It remains a byte and stays at the value 127.
*d. It remains a byte and takes on the value -128.
e. It remains a byte and takes on the value 0.
f. "
g. "
h. "
i. "
j. "
General Feedback:
When you exceed the maximum value of an integral type, you wrap around to other extreme. Bytes range from -128 to 127.
634186
What is the size of the largest min-heap which is also a BST?
a. 2
*b. 1
c. 4
d. 3
e.
f. "
g. "
h. "
i. "
General Feedback:
You cannot add a left child to the root because it would break the BST/min-heap property.
634968
Which of the following is true about both interfaces and abstract classes?
a. They do not have constructors
b. All of their methods are abstract
c. They can have both abstract and non-abstract methods
d. They can be used to model shared capabilities of unrelated classes
*e. None of the above
f. "
g. "
h. "
i. "
j. "
General Feedback:
Abstract methods have constructors, interfaces do not
An anstract class can include both abstract and non-abstract methods, while all methods in interfaces are abstract
An abstract class can model shared capabilities of classes, but only those who share the abstract class as an ancestor (so related), whereas there is no such restriction on interfaces.
634940
Jacob has written some code using our standard linked list node definition (it has an int val and a struct node *next, in that order ):
struct node *my_node = malloc(sizeof(struct node));
my_node->val = 4;
my_node->next = NULL;
struct node **ptr_node = &my_node;
*(*ptr_node)++;
If the value of my_node is initially 0x50085008, what will the value of my_node most likely be after this code?
a. 0x50085008
b. 0x50085010
c. 0x5008500C
*d. 0x50085018
e.
f. "
g. "
h. "
i. "
General Feedback:
You would it expect it to be 0x50085014 (0x50085008 + size of int + size of pointer), but C actually does address padding, pushing it up to 0x50085018, which is wha you would see if you run the code.
633552
A coworker suggests that when you delete a node from a hashtable that you just set it to null. What is your response?
a. Yes, that way the garbage collector can reclaim the memory.
b. Yes, that will speed up probing.
c. No, the user may have alias references to the node.
*d. No, doing so may make other nodes irretrievable.
e.
f. "
g. "
h. "
i. "
General Feedback:
When using probing to resolve collisions, the probing algorithm walks along the probing sequence until it finds a null element. If nulls appear in the wrong places, probing may terminate earlier than it should.
634431
Suppose you are trying to choose between an array and a singly linked list to store the data in your program. Which data structure must be traversed one element at a time to reach a particular point?
a. an array
*b. a linked list
c. both
d. neither
e.
f. "
g. "
h. "
i. "
General Feedback:
In a basic linked list, each element is connected to the one after it. To reach a given element, you must start with the first node, if it's not the one you want, follow the link to the next one, and so forth until you reach the end of the list. (These are called "singly linked lists"; it is also possible to construct a doubly linked list, where each node is connected to the previous *and* the next element.)
634945
Richard has the following struct:
struct node{
int val;
struct node *next;
};
And he creates a node:
struct node **ptr = &( malloc(sizeof(struct node)) );
Which of the following will NOT set ptr’s associated val to 6?
*a. (***ptr).val = 6
b. (**ptr).val = 6
c. (*ptr)->val = 6
d.
e.
f. "
g. "
h. "
General Feedback:
***ptr is not useful here
627766
Consider the following recursive method:
public int examMethod(int n) {
if (n == 1) return 1;
else return (n + this.examMethod(n-1));
}
What value is returned by the call examMethod(16)?
a. 1
b. 16
*c. 136
d. None of the above.
e.
f. "
g. "
h. "
i. "
General Feedback:
This method returns the sum of the numbers from 1 to the parameter n (as long as n is greater than or equal to 1).
632575
Suppose you place m items in a hash table with an array size of s. What is the correct formula for the load factor?
a. s + m
b. s â?? m
c. m â?? s
d. s/m
*e. m/s
f. "
g. "
h. "
i. "
j. "
General Feedback:
The load factor is the number of elements in the array, divided by the size of the array. It gives you an idea of how full the hashtable is.
634916
The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13 ... Any term (value) of the sequence that follows the first two terms (0 and 1) is equal to the sum of the preceding two terms. Consider the following incomplete method to compute any term of the Fibonacci sequence:
public static int fibonacci(int term)
{
int fib1 = 0; // Line 1
int fib2 = 1; // Line 2
int fibn = 0; // Line 3
if (term == 1) // Line 4
{
return fib1; // Line 5
}
if (term == 2) // Line 6
{
return fib2; // Line 7
}
for (__________) // Line 8: loop to the nth term
{
fibn = __________; // Line 9: compute the next term
fib1 = __________; // Line 10: reset the second preceding term
fib2 = __________; // Line 11: reset the immediate preceding term
}
return fibn; // Line 12: return the computed term
}
Choose the best answer to fill in the blank on line 8.
a. int n = 0; n < term; n++
*b. int n = 1; n < term; n++
c. int n = 2; n < term; n++
d. int n = 3; n < term; n++
e. int n = 4; n < term; n++
f. "
g. "
h. "
i. "
j. "
General Feedback:
From the question, it is clear that the terms in the sequence are numbered starting at 1. The two base cases cover terms 1 and 2, and the loop must then repeat (term - 2) times in total. This will be achieved if the initial value on the loop counter is 1.
633661
Which of these relationships best exemplify the "IS-A" relationships in object-oriented programming (OOP)?
a. Empire State Building IS-A Building
*b. Cat IS-A Mammal
c. Angry Cat IS-A Cat
(Note that "Angry Cat" is a specific cat that has become an online internet meme)
d. All of the above
e. None of the above
f. "
g. "
h. "
i. "
j. "
General Feedback:
A and C are wrong because the Empire State Building would be best be described as an instance of the category Cuilding, while Angry Cat is an instance of the category Cat.
B is correct because Cat is a sub-category of Mammal, which best exemplifies the IS-A relations. In OOP, the IS-A relationship is used to denote relationships between classes, which are sort of like categories.
632850
What does this print when x is assigned 1?
if (x==1) {
System.out.println("x is 1");
} if (x==2) {
System.out.println("x is 2");
} else {
System.out.println("not 1 or 2");
}
a. x is 1
b. x is 2
c. x is 1
x is 2
*d. x is 1
not 1 or 2
e. x is 2
not 1 or 2
f. "
g. "
h. "
i. "
j. "
General Feedback:
This code snipped does not have an else-if! It may as well be written like this:
if (x==1) {
System.out.println("x is 1");
}
if (x==2) {
System.out.println("x is 2");
} else {
System.out.println("not 1 or 2");
}
So when x==1, clearly we get "x is 1" printed, bu since 1 does not equal 2, we also get "not 1 or 2" with the else.
Be aware of when you are using an if, and when are are using an else-if, because they are quite different!
634127
In C, which of the following will return 1, if s1 = "hi" and s2 = "hi"? Circle all that apply.
a. s1 == s2
*b. strcmp(s1, s2)
c. strlen(s1)
d. s1 == "hi"
e.
f. "
g. "
h. "
i. "
General Feedback:
C is not Python! Only strcmp will do what Python and similar languages would do. (My CS2 students come into C having learnt Python.)
633291
Inserting a node into a heap is
a. O(1)
*b. O(log N)
c. O(N)
d. O(N log N)
e. O(N2)
f. "
g. "
h. "
i. "
j. "
General Feedback:
Inserting a node involves putting the new node into the bottom level of the heap and trickling up, possibly to the root level. Since heaps are balanced and the number of levels is log N, the performance is O(log N).
630996
Consider the following Java method:
public void printMenu(){
System.out.println("Choose one of the following options:");
System.out.println("1) Display account balance");
System.out.println("2) Make deposit");
System.out.println("3) Make withdrawal");
System.out.println("4) Quit");
}
Select the best reason for making this a separate method with a name, instead of including the code in some other method:
*a. Otherwise, you would have to write the same code more than once.
b. By breaking your program up into logical parts, you make it more readable.
c. By giving this block of code a name, you make your program more readable.
d. It's necessary to keep the calling method under 20 lines long.
e.
f. "
g. "
h. "
i. "
General Feedback:
There are multiple correct answers to this question. A, B, and C are all reasonable answers. D is less good, because while it's a good idea to keep your methods relatively short, it won't always make sense to stick to an arbitrary limit like 20 lines.
635037
A given O(n2) algorithm takes five seconds to execute on a data set size of 100. Using the same computer and the same algorithm, how many seconds should this algorithm run for when executed on a data set of size 500?
a. 25 seconds
b. 100 seconds
c. 42 seconds
*d. 125 seconds
e. None of the above.
f. "
g. "
h. "
i. "
j. "
General Feedback:
Quadratic algorithms represent an n2 increase in run time. Hence if the data set size increases by a factor of five, for a quadratic algorithm, the increase in run time becomes a factor of 25. Hence 25 times 5 (base run time) is 125.
632280
If the StackADT operation push is implemented using a linked list, the big-Oh time complexity of the method is:
*a. O(1)
b. O(log2n)
c. O(n)
d. O(n2)
e. O(2n)
f. "
g. "
h. "
i. "
j. "
General Feedback:
The push method adds a new element to a stack. Regardless of the size of the list, the operations needed to add a new element include creating a node to store it in and setting the values of a few pointers, all of which can be done in constant time.
618622
Which line of the following code has a compilation error?
import java.util.*;
public class bicycles {
public static void main(String[] Main) {
Vector