CS 030 Final Review

A completely filled binary tree with a height of 4 has ____ nodes.
Select one:
a. 8
b. 16
c. 12
d. 15

D

A balanced binary tree with 260 nodes has a height of approximately ____.
Select one:
a. 12
b. 8
c. 13
d. 10

B

What is the efficiency of adding an element to a red-black tree?
Select one:
a. O(1)
b. O(log (n))
c. O(n)
d. O(n2)

B

Removing an element from a balanced binary search tree takes ____ time.
Select one:
a. O(n)
b. O(log (n))
c. O(1)
d. O(n2)

B

The height of a tree can be obtained by recursively computing the heights of its subtrees, while keeping track of the height of the deepest subtree. Given the Node class (partially shown below), select an expression to complete the recursive method height

C

What is the efficiency of the heapsort algorithm?
Select one:
a. O(log (n))
b. O(n2)
c. O(1)
d. O(n log (n))

D

If the postorder traversal of an expression tree is 8, 2, +, 5, /, what is the preorder traversal?
Select one:
a. 8, +, 2, /, 5
b. /, +, 8, 2, 5
c. +, /, 8, 2, 5
d. /, +, 2, 8, 5

B

Adding an element to a balanced binary search tree takes ____ time.
Select one:
a. O(1)
b. O(n)
c. O(n2)
d. O(log (n))

D

If the child references of a binary tree node are both null, the node is ____.
Select one:
a. a parent node
b. a root node
c. an interior node
d. a leaf node

D

You wish to traverse a binary search tree in sorted order. Arrange the following actions in the correct order to accomplish this.
I Print the right subtree recursively
II Print the root
III Print the left subtree recursively
Select one:
a. III, II, I
b. I

A

Which of the following statements about the heapsort algorithm is correct?
Select one:
a. The heapsort algorithm is based on inserting elements into a heap and removing them in sorted order.
b. Each insertion and removal is O(n log(n)).
c. The heapsort so

A

A portion of your program implements a loop over n elements in which each step contains a fixed number of actions. What can you conclude about the running time of this section of code?
Select one:
a. Its running time will be O(n).
b. Its running time will

A

When the size of an array increases by a factor of 100, the time required by selection sort increases by a factor of ____.
Select one:
a. 5,000
b. 10,000
c. 12,000
d. 2,000

B

Given an ordered array with 15 elements, how many elements must be visited in the worst case of binary search?
Select one:
a. 2
b. 3
c. 8
d. 4

D

Suppose objects a and b are from a user-defined class that implements the Comparable interface. What must be true about the return value of a.compareTo(b) for the compareTo method that this class implements?
Select one:
a. It must return 1 if a comes befo

C

Suppose we are using binary search on an array with approximately 1,000,000 elements. How many visits should we expect to make in the worst case?
Select one:
a. 1
b. 30
c. 20
d. 16

C

An algorithm that cuts the work in half in each step is an ____ algorithm.
Select one:
a. O(n log (n))
b. O(n)
c. O(log (n))
d. O(n^2)

C

Consider an array with n elements. If we visit each element n times, how many total visits will there be?
Select one:
a. n^2
b. 2^n
c. n
d. n^n

A

In the worst case, quicksort is a(n) ____ algorithm.
Select one:
a. O(log(n))
b. O(n)
c. O(n2)
d. O(n log n)

C

Consider the swap method shown below from the SelectionSorter class. If we modified it as shown in the swap2 method shown below, what would be the effect on the sort method?
private static void swap(int[] a, int i, int j)
{
int temp = a[i];
a[i] = a[j];
a

B

In Big-Oh notation, selection sort is a(n) ____ algorithm.
Select one:
a. O(n^2)
b. O(log n2)
c. O(1)
d. log n

A

What is the worst-case performance of insertion sort?
Select one:
a. O(n log (n))
b. O(log (n))
c. O(n2)
d. O(n)

C

Which sort algorithm starts by partitioning the array and selecting a pivot element?
Select one:
a. selection sort
b. merge sort
c. insertion sort
d. quicksort

D

The ____ class contains a sort method that can sort array lists.
Select one:
a. Linear
b. Collections
c. Sorting
d. Arrays

B

Can you search the following array using binary search?
int[] A = {6, 5, 4, 2, 0, 1, -1, -17};
Select one:
a. No, negative numbers are not allowed because they indicate that a value is not present.
b. No. Binary search can be applied to a sorted array onl

B

he method findLargest examines the elements of an array arr which contains non-negative values
public static int findLargest(int[] arr)
{
int curLargest = -1;
for (int i = 0; i < arr.length; i++)
{
if (arr[i] >= curLargest)
{
curLargest = arr[i];
}
}
retu

C

The partial binary search method below is designed to search an array of String objects sorted in ascending order. Select the expression that would be needed to complete the method.
public static int search(String[] a, int low, int high, String item)
{
if

D

The method checkArray examines an array arr:
public static boolean checkArray(int[] arr)
{
if (arr[0] >= arr[arr.length -1])
{
return true;
}
return false;
}
What can you conclude about the running time of this section of code?
Select one:
a. Its running

B

The following code is an example of a ___ search.
public static int search(int[] a, int v)
{
for (int i = 0; i < a.length; i++)
{
if (a[i] == v) { return i; }
}
return -1;
}
Select one:
a. binary
b. linear
c. sorted
d. random

B

Find the simplest order of growth of the following expression: (n^3 + n + 3)^2.
Select one:
a. ?(n^6)
b. ?(2^n)
c. ?(n^5)
d. ?(n^9)

A

Consider the sort method for selection sort shown below:
public static void sort(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
int minPos = minimumPosition(i);
swap(minPos, i);
}
}
Suppose we modify the loop control to read int i = 1; i < a.length -

D

Which of the following statements about a binary search tree is correct?
Select one:
a. Adding elements that are already sorted will result in a balanced binary search tree.
b. Locating an element in a balanced binary search tree takes O(n) time.
c. You c

d. The speed of inserting or removing a node is dependent on the shape of the tree.

Given the BinarySearchTree and Node classes (partially shown below), select an expression to complete the recursive method smallest in the Node class. The method returns the smallest data value in the binary search tree rooted at a node.
public class Bina

b. left.smallest()

If the postorder traversal of an expression tree is, 8, 2, +, 5, /, what is the result of an inorder traversal?
Select one:
a. 8, +, 2, /, 5
b. /, +, 8, 2, 5
c. +, /, 8, 2, 5
d. /, +, 2, 8, 5

a. 8, +, 2, /, 5

Consider the following tree diagram:
What is the size of this tree?
Select one:
a. 4
b. 13
c. 6
d. 5

b. 13

A completely filled binary tree with a height of 4 has ____ nodes.
Select one:
a. 15
b. 16
c. 8
d. 12

a. 15

In a binary search tree, where the root node data value = 45, what do we know about the values of all the descendants in the right subtree of the root?
Select one:
a. all will be > 45
b. approximately half the values are < 45, the other half are > 45
c. t

a. all will be > 45

Which of the following statements about breadth-first and depth-first traversal is NOT correct?
Select one:
a. Depth-first search uses a stack to track the nodes that it visits.
b. Breadth-first search first visits all nodes on the same level before visit

c. Breadth-first and depth-first search only work on a binary tree.

A balanced binary tree with 260 nodes has a height of approximately ____.
Select one:
a. 10
b. 8
c. 12
d. 13

b. 8

Which of the following statements about the three tree traversal schemes studied is correct?
Select one:
a. Postorder traversal is used for copying file directories.
b. Postorder traversal is used for removing file directories by removing subdirectories f

b. Postorder traversal is used for removing file directories by removing subdirectories first.

Adding an element to a balanced binary search tree takes ____ time.
Select one:
a. O(1)
b. O(n)
c. O(n2)
d. O(log (n))

d. O(log (n))

If a min-heap has 14 nodes, what is known for certain when we add a new node?
I every level of the tree will be fully occupied
II the tree does not grow a new level
III the root contains the smallest element
Select one:
a. I only
b. I, II and III
c. I and

b. I, II and III

Which of the following statements about inserting a node into a red-black tree is correct?
Select one:
a. If the parent of the new node is red, no other actions are required.
b. If it is the first node, it must be red.
c. Color the new node black.
d. If a

d. If a double-red situation results, you must correct this.

Assuming the programmer wishes to output the phrase "Welcome!", which of the following is true about the following Java statement.
System.out.Println("Wlcome!");
Select one:
a. There is a compile-time error.
b. There is a run-time error.
c. There are mult

C

The line public class HelloPrinter indicates which declaration below?
a. Declaration of the class HelloPrinter.
b. Declaration of the variable public.
c. Declaration of the class public.
d. Declaration of the variable class.

A

The error message "cannot find symbol" is usually a good clue that what kind of error has been made?
a. division by zero
b. spelling
c. logic
d. run-time

B

In Java, every statement must end with which symbol?
a. !
b. .
c. )
d. ;

D

What term is used to refer to an individual instruction inside a method?
a. statement
b. comment
c. object
d. constant

A

Evaluate the given pseudocode to calculate the weighted score for a student:
The program score (program) = 92
The weight of programs (pgmWeight) = 40%
The exam score (exams) = 85
The weight of exams(exWeight) = 60%
input program
input pgmWeight
input exam

C

Whenever a method is called in Java, what must be specified?
Select one:
a. method name, arguments
b. program name, method name
c. strings, method name
d. the main method, arguments

A

What is the output of the following Java statement?
System.out.println("4 + 6");
Select one:
a. 10
b. 4 + 6
c. 46
d. 4

B

Who or what is responsible for inspecting and testing the program to guard against logic errors?
Select one:
a. programmer
b. compiler
c. end-user
d. JVM

A

What is the purpose of the following algorithm?
input somenum
Repeat the following steps for 14 times
input variable1
if variable1 < somenum then
somenum = variable1
print somenum
Select one:
a. To search for a particular number among 15 numbers.
b. To fi

D

Evaluate the given pseudocode to calculate the efficiency of a vehicle's fuel consumption using the following test values:
The trip odometer reading (odometer) = 300
The amount to fill the gas tank (amount) = 15
input odometer
input amount
output odometer

D

Suppose that a computer virus infects your computer and corrupts the files you were going to submit for your current homework assignment. What precaution could have saved you from a disastrously bad grade for this assignment?
Select one:
a. Purchase an ex

C

Which statement regarding computer programs is correct?
Select one:
a. Computer programs can decide what task to perform.
b. Computer programs are composed of extremely primitive operations.
c. Small computer programs are not documented.
d. Large and comp

B

What term is used to refer to an informal description of a sequence of steps for solving a problem?
Select one:
a. pseudocode
b. assembly language instructions
c. Java virtual machine instructions
d. machine instructions for a specific CPU

A

What is the argument in the given method call?
System.out.println("Welcome");
Select one:
a. "Welcome"
b. println
c. System
d. out

A

A sequence of steps that eventually comes to an end is ______________.
Select one:
a. executable
b. documented
c. terminating
d. unambiguous

C

Which of the following options is true about algorithms?
Select one:
a. Algorithms are described informally and can contain ambiguous steps.
b. Algorithms can replace the source code in programs.
c. Algorithms are written in a programming language.
d. You

D

What is the purpose of the following algorithm?
input num
Repeat the following steps for 9 times
input var1
if var1 > num then
num = var1
print num
Select one:
a. To find the smallest among 10 numbers
b. To search for a particular number among 10 numbers

D

Complete the following code snippet, which is intended to be a recursive method that reverses a String value:
public static String reverseIt(String s)
{
if (s.length() <= 1)
{
return s;
}
else
{
________________________
}
}
Select one:
a. return reverseIt

C

Assume that recursive method search returns true if argument value is one of the elements in the section of the array limited by the firstIndex and lastIndex arguments. What statement can be used in main to determine if the value 7 is one of the elements

B

When a recursive method is called correctly, and it does not perform recursion, what must be true?
Select one:
a. A terminating condition was true.
b. One recursive case condition was true.
c. All recursive case conditions were true.
d. Both a terminating

A

Which of the following statements about recursion is correct?
Select one:
a. In many cases, a recursive solution may be easier to understand and to implement than an iterative solution.
b. A recursive solution will always run faster than an equivalent ite

A

Consider the getArea method from the textbook shown below.
public int getArea()
{
if (width <= 0) { return 0; } // line #1
else if (width == 1) { return 1; } // line #2
else
{
Triangle smallerTriangle = new Triangle(width - 1); // line #3
int smallerArea

C

Recursion does NOT take place if any of the following happen:
I method A calls method B, which calls method C, which calls method B
II method A calls method B, which calls method A
III method A calls method B, B returns, and A calls B again
Select one:
a.

A

Consider the recursive version of the fib method from the textbook shown below:
public static long fib(int n)
{
if (n <= 2)
{
return 1;
}
else
{
return fib(n - 1) + fib(n - 2);
}
}
How many recursive calls to fib(2) will be made from the original call of

C

Given the following code snippet:
public static int newCalc(int n)
{
if (n < 0)
{
return -1;
}
else if (n < 10)
{
return n;
}
else
{
return (1 + newCalc(n / 10));
}
}
What value will be returned when this code is executed with a call to newCalc(15)?
Selec

B

Which of the following is NOT true about debugging a recursive method by setting a breakpoint on the line containing a return statement?
Select one:
a. The debugger's call stack will show all the calls to the recursive method that are pending.
b. The last

D

In a _____________, a set of cooperating methods calls each other repeatedly.
Select one:
a. mutual recursion
b. permutation
c. stack overflow error
d. infinite recursion

A

Assuming the programmer wishes to output the phrase "Welcome!", Which statement is true about the following Java statement:
System.out.println("Welcome!");
Select one:
a. There is a compile-time error.
b. There are no errors.
c. There are multiple errors.

B

What is the term used to specify the collection of things you can do with objects that belong to a class?
Select one:
a. private implementation
b. hidden implementation
c. public interface
d. private interface

C

Assume the class Circle has an accessor called getRadius and a mutator called setRadius. What is the output of the following code?
Circle c1 = new Circle(3);
Circle c2 = c1;
c1.setRadius(4);
System.out.println(c2.getRadius());
Select one:
a. 4
b. 3
c. 8
d

A

What is the output of the following code:
int num1 = 6;
int num2 = num1;
num2 = num2 + 10;
System.out.println(num1);
Select one:
a. 16
b. 10
c. 6
d. 4

C

Which method call represents the invocation of a method that does not have arguments?
Select one:
a. greeting.length
b. greeting.length()
c. System.out.println(greeting);
d. greeting.replace("Hello", "Welcome");

B

Assume that the variable count has been declared as type int. Which statement adds 10 to count?
Select one:
a. count + 10;
b. count = count + 10;
c. count == count + 10;
d. count = 10;

B

Which of the following declares a variable that will store a measurement with fractional parts?
Select one:
a. int measure;
b. integer measure;
c. String measure;
d. double measure;

D

Complete this code fragment to ensure that the frame is shown:
JFrame frame = new JFrame();
Select one:
a. frame.setVisible();
b. frame.setVisible(true);
c. frame.visible = true;
d. JFrame.setVisible();

B

If greeting refers to a String object, which of the following is a syntactically correct Java statement?
Select one:
a. System.out.println(length().greeting);
b. greeting.println("Hello");
c. System.out.println(greeting.length());
d. System.out.println(gr

C

What is the declared return type for a method that does not have a return value?
Select one:
a. String
b. There is no declared return type when a method does not return a value.
c. void
d. A method must return a value.

C

What is the name of the type that denotes a string of characters?
Select one:
a. char
b. Characters
c. charString
d. String

D

A method header consists of which of the following parts?
Select one:
a. an access specifier, a return type, a method name, and a list of the parameters (if any)
b. an access specifier, the type of the instance variable, and the name of the instance varia

A

In the statement below, amount is referred to as the ____ parameter.
public void deposit(double amount)
Select one:
a. implicit
b. public
c. explicit
d. private

C

The javadoc utility is used to
Select one:
a. convert the Java program into machine-executable code.
b. ensure that comments are appropriately descriptive.
c. assist the compiler in checking the syntax of the Java program.
d. automatically generate HTML p

D

Fill in the blank in the comment for this method header.
/**
Gets the interest for the bank account
_________________________________
*/
public double getInterest()
. . .
Select one:
a. @return double the interest
b. @return the interest
c. return double

B

What is the name of the constructor for the BankAccount class?
Select one:
a. withdraw
b. BankAccount
c. balance
d. deposit

B

Which statement about private instance variables is true?
Select one:
a. They can only be accessed by methods of a different class
b. They can only be accessed by methods of the same class
c. They can only be accessed by the constructor of the class
d. Th

B

An instance variable declaration consists of which of the following parts?
Select one:
a. the type of the instance variable, an access specifier, a list of the parameters (if any), and the body of the method.
b. the return type, the name of the method, an

C

Fill in the third line of this SquareTester program so that it prints out the expected outcome.
public class SquareTester
{
public static void main(String[] args)
{
/*
Step 1: declare and initialize a variable mySquare as an
instance of a Square class wit

C

When tracing the execution of an object by hand, what is one way to indicate the change of an instance variable when a mutator method is executed?
Select one:
a. Cross out the old value and write down the new value.
b. Turn the card over and check the "mu

A

The black boxes from which a program is manufactured are called ___.
Select one:
a. objects
b. access specifiers
c. methods
d. instance variables

A

Given this method comment, fill in the blank in the method implementation.
/**
Gets the current balance of the bank account
@return the current balance
*/
public double getBalance()
{
__________ balance;
}
Select one:
a. double
b. return
c. balance
d. @re

B

Consider the following method comment and method header:
/**
Converts from a source measurement to a target measurement.
@param _______________ the measurement
@return the input value converted to the target unit
*/
public double convertTo(double fromMeas

B

The private implementation of a class consists of ___.
Select one:
a. parameter variables and the method bodies
b. instance variables and the implementation of the constructors and methods
c. local variables and the method headers
d. instance variables an

B

What is the name of the instance variable for a Counter object?
Select one:
a. concertCounter
b. click
c. getValue
d. value

D

Which statement is true about the following constructor of the BankAccount class?
public BankAccount(double balance)
{
this.balance = balance;
}
Select one:
a. You can't have an instance variable and a parameter variable with the same name.
b. The code ha

D

When are instance variables initialized?
Select one:
a. You must initialize instance variables in a method body.
b. Instance variables are initialized with a default value before a constructor is invoked.
c. Instance variables are initialized when the met

B

Consider the following method comment and method header:
/**
Converts from a source measurement to a target measurement.
@param fromMeasurement the measurement
__________ the input value converted to the target unit
*/
public double convertTo(double fromM

B

Which of the following corresponds to the getArea method header for a Square class assuming an integer value for a side length?
Select one:
a. public integer getArea()
b. public int getArea
c. public integer getArea
d. public int getArea()

D

Given the following constructor for the BankAccount class, which references the instance variable balance declared to be of type double, what output is generated by a call to new BankAccount()?
public BankAccount()
{
System.out.println(balance);
}
Select

A

We want to create a class that represents a geometric sequence. A geometric sequence is a sequence of numbers that begin at some value and then multiplies each value by some constant to get the next value. For example, the geometric sequence 1, 2, 4, 8, 1

A

What happens to the fractional part when a division is performed on two integer variables?
Select one:
a. Instead of using an integer division, you should use the modulus operator to perform floating-point division.
b. The fractional part is rounded off t

C

What will be printed by the statements below?
final int BONUS = 20;
int salary = 100;
int netSalary = salary + BONUS;
System.out.print(netSalary);
Select one:
a. salaryBONUS
b. Nothing will be printed because constants cannot be used in expressions.
c. 10

D

What will be the value of the variables x and y after the given set of assignments?
int x = 20;
int y = 10;
x = (x - y) * 2;
y = x / 2;
Select one:
a. x = 40, y = 20
b. x = 10, y = 20
c. x = 20, y = 10
d. x = 20, y = 20

C

How do you compute the length of the string str?
Select one:
a. length(str)
b. str.length
c. str.length()
d. length.str

C

Which is the Java equivalent of the following mathematical expression?
c = ?(a2 + b2)
Select one:
a. c = Math.sqrt(a
2) + Math.sqrt(b
2);
b. c = Math.sqrt(a
2 + b
2);
c. c = Math.sqrt(Math.pow(a, 2)) + Math.sqrt(Math.pow(b, 2));
d. c = Math.sqrt(Math.pow(

D

What does the following statement sequence print if the user input is 123?
Scanner in = new Scanner(System.in);
System.out.print("Enter a number ");
int myInt = in.nextInt();
myInt += 456;
System.out.println(myInt);
Select one:
a. Run-time error
b. Compil

D

Which of the following is the Java equivalent of the following mathematical expression?
p = 2 � ? � radius3
Select one:
a. p = Math.PI * Math.pow(3,radius);
b. p = 2
Math.PI
Math.pow(radius, 3);
c. p = 2
Math.PI
(radius * 3);
d. p = 2
Math.pow(Math.PI
rad

B

Which one of the following statements gives the absolute value of the floating-point number x = -25.50?
Select one:
a. x.absolute();
b. Math.abs(x);
c. x.abs();
d. abs(x);

B

What will be the value stored in the variable x after the execution of the following code snippet?
int a = 10;
int b = 20;
int c = 2;
int x = b / a /
c
/;
Select one:
a. 2
b. The code has a syntax error
c. 1
d. 4

A

Which one of the following statements displays the output as -1.23e+02?
Select one:
a. System.out.printf("%5.2E", -123.0);
b. System.out.printf("^5.2e", -123.0);
c. System.out.printf("%5.1e", -123.0);
d. System.out.printf("%5.2e", -123.0);

D

What is the value of the following expression?
1 % 12
Select one:
a. -11
b. This is an error because 12 is greater than 1
c. 1
d. 0

C

Which of the following guidelines will make code more explanatory for others?
Select one:
a. Use shorter statements and shorter variable names in source code.
b. Avoid usage of complex calculations in source code.
c. Add comments to source code.
d. Always

C

Assume the following variable has been declared and given values as shown:
String name = "Mamey, Jean";
Which statement will print the name as "Jean Mamey"?
Select one:
a. System.out.print(name.substring(2) + " " + name.substring(1));
b. System.out.print(

B

What (if any) type of error occurs with the following code if the user input is ABC?
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
String str = in.next();
int count = Integer.parseInt(str);
System.out.println("Input is " + cou

C

What is the output of the following code snippet?
int num1 = 10;
int num2 = 5;
int num3 = 200;
num3 = num3 % (num1 * num2);
System.out.println(num3);
Select one:
a. 10
b. 0
c. 250
d. 4

B

Assume the following variables have been declared and given values as shown:
int i = 2345;
double m = 67.8;
What will be printed by the statement below?
System.out.printf("Values are %10d and %7.2f", i, j);
Select one:
a. Values are 2345 and 67.8
b. Value

D

What is wrong with the following code snippet?
int average;
average = 78A;
Select one:
a. The average variable is assigned a non-numeric value.
b. The data type for the average variable is not specified.
c. The average variable is never initialized.
d. Th

A

Why is the double type not appropriate for financial calculations?
Select one:
a. The calculations are too slow.
b. There are too few significant digits.
c. Roundoff errors occur when an exact representation of a floating point number is not possible.
d.

C

What term is used to refer to text in a program that is an explanation for human readers of the code?
Select one:
a. [
and
]
b. constants
c. methods
d. comments

D

A method name is ____________________ if a class has more than one method with that name (but different parameter types).
Select one:
a. overloaded
b. overimplemented
c. overridden
d. overwhelmed

A

Which package is automatically imported in any Java program?
Select one:
a. java.lang
b. java.util
c. java.system
d. java.language

A

Assuming the following Java statement:
int num = 10;
What does the variable num store?
Select one:
a. A reference to the memory location where the value 10 is stored.
b. The numeric value 10.
c. A reference to the int primitive type.
d. An object represen

B

What is the nickname for the graphical user interface library in Java?
Select one:
a. GUI
b. Swing
c. JComponent
d. Applet

B

Based on the following statement, which of the following statements sets the title of the frame:
JFrame frame = new JFrame();
Select one:
a. frame.addTitle("An Empty Frame");
b. frame.setTitle(JFrame.EMPTY);
c. frame.setTitle("An Empty Frame");
d. frame.t

C

Which of the following represents a method declaration with a void return type?
Select one:
a. void int getValue() { ... }
b. public void int getValue() { ... }
c. void public setValue(int value) { ... }
d. public void setValue(int value) { ... }

D

What is the name of the = operator in Java?
Select one:
a. assignment
b. identity
c. equality
d. inequality

A

The Java compiler ignores any text between ____.
Select one:
a. {
and
}
b. /
and
/
c. // and //
d. (
and
)

B

What is the output of the following code:
int num1 = 6;
int num2 = 10;
num1 = num2;
num2 = num1;
System.out.println(num1 + ", " + num2);
Select one:
a. 10, 6
b. 6, 6
c. 6, 10
d. 10, 10

D

What feature of the ArrayList class makes it much better for a binary search than the LinkedList class?
Select one:
a. it is a generic class
b. it is an abstract class
c. has no link references
d. indexing

d

What are Collisions in computer science?
Select one:
a. Identical hash codes for different objects
b. Attempting to assign a list as a map
c. A NullPointerException
d. When an iterator points into the middle of a node chain

a

Array lists and linked lists both have the same ____.
Select one:
a. add/remove efficiency
b. random element access efficiency
c. concrete implementations
d. linear traversal step efficiency

d

Using the textbook's implementation of a linked list, why is the LinkedListIterator inner class NOT declared as an inner static class?
Select one:
a. Because the LinkedList class must have access to the instance variables of the LinkedListIterator class.

d

An array list maintains a reference to an array of elements called a ____.
Select one:
a. tree map
b. buffer
c. bucket
d. hash table

b

Suppose we maintain an array A of n int values as follows:
A[0] < A[1] < . . . < A[i] > A[i + 1] > A[i + 2] > . . . > A[n - 1]
The ith element is the maximum in the array. What would be the lowest big-Oh notation for finding that element? Consider a varia

b

Which of the following actions must be taken to remove a node X from the middle of a doubly-linked list?
I Update the next reference in the node before X
II Update the previous reference in the node after X
III Update the list's first reference
Select one

a

Which of the following statements about using iterators with hash tables is NOT correct?
Select one:
a. The iterator must track the bucket number.
b. The iterator must skip over empty buckets.
c. Two iterators are required: one to traverse the buckets, an

c

Consider the following code snippet, which computes h, the array index for storing x in a hash table.
int h = x.hashCode();
if (h < 0) { h = -h; }
h = h % size;
What does size correspond to?
Select one:
a. The number of elements to be stored.
b. The index

d

If we want a create a doubly-linked list data structure so that we can move from a node to the next node as well as to a previous node, we need to add a previous reference to the Node class. Each such DNode (doubly-linked Node) will have a data portion an

c

Which statement about handling collisions in a hash table using the open addressing technique is correct?
Select one:
a. To find an element, you must search from the hash code location until a match or an element with a different hash code is found.
b. Th

b

Consider the following code snippet:
LinkedList<String> words = new LinkedList<String>();
words.addFirst("xyz");
words.addLast("jkl");
words.addLast("def");
System.out.print(words.removeFirst());
System.out.print(words.removeLast());
System.out.print(word

b

The advantage of using the open addressing technique over the separate chaining technique for handling collisions in a hash table is that open addressing ____.
Select one:
a. is simpler in implementation
b. requires less memory storage for the hash table

b

Which of the following actions must be taken to add a node X at the end of a doubly-linked list?
I Update the next reference in the node before the position where X will be placed
II Update the previous reference in the node before the position where X wi

b

Which of the following statements are true about the Node class?
I The instance variables are private
II It holds a reference first to the first node
III It is a private inner class of the LinkedList class
Select one:
a. All of the above
b. III
c. II
d. I

b

A hash function is considered good if it ____.
Select one:
a. does not require compression.
b. minimizes collisions.
c. results in low integer values.
d. detects duplicate elements.

b

Which of the following statements about removing a node from a linked list is correct?
Select one:
a. A node's data is returned to the program when the node is removed from the linked list, and its memory space is reclaimed later by the garbage collector.

a

Assume that you have a hash table in which there are few or no collisions. What is the time required to remove an element from this hash table?
Select one:
a. O(log (n))
b. O(1)
c. O(n2)
d. O(n)

b

Complete the following code, which is intended to add an element to the top of a stack implemented as a linked list.
Node newNode = new Node();
newNode.data = element;
_________________
_________________
Select one:
a.
first = newNode;
newNode.previous =

b

Given an array myArray, which of the following represents an expression to determine the type of the elements of the array?
Select one:
a. myArray.getComponentType().getClass()
b. myArray[0].getClass()
c. myArray.getClass().getComponentType()
d. myArray.g

c. myArray.getClass().getComponentType()

Consider the following code snippet that declares the GraduateStudent class:
public GraduateStudent extends Student { . . .}
Which of these statements are false?
I GraduateStudent is a subclass of Student
II Stack<GraduateStudent> is a subclass of Stack<S

c. II and III only

Consider the following code snippet:
public static <E extends Measurable> E min(E[] objects)
Which of the following represents the result of type erasure on this code?
Select one:
a. This code is illegal and type erasure will not occur.
b. public static M

b. public static Measurable min(Measurable[] objects)

Suppose a generic method accepts a generic unconstrained argument p. What restrictions are placed on p by the compiler?
Select one:
a. Can only execute Object class methods.
b. There are no restrictions.
c. Can not execute any methods.
d. Can only execute

a. Can only execute Object class methods.

Consider the following code snippet:
public class Box<E>
{
private E data;
public Box() { . . . }
public void insert(E value) { . . . }
public E getData() { . . . }
}
What will result from executing the following code?
Box<Boolean> box = new Box<>();
Box

d. compile-time error

Which of the following satisfies the wildcard ? extends Component?
Select one:
a. JButton
b. String
c. Scanner
d. Color

a.JButton

Given the following declaration, what is the type parameter in Stack<E> replaced with?
private Stack<String> myStack = new Stack<>();
Select one:
a. String
b. int
c. Object
d. Stack<String>

a. String

Which of these Java library classes are implemented using type variables?
I HashMap
II LinkedList
III ArrayList
Select one:
a. I and III only
b. I and II only
c. I, II and III
d. II and III only

c. I, II and III

Select the correct header for this generic print method.
public static void print(E[] a)
{
for (int i = 0; i < a.length; i++)
{
System.out.println(a[i] + " ");
}
}
Select one:
a. public void print(E [] a)
b. public static <E> void print(E[] a)
c. public s

b. public static <E> void print(E[] a)

Which of the following necessitates the type erasure process?
I The Java virtual machine does not work with generic types
II To maintain backward compatibility to older versions of Java
III Generics do not work with primitives
Select one:
a. I only
b. II

c. I and II only

Suppose a linked-list class called MyLinkedList with a generic E type has been instantiated with a java.awt.Component type variable. Consider its instance method locate with the following header:
public void locate(MyLinkedList<? extends E>) { . . . }
Whi

b. I, II, and III

What does the following code snippet mean:
<E extends Comparable<E> & Measurable>
Select one:
a. The class represented by E extends Comparable and Measurable.
b. The class represented by E implements Comparable and extends Measurable.
c. The class represe

c. The class represented by E implements Comparable and Measurable.

Which Java technique(s) allows generic programming?
I type variables
II primitive types
III inheritance
Select one:
a. II only
b. III only
c. I only
d. I and III only

d. 1 and III only

Consider the following code snippet:
public class LinkedList<E>
{
private E defaultValue;
public static List<E> replicate(E value, int n) { . . . }
private class Node { public String data; public Node next;)
. . .
}
What is wrong with this code?
Select on

b. Cannot have a static method in a generic class as shown.

Consider the following code snippet:
public class Box<E>
{
private E data;
public Box() { . . . }
public void insert(E value) { . . . }
public E getData(){ . . . }
}
What will result from the following code?
Box<String> box = new Box<>();
. . .
box.insert

b. run-time error

Consider the following class declaration:
public class SavingsAccount extends BankAccount
{ . . . }
Which of the following statements about these classes is correct?
Select one:
a. ArrayList<BankAccount> is a subclass of ArrayList<SavingsAccount>.
b. Ther

b. There is no relationship between ArrayList<BankAccount> and ArrayList<SavingsAccount>

In Java, generic programming can be achieved with ____.
Select one:
a. encapsulation
b. polymorphism
c. arrays
d. inheritance

d. Inheritance

Consider the following code snippet:
public static <T> void fun(T[] t) { . . . }
Erasure by the compiler of method fun will generate which result?
Select one:
a. public static void fun(Object t) { . . . }
b. public static void fun(Object[] t) { . . . }
c.

b. public static void fun(Object[] t) {...}

Consider the following code snippet:
public interface MyInterface<E> { . . . }
public interface YourInterface<E, T> extends MyInterface<E> { . . . }
Which of these are legal class declarations?
I public class SomeClass implements YourInterface<String, Dou

c. I and II only

Consider the following code snippet:
public class Box<E>
{
private E data;
public Box(){ . . . }
public void insert(E value) { . . . }
public E getData() { . . . }
}
What will result from executing the following code?
Box<String> box = new Box<>();
. . .

c. correct, but unnecessary cast

Would switching the special case order affect the return value of the following method?
public int mystery(int n, int m)
{
if (n == 0) // special case #1
{
return 0;
}
if (n == 1) // special case #2
{
return m;
}
return m + mystery(n - 1, m);
}
Select one

a. No

Consider the permutations method from the textbook, which is intended to return all permutations of the word passed in as a parameter. What special cases for the simplest values are used by the permutations method to terminate the recursion?
public static

d. It terminates the recursion when it encounters the empty string.

Question 3Given the following class code:
public class RecurseMore
{
private static int total;
public static void main(String[] args)
{
System.out.println(recurse(4));
}
public static int recurse(int n)
{
int total = 0;
if (n == 0)
{
return 0;
}
else
{
to

c. 8

A recursive method without a special terminating case would _________
Select one:
a. be more efficient.
b. not be recursive.
c. never terminate.
d. end immediately.

c. never terminate

Given the following class code:
public class RecurseMore
{
public static void main(String[] args)
{
recurse(4);
}
public static int recurse(int n)
{
int total = 0;
if (n == 0)
{
return 0;
}
else
{
total = 4 + recurse(n - 2);
}
System.out.println(total);
r

b. 4 and 8

Complete the code for the calcPower recursive method shown below, which is intended to raise the base number passed into the method to the exponent power passed into the method:
public static int calcPower(int baseNum, int exponent)
{
int answer = 0;
____

a. if (exponent == 0)

Insert the missing code in the following code fragment. This fragment is intended to recursively compute xn, where x and n are both non-negative integers:
public int power(int x, int n)
{
if (n == 0)
{
____________________
}
else
{
return x * power(x, n -

c. return 1;

Why does the best recursive method usually run slightly slower than its iterative counterpart?
Select one:
a. Multiple recursive cases must be considered.
b. Each recursive method call takes processor time.
c. Testing the terminating condition takes longe

b. Each recursive method call takes processor time.

Consider the method below, which displays the characters from a String in reverse order. Each character appears on a separate line. Select the statement that should be used to complete the method so that it performs a recursive method call correctly.
publ

c. printReverse(word.substring(1));

Consider the permutations method from the textbook, which is intended to return all permutations of the word passed in as a parameter. Which line contains the recursive call in the permutations method?
public static ArrayList<String> permutations(String w

d. line #6

Which statement is true about backtracking?
Select one:
a. Backtracking starts with a partial solution and builds it up to get closer to the goal.
b. Backtracking never abandons a partial solution.
c. Backtracking starts from the end of the program and wo

a. Backtracking starts with a partial solution and builds it up to get closer to the goal.

Consider the permutations method from the textbook, which is intended to return all permutations of the word passed in as a parameter. Which line contains the terminating condition in the permutations recursive method?
public static ArrayList<String> perm

a.line #1

Consider the method below, which prints the digits of an arbitrary positive integer in reverse order, one digit per line. The method should print the last digit first. Then, it should recursively print the integer obtained by removing the last digit. Sele

a.System.out.println(value % 10);
printReverse(value / 10);

Complete the following code snippet, which is intended to be a recursive method that will find the sum of all elements in an array of double values from the beginning of the array to index:
// return the sum of all elements in arr[]
public static double f

b. return arr[index];

Consider the square method shown below that takes a non-negative int argument:
public int square(int n)
{
return squareHelper(n, n);
}
public int squareHelper(int c, int n)
{
if (c == 1)
{
return n;
}
else
{
return n + squareHelper(c - 1, n);
}
}
Assume t

b. 7

Complete the code for the calcPower recursive method shown below, which is intended to raise the base number passed into the method to the exponent power passed into the method:
public static int calcPower(int baseNum, int exponent)
{
int answer = 0;
if (

a. answer = baseNum * calcPower (baseNum, exponent - 1);

A termination condition in a loop is analogous to _____________ in a recursive method.
Select one:
a. an initialization condition
b. a recursive call
c. a special case
d. iteration

c. a special case

A palindrome is a word or phrase that reads the same forward or backward. Consider the methods palindrome and isPal shown below:
public boolean palindrome(String string)
{
return isPal(string, 0, string.length() - 1);
}
private boolean isPal(String string

d. recursive helper

Consider the following code snippet for recursive addition:
int add(int i, int j)
{
// assumes i >= 0
if (i == 0)
{
return j;
}
else
{
return add(i - 1, j + 1);
}
}
Identify the terminating condition in this recursive method.
Select one:
a. i == 0
b. retu

a. i == 0

Consider the mutually recursive methods below. Select the method call that could be used to generate the output sequence: A5 B4 A3 B2 A1
public static void methodA(int value)
{
if (value > 0)
{
System.out.print(" A" + value);
methodB(value - 1);
}
}
publi

c. methodA(5);

Selection sort has O(n2) complexity. If a computer can sort 1,000 elements in 4 seconds, approximately how many seconds will it take the computer to sort 1,000 times that many, or 1,000,000 elements?
Select one:
a. 16
b. 1,000,000
c. 1,000
d. 4,000,000

d. 4,000,000

In Big-Oh notation, selection sort is a(n) ____ algorithm.
Select one:
a. O(n^2)
b. log n
c. O(log n^2)
d. O(1)

a. 0(n^2)

The performance of an algorithm is most closely related to what?
Select one:
a. The type of elements
b. The total number of element visits
c. The total number of elements
d. The number of lines of code in the method

b. The total number of element visits

Consider the sort method shown below for selection sort:
public static void sort(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
int minPos = minimumPosition(i);
swap(minPos, i);
}
}
Suppose we modify the call to the swap method call to read swap(i, m

b.The sort would produce correct results.

A search technique where, in each step, you split the size of the search in half is called a____ search.
Select one:
a. binary
b. random
c. linear
d. merging

a. binary

The Comparable interface consists of a single method called ____.
Select one:
a. compareTo
b. comparable
c. compare
d. comparator

a. compareTo

Binary search is an ____ algorithm.
Select one:
a. O(n2)
b. O(n)
c. O(log n)
d. O(n log n)

c. O(log n)

Assume we are using quicksort to sort an array in ascending order. Into which array location does quicksort's strategy place a pivot element after partitioning?
Select one:
a. highest index in array still available
b. closer to its correct final location

c. its final correct location

What is the worst-case performance of insertion sort?
Select one:
a. O(n^2)
b. O(n log (n))
c. O(n)
d. O(log (n))

a. O(n^2)

The partial binary search method below is designed to search an array of String objects sorted in ascending order. Select the expression that would be needed to complete the method.
public static int search(String[] a, int low, int high, String item)
{
if

a. a[mid].compareTo(item)

The code segment below displays a pattern of asterisks. Select an expression to complete the code segment so that the resulting algorithm has O(n) running time.
for (int k = 0; k < n; k++)
{
for _______________________
{
System.out.print("*");
}
System.ou

b. (int j = 1; j <= 10; j++)

Which of the sorts in the textbook are based on the strategy of divide and conquer?
I quicksort
II mergesort
III insertion sort
Select one:
a. I and II only
b. II only
c. I only
d. III only

a. I and II only

A binary search is generally ____ a linear search.
Select one:
a. faster than
b. less efficient than
c. equal to
d. slower than

a. faster than

Which of the sorts in the textbook can be characterized by the fact that even in the worst case the running time will be O(n log(n)))?
I quicksort
II selection sort
III merge sort
Select one:
a. I only
b. I and III only
c. III only
d. II only

c. III only

Merge sort is a(n) ____ algorithm.
Select one:
a. O(n log(n))
b. O(n2)
c. O(log n)
d. O(n)

a. O(n log(n))

If an element is present in an array of length n, how many element visits, on average, are necessary to find it using a linear search?
Select one:
a. n / 2
b. n
c. 2n
d. n^2

a. n / 2

Suppose objects a and b are from a user-defined class that implements the Comparable interface. What must be true about the return value of a.compareTo(b) for the compareTo method that this class implements?
Select one:
a. It must return -1 if a comes bef

c. It must return a negative value if a comes before b, 0 if they are the same, and a positive value if a comes after b.

Assume that names is an array of String objects that has been initialized with a large number of elements. Select the statement that would sort the elements in names in ascending alphabetic order.
Select one:
a. Arrays.sort(names);
b. Collections.sort(nam

a. Arrays.sort(names);

The code segment below prints some of the elements in an array with size n. Select an expression to complete the code segment so that the resulting algorithm has O(log n) running time.
for __________________________
{
System.out.println(array[j]);
}
Selec

c. (int j = 1; j < array.length; j = j * 2)

In general, the expression ____ means that f grows no faster than g.
Select one:
a. f(n) = log g2
b. f(n) = log g
c. g(n) = O(f(n))
d. f(n) = O(g(n))

d. f(n) = O(g(n))